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
37 MPC_API int mpc_bits_get_block(mpc_bits_reader * r, mpc_block * p_block);
38 mpc_int32_t mpc_bits_golomb_dec(mpc_bits_reader * r, const mpc_uint_t k);
39 MPC_API unsigned int mpc_bits_get_size(mpc_bits_reader * r, mpc_uint64_t * p_size);
40 mpc_uint32_t mpc_bits_log_dec(mpc_bits_reader * r, mpc_uint_t max);
41
42 extern const mpc_uint32_t Cnk[MAX_ENUM / 2][MAX_ENUM];
43 extern const mpc_uint8_t Cnk_len[MAX_ENUM / 2][MAX_ENUM];
44 extern const mpc_uint32_t Cnk_lost[MAX_ENUM / 2][MAX_ENUM];
45
46 // can read up to 31 bits
mpc_bits_read(mpc_bits_reader * r,const unsigned int nb_bits)47 static 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 // basic huffman decoding routine
65 // works with maximum lengths up to 16
mpc_bits_huff_dec(mpc_bits_reader * r,const mpc_huffman * Table)66 static mpc_inline mpc_int32_t mpc_bits_huff_dec(mpc_bits_reader * r, const mpc_huffman *Table)
67 {
68 mpc_uint16_t code;
69 code = (mpc_uint16_t)((((r->buff[0] << 16) | (r->buff[1] << 8) | r->buff[2]) >> r->count) & 0xFFFF);
70
71 while (code < Table->Code) Table++;
72
73 r->buff -= (int)(r->count - Table->Length) >> 3;
74 r->count = (r->count - Table->Length) & 0x07;
75
76 return Table->Value;
77 }
78
mpc_bits_can_dec(mpc_bits_reader * r,const mpc_can_data * can)79 static mpc_inline mpc_int32_t mpc_bits_can_dec(mpc_bits_reader * r, const mpc_can_data *can)
80 {
81 mpc_uint16_t code;
82 mpc_huff_lut tmp;
83 const mpc_huffman * Table;
84 code = (mpc_uint16_t)((((r->buff[0] << 16) | (r->buff[1] << 8) | r->buff[2]) >> r->count) & 0xFFFF);
85
86 tmp = can->lut[code >> (16 - LUT_DEPTH)];
87 if (tmp.Length != 0) {
88 r->buff -= (int)(r->count - tmp.Length) >> 3;
89 r->count = (r->count - tmp.Length) & 0x07;
90 return tmp.Value;
91 }
92
93 Table = can->table + (unsigned char)tmp.Value;
94 while (code < Table->Code) Table++;
95
96 r->buff -= (int)(r->count - Table->Length) >> 3;
97 r->count = (r->count - Table->Length) & 0x07;
98
99 return can->sym[(Table->Value - (code >> (16 - Table->Length))) & 0xFF] ;
100 }
101
102 // LUT-based huffman decoding routine
103 // works with maximum lengths up to 16
mpc_bits_huff_lut(mpc_bits_reader * r,const mpc_lut_data * lut)104 static mpc_inline mpc_int32_t mpc_bits_huff_lut(mpc_bits_reader * r, const mpc_lut_data *lut)
105 {
106 mpc_uint16_t code;
107 mpc_huff_lut tmp;
108 const mpc_huffman * Table;
109 code = (mpc_uint16_t)((((r->buff[0] << 16) | (r->buff[1] << 8) | r->buff[2]) >> r->count) & 0xFFFF);
110
111 tmp = lut->lut[code >> (16 - LUT_DEPTH)];
112 if (tmp.Length != 0) {
113 r->buff -= (int)(r->count - tmp.Length) >> 3;
114 r->count = (r->count - tmp.Length) & 0x07;
115 return tmp.Value;
116 }
117
118 Table = lut->table + (unsigned char)tmp.Value;
119 while (code < Table->Code) Table++;
120
121 r->buff -= (int)(r->count - Table->Length) >> 3;
122 r->count = (r->count - Table->Length) & 0x07;
123
124 return Table->Value;
125 }
126
mpc_bits_enum_dec(mpc_bits_reader * r,mpc_uint_t k,mpc_uint_t n)127 static mpc_inline mpc_uint32_t mpc_bits_enum_dec(mpc_bits_reader * r, mpc_uint_t k, mpc_uint_t n)
128 {
129 mpc_uint32_t bits = 0;
130 mpc_uint32_t code;
131 const mpc_uint32_t * C = Cnk[k-1];
132
133 code = mpc_bits_read(r, Cnk_len[k-1][n-1] - 1);
134
135 if (code >= Cnk_lost[k-1][n-1])
136 code = ((code << 1) | mpc_bits_read(r, 1)) - Cnk_lost[k-1][n-1];
137
138 do {
139 n--;
140 if (code >= C[n]) {
141 bits |= 1 << n;
142 code -= C[n];
143 C -= MAX_ENUM;
144 k--;
145 }
146 } while(k > 0);
147
148 return bits;
149 }
150