diff options
author | Dave Chapman <dave@dchapman.com> | 2007-06-05 16:58:29 +0000 |
---|---|---|
committer | Dave Chapman <dave@dchapman.com> | 2007-06-05 16:58:29 +0000 |
commit | 520274219a0745384cb9bc6df4d7ee7905090f5d (patch) | |
tree | 8d6f4536b6758d00e72a2a9457f892ce28a591bd /apps/codecs/demac/libdemac/rangecoding.h | |
parent | 887b31c01aebb46c0fcc6910241a4a64d9e7991e (diff) | |
download | rockbox-520274219a0745384cb9bc6df4d7ee7905090f5d.tar.gz rockbox-520274219a0745384cb9bc6df4d7ee7905090f5d.zip |
Initial commit of Monkey's Audio (.ape/.mac) support. Note that Monkey's is an extremely CPU-intensive codec, and that the decoding speed is directly related to the compression level (-c1000, -c2000, -c3000, -c4000 or -c5000) used when encoding the file. Current performance is: -c1000 to -c3000 are realtime on a Gigabeat, -c1000 is realtime on Coldfire targets (H100, H300 and Cowon), and nothing is realtime on PortalPlayer targets (iPods, H10, Sansa). Hopefully this can be improved. More information at FS #7256.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@13562 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/codecs/demac/libdemac/rangecoding.h')
-rw-r--r-- | apps/codecs/demac/libdemac/rangecoding.h | 180 |
1 files changed, 180 insertions, 0 deletions
diff --git a/apps/codecs/demac/libdemac/rangecoding.h b/apps/codecs/demac/libdemac/rangecoding.h new file mode 100644 index 0000000000..8be5e923ba --- /dev/null +++ b/apps/codecs/demac/libdemac/rangecoding.h | |||
@@ -0,0 +1,180 @@ | |||
1 | /* | ||
2 | |||
3 | libdemac - A Monkey's Audio decoder | ||
4 | |||
5 | $Id:$ | ||
6 | |||
7 | Copyright (C) Dave Chapman 2007 | ||
8 | |||
9 | This program is free software; you can redistribute it and/or modify | ||
10 | it under the terms of the GNU General Public License as published by | ||
11 | the Free Software Foundation; either version 2 of the License, or | ||
12 | (at your option) any later version. | ||
13 | |||
14 | This program is distributed in the hope that it will be useful, | ||
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
17 | GNU General Public License for more details. | ||
18 | |||
19 | You should have received a copy of the GNU General Public License | ||
20 | along with this program; if not, write to the Free Software | ||
21 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA | ||
22 | |||
23 | */ | ||
24 | |||
25 | /* | ||
26 | |||
27 | Range decoder adapted from rangecod.c included in: | ||
28 | |||
29 | http://www.compressconsult.com/rangecoder/rngcod13.zip | ||
30 | |||
31 | rangecod.c range encoding | ||
32 | |||
33 | (c) Michael Schindler | ||
34 | 1997, 1998, 1999, 2000 | ||
35 | http://www.compressconsult.com/ | ||
36 | michael@compressconsult.com | ||
37 | |||
38 | This program is free software; you can redistribute it and/or modify | ||
39 | it under the terms of the GNU General Public License as published by | ||
40 | the Free Software Foundation; either version 2 of the License, or | ||
41 | (at your option) any later version. | ||
42 | |||
43 | |||
44 | The encoding functions were removed, and functions turned into "static | ||
45 | inline" functions and moved to a .h file. Some minor cosmetic changes | ||
46 | were made (e.g. turning pre-processor symbols into upper-case, | ||
47 | removing the rc parameter from each function (and the RNGC macro)). | ||
48 | |||
49 | |||
50 | */ | ||
51 | |||
52 | |||
53 | /* BITSTREAM READING FUNCTIONS */ | ||
54 | |||
55 | /* We deal with the input data one byte at a time - to ensure | ||
56 | functionality on CPUs of any endianness regardless of any requirements | ||
57 | for aligned reads. | ||
58 | */ | ||
59 | |||
60 | static unsigned char* bytebuffer IBSS_ATTR; | ||
61 | static int bytebufferoffset IBSS_ATTR; | ||
62 | |||
63 | static inline void skip_byte(void) | ||
64 | { | ||
65 | if (bytebufferoffset) { | ||
66 | bytebufferoffset--; | ||
67 | } else { | ||
68 | bytebufferoffset = 3; | ||
69 | bytebuffer += 4; | ||
70 | } | ||
71 | } | ||
72 | |||
73 | static inline int read_byte(void) | ||
74 | { | ||
75 | int ch = bytebuffer[bytebufferoffset]; | ||
76 | |||
77 | skip_byte(); | ||
78 | |||
79 | return ch; | ||
80 | } | ||
81 | |||
82 | /* RANGE DECODING FUNCTIONS */ | ||
83 | |||
84 | /* SIZE OF RANGE ENCODING CODE VALUES. */ | ||
85 | |||
86 | #define CODE_BITS 32 | ||
87 | #define TOP_VALUE ((unsigned int)1 << (CODE_BITS-1)) | ||
88 | #define SHIFT_BITS (CODE_BITS - 9) | ||
89 | #define EXTRA_BITS ((CODE_BITS-2) % 8 + 1) | ||
90 | #define BOTTOM_VALUE (TOP_VALUE >> 8) | ||
91 | |||
92 | struct rangecoder_t | ||
93 | { | ||
94 | uint32_t low; /* low end of interval */ | ||
95 | uint32_t range; /* length of interval */ | ||
96 | uint32_t help; /* bytes_to_follow resp. intermediate value */ | ||
97 | unsigned int buffer; /* buffer for input/output */ | ||
98 | }; | ||
99 | |||
100 | static struct rangecoder_t rc; | ||
101 | |||
102 | /* Start the decoder */ | ||
103 | static inline void range_start_decoding(void) | ||
104 | { | ||
105 | rc.buffer = read_byte(); | ||
106 | rc.low = rc.buffer >> (8 - EXTRA_BITS); | ||
107 | rc.range = (uint32_t) 1 << EXTRA_BITS; | ||
108 | } | ||
109 | |||
110 | static inline void range_dec_normalize(void) | ||
111 | { | ||
112 | while (rc.range <= BOTTOM_VALUE) | ||
113 | { | ||
114 | rc.buffer = (rc.buffer << 8) | read_byte(); | ||
115 | rc.low = (rc.low << 8) | ((rc.buffer >> 1) & 0xff); | ||
116 | rc.range <<= 8; | ||
117 | } | ||
118 | } | ||
119 | |||
120 | /* Calculate culmulative frequency for next symbol. Does NO update!*/ | ||
121 | /* tot_f is the total frequency */ | ||
122 | /* or: totf is (code_value)1<<shift */ | ||
123 | /* returns the culmulative frequency */ | ||
124 | static inline int range_decode_culfreq(int tot_f) | ||
125 | { int tmp; | ||
126 | |||
127 | range_dec_normalize(); | ||
128 | |||
129 | rc.help = rc.range / tot_f; | ||
130 | tmp = rc.low / rc.help; | ||
131 | |||
132 | return tmp; | ||
133 | } | ||
134 | |||
135 | static inline int range_decode_culshift(int shift) | ||
136 | { | ||
137 | int tmp; | ||
138 | range_dec_normalize(); | ||
139 | rc.help = rc.range>>shift; | ||
140 | tmp = rc.low/rc.help; | ||
141 | return tmp; | ||
142 | } | ||
143 | |||
144 | |||
145 | /* Update decoding state */ | ||
146 | /* sy_f is the interval length (frequency of the symbol) */ | ||
147 | /* lt_f is the lower end (frequency sum of < symbols) */ | ||
148 | static inline void range_decode_update(int sy_f, int lt_f) | ||
149 | { int tmp; | ||
150 | tmp = rc.help * lt_f; | ||
151 | rc.low -= tmp; | ||
152 | rc.range = rc.help * sy_f; | ||
153 | } | ||
154 | |||
155 | |||
156 | /* Decode a byte/short without modelling */ | ||
157 | static inline unsigned char decode_byte(void) | ||
158 | { int tmp = range_decode_culshift(8); | ||
159 | range_decode_update( 1,tmp); | ||
160 | return tmp; | ||
161 | } | ||
162 | |||
163 | static inline int short range_decode_short(void) | ||
164 | { int tmp = range_decode_culshift(16); | ||
165 | range_decode_update( 1,tmp); | ||
166 | return tmp; | ||
167 | } | ||
168 | |||
169 | /* Decode n bits (n <= 16) without modelling - based on range_decode_short */ | ||
170 | static inline int range_decode_bits(int n) | ||
171 | { int tmp = range_decode_culshift(n); | ||
172 | range_decode_update( 1,tmp); | ||
173 | return tmp; | ||
174 | } | ||
175 | |||
176 | |||
177 | /* Finish decoding */ | ||
178 | static inline void range_done_decoding(void) | ||
179 | { range_dec_normalize(); /* normalize to use up all bytes */ | ||
180 | } | ||