Some entropy decoder tweaks. Also removed unnecessary 'tmp' variables.
[kugel-rb.git] / apps / codecs / demac / libdemac / entropy.c
blob86ea06d3897aa95ccad0b872e2197f0cf775c607
1 /*
3 libdemac - A Monkey's Audio decoder
5 $Id$
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
25 #include <inttypes.h>
26 #include <string.h>
28 #include "parser.h"
29 #include "entropy.h"
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,
50 65536
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,
58 19,11,7,4,2,1,1,1,
59 1,1,1,1,1,1,1,1,
60 1,1,1,1,1,1,1,1,
61 1,1,1,1,1,1,1,1,
62 1,1,1,1,1,1,1,1,
63 1,1,1,1,1,1,1,1
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,
76 65536
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,
85 3,2,1,1,1,1,1,1,
86 1,1,1,1,1,1,1,1,
87 1,1,1,1,1,1,1,1,
88 1,1,1,1,1,1,1,1,
89 1,1,1,1,1,1,1,1,
90 1,1,1,1,1,1,1,1
94 range_get_symbol_* functions based on main decoding loop in simple_d.c from
95 http://www.compressconsult.com/rangecoder/rngcod13.zip
96 (c) Michael Schindler
99 static inline int range_get_symbol_3980(void)
101 int symbol, cf;
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]);
110 return symbol;
113 static inline int range_get_symbol_3970(void)
115 int symbol, cf;
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]);
124 return symbol;
127 /* MAIN DECODING FUNCTIONS */
129 struct rice_t
131 uint32_t k;
132 uint32_t ksum;
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);
142 if (rice->k == 0) {
143 rice->k = 1;
144 } else {
145 uint32_t lim = 1 << (rice->k + 4);
146 if (rice->ksum < lim) {
147 rice->k--;
148 } else if (rice->ksum >= 2 * lim) {
149 rice->k++;
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;
188 } else {
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 */
198 if (x & 1)
199 return (x >> 1) + 1;
200 else
201 return -(x >> 1);
205 static inline int entropy_decode3970(struct rice_t* rice)
207 int x, tmpk;
209 int overflow = range_get_symbol_3970();
211 if (overflow == (MODEL_ELEMENTS - 1)) {
212 tmpk = range_decode_bits(5);
213 overflow = 0;
214 } else {
215 tmpk = (rice->k < 1) ? 0 : rice->k - 1;
218 if (tmpk <= 16) {
219 x = range_decode_bits(tmpk);
220 } else {
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 */
229 if (x & 1)
230 return (x >> 1) + 1;
231 else
232 return -(x >> 1);
235 void init_entropy_decoder(struct ape_ctx_t* ape_ctx,
236 unsigned char* inbuffer, int* firstbyte,
237 int* bytesconsumed)
239 bytebuffer = inbuffer;
240 bytebufferoffset = *firstbyte;
242 /* Read the CRC */
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 */
262 riceX.k = 10;
263 riceX.ksum = (1 << riceX.k) * 16;
264 riceY.k = 10;
265 riceY.ksum = (1 << riceY.k) * 16;
267 /* The first 8 bits of input are ignored. */
268 skip_byte();
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,
279 int* bytesconsumed,
280 int32_t* decoded0, int32_t* decoded1,
281 int blockstodecode)
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));
292 } else {
293 if (ape_ctx->fileversion > 3970) {
294 while (blockstodecode--) {
295 *(decoded0++) = entropy_decode3980(&riceY);
296 if (decoded1 != NULL)
297 *(decoded1++) = entropy_decode3980(&riceX);
299 } else {
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;
317 return(0);