1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
|
/*
libdemac - A Monkey's Audio decoder
$Id: entropy.c 19552 2008-12-21 23:49:02Z amiconn $
Copyright (C) Dave Chapman 2007
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
*/
#include <inttypes.h>
#include <string.h>
#include "parser.h"
#include "entropy.h"
#include "demac_config.h"
#define MODEL_ELEMENTS 64
/*
The following counts arrays for use with the range decoder are
hard-coded in the Monkey's Audio decoder.
*/
static const int counts_3970[65] ICONST_ATTR =
{
0,14824,28224,39348,47855,53994,58171,60926,
62682,63786,64463,64878,65126,65276,65365,65419,
65450,65469,65480,65487,65491,65493,65494,65495,
65496,65497,65498,65499,65500,65501,65502,65503,
65504,65505,65506,65507,65508,65509,65510,65511,
65512,65513,65514,65515,65516,65517,65518,65519,
65520,65521,65522,65523,65524,65525,65526,65527,
65528,65529,65530,65531,65532,65533,65534,65535,
65536
};
/* counts_diff_3970[i] = counts_3970[i+1] - counts_3970[i] */
static const int counts_diff_3970[64] ICONST_ATTR =
{
14824,13400,11124,8507,6139,4177,2755,1756,
1104,677,415,248,150,89,54,31,
19,11,7,4,2,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1
};
static const int counts_3980[65] ICONST_ATTR =
{
0,19578,36160,48417,56323,60899,63265,64435,
64971,65232,65351,65416,65447,65466,65476,65482,
65485,65488,65490,65491,65492,65493,65494,65495,
65496,65497,65498,65499,65500,65501,65502,65503,
65504,65505,65506,65507,65508,65509,65510,65511,
65512,65513,65514,65515,65516,65517,65518,65519,
65520,65521,65522,65523,65524,65525,65526,65527,
65528,65529,65530,65531,65532,65533,65534,65535,
65536
};
/* counts_diff_3980[i] = counts_3980[i+1] - counts_3980[i] */
static const int counts_diff_3980[64] ICONST_ATTR =
{
19578,16582,12257,7906,4576,2366,1170,536,
261,119,65,31,19,10,6,3,
3,2,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1
};
/*
Range decoder adapted from rangecod.c included in:
http://www.compressconsult.com/rangecoder/rngcod13.zip
rangecod.c range encoding
(c) Michael Schindler
1997, 1998, 1999, 2000
http://www.compressconsult.com/
michael@compressconsult.com
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
The encoding functions were removed, and functions turned into "static
inline" functions. Some minor cosmetic changes were made (e.g. turning
pre-processor symbols into upper-case, removing the rc parameter from
each function (and the RNGC macro)).
*/
/* BITSTREAM READING FUNCTIONS */
/* We deal with the input data one byte at a time - to ensure
functionality on CPUs of any endianness regardless of any requirements
for aligned reads.
*/
static unsigned char* bytebuffer IBSS_ATTR;
static int bytebufferoffset IBSS_ATTR;
static inline void skip_byte(void)
{
bytebufferoffset--;
bytebuffer += bytebufferoffset & 4;
bytebufferoffset &= 3;
}
static inline int read_byte(void)
{
int ch = bytebuffer[bytebufferoffset];
skip_byte();
return ch;
}
/* RANGE DECODING FUNCTIONS */
/* SIZE OF RANGE ENCODING CODE VALUES. */
#define CODE_BITS 32
#define TOP_VALUE ((unsigned int)1 << (CODE_BITS-1))
#define SHIFT_BITS (CODE_BITS - 9)
#define EXTRA_BITS ((CODE_BITS-2) % 8 + 1)
#define BOTTOM_VALUE (TOP_VALUE >> 8)
struct rangecoder_t
{
uint32_t low; /* low end of interval */
uint32_t range; /* length of interval */
uint32_t help; /* bytes_to_follow resp. intermediate value */
unsigned int buffer; /* buffer for input/output */
};
static struct rangecoder_t rc IBSS_ATTR;
/* Start the decoder */
static inline void range_start_decoding(void)
{
rc.buffer = read_byte();
rc.low = rc.buffer >> (8 - EXTRA_BITS);
rc.range = (uint32_t) 1 << EXTRA_BITS;
}
static inline void range_dec_normalize(void)
{
while (rc.range <= BOTTOM_VALUE)
{
rc.buffer = (rc.buffer << 8) | read_byte();
rc.low = (rc.low << 8) | ((rc.buffer >> 1) & 0xff);
rc.range <<= 8;
}
}
/* Calculate culmulative frequency for next symbol. Does NO update!*/
/* tot_f is the total frequency */
/* or: totf is (code_value)1<<shift */
/* returns the culmulative frequency */
static inline int range_decode_culfreq(int tot_f)
{
range_dec_normalize();
rc.help = UDIV32(rc.range, tot_f);
return UDIV32(rc.low, rc.help);
}
static inline int range_decode_culshift(int shift)
{
range_dec_normalize();
rc.help = rc.range >> shift;
return UDIV32(rc.low, rc.help);
}
/* Update decoding state */
/* sy_f is the interval length (frequency of the symbol) */
/* lt_f is the lower end (frequency sum of < symbols) */
static inline void range_decode_update(int sy_f, int lt_f)
{
rc.low -= rc.help * lt_f;
rc.range = rc.help * sy_f;
}
/* Decode a byte/short without modelling */
static inline unsigned char decode_byte(void)
{ int tmp = range_decode_culshift(8);
range_decode_update( 1,tmp);
return tmp;
}
static inline int short range_decode_short(void)
{ int tmp = range_decode_culshift(16);
range_decode_update( 1,tmp);
return tmp;
}
/* Decode n bits (n <= 16) without modelling - based on range_decode_short */
static inline int range_decode_bits(int n)
{ int tmp = range_decode_culshift(n);
range_decode_update( 1,tmp);
return tmp;
}
/* Finish decoding */
static inline void range_done_decoding(void)
{ range_dec_normalize(); /* normalize to use up all bytes */
}
/*
range_get_symbol_* functions based on main decoding loop in simple_d.c from
http://www.compressconsult.com/rangecoder/rngcod13.zip
(c) Michael Schindler
*/
static inline int range_get_symbol_3980(void)
{
int symbol, cf;
cf = range_decode_culshift(16);
/* figure out the symbol inefficiently; a binary search would be much better */
for (symbol = 0; counts_3980[symbol+1] <= cf; symbol++);
range_decode_update(counts_diff_3980[symbol],counts_3980[symbol]);
return symbol;
}
static inline int range_get_symbol_3970(void)
{
int symbol, cf;
cf = range_decode_culshift(16);
/* figure out the symbol inefficiently; a binary search would be much better */
for (symbol = 0; counts_3970[symbol+1] <= cf; symbol++);
range_decode_update(counts_diff_3970[symbol],counts_3970[symbol]);
return symbol;
}
/* MAIN DECODING FUNCTIONS */
struct rice_t
{
uint32_t k;
uint32_t ksum;
};
static struct rice_t riceX IBSS_ATTR;
static struct rice_t riceY IBSS_ATTR;
static inline void update_rice(struct rice_t* rice, int x)
{
rice->ksum += ((x + 1) / 2) - ((rice->ksum + 16) >> 5);
if (UNLIKELY(rice->k == 0)) {
rice->k = 1;
} else {
uint32_t lim = 1 << (rice->k + 4);
if (UNLIKELY(rice->ksum < lim)) {
rice->k--;
} else if (UNLIKELY(rice->ksum >= 2 * lim)) {
rice->k++;
}
}
}
static inline int entropy_decode3980(struct rice_t* rice)
{
int base, x, pivot, overflow;
pivot = rice->ksum >> 5;
if (UNLIKELY(pivot == 0))
pivot=1;
overflow = range_get_symbol_3980();
if (UNLIKELY(overflow == (MODEL_ELEMENTS-1))) {
overflow = range_decode_short() << 16;
overflow |= range_decode_short();
}
if (pivot >= 0x10000) {
/* Codepath for 24-bit streams */
int nbits, lo_bits, base_hi, base_lo;
/* Count the number of bits in pivot */
nbits = 17; /* We know there must be at least 17 bits */
while ((pivot >> nbits) > 0) { nbits++; }
/* base_lo is the low (nbits-16) bits of base
base_hi is the high 16 bits of base
*/
lo_bits = (nbits - 16);
base_hi = range_decode_culfreq((pivot >> lo_bits) + 1);
range_decode_update(1, base_hi);
base_lo = range_decode_culshift(lo_bits);
range_decode_update(1, base_lo);
base = (base_hi << lo_bits) + base_lo;
} else {
/* Codepath for 16-bit streams */
base = range_decode_culfreq(pivot);
range_decode_update(1, base);
}
x = base + (overflow * pivot);
update_rice(rice, x);
/* Convert to signed */
if (x & 1)
return (x >> 1) + 1;
else
return -(x >> 1);
}
static inline int entropy_decode3970(struct rice_t* rice)
{
int x, tmpk;
int overflow = range_get_symbol_3970();
if (UNLIKELY(overflow == (MODEL_ELEMENTS - 1))) {
tmpk = range_decode_bits(5);
overflow = 0;
} else {
tmpk = (rice->k < 1) ? 0 : rice->k - 1;
}
if (tmpk <= 16) {
x = range_decode_bits(tmpk);
} else {
x = range_decode_short();
x |= (range_decode_bits(tmpk - 16) << 16);
}
x += (overflow << tmpk);
update_rice(rice, x);
/* Convert to signed */
if (x & 1)
return (x >> 1) + 1;
else
return -(x >> 1);
}
void init_entropy_decoder(struct ape_ctx_t* ape_ctx,
unsigned char* inbuffer, int* firstbyte,
int* bytesconsumed)
{
bytebuffer = inbuffer;
bytebufferoffset = *firstbyte;
/* Read the CRC */
ape_ctx->CRC = read_byte();
ape_ctx->CRC = (ape_ctx->CRC << 8) | read_byte();
ape_ctx->CRC = (ape_ctx->CRC << 8) | read_byte();
ape_ctx->CRC = (ape_ctx->CRC << 8) | read_byte();
/* Read the frame flags if they exist */
ape_ctx->frameflags = 0;
if ((ape_ctx->fileversion > 3820) && (ape_ctx->CRC & 0x80000000)) {
ape_ctx->CRC &= ~0x80000000;
ape_ctx->frameflags = read_byte();
ape_ctx->frameflags = (ape_ctx->frameflags << 8) | read_byte();
ape_ctx->frameflags = (ape_ctx->frameflags << 8) | read_byte();
ape_ctx->frameflags = (ape_ctx->frameflags << 8) | read_byte();
}
/* Keep a count of the blocks decoded in this frame */
ape_ctx->blocksdecoded = 0;
/* Initialise the rice structs */
riceX.k = 10;
riceX.ksum = (1 << riceX.k) * 16;
riceY.k = 10;
riceY.ksum = (1 << riceY.k) * 16;
/* The first 8 bits of input are ignored. */
skip_byte();
range_start_decoding();
/* Return the new state of the buffer */
*bytesconsumed = (intptr_t)bytebuffer - (intptr_t)inbuffer;
*firstbyte = bytebufferoffset;
}
void ICODE_ATTR_DEMAC entropy_decode(struct ape_ctx_t* ape_ctx,
unsigned char* inbuffer, int* firstbyte,
int* bytesconsumed,
int32_t* decoded0, int32_t* decoded1,
int blockstodecode)
{
bytebuffer = inbuffer;
bytebufferoffset = *firstbyte;
ape_ctx->blocksdecoded += blockstodecode;
if ((ape_ctx->frameflags & APE_FRAMECODE_LEFT_SILENCE)
&& ((ape_ctx->frameflags & APE_FRAMECODE_RIGHT_SILENCE)
|| (decoded1 == NULL))) {
/* We are pure silence, just memset the output buffer. */
memset(decoded0, 0, blockstodecode * sizeof(int32_t));
if (decoded1 != NULL)
memset(decoded1, 0, blockstodecode * sizeof(int32_t));
} else {
if (ape_ctx->fileversion > 3970) {
while (LIKELY(blockstodecode--)) {
*(decoded0++) = entropy_decode3980(&riceY);
if (decoded1 != NULL)
*(decoded1++) = entropy_decode3980(&riceX);
}
} else {
while (LIKELY(blockstodecode--)) {
*(decoded0++) = entropy_decode3970(&riceY);
if (decoded1 != NULL)
*(decoded1++) = entropy_decode3970(&riceX);
}
}
}
if (ape_ctx->blocksdecoded == ape_ctx->currentframeblocks)
{
range_done_decoding();
}
/* Return the new state of the buffer */
*bytesconsumed = bytebuffer - inbuffer;
*firstbyte = bytebufferoffset;
}
|