3 libdemac - A Monkey's Audio decoder
7 Copyright (C) Dave Chapman 2007
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.
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.
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
30 #include "rangecoding.h" /* Range-coding (static inline) functions */
31 #include "demac_iram.h"
33 #define MODEL_ELEMENTS 64
36 The following counts arrays for use with the range decoder are
37 hard-coded in the Monkey's Audio decoder.
40 static const int counts_3970
[65] ICONST_ATTR
=
42 0,14824,28224,39348,47855,53994,58171,60926,
43 62682,63786,64463,64878,65126,65276,65365,65419,
44 65450,65469,65480,65487,65491,65493,65494,65495,
45 65496,65497,65498,65499,65500,65501,65502,65503,
46 65504,65505,65506,65507,65508,65509,65510,65511,
47 65512,65513,65514,65515,65516,65517,65518,65519,
48 65520,65521,65522,65523,65524,65525,65526,65527,
49 65528,65529,65530,65531,65532,65533,65534,65535,
53 /* counts_diff_3970[i] = counts_3970[i+1] - counts_3970[i] */
54 static const int counts_diff_3970
[64] ICONST_ATTR
=
56 14824,13400,11124,8507,6139,4177,2755,1756,
57 1104,677,415,248,150,89,54,31,
66 static const int counts_3980
[65] ICONST_ATTR
=
68 0,19578,36160,48417,56323,60899,63265,64435,
69 64971,65232,65351,65416,65447,65466,65476,65482,
70 65485,65488,65490,65491,65492,65493,65494,65495,
71 65496,65497,65498,65499,65500,65501,65502,65503,
72 65504,65505,65506,65507,65508,65509,65510,65511,
73 65512,65513,65514,65515,65516,65517,65518,65519,
74 65520,65521,65522,65523,65524,65525,65526,65527,
75 65528,65529,65530,65531,65532,65533,65534,65535,
79 /* counts_diff_3980[i] = counts_3980[i+1] - counts_3980[i] */
81 static const int counts_diff_3980
[64] ICONST_ATTR
=
83 19578,16582,12257,7906,4576,2366,1170,536,
84 261,119,65,31,19,10,6,3,
94 range_get_symbol_* functions based on main decoding loop in simple_d.c from
95 http://www.compressconsult.com/rangecoder/rngcod13.zip
99 static inline int range_get_symbol_3980(void)
103 cf
= range_decode_culshift(16);
105 /* figure out the symbol inefficiently; a binary search would be much better */
106 for (symbol
= 0; counts_3980
[symbol
+1] <= cf
; symbol
++);
108 range_decode_update(counts_diff_3980
[symbol
],counts_3980
[symbol
]);
113 static inline int range_get_symbol_3970(void)
117 cf
= range_decode_culshift(16);
119 /* figure out the symbol inefficiently; a binary search would be much better */
120 for (symbol
= 0; counts_3970
[symbol
+1] <= cf
; symbol
++);
122 range_decode_update(counts_diff_3970
[symbol
],counts_3970
[symbol
]);
127 /* MAIN DECODING FUNCTIONS */
135 static struct rice_t riceX IBSS_ATTR
;
136 static struct rice_t riceY IBSS_ATTR
;
138 static inline void update_rice(struct rice_t
* rice
, int x
)
140 rice
->ksum
+= ((x
+ 1) / 2) - ((rice
->ksum
+ 16) >> 5);
145 uint32_t lim
= 1 << (rice
->k
+ 4);
146 if (rice
->ksum
< lim
) {
148 } else if (rice
->ksum
>= 2 * lim
) {
154 static inline int entropy_decode3980(struct rice_t
* rice
)
156 int base
, x
, pivot
, overflow
;
158 pivot
= rice
->ksum
>> 5;
159 if (pivot
== 0) pivot
=1;
161 overflow
= range_get_symbol_3980();
163 if (overflow
== (MODEL_ELEMENTS
-1)) {
164 overflow
= range_decode_short() << 16;
165 overflow
|= range_decode_short();
168 if (pivot
>= 0x10000) {
169 /* Codepath for 24-bit streams */
170 int nbits
, lo_bits
, base_hi
, base_lo
;
172 /* Count the number of bits in pivot */
173 nbits
= 17; /* We know there must be at least 17 bits */
174 while ((pivot
>> nbits
) > 0) { nbits
++; }
176 /* base_lo is the low (nbits-16) bits of base
177 base_hi is the high 16 bits of base
179 lo_bits
= (nbits
- 16);
181 base_hi
= range_decode_culfreq((pivot
>> lo_bits
) + 1);
182 range_decode_update(1, base_hi
);
184 base_lo
= range_decode_culshift(lo_bits
);
185 range_decode_update(1, base_lo
);
187 base
= (base_hi
<< lo_bits
) + base_lo
;
189 /* Codepath for 16-bit streams */
190 base
= range_decode_culfreq(pivot
);
191 range_decode_update(1, base
);
194 x
= base
+ (overflow
* pivot
);
195 update_rice(rice
, x
);
197 /* Convert to signed */
205 static inline int entropy_decode3970(struct rice_t
* rice
)
209 int overflow
= range_get_symbol_3970();
211 if (overflow
== (MODEL_ELEMENTS
- 1)) {
212 tmpk
= range_decode_bits(5);
215 tmpk
= (rice
->k
< 1) ? 0 : rice
->k
- 1;
219 x
= range_decode_bits(tmpk
);
221 x
= range_decode_short();
222 x
|= (range_decode_bits(tmpk
- 16) << 16);
224 x
+= (overflow
<< tmpk
);
226 update_rice(rice
, x
);
228 /* Convert to signed */
235 void init_entropy_decoder(struct ape_ctx_t
* ape_ctx
,
236 unsigned char* inbuffer
, int* firstbyte
,
239 bytebuffer
= inbuffer
;
240 bytebufferoffset
= *firstbyte
;
243 ape_ctx
->CRC
= read_byte();
244 ape_ctx
->CRC
= (ape_ctx
->CRC
<< 8) | read_byte();
245 ape_ctx
->CRC
= (ape_ctx
->CRC
<< 8) | read_byte();
246 ape_ctx
->CRC
= (ape_ctx
->CRC
<< 8) | read_byte();
248 /* Read the frame flags if they exist */
249 ape_ctx
->frameflags
= 0;
250 if ((ape_ctx
->fileversion
> 3820) && (ape_ctx
->CRC
& 0x80000000)) {
251 ape_ctx
->CRC
&= ~0x80000000;
253 ape_ctx
->frameflags
= read_byte();
254 ape_ctx
->frameflags
= (ape_ctx
->frameflags
<< 8) | read_byte();
255 ape_ctx
->frameflags
= (ape_ctx
->frameflags
<< 8) | read_byte();
256 ape_ctx
->frameflags
= (ape_ctx
->frameflags
<< 8) | read_byte();
258 /* Keep a count of the blocks decoded in this frame */
259 ape_ctx
->blocksdecoded
= 0;
261 /* Initialise the rice structs */
263 riceX
.ksum
= (1 << riceX
.k
) * 16;
265 riceY
.ksum
= (1 << riceY
.k
) * 16;
267 /* The first 8 bits of input are ignored. */
270 range_start_decoding();
272 /* Return the new state of the buffer */
273 *bytesconsumed
= (intptr_t)bytebuffer
- (intptr_t)inbuffer
;
274 *firstbyte
= bytebufferoffset
;
277 int ICODE_ATTR_DEMAC
entropy_decode(struct ape_ctx_t
* ape_ctx
,
278 unsigned char* inbuffer
, int* firstbyte
,
280 int32_t* decoded0
, int32_t* decoded1
,
283 bytebuffer
= inbuffer
;
284 bytebufferoffset
= *firstbyte
;
286 ape_ctx
->blocksdecoded
+= blockstodecode
;
288 if (ape_ctx
->frameflags
& APE_FRAMECODE_STEREO_SILENCE
) {
289 /* We are pure silence, just memset the output buffer. */
290 memset(decoded0
, 0, blockstodecode
* sizeof(int32_t));
291 memset(decoded1
, 0, blockstodecode
* sizeof(int32_t));
293 if (ape_ctx
->fileversion
> 3970) {
294 while (blockstodecode
--) {
295 *(decoded0
++) = entropy_decode3980(&riceY
);
296 if (decoded1
!= NULL
)
297 *(decoded1
++) = entropy_decode3980(&riceX
);
300 while (blockstodecode
--) {
301 *(decoded0
++) = entropy_decode3970(&riceY
);
302 if (decoded1
!= NULL
)
303 *(decoded1
++) = entropy_decode3970(&riceX
);
308 if (ape_ctx
->blocksdecoded
== ape_ctx
->currentframeblocks
)
310 range_done_decoding();
313 /* Return the new state of the buffer */
314 *bytesconsumed
= bytebuffer
- inbuffer
;
315 *firstbyte
= bytebufferoffset
;