summaryrefslogtreecommitdiff
path: root/apps/codecs/demac/libdemac
diff options
context:
space:
mode:
Diffstat (limited to 'apps/codecs/demac/libdemac')
-rw-r--r--apps/codecs/demac/libdemac/Makefile43
-rw-r--r--apps/codecs/demac/libdemac/SOURCES10
-rw-r--r--apps/codecs/demac/libdemac/crc.c119
-rw-r--r--apps/codecs/demac/libdemac/decoder.c184
-rw-r--r--apps/codecs/demac/libdemac/decoder.h40
-rw-r--r--apps/codecs/demac/libdemac/demac.h45
-rw-r--r--apps/codecs/demac/libdemac/entropy.c314
-rw-r--r--apps/codecs/demac/libdemac/entropy.h40
-rw-r--r--apps/codecs/demac/libdemac/filter.c215
-rw-r--r--apps/codecs/demac/libdemac/filter.h48
-rw-r--r--apps/codecs/demac/libdemac/filter_1280_15.c27
-rw-r--r--apps/codecs/demac/libdemac/filter_16_11.c27
-rw-r--r--apps/codecs/demac/libdemac/filter_256_13.c27
-rw-r--r--apps/codecs/demac/libdemac/filter_32_10.c27
-rw-r--r--apps/codecs/demac/libdemac/filter_64_11.c27
-rw-r--r--apps/codecs/demac/libdemac/parser.c357
-rw-r--r--apps/codecs/demac/libdemac/parser.h137
-rw-r--r--apps/codecs/demac/libdemac/predictor.c196
-rw-r--r--apps/codecs/demac/libdemac/predictor.h36
-rw-r--r--apps/codecs/demac/libdemac/rangecoding.h180
-rw-r--r--apps/codecs/demac/libdemac/vector_math16.h140
-rw-r--r--apps/codecs/demac/libdemac/vector_math32.h54
22 files changed, 2293 insertions, 0 deletions
diff --git a/apps/codecs/demac/libdemac/Makefile b/apps/codecs/demac/libdemac/Makefile
new file mode 100644
index 0000000000..7fc784dd5f
--- /dev/null
+++ b/apps/codecs/demac/libdemac/Makefile
@@ -0,0 +1,43 @@
1# __________ __ ___.
2# Open \______ \ ____ ____ | | _\_ |__ _______ ___
3# Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
4# Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
5# Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
6# \/ \/ \/ \/ \/
7# $Id:$
8#
9
10INCLUDES=-I$(APPSDIR) -I.. -I. -I$(FIRMDIR)/include -I$(FIRMDIR)/export \
11 -I$(FIRMDIR)/common -I$(FIRMDIR)/drivers -I$(BUILDDIR)
12
13ifdef APPEXTRA
14 INCLUDES += $(patsubst %,-I$(APPSDIR)/%,$(subst :, ,$(APPEXTRA)))
15endif
16
17DEMACOPTS = -O3 -DROCKBOX
18CFLAGS = $(INCLUDES) $(GCCOPTS) $(TARGET_INC) $(DEMACOPTS) $(TARGET) \
19$(EXTRA_DEFINES) -DMEM=${MEMORYSIZE} $(PROFILE_OPTS)
20
21# This sets up 'SRC' based on the files mentioned in SOURCES
22include $(TOOLSDIR)/makesrc.inc
23
24SOURCES = $(SRC)
25OBJS2 := $(SRC:%.c=$(OBJDIR)/%.o)
26OBJS = $(patsubst %.S, $(OBJDIR)/%.o, $(OBJS2))
27DEPFILE = $(OBJDIR)/dep-libdemac
28DIRS =
29
30all: $(OUTPUT)
31
32$(OUTPUT): $(OBJS)
33 $(call PRINTS,AR+RANLIB $(@F))$(AR) ruv $@ $+ >/dev/null 2>&1
34 $(SILENT)$(RANLIB) $@
35
36include $(TOOLSDIR)/make.inc
37
38clean:
39 $(call PRINTS,cleaning libdemac)rm -f $(OBJS) $(OUTPUT) $(DEPFILE)
40
41ifneq ($(MAKECMDGOALS),clean)
42-include $(DEPFILE)
43endif
diff --git a/apps/codecs/demac/libdemac/SOURCES b/apps/codecs/demac/libdemac/SOURCES
new file mode 100644
index 0000000000..76b891a90d
--- /dev/null
+++ b/apps/codecs/demac/libdemac/SOURCES
@@ -0,0 +1,10 @@
1crc.c
2predictor.c
3entropy.c
4decoder.c
5parser.c
6filter_1280_15.c
7filter_16_11.c
8filter_256_13.c
9filter_32_10.c
10filter_64_11.c
diff --git a/apps/codecs/demac/libdemac/crc.c b/apps/codecs/demac/libdemac/crc.c
new file mode 100644
index 0000000000..c23de7d043
--- /dev/null
+++ b/apps/codecs/demac/libdemac/crc.c
@@ -0,0 +1,119 @@
1/*
2
3libdemac - A Monkey's Audio decoder
4
5$Id:$
6
7Copyright (C) Dave Chapman 2007
8
9This program is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2 of the License, or
12(at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
22
23*/
24
25#include <inttypes.h>
26
27static uint32_t crctab32[] =
28{
29 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA,
30 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3,
31 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988,
32 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91,
33 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE,
34 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7,
35 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC,
36 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5,
37 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172,
38 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B,
39 0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940,
40 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59,
41 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116,
42 0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F,
43 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924,
44 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D,
45
46 0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A,
47 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433,
48 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818,
49 0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01,
50 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E,
51 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457,
52 0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C,
53 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65,
54 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2,
55 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB,
56 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0,
57 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9,
58 0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086,
59 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F,
60 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4,
61 0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD,
62
63 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A,
64 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683,
65 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8,
66 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1,
67 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE,
68 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7,
69 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC,
70 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5,
71 0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252,
72 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B,
73 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60,
74 0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79,
75 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236,
76 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F,
77 0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04,
78 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D,
79
80 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A,
81 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713,
82 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38,
83 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21,
84 0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E,
85 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777,
86 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C,
87 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45,
88 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2,
89 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB,
90 0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0,
91 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9,
92 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6,
93 0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF,
94 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94,
95 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D
96};
97
98uint32_t ape_initcrc(void)
99{
100 return 0xffffffff;
101}
102
103/* Update the CRC from a block of WAV-format audio data */
104uint32_t ape_updatecrc(unsigned char *block, int count, uint32_t crc)
105{
106 while (count--)
107 crc = (crc >> 8) ^ crctab32[(crc & 0xff) ^ *block++];
108
109 return crc;
110}
111
112uint32_t ape_finishcrc(uint32_t crc)
113{
114 crc ^= 0xffffffff;
115 crc >>= 1;
116
117 return crc;
118}
119
diff --git a/apps/codecs/demac/libdemac/decoder.c b/apps/codecs/demac/libdemac/decoder.c
new file mode 100644
index 0000000000..22b6e8d325
--- /dev/null
+++ b/apps/codecs/demac/libdemac/decoder.c
@@ -0,0 +1,184 @@
1/*
2
3libdemac - A Monkey's Audio decoder
4
5$Id:$
6
7Copyright (C) Dave Chapman 2007
8
9This program is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2 of the License, or
12(at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
22
23*/
24
25#include <inttypes.h>
26#include <string.h>
27
28#include "demac.h"
29#include "predictor.h"
30#include "entropy.h"
31#include "filter.h"
32
33/* Statically allocate the filter buffers */
34
35static int16_t filterbuf32[(32*3 + HISTORY_SIZE) * 2] IBSS_ATTR; /* 4480 bytes */
36static int16_t filterbuf256[(256*3 + HISTORY_SIZE) * 2] IBSS_ATTR; /* 5120 bytes */
37
38/* This is only needed for "insane" files, and no Rockbox targets can
39 hope to decode them in realtime anyway. */
40static int16_t filterbuf1280[(1280*3 + HISTORY_SIZE) * 2]; /* 17408 bytes */
41
42void init_frame_decoder(struct ape_ctx_t* ape_ctx,
43 unsigned char* inbuffer, int* firstbyte,
44 int* bytesconsumed)
45{
46 init_entropy_decoder(ape_ctx, inbuffer, firstbyte, bytesconsumed);
47 //printf("CRC=0x%08x\n",ape_ctx->CRC);
48 //printf("Flags=0x%08x\n",ape_ctx->frameflags);
49
50 init_predictor_decoder(ape_ctx);
51
52 switch (ape_ctx->compressiontype)
53 {
54 case 2000:
55 init_filter_16_11(filterbuf32);
56 break;
57
58 case 3000:
59 init_filter_64_11(filterbuf256);
60 break;
61
62 case 4000:
63 init_filter_256_13(filterbuf256);
64 init_filter_32_10(filterbuf32);
65 break;
66
67 case 5000:
68 init_filter_1280_15(filterbuf1280);
69 init_filter_256_13(filterbuf256);
70 init_filter_16_11(filterbuf32);
71 }
72}
73
74int decode_chunk(struct ape_ctx_t* ape_ctx,
75 unsigned char* inbuffer, int* firstbyte,
76 int* bytesconsumed,
77 int32_t* decoded0, int32_t* decoded1,
78 int count)
79{
80 int res;
81 int32_t left, right;
82#ifdef ROCKBOX
83 int scale = (APE_OUTPUT_DEPTH - ape_ctx->bps);
84 #define SCALE(x) ((x) << scale)
85#else
86 #define SCALE(x) (x)
87#endif
88
89 if ((ape_ctx->channels==1) || (ape_ctx->frameflags & APE_FRAMECODE_PSEUDO_STEREO)) {
90 if (ape_ctx->frameflags & APE_FRAMECODE_STEREO_SILENCE) {
91 res = entropy_decode(ape_ctx, inbuffer, firstbyte, bytesconsumed, decoded0, decoded1, count);
92 /* We are pure silence, so we're done. */
93 return 0;
94 } else {
95 res = entropy_decode(ape_ctx, inbuffer, firstbyte, bytesconsumed, decoded0, NULL, count);
96 }
97
98 switch (ape_ctx->compressiontype)
99 {
100 case 2000:
101 apply_filter_16_11(ape_ctx->fileversion,decoded0,NULL,count);
102 break;
103
104 case 3000:
105 apply_filter_64_11(ape_ctx->fileversion,decoded0,NULL,count);
106 break;
107
108 case 4000:
109 apply_filter_32_10(ape_ctx->fileversion,decoded0,NULL,count);
110 apply_filter_256_13(ape_ctx->fileversion,decoded0,NULL,count);
111 break;
112
113 case 5000:
114 apply_filter_16_11(ape_ctx->fileversion,decoded0,NULL,count);
115 apply_filter_256_13(ape_ctx->fileversion,decoded0,NULL,count);
116 apply_filter_1280_15(ape_ctx->fileversion,decoded0,NULL,count);
117 }
118
119 /* Now apply the predictor decoding */
120 predictor_decode_mono(ape_ctx,decoded0,count);
121
122 if (ape_ctx->channels==2) {
123 /* Pseudo-stereo - just copy left channel to right channel */
124 while (count--)
125 {
126 left = *decoded0;
127 *(decoded1++) = *(decoded0++) = SCALE(left);
128 }
129 } else {
130 /* Mono - do nothing unless it's 8-bit audio */
131 if (ape_ctx->bps == 8) {
132 /* TODO: Handle 8-bit streams */
133 }
134 }
135 } else { /* Stereo */
136 if (ape_ctx->frameflags & APE_FRAMECODE_STEREO_SILENCE) {
137 /* We are pure silence, so we're done. */
138 return 0;
139 }
140
141 res = entropy_decode(ape_ctx, inbuffer, firstbyte, bytesconsumed, decoded0, decoded1, count);
142
143 /* Apply filters - compression type 1000 doesn't have any */
144 switch (ape_ctx->compressiontype)
145 {
146 case 2000:
147 apply_filter_16_11(ape_ctx->fileversion,decoded0,decoded1,count);
148 break;
149
150 case 3000:
151 apply_filter_64_11(ape_ctx->fileversion,decoded0,decoded1,count);
152 break;
153
154 case 4000:
155 apply_filter_32_10(ape_ctx->fileversion,decoded0,decoded1,count);
156 apply_filter_256_13(ape_ctx->fileversion,decoded0,decoded1,count);
157 break;
158
159 case 5000:
160 apply_filter_16_11(ape_ctx->fileversion,decoded0,decoded1,count);
161 apply_filter_256_13(ape_ctx->fileversion,decoded0,decoded1,count);
162 apply_filter_1280_15(ape_ctx->fileversion,decoded0,decoded1,count);
163 }
164
165 /* Now apply the predictor decoding */
166 predictor_decode_stereo(ape_ctx,decoded0,decoded1,count);
167
168 if (ape_ctx->bps == 8) {
169 /* TODO: Handle 8-bit streams */
170 } else {
171 /* Decorrelate and scale to output depth */
172 while (count--)
173 {
174 left = *decoded1 - (*decoded0 / 2);
175 right = left + *decoded0;
176
177 *(decoded0++) = SCALE(left);
178 *(decoded1++) = SCALE(right);
179 }
180 }
181 }
182
183 return res;
184}
diff --git a/apps/codecs/demac/libdemac/decoder.h b/apps/codecs/demac/libdemac/decoder.h
new file mode 100644
index 0000000000..0c3bd15b37
--- /dev/null
+++ b/apps/codecs/demac/libdemac/decoder.h
@@ -0,0 +1,40 @@
1/*
2
3libdemac - A Monkey's Audio decoder
4
5$Id:$
6
7Copyright (C) Dave Chapman 2007
8
9This program is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2 of the License, or
12(at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
22
23*/
24
25#ifndef _APE_DECODER_H
26#define _APE_DECODER_H
27
28#include <inttypes.h>
29#include "parser.h"
30
31void init_frame_decoder(struct ape_ctx_t* ape_ctx,
32 unsigned char* inbuffer, int* firstbyte,
33 int* bytesconsumed);
34
35int decode_chunk(struct ape_ctx_t* ape_ctx,
36 unsigned char* inbuffer, int* firstbyte,
37 int* bytesconsumed,
38 int32_t* decoded0, int32_t* decoded1,
39 int count);
40#endif
diff --git a/apps/codecs/demac/libdemac/demac.h b/apps/codecs/demac/libdemac/demac.h
new file mode 100644
index 0000000000..d1bb361574
--- /dev/null
+++ b/apps/codecs/demac/libdemac/demac.h
@@ -0,0 +1,45 @@
1/*
2
3libdemac - A Monkey's Audio decoder
4
5$Id:$
6
7Copyright (C) Dave Chapman 2007
8
9This program is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2 of the License, or
12(at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
22
23*/
24
25#ifndef _APE_DECODER_H
26#define _APE_DECODER_H
27
28#include <inttypes.h>
29#include "parser.h"
30
31void init_frame_decoder(struct ape_ctx_t* ape_ctx,
32 unsigned char* inbuffer, int* firstbyte,
33 int* bytesconsumed);
34
35int decode_chunk(struct ape_ctx_t* ape_ctx,
36 unsigned char* inbuffer, int* firstbyte,
37 int* bytesconsumed,
38 int32_t* decoded0, int32_t* decoded1,
39 int count);
40
41uint32_t ape_initcrc(void);
42uint32_t ape_updatecrc(unsigned char *block, int count, uint32_t crc);
43uint32_t ape_finishcrc(uint32_t crc);
44
45#endif
diff --git a/apps/codecs/demac/libdemac/entropy.c b/apps/codecs/demac/libdemac/entropy.c
new file mode 100644
index 0000000000..2c60420250
--- /dev/null
+++ b/apps/codecs/demac/libdemac/entropy.c
@@ -0,0 +1,314 @@
1/*
2
3libdemac - A Monkey's Audio decoder
4
5$Id:$
6
7Copyright (C) Dave Chapman 2007
8
9This program is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2 of the License, or
12(at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
22
23*/
24
25#include <inttypes.h>
26#include <string.h>
27
28#include "parser.h"
29#include "entropy.h"
30#include "rangecoding.h" /* Range-coding (static inline) functions */
31
32#define MODEL_ELEMENTS 64
33
34/*
35 The following counts arrays for use with the range decoder are
36 hard-coded in the Monkey's Audio decoder.
37*/
38
39static const int counts_3970[65] ICONST_ATTR =
40{
41 0,14824,28224,39348,47855,53994,58171,60926,
42 62682,63786,64463,64878,65126,65276,65365,65419,
43 65450,65469,65480,65487,65491,65493,65494,65495,
44 65496,65497,65498,65499,65500,65501,65502,65503,
45 65504,65505,65506,65507,65508,65509,65510,65511,
46 65512,65513,65514,65515,65516,65517,65518,65519,
47 65520,65521,65522,65523,65524,65525,65526,65527,
48 65528,65529,65530,65531,65532,65533,65534,65535,
49 65536
50};
51
52/* counts_diff_3970[i] = counts_3970[i+1] - counts_3970[i] */
53static const int counts_diff_3970[64] ICONST_ATTR =
54{
55 14824,13400,11124,8507,6139,4177,2755,1756,
56 1104,677,415,248,150,89,54,31,
57 19,11,7,4,2,1,1,1,
58 1,1,1,1,1,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};
64
65static const int counts_3980[65] ICONST_ATTR =
66{
67 0,19578,36160,48417,56323,60899,63265,64435,
68 64971,65232,65351,65416,65447,65466,65476,65482,
69 65485,65488,65490,65491,65492,65493,65494,65495,
70 65496,65497,65498,65499,65500,65501,65502,65503,
71 65504,65505,65506,65507,65508,65509,65510,65511,
72 65512,65513,65514,65515,65516,65517,65518,65519,
73 65520,65521,65522,65523,65524,65525,65526,65527,
74 65528,65529,65530,65531,65532,65533,65534,65535,
75 65536
76};
77
78/* counts_diff_3980[i] = counts_3980[i+1] - counts_3980[i] */
79
80static const int counts_diff_3980[64] ICONST_ATTR =
81{
82 19578,16582,12257,7906,4576,2366,1170,536,
83 261,119,65,31,19,10,6,3,
84 3,2,1,1,1,1,1,1,
85 1,1,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};
91
92/*
93 range_get_symbol_* functions based on main decoding loop in simple_d.c from
94 http://www.compressconsult.com/rangecoder/rngcod13.zip
95 (c) Michael Schindler
96*/
97
98static inline int range_get_symbol_3980(void)
99{
100 int symbol, cf;
101
102 cf = range_decode_culshift(16);
103
104 /* figure out the symbol inefficiently; a binary search would be much better */
105 for (symbol = 0; counts_3980[symbol+1] <= cf; symbol++);
106
107 range_decode_update(counts_diff_3980[symbol],counts_3980[symbol]);
108
109 return symbol;
110}
111
112static inline int range_get_symbol_3970(void)
113{
114 int symbol, cf;
115
116 cf = range_decode_culshift(16);
117
118 /* figure out the symbol inefficiently; a binary search would be much better */
119 for (symbol = 0; counts_3970[symbol+1] <= cf; symbol++);
120
121 range_decode_update(counts_diff_3970[symbol],counts_3970[symbol]);
122
123 return symbol;
124}
125
126/* MAIN DECODING FUNCTIONS */
127
128struct rice_t
129{
130 uint32_t k;
131 uint32_t ksum;
132};
133
134static struct rice_t riceX IBSS_ATTR;
135static struct rice_t riceY IBSS_ATTR;
136
137static inline void update_rice(struct rice_t* rice, int x)
138{
139 rice->ksum += ((x + 1) / 2) - ((rice->ksum + 16) >> 5);
140
141 if (rice->k == 0) {
142 rice->k = 1;
143 } else if (rice->ksum < ((uint32_t)1 << (rice->k + 4))) {
144 rice->k--;
145 } else if (rice->ksum >= ((uint32_t)1 << (rice->k + 5))) {
146 rice->k++;
147 }
148}
149
150static inline int entropy_decode3980(struct rice_t* rice)
151{
152 int base, x, pivot, overflow;
153
154 pivot = rice->ksum >> 5;
155 if (pivot == 0) pivot=1;
156
157 overflow = range_get_symbol_3980();
158
159 if (overflow == (MODEL_ELEMENTS-1)) {
160 overflow = range_decode_short() << 16;
161 overflow |= range_decode_short();
162 }
163
164 if (pivot >= 0x10000) {
165 /* Codepath for 24-bit streams */
166 int nbits, lo_bits, base_hi, base_lo;
167
168 /* Count the number of bits in pivot */
169 nbits = 17; /* We know there must be at least 17 bits */
170 while ((pivot >> nbits) > 0) { nbits++; }
171
172 /* base_lo is the low (nbits-16) bits of base
173 base_hi is the high 16 bits of base
174 */
175 lo_bits = (nbits - 16);
176
177 base_hi = range_decode_culfreq((pivot >> lo_bits) + 1);
178 range_decode_update(1, base_hi);
179
180 base_lo = range_decode_culfreq(1 << lo_bits);
181 range_decode_update(1, base_lo);
182
183 base = (base_hi << lo_bits) + base_lo;
184 } else {
185 /* Codepath for 16-bit streams */
186 base = range_decode_culfreq(pivot);
187 range_decode_update(1, base);
188 }
189
190 x = base + (overflow * pivot);
191 update_rice(rice, x);
192
193 /* Convert to signed */
194 if (x & 1)
195 return (x >> 1) + 1;
196 else
197 return -(x >> 1);
198}
199
200
201static inline int entropy_decode3970(struct rice_t* rice)
202{
203 int x, tmpk;
204
205 int overflow = range_get_symbol_3970();
206
207 if (overflow == (MODEL_ELEMENTS - 1)) {
208 tmpk = range_decode_bits(5);
209 overflow = 0;
210 } else {
211 tmpk = (rice->k < 1) ? 0 : rice->k - 1;
212 }
213
214 if (tmpk <= 16) {
215 x = range_decode_bits(tmpk);
216 } else {
217 x = range_decode_short();
218 x |= (range_decode_bits(tmpk - 16) << 16);
219 }
220 x += (overflow << tmpk);
221
222 update_rice(rice, x);
223
224 /* Convert to signed */
225 if (x & 1)
226 return (x >> 1) + 1;
227 else
228 return -(x >> 1);
229}
230
231void init_entropy_decoder(struct ape_ctx_t* ape_ctx,
232 unsigned char* inbuffer, int* firstbyte,
233 int* bytesconsumed)
234{
235 bytebuffer = inbuffer;
236 bytebufferoffset = *firstbyte;
237
238 /* Read the CRC */
239 ape_ctx->CRC = read_byte();
240 ape_ctx->CRC = (ape_ctx->CRC << 8) | read_byte();
241 ape_ctx->CRC = (ape_ctx->CRC << 8) | read_byte();
242 ape_ctx->CRC = (ape_ctx->CRC << 8) | read_byte();
243
244 /* Read the frame flags if they exist */
245 ape_ctx->frameflags = 0;
246 if ((ape_ctx->fileversion > 3820) && (ape_ctx->CRC & 0x80000000)) {
247 ape_ctx->CRC &= ~0x80000000;
248
249 ape_ctx->frameflags = read_byte();
250 ape_ctx->frameflags = (ape_ctx->frameflags << 8) | read_byte();
251 ape_ctx->frameflags = (ape_ctx->frameflags << 8) | read_byte();
252 ape_ctx->frameflags = (ape_ctx->frameflags << 8) | read_byte();
253 }
254 /* Keep a count of the blocks decoded in this frame */
255 ape_ctx->blocksdecoded = 0;
256
257 /* Initialise the rice structs */
258 riceX.k = 10;
259 riceX.ksum = (1 << riceX.k) * 16;
260 riceY.k = 10;
261 riceY.ksum = (1 << riceY.k) * 16;
262
263 /* The first 8 bits of input are ignored. */
264 skip_byte();
265
266 range_start_decoding();
267
268 /* Return the new state of the buffer */
269 *bytesconsumed = (intptr_t)bytebuffer - (intptr_t)inbuffer;
270 *firstbyte = bytebufferoffset;
271}
272
273int entropy_decode(struct ape_ctx_t* ape_ctx,
274 unsigned char* inbuffer, int* firstbyte,
275 int* bytesconsumed,
276 int32_t* decoded0, int32_t* decoded1,
277 int blockstodecode)
278{
279 bytebuffer = inbuffer;
280 bytebufferoffset = *firstbyte;
281
282 ape_ctx->blocksdecoded += blockstodecode;
283
284 if (ape_ctx->frameflags & APE_FRAMECODE_STEREO_SILENCE) {
285 /* We are pure silence, just memset the output buffer. */
286 memset(decoded0, 0, blockstodecode * sizeof(int32_t));
287 memset(decoded1, 0, blockstodecode * sizeof(int32_t));
288 } else {
289 if (ape_ctx->fileversion > 3970) {
290 while (blockstodecode--) {
291 *(decoded0++) = entropy_decode3980(&riceY);
292 if (decoded1 != NULL)
293 *(decoded1++) = entropy_decode3980(&riceX);
294 }
295 } else {
296 while (blockstodecode--) {
297 *(decoded0++) = entropy_decode3970(&riceY);
298 if (decoded1 != NULL)
299 *(decoded1++) = entropy_decode3970(&riceX);
300 }
301 }
302 }
303
304 if (ape_ctx->blocksdecoded == ape_ctx->currentframeblocks)
305 {
306 range_done_decoding();
307 }
308
309 /* Return the new state of the buffer */
310 *bytesconsumed = bytebuffer - inbuffer;
311 *firstbyte = bytebufferoffset;
312
313 return(0);
314}
diff --git a/apps/codecs/demac/libdemac/entropy.h b/apps/codecs/demac/libdemac/entropy.h
new file mode 100644
index 0000000000..dd6736cb15
--- /dev/null
+++ b/apps/codecs/demac/libdemac/entropy.h
@@ -0,0 +1,40 @@
1/*
2
3libdemac - A Monkey's Audio decoder
4
5$Id:$
6
7Copyright (C) Dave Chapman 2007
8
9This program is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2 of the License, or
12(at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
22
23*/
24
25#ifndef _APE_ENTROPY_H
26#define _APE_ENTROPY_H
27
28#include <inttypes.h>
29
30void init_entropy_decoder(struct ape_ctx_t* ape_ctx,
31 unsigned char* inbuffer, int* firstbyte,
32 int* bytesconsumed);
33
34int entropy_decode(struct ape_ctx_t* ape_ctx,
35 unsigned char* inbuffer, int* firstbyte,
36 int* bytesconsumed,
37 int32_t* decoded0, int32_t* decoded1,
38 int blockstodecode);
39
40#endif
diff --git a/apps/codecs/demac/libdemac/filter.c b/apps/codecs/demac/libdemac/filter.c
new file mode 100644
index 0000000000..76faa67aa7
--- /dev/null
+++ b/apps/codecs/demac/libdemac/filter.c
@@ -0,0 +1,215 @@
1/*
2
3libdemac - A Monkey's Audio decoder
4
5$Id:$
6
7Copyright (C) Dave Chapman 2007
8
9This program is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2 of the License, or
12(at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
22
23*/
24
25#include <string.h>
26#include <inttypes.h>
27
28#include "demac.h"
29#include "filter.h"
30
31#include "vector_math16.h"
32
33struct filter_t {
34 int16_t* coeffs; /* ORDER entries */
35
36 /* We store all the filter delays in a single buffer */
37 int16_t* historybuffer; /* ORDER*2 + HISTORY_SIZE entries */
38
39 int16_t* delay;
40 int16_t* adaptcoeffs;
41
42 int avg;
43};
44
45/* We name the functions according to the ORDER and FRACBITS
46 pre-processor symbols and build multiple .o files from this .c file
47 - this increases code-size but gives the compiler more scope for
48 optimising the individual functions, as well as replacing a lot of
49 variables with constants.
50*/
51
52#if FRACBITS == 11
53 #if ORDER == 16
54 #define INIT_FILTER init_filter_16_11
55 #define APPLY_FILTER apply_filter_16_11
56 #elif ORDER == 64
57 #define INIT_FILTER init_filter_64_11
58 #define APPLY_FILTER apply_filter_64_11
59 #endif
60#elif FRACBITS == 13
61 #define INIT_FILTER init_filter_256_13
62 #define APPLY_FILTER apply_filter_256_13
63#elif FRACBITS == 10
64 #define INIT_FILTER init_filter_32_10
65 #define APPLY_FILTER apply_filter_32_10
66#elif FRACBITS == 15
67 #define INIT_FILTER init_filter_1280_15
68 #define APPLY_FILTER apply_filter_1280_15
69#endif
70
71/* Some macros to handle the fixed-point stuff */
72
73#define FP_HALF (1 << (FRACBITS - 1)) /* 0.5 in fixed-point format. */
74#define FP_TO_INT(x) ((x + FP_HALF) >> FRACBITS); /* round(x) */
75
76#define SATURATE(x) (int16_t)(((x) == (int16_t)(x)) ? (x) : ((x) >> 31) ^ 0x7FFF);
77
78/* Apply the filter with state f to count entries in data[] */
79
80static inline void do_apply_filter_3980(struct filter_t* f, int32_t* data, int count)
81{
82 int res;
83 int absres;
84
85 while(count--)
86 {
87 res = FP_TO_INT(scalarproduct(f->delay - ORDER, f->coeffs));
88
89 if (*data < 0)
90 vector_add(f->coeffs, f->adaptcoeffs - ORDER);
91 else if (*data > 0)
92 vector_sub(f->coeffs, f->adaptcoeffs - ORDER);
93
94 /* Convert res from (32-FRACBITS).FRACBITS fixed-point format to an
95 integer (rounding to nearest) and add the input value to
96 it */
97 res += *data;
98
99 *data++ = res;
100
101 /* Update the output history */
102 *f->delay++ = SATURATE(res);
103
104 /* Version 3.98 and later files */
105
106 /* Update the adaption coefficients */
107 absres = (res < 0 ? -res : res);
108
109 if (absres > (f->avg * 3))
110 *f->adaptcoeffs = ((res >> 25) & 64) - 32;
111 else if (absres > (f->avg * 4) / 3)
112 *f->adaptcoeffs = ((res >> 26) & 32) - 16;
113 else if (absres > 0)
114 *f->adaptcoeffs = ((res >> 27) & 16) - 8;
115 else
116 *f->adaptcoeffs = 0;
117
118 f->avg += (absres - f->avg) / 16;
119
120 f->adaptcoeffs[-1] >>= 1;
121 f->adaptcoeffs[-2] >>= 1;
122 f->adaptcoeffs[-8] >>= 1;
123
124 f->adaptcoeffs++;
125
126 /* Have we filled the history buffer? */
127 if (f->delay == f->historybuffer + HISTORY_SIZE + (ORDER*2)) {
128 memmove(f->historybuffer, f->delay - (ORDER*2),
129 (ORDER*2) * sizeof(int16_t));
130 f->delay = f->historybuffer + ORDER*2;
131 f->adaptcoeffs = f->historybuffer + ORDER;
132 }
133 }
134}
135
136static inline void do_apply_filter_3970(struct filter_t* f, int32_t* data, int count)
137{
138 int res;
139
140 while(count--)
141 {
142 res = FP_TO_INT(scalarproduct(f->delay - ORDER, f->coeffs));
143
144 if (*data < 0)
145 vector_add(f->coeffs, f->adaptcoeffs - ORDER);
146 else if (*data > 0)
147 vector_sub(f->coeffs, f->adaptcoeffs - ORDER);
148
149 /* Convert res from (32-FRACBITS).FRACBITS fixed-point format to an
150 integer (rounding to nearest) and add the input value to
151 it */
152 res += *data;
153
154 *data++ = res;
155
156 /* Update the output history */
157 *f->delay++ = SATURATE(res);
158
159 /* Version ??? to < 3.98 files (untested) */
160 f->adaptcoeffs[0] = (res == 0) ? 0 : ((res >> 28) & 8) - 4;
161 f->adaptcoeffs[-4] >>= 1;
162 f->adaptcoeffs[-8] >>= 1;
163
164 f->adaptcoeffs++;
165
166 /* Have we filled the history buffer? */
167 if (f->delay == f->historybuffer + HISTORY_SIZE + (ORDER*2)) {
168 memmove(f->historybuffer, f->delay - (ORDER*2),
169 (ORDER*2) * sizeof(int16_t));
170 f->delay = f->historybuffer + ORDER*2;
171 f->adaptcoeffs = f->historybuffer + ORDER;
172 }
173 }
174}
175
176static struct filter_t filter0 IBSS_ATTR;
177static struct filter_t filter1 IBSS_ATTR;
178
179static void do_init_filter(struct filter_t* f, int16_t* buf)
180{
181 f->coeffs = buf;
182 f->historybuffer = buf + ORDER;
183
184 /* Zero the output history buffer */
185 memset(f->historybuffer, 0 , (ORDER*2) * sizeof(int16_t));
186 f->delay = f->historybuffer + ORDER*2;
187 f->adaptcoeffs = f->historybuffer + ORDER;
188
189 /* Zero the co-efficients */
190 memset(f->coeffs, 0, ORDER * sizeof(int16_t));
191
192 /* Zero the running average */
193 f->avg = 0;
194}
195
196void INIT_FILTER(int16_t* buf)
197{
198 do_init_filter(&filter0, buf);
199 do_init_filter(&filter1, buf + ORDER * 3 + HISTORY_SIZE);
200}
201
202int APPLY_FILTER(int fileversion, int32_t* data0, int32_t* data1, int count)
203{
204 if (fileversion >= 3980) {
205 do_apply_filter_3980(&filter0, data0, count);
206 if (data1 != NULL)
207 do_apply_filter_3980(&filter1, data1, count);
208 } else {
209 do_apply_filter_3970(&filter0, data0, count);
210 if (data1 != NULL)
211 do_apply_filter_3970(&filter1, data1, count);
212 }
213
214 return 0;
215}
diff --git a/apps/codecs/demac/libdemac/filter.h b/apps/codecs/demac/libdemac/filter.h
new file mode 100644
index 0000000000..b98403cfc8
--- /dev/null
+++ b/apps/codecs/demac/libdemac/filter.h
@@ -0,0 +1,48 @@
1/*
2
3libdemac - A Monkey's Audio decoder
4
5$Id:$
6
7Copyright (C) Dave Chapman 2007
8
9This program is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2 of the License, or
12(at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
22
23*/
24
25#ifndef _APE_FILTER_H
26#define _APE_FILTER_H
27
28#include <inttypes.h>
29
30/* The size of the history buffers */
31#define HISTORY_SIZE 512
32
33void init_filter_16_11(int16_t* buf);
34int apply_filter_16_11(int fileversion, int32_t* decoded0, int32_t* decoded1, int count);
35
36void init_filter_64_11(int16_t* buf);
37int apply_filter_64_11(int fileversion, int32_t* decoded0, int32_t* decoded1, int count);
38
39void init_filter_32_10(int16_t* buf);
40int apply_filter_32_10(int fileversion, int32_t* decoded0, int32_t* decoded1, int count);
41
42void init_filter_256_13(int16_t* buf);
43int apply_filter_256_13(int fileversion, int32_t* decoded0, int32_t* decoded1, int count);
44
45void init_filter_1280_15(int16_t* buf);
46int apply_filter_1280_15(int fileversion, int32_t* decoded0, int32_t* decoded1, int count);
47
48#endif
diff --git a/apps/codecs/demac/libdemac/filter_1280_15.c b/apps/codecs/demac/libdemac/filter_1280_15.c
new file mode 100644
index 0000000000..edf1ce1bb1
--- /dev/null
+++ b/apps/codecs/demac/libdemac/filter_1280_15.c
@@ -0,0 +1,27 @@
1/*
2
3libdemac - A Monkey's Audio decoder
4
5$Id:$
6
7Copyright (C) Dave Chapman 2007
8
9This program is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2 of the License, or
12(at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
22
23*/
24
25#define ORDER 1280
26#define FRACBITS 15
27#include "filter.c"
diff --git a/apps/codecs/demac/libdemac/filter_16_11.c b/apps/codecs/demac/libdemac/filter_16_11.c
new file mode 100644
index 0000000000..07e4b96dc2
--- /dev/null
+++ b/apps/codecs/demac/libdemac/filter_16_11.c
@@ -0,0 +1,27 @@
1/*
2
3libdemac - A Monkey's Audio decoder
4
5$Id:$
6
7Copyright (C) Dave Chapman 2007
8
9This program is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2 of the License, or
12(at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
22
23*/
24
25#define ORDER 16
26#define FRACBITS 11
27#include "filter.c"
diff --git a/apps/codecs/demac/libdemac/filter_256_13.c b/apps/codecs/demac/libdemac/filter_256_13.c
new file mode 100644
index 0000000000..370dec1d5a
--- /dev/null
+++ b/apps/codecs/demac/libdemac/filter_256_13.c
@@ -0,0 +1,27 @@
1/*
2
3libdemac - A Monkey's Audio decoder
4
5$Id:$
6
7Copyright (C) Dave Chapman 2007
8
9This program is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2 of the License, or
12(at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
22
23*/
24
25#define ORDER 256
26#define FRACBITS 13
27#include "filter.c"
diff --git a/apps/codecs/demac/libdemac/filter_32_10.c b/apps/codecs/demac/libdemac/filter_32_10.c
new file mode 100644
index 0000000000..b585de98a4
--- /dev/null
+++ b/apps/codecs/demac/libdemac/filter_32_10.c
@@ -0,0 +1,27 @@
1/*
2
3libdemac - A Monkey's Audio decoder
4
5$Id:$
6
7Copyright (C) Dave Chapman 2007
8
9This program is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2 of the License, or
12(at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
22
23*/
24
25#define ORDER 32
26#define FRACBITS 10
27#include "filter.c"
diff --git a/apps/codecs/demac/libdemac/filter_64_11.c b/apps/codecs/demac/libdemac/filter_64_11.c
new file mode 100644
index 0000000000..8b8d326d1d
--- /dev/null
+++ b/apps/codecs/demac/libdemac/filter_64_11.c
@@ -0,0 +1,27 @@
1/*
2
3libdemac - A Monkey's Audio decoder
4
5$Id:$
6
7Copyright (C) Dave Chapman 2007
8
9This program is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2 of the License, or
12(at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
22
23*/
24
25#define ORDER 64
26#define FRACBITS 11
27#include "filter.c"
diff --git a/apps/codecs/demac/libdemac/parser.c b/apps/codecs/demac/libdemac/parser.c
new file mode 100644
index 0000000000..bcb542dbb6
--- /dev/null
+++ b/apps/codecs/demac/libdemac/parser.c
@@ -0,0 +1,357 @@
1/*
2
3libdemac - A Monkey's Audio decoder
4
5$Id:$
6
7Copyright (C) Dave Chapman 2007
8
9This program is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2 of the License, or
12(at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
22
23*/
24
25#include <inttypes.h>
26#include <string.h>
27#ifndef ROCKBOX
28#include <stdio.h>
29#include <stdlib.h>
30#include <sys/types.h>
31#include <sys/stat.h>
32#include <fcntl.h>
33#include <unistd.h>
34#endif
35
36#include "parser.h"
37
38
39static inline int16_t get_int16(unsigned char* buf)
40{
41 return(buf[0] | (buf[1] << 8));
42}
43
44static inline uint16_t get_uint16(unsigned char* buf)
45{
46 return(buf[0] | (buf[1] << 8));
47}
48
49static inline uint32_t get_uint32(unsigned char* buf)
50{
51 return(buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24));
52}
53
54
55int ape_parseheaderbuf(unsigned char* buf, struct ape_ctx_t* ape_ctx)
56{
57 unsigned char* header;
58
59 memset(ape_ctx,0,sizeof(struct ape_ctx_t));
60 /* TODO: Skip any leading junk such as id3v2 tags */
61 ape_ctx->junklength = 0;
62
63 memcpy(ape_ctx->magic, buf, 4);
64 if (memcmp(ape_ctx->magic,"MAC ",4)!=0)
65 {
66 return -1;
67 }
68
69 ape_ctx->fileversion = get_int16(buf + 4);
70
71 if (ape_ctx->fileversion >= 3980)
72 {
73 ape_ctx->padding1 = get_int16(buf + 6);
74 ape_ctx->descriptorlength = get_uint32(buf + 8);
75 ape_ctx->headerlength = get_uint32(buf + 12);
76 ape_ctx->seektablelength = get_uint32(buf + 16);
77 ape_ctx->wavheaderlength = get_uint32(buf + 20);
78 ape_ctx->audiodatalength = get_uint32(buf + 24);
79 ape_ctx->audiodatalength_high = get_uint32(buf + 28);
80 ape_ctx->wavtaillength = get_uint32(buf + 32);
81 memcpy(ape_ctx->md5, buf + 36, 16);
82
83 header = buf + ape_ctx->descriptorlength;
84
85 /* Read header data */
86 ape_ctx->compressiontype = get_uint16(header + 0);
87 ape_ctx->formatflags = get_uint16(header + 2);
88 ape_ctx->blocksperframe = get_uint32(header + 4);
89 ape_ctx->finalframeblocks = get_uint32(header + 8);
90 ape_ctx->totalframes = get_uint32(header + 12);
91 ape_ctx->bps = get_uint16(header + 16);
92 ape_ctx->channels = get_uint16(header + 18);
93 ape_ctx->samplerate = get_uint32(header + 20);
94
95 ape_ctx->firstframe = ape_ctx->junklength + ape_ctx->descriptorlength +
96 ape_ctx->headerlength + ape_ctx->seektablelength +
97 ape_ctx->wavheaderlength;
98 } else {
99 ape_ctx->compressiontype = get_uint16(buf + 6);
100 ape_ctx->formatflags = get_uint16(buf + 8);
101 ape_ctx->channels = get_uint16(buf + 10);
102 ape_ctx->samplerate = get_uint32(buf + 14);
103 ape_ctx->wavheaderlength = get_uint32(buf + 18);
104 ape_ctx->totalframes = get_uint32(buf + 26);
105 ape_ctx->finalframeblocks = get_uint32(buf + 30);
106 }
107
108 ape_ctx->totalsamples = ape_ctx->finalframeblocks;
109 if (ape_ctx->totalframes > 1)
110 ape_ctx->totalsamples += ape_ctx->blocksperframe * (ape_ctx->totalframes-1);
111
112 /* TODO: Parse and store seektable */
113
114 return 0;
115}
116
117
118#ifndef ROCKBOX
119/* Helper functions */
120
121static int read_uint16(int fd, uint16_t* x)
122{
123 unsigned char tmp[2];
124 int n;
125
126 n = read(fd,tmp,2);
127
128 if (n != 2)
129 return -1;
130
131 *x = tmp[0] | (tmp[1] << 8);
132
133 return 0;
134}
135
136static int read_int16(int fd, int16_t* x)
137{
138 return read_uint16(fd, (uint16_t*)x);
139}
140
141static int read_uint32(int fd, uint32_t* x)
142{
143 unsigned char tmp[4];
144 int n;
145
146 n = read(fd,tmp,4);
147
148 if (n != 4)
149 return -1;
150
151 *x = tmp[0] | (tmp[1] << 8) | (tmp[2] << 16) | (tmp[3] << 24);
152
153 return 0;
154}
155
156int ape_parseheader(int fd, struct ape_ctx_t* ape_ctx)
157{
158 int i,n;
159
160 /* TODO: Skip any leading junk such as id3v2 tags */
161 ape_ctx->junklength = 0;
162
163 lseek(fd,ape_ctx->junklength,SEEK_SET);
164
165 n = read(fd,&ape_ctx->magic,4);
166 if (n != 4) return -1;
167
168 if (memcmp(ape_ctx->magic,"MAC ",4)!=0)
169 {
170 return -1;
171 }
172
173 if (read_int16(fd,&ape_ctx->fileversion) < 0)
174 return -1;
175
176 if (ape_ctx->fileversion >= 3980)
177 {
178 if (read_int16(fd,&ape_ctx->padding1) < 0)
179 return -1;
180 if (read_uint32(fd,&ape_ctx->descriptorlength) < 0)
181 return -1;
182 if (read_uint32(fd,&ape_ctx->headerlength) < 0)
183 return -1;
184 if (read_uint32(fd,&ape_ctx->seektablelength) < 0)
185 return -1;
186 if (read_uint32(fd,&ape_ctx->wavheaderlength) < 0)
187 return -1;
188 if (read_uint32(fd,&ape_ctx->audiodatalength) < 0)
189 return -1;
190 if (read_uint32(fd,&ape_ctx->audiodatalength_high) < 0)
191 return -1;
192 if (read_uint32(fd,&ape_ctx->wavtaillength) < 0)
193 return -1;
194 if (read(fd,&ape_ctx->md5,16) != 16)
195 return -1;
196
197 /* Skip any unknown bytes at the end of the descriptor. This is for future
198 compatibility */
199 if (ape_ctx->descriptorlength > 52)
200 lseek(fd,ape_ctx->descriptorlength - 52, SEEK_CUR);
201
202 /* Read header data */
203 if (read_uint16(fd,&ape_ctx->compressiontype) < 0)
204 return -1;
205 if (read_uint16(fd,&ape_ctx->formatflags) < 0)
206 return -1;
207 if (read_uint32(fd,&ape_ctx->blocksperframe) < 0)
208 return -1;
209 if (read_uint32(fd,&ape_ctx->finalframeblocks) < 0)
210 return -1;
211 if (read_uint32(fd,&ape_ctx->totalframes) < 0)
212 return -1;
213 if (read_uint16(fd,&ape_ctx->bps) < 0)
214 return -1;
215 if (read_uint16(fd,&ape_ctx->channels) < 0)
216 return -1;
217 if (read_uint32(fd,&ape_ctx->samplerate) < 0)
218 return -1;
219 } else {
220 ape_ctx->descriptorlength = 0;
221 ape_ctx->headerlength = 32;
222
223 if (read_uint16(fd,&ape_ctx->compressiontype) < 0)
224 return -1;
225 if (read_uint16(fd,&ape_ctx->formatflags) < 0)
226 return -1;
227 if (read_uint16(fd,&ape_ctx->channels) < 0)
228 return -1;
229 if (read_uint32(fd,&ape_ctx->samplerate) < 0)
230 return -1;
231 if (read_uint32(fd,&ape_ctx->wavheaderlength) < 0)
232 return -1;
233 if (read_uint32(fd,&ape_ctx->wavtaillength) < 0)
234 return -1;
235 if (read_uint32(fd,&ape_ctx->totalframes) < 0)
236 return -1;
237 if (read_uint32(fd,&ape_ctx->finalframeblocks) < 0)
238 return -1;
239
240 if (ape_ctx->formatflags & MAC_FORMAT_FLAG_HAS_PEAK_LEVEL)
241 {
242 lseek(fd, 4, SEEK_CUR); /* Skip the peak level */
243 ape_ctx->headerlength += 4;
244 }
245
246 if (ape_ctx->formatflags & MAC_FORMAT_FLAG_HAS_SEEK_ELEMENTS)
247 {
248 if (read_uint32(fd,&ape_ctx->seektablelength) < 0)
249 return -1;
250 ape_ctx->headerlength += 4;
251 ape_ctx->seektablelength *= sizeof(int32_t);
252 } else {
253 ape_ctx->seektablelength = ape_ctx->totalframes * sizeof(int32_t);
254 }
255
256 if (ape_ctx->formatflags & MAC_FORMAT_FLAG_8_BIT)
257 ape_ctx->bps = 8;
258 else if (ape_ctx->formatflags & MAC_FORMAT_FLAG_24_BIT)
259 ape_ctx->bps = 24;
260 else
261 ape_ctx->bps = 16;
262
263 if (ape_ctx->fileversion >= 3950)
264 ape_ctx->blocksperframe = 73728 * 4;
265 else if ((ape_ctx->fileversion >= 3900) || (ape_ctx->fileversion >= 3800 && ape_ctx->compressiontype >= 4000))
266 ape_ctx->blocksperframe = 73728;
267 else
268 ape_ctx->blocksperframe = 9216;
269
270 /* Skip any stored wav header */
271 if (!(ape_ctx->formatflags & MAC_FORMAT_FLAG_CREATE_WAV_HEADER))
272 {
273 lseek(fd, ape_ctx->wavheaderlength, SEEK_CUR);
274 }
275 }
276
277 ape_ctx->totalsamples = ape_ctx->finalframeblocks;
278 if (ape_ctx->totalframes > 1)
279 ape_ctx->totalsamples += ape_ctx->blocksperframe * (ape_ctx->totalframes-1);
280
281 if (ape_ctx->seektablelength > 0)
282 {
283 ape_ctx->seektable = malloc(ape_ctx->seektablelength);
284 if (ape_ctx->seektable == NULL)
285 return -1;
286 for (i=0; i < ape_ctx->seektablelength / sizeof(uint32_t); i++)
287 {
288 if (read_uint32(fd,&ape_ctx->seektable[i]) < 0)
289 {
290 free(ape_ctx->seektable);
291 return -1;
292 }
293 }
294 }
295
296 ape_ctx->firstframe = ape_ctx->junklength + ape_ctx->descriptorlength +
297 ape_ctx->headerlength + ape_ctx->seektablelength +
298 ape_ctx->wavheaderlength;
299
300 return 0;
301}
302
303void ape_dumpinfo(struct ape_ctx_t* ape_ctx)
304{
305 int i;
306
307 printf("Descriptor Block:\n\n");
308 printf("magic = \"%c%c%c%c\"\n",
309 ape_ctx->magic[0],ape_ctx->magic[1],
310 ape_ctx->magic[2],ape_ctx->magic[3]);
311 printf("fileversion = %d\n",ape_ctx->fileversion);
312 printf("descriptorlength = %d\n",ape_ctx->descriptorlength);
313 printf("headerlength = %d\n",ape_ctx->headerlength);
314 printf("seektablelength = %d\n",ape_ctx->seektablelength);
315 printf("wavheaderlength = %d\n",ape_ctx->wavheaderlength);
316 printf("audiodatalength = %d\n",ape_ctx->audiodatalength);
317 printf("audiodatalength_high = %d\n",ape_ctx->audiodatalength_high);
318 printf("wavtaillength = %d\n",ape_ctx->wavtaillength);
319 printf("md5 = ");
320 for (i = 0; i < 16; i++)
321 printf("%02x",ape_ctx->md5[i]);
322 printf("\n");
323
324 printf("\nHeader Block:\n\n");
325
326 printf("compressiontype = %d\n",ape_ctx->compressiontype);
327 printf("formatflags = %d\n",ape_ctx->formatflags);
328 printf("blocksperframe = %d\n",ape_ctx->blocksperframe);
329 printf("finalframeblocks = %d\n",ape_ctx->finalframeblocks);
330 printf("totalframes = %d\n",ape_ctx->totalframes);
331 printf("bps = %d\n",ape_ctx->bps);
332 printf("channels = %d\n",ape_ctx->channels);
333 printf("samplerate = %d\n",ape_ctx->samplerate);
334
335 printf("\nSeektable\n\n");
336 if ((ape_ctx->seektablelength / sizeof(uint32_t)) != ape_ctx->totalframes)
337 {
338 printf("No seektable\n");
339 }
340 else
341 {
342 for ( i = 0; i < ape_ctx->seektablelength / sizeof(uint32_t) ; i++)
343 {
344 if (i < ape_ctx->totalframes-1) {
345 printf("%8d %d (%d bytes)\n",i,ape_ctx->seektable[i],ape_ctx->seektable[i+1]-ape_ctx->seektable[i]);
346 } else {
347 printf("%8d %d\n",i,ape_ctx->seektable[i]);
348 }
349 }
350 }
351 printf("\nCalculated information:\n\n");
352 printf("junklength = %d\n",ape_ctx->junklength);
353 printf("firstframe = %d\n",ape_ctx->firstframe);
354 printf("totalsamples = %d\n",ape_ctx->totalsamples);
355}
356
357#endif /* !ROCKBOX */
diff --git a/apps/codecs/demac/libdemac/parser.h b/apps/codecs/demac/libdemac/parser.h
new file mode 100644
index 0000000000..edf4222fd5
--- /dev/null
+++ b/apps/codecs/demac/libdemac/parser.h
@@ -0,0 +1,137 @@
1/*
2
3libdemac - A Monkey's Audio decoder
4
5$Id:$
6
7Copyright (C) Dave Chapman 2007
8
9This program is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2 of the License, or
12(at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
22
23*/
24
25#ifndef _APE_PARSER_H
26#define _APE_PARSER_H
27
28#include <inttypes.h>
29
30#ifdef ROCKBOX
31/* Include the Rockbox Codec API when building for Rockbox */
32#define APE_OUTPUT_DEPTH 29
33#ifndef ROCKBOX_PLUGIN
34#include "../lib/codeclib.h"
35#include <codecs.h>
36#endif
37#else
38#define APE_OUTPUT_DEPTH (ape_ctx->bps)
39#define IBSS_ATTR
40#define ICONST_ATTR
41#define ICODE_ATTR
42#endif
43
44/* The earliest and latest file formats supported by this library */
45#define APE_MIN_VERSION 3970
46#define APE_MAX_VERSION 3990
47
48#define MAC_FORMAT_FLAG_8_BIT 1 // is 8-bit [OBSOLETE]
49#define MAC_FORMAT_FLAG_CRC 2 // uses the new CRC32 error detection [OBSOLETE]
50#define MAC_FORMAT_FLAG_HAS_PEAK_LEVEL 4 // uint32 nPeakLevel after the header [OBSOLETE]
51#define MAC_FORMAT_FLAG_24_BIT 8 // is 24-bit [OBSOLETE]
52#define MAC_FORMAT_FLAG_HAS_SEEK_ELEMENTS 16 // has the number of seek elements after the peak level
53#define MAC_FORMAT_FLAG_CREATE_WAV_HEADER 32 // create the wave header on decompression (not stored)
54
55
56/* Special frame codes:
57
58 MONO_SILENCE - All PCM samples in frame are zero (mono streams only)
59 LEFT_SILENCE - All PCM samples for left channel in frame are zero (stereo streams)
60 RIGHT_SILENCE - All PCM samples for left channel in frame are zero (stereo streams)
61 PSEUDO_STEREO - Left and Right channels are identical
62
63*/
64
65#define APE_FRAMECODE_MONO_SILENCE 1
66#define APE_FRAMECODE_STEREO_SILENCE 3
67#define APE_FRAMECODE_PSEUDO_STEREO 4
68
69#define HISTORY_SIZE 512
70#define PREDICTOR_ORDER 8
71
72struct predictor_t
73{
74 /* Adaption co-efficients */
75 int32_t coeffsA[4];
76 int32_t coeffsB[5];
77
78 /* Filter histories */
79 int32_t historybuffer[HISTORY_SIZE + PREDICTOR_ORDER * 4];
80 int32_t* delayA;
81 int32_t* delayB;
82 int32_t* adaptcoeffsA;
83 int32_t* adaptcoeffsB;
84
85 int32_t lastA;
86
87 int32_t filterA;
88 int32_t filterB;
89};
90
91struct ape_ctx_t
92{
93 /* Derived fields */
94 uint32_t junklength;
95 uint32_t firstframe;
96 uint32_t totalsamples;
97
98 /* Info from Descriptor Block */
99 char magic[4];
100 int16_t fileversion;
101 int16_t padding1;
102 uint32_t descriptorlength;
103 uint32_t headerlength;
104 uint32_t seektablelength;
105 uint32_t wavheaderlength;
106 uint32_t audiodatalength;
107 uint32_t audiodatalength_high;
108 uint32_t wavtaillength;
109 uint8_t md5[16];
110
111 /* Info from Header Block */
112 uint16_t compressiontype;
113 uint16_t formatflags;
114 uint32_t blocksperframe;
115 uint32_t finalframeblocks;
116 uint32_t totalframes;
117 uint16_t bps;
118 uint16_t channels;
119 uint32_t samplerate;
120
121 /* Seektable */
122 uint32_t* seektable;
123
124 /* Decoder state */
125 uint32_t CRC;
126 int frameflags;
127 int currentframeblocks;
128 int blocksdecoded;
129 struct predictor_t predictorY;
130 struct predictor_t predictorX;
131};
132
133int ape_parseheader(int fd, struct ape_ctx_t* ape_ctx);
134int ape_parseheaderbuf(unsigned char* buf, struct ape_ctx_t* ape_ctx);
135void ape_dumpinfo(struct ape_ctx_t* ape_ctx);
136
137#endif
diff --git a/apps/codecs/demac/libdemac/predictor.c b/apps/codecs/demac/libdemac/predictor.c
new file mode 100644
index 0000000000..ef72fedfbd
--- /dev/null
+++ b/apps/codecs/demac/libdemac/predictor.c
@@ -0,0 +1,196 @@
1/*
2
3libdemac - A Monkey's Audio decoder
4
5$Id:$
6
7Copyright (C) Dave Chapman 2007
8
9This program is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2 of the License, or
12(at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
22
23*/
24
25#include <inttypes.h>
26#include <string.h>
27
28#include "parser.h"
29#include "predictor.h"
30
31#include "vector_math32.h"
32
33/* Return 0 if x is zero, -1 if x is positive, 1 if x is negative */
34#define SIGN(x) (x) ? (((x) > 0) ? -1 : 1) : 0
35
36static const int32_t initial_coeffs[4] = {
37 360, 317, -109, 98
38};
39
40static void init_predictor(struct predictor_t* p)
41{
42 /* Zero the history buffers */
43 memset(p->historybuffer, 0, (PREDICTOR_ORDER*4) * sizeof(int32_t));
44 p->delayA = p->historybuffer + PREDICTOR_ORDER*4;
45 p->delayB = p->historybuffer + PREDICTOR_ORDER*3;
46 p->adaptcoeffsA = p->historybuffer + PREDICTOR_ORDER*2;
47 p->adaptcoeffsB = p->historybuffer + PREDICTOR_ORDER;
48
49 /* Initialise and zero the co-efficients */
50 memcpy(p->coeffsA, initial_coeffs, sizeof(initial_coeffs));
51 memset(p->coeffsB, 0, sizeof(p->coeffsB));
52
53 p->filterA = 0;
54 p->filterB = 0;
55
56 p->lastA = 0;
57}
58
59static int do_predictor_decode(struct predictor_t* p, int32_t A, int32_t B)
60{
61 int32_t predictionA, predictionB, currentA;
62
63 p->delayA[0] = p->lastA;
64 p->delayA[-1] = p->delayA[0] - p->delayA[-1];
65
66 predictionA = scalarproduct4_rev32(p->coeffsA,p->delayA);
67
68 /* Apply a scaled first-order filter compression */
69 p->delayB[0] = B - ((p->filterB * 31) >> 5);
70 p->filterB = B;
71
72 p->delayB[-1] = p->delayB[0] - p->delayB[-1];
73
74 predictionB = scalarproduct5_rev32(p->coeffsB,p->delayB);
75
76 currentA = A + ((predictionA + (predictionB >> 1)) >> 10);
77
78 p->adaptcoeffsA[0] = SIGN(p->delayA[0]);
79 p->adaptcoeffsA[-1] = SIGN(p->delayA[-1]);
80
81 p->adaptcoeffsB[0] = SIGN(p->delayB[0]);
82 p->adaptcoeffsB[-1] = SIGN(p->delayB[-1]);
83
84 if (A > 0)
85 {
86 vector_sub4_rev32(p->coeffsA, p->adaptcoeffsA);
87 vector_sub5_rev32(p->coeffsB, p->adaptcoeffsB);
88 }
89 else if (A < 0)
90 {
91 vector_add4_rev32(p->coeffsA, p->adaptcoeffsA);
92 vector_add5_rev32(p->coeffsB, p->adaptcoeffsB);
93 }
94
95 p->delayA++;
96 p->delayB++;
97 p->adaptcoeffsA++;
98 p->adaptcoeffsB++;
99
100 /* Have we filled the history buffer? */
101 if (p->delayA == p->historybuffer + HISTORY_SIZE + (PREDICTOR_ORDER*4)) {
102 memmove(p->historybuffer, p->delayA - (PREDICTOR_ORDER*4),
103 (PREDICTOR_ORDER*4) * sizeof(int32_t));
104 p->delayA = p->historybuffer + PREDICTOR_ORDER*4;
105 p->delayB = p->historybuffer + PREDICTOR_ORDER*3;
106 p->adaptcoeffsA = p->historybuffer + PREDICTOR_ORDER*2;
107 p->adaptcoeffsB = p->historybuffer + PREDICTOR_ORDER;
108 }
109
110 p->lastA = currentA;
111 p->filterA = currentA + ((p->filterA * 31) >> 5);
112
113 return p->filterA;
114}
115
116static int32_t X;
117
118void init_predictor_decoder(struct ape_ctx_t* ape_ctx)
119{
120 X = 0;
121
122 init_predictor(&ape_ctx->predictorY);
123 init_predictor(&ape_ctx->predictorX);
124}
125
126int predictor_decode_stereo(struct ape_ctx_t* ape_ctx, int32_t* decoded0, int32_t* decoded1, int count) ICODE_ATTR;
127int predictor_decode_stereo(struct ape_ctx_t* ape_ctx, int32_t* decoded0, int32_t* decoded1, int count)
128{
129 while (count--)
130 {
131 *decoded0 = do_predictor_decode(&ape_ctx->predictorY, *decoded0, X);
132 X = do_predictor_decode(&ape_ctx->predictorX, *decoded1, *(decoded0)++);
133 *(decoded1++) = X;
134 }
135
136 return 0;
137}
138
139int predictor_decode_mono(struct ape_ctx_t* ape_ctx, int32_t* decoded0, int count)
140{
141 struct predictor_t* p = &ape_ctx->predictorY;
142 int32_t predictionA, currentA, A;
143
144 currentA = p->lastA;
145
146 while (count--)
147 {
148 A = *decoded0;
149
150 p->delayA[0] = currentA;
151 p->delayA[-1] = p->delayA[0] - p->delayA[-1];
152
153 predictionA = (p->delayA[0] * p->coeffsA[0]) +
154 (p->delayA[-1] * p->coeffsA[1]) +
155 (p->delayA[-2] * p->coeffsA[2]) +
156 (p->delayA[-3] * p->coeffsA[3]);
157
158 currentA = A + (predictionA >> 10);
159
160 p->adaptcoeffsA[0] = SIGN(p->delayA[0]);
161 p->adaptcoeffsA[-1] = SIGN(p->delayA[-1]);
162
163 if (A > 0)
164 {
165 p->coeffsA[0] -= p->adaptcoeffsA[0];
166 p->coeffsA[1] -= p->adaptcoeffsA[-1];
167 p->coeffsA[2] -= p->adaptcoeffsA[-2];
168 p->coeffsA[3] -= p->adaptcoeffsA[-3];
169 }
170 else if (A < 0)
171 {
172 p->coeffsA[0] += p->adaptcoeffsA[0];
173 p->coeffsA[1] += p->adaptcoeffsA[-1];
174 p->coeffsA[2] += p->adaptcoeffsA[-2];
175 p->coeffsA[3] += p->adaptcoeffsA[-3];
176 }
177
178 p->delayA++;
179 p->adaptcoeffsA++;
180
181 /* Have we filled the history buffer? */
182 if (p->delayA == p->historybuffer + HISTORY_SIZE + (PREDICTOR_ORDER*4)) {
183 memmove(p->historybuffer, p->delayA - (PREDICTOR_ORDER*4),
184 (PREDICTOR_ORDER*4) * sizeof(int32_t));
185 p->delayA = p->historybuffer + PREDICTOR_ORDER*4;
186 p->adaptcoeffsA = p->historybuffer + PREDICTOR_ORDER*2;
187 }
188
189 p->filterA = currentA + ((p->filterA * 31) >> 5);
190 *(decoded0++) = p->filterA;
191 }
192
193 p->lastA = currentA;
194
195 return 0;
196}
diff --git a/apps/codecs/demac/libdemac/predictor.h b/apps/codecs/demac/libdemac/predictor.h
new file mode 100644
index 0000000000..3c023c8188
--- /dev/null
+++ b/apps/codecs/demac/libdemac/predictor.h
@@ -0,0 +1,36 @@
1/*
2
3libdemac - A Monkey's Audio decoder
4
5$Id:$
6
7Copyright (C) Dave Chapman 2007
8
9This program is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2 of the License, or
12(at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
22
23*/
24
25#ifndef _APE_PREDICTOR_H
26#define _APE_PREDICTOR_H
27
28#include <inttypes.h>
29#include "parser.h"
30#include "filter.h"
31
32void init_predictor_decoder(struct ape_ctx_t* ape_ctx);
33int predictor_decode_stereo(struct ape_ctx_t* ape_ctx, int32_t* decoded0, int32_t* decoded1, int count);
34int predictor_decode_mono(struct ape_ctx_t* ape_ctx, int32_t* decoded0, int count);
35
36#endif
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
3libdemac - A Monkey's Audio decoder
4
5$Id:$
6
7Copyright (C) Dave Chapman 2007
8
9This program is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2 of the License, or
12(at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
22
23*/
24
25/*
26
27Range 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
44The encoding functions were removed, and functions turned into "static
45inline" functions and moved to a .h file. Some minor cosmetic changes
46were made (e.g. turning pre-processor symbols into upper-case,
47removing 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
60static unsigned char* bytebuffer IBSS_ATTR;
61static int bytebufferoffset IBSS_ATTR;
62
63static inline void skip_byte(void)
64{
65 if (bytebufferoffset) {
66 bytebufferoffset--;
67 } else {
68 bytebufferoffset = 3;
69 bytebuffer += 4;
70 }
71}
72
73static 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
92struct 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
100static struct rangecoder_t rc;
101
102/* Start the decoder */
103static 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
110static 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 */
124static 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
135static 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) */
148static 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 */
157static inline unsigned char decode_byte(void)
158{ int tmp = range_decode_culshift(8);
159 range_decode_update( 1,tmp);
160 return tmp;
161}
162
163static 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 */
170static 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 */
178static inline void range_done_decoding(void)
179{ range_dec_normalize(); /* normalize to use up all bytes */
180}
diff --git a/apps/codecs/demac/libdemac/vector_math16.h b/apps/codecs/demac/libdemac/vector_math16.h
new file mode 100644
index 0000000000..5d82abe930
--- /dev/null
+++ b/apps/codecs/demac/libdemac/vector_math16.h
@@ -0,0 +1,140 @@
1/*
2
3libdemac - A Monkey's Audio decoder
4
5$Id:$
6
7Copyright (C) Dave Chapman 2007
8
9This program is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2 of the License, or
12(at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
22
23*/
24
25static inline void vector_add(int16_t* v1, int16_t* v2)
26{
27#if ORDER > 32
28 int order = (ORDER >> 5);
29 while (order--)
30#endif
31 {
32 *v1++ += *v2++;
33 *v1++ += *v2++;
34 *v1++ += *v2++;
35 *v1++ += *v2++;
36 *v1++ += *v2++;
37 *v1++ += *v2++;
38 *v1++ += *v2++;
39 *v1++ += *v2++;
40 *v1++ += *v2++;
41 *v1++ += *v2++;
42 *v1++ += *v2++;
43 *v1++ += *v2++;
44 *v1++ += *v2++;
45 *v1++ += *v2++;
46 *v1++ += *v2++;
47 *v1++ += *v2++;
48#if ORDER > 16
49 *v1++ += *v2++;
50 *v1++ += *v2++;
51 *v1++ += *v2++;
52 *v1++ += *v2++;
53 *v1++ += *v2++;
54 *v1++ += *v2++;
55 *v1++ += *v2++;
56 *v1++ += *v2++;
57 *v1++ += *v2++;
58 *v1++ += *v2++;
59 *v1++ += *v2++;
60 *v1++ += *v2++;
61 *v1++ += *v2++;
62 *v1++ += *v2++;
63 *v1++ += *v2++;
64 *v1++ += *v2++;
65#endif
66 }
67}
68
69static inline void vector_sub(int16_t* v1, int16_t* v2)
70{
71#if ORDER > 32
72 int order = (ORDER >> 5);
73 while (order--)
74#endif
75 {
76 *v1++ -= *v2++;
77 *v1++ -= *v2++;
78 *v1++ -= *v2++;
79 *v1++ -= *v2++;
80 *v1++ -= *v2++;
81 *v1++ -= *v2++;
82 *v1++ -= *v2++;
83 *v1++ -= *v2++;
84 *v1++ -= *v2++;
85 *v1++ -= *v2++;
86 *v1++ -= *v2++;
87 *v1++ -= *v2++;
88 *v1++ -= *v2++;
89 *v1++ -= *v2++;
90 *v1++ -= *v2++;
91 *v1++ -= *v2++;
92#if ORDER > 16
93 *v1++ -= *v2++;
94 *v1++ -= *v2++;
95 *v1++ -= *v2++;
96 *v1++ -= *v2++;
97 *v1++ -= *v2++;
98 *v1++ -= *v2++;
99 *v1++ -= *v2++;
100 *v1++ -= *v2++;
101 *v1++ -= *v2++;
102 *v1++ -= *v2++;
103 *v1++ -= *v2++;
104 *v1++ -= *v2++;
105 *v1++ -= *v2++;
106 *v1++ -= *v2++;
107 *v1++ -= *v2++;
108 *v1++ -= *v2++;
109#endif
110 }
111}
112
113static inline int32_t scalarproduct(int16_t* v1, int16_t* v2)
114{
115 int res = 0;
116
117#if ORDER > 16
118 int order = (ORDER >> 4);
119 while (order--)
120#endif
121 {
122 res += *v1++ * *v2++;
123 res += *v1++ * *v2++;
124 res += *v1++ * *v2++;
125 res += *v1++ * *v2++;
126 res += *v1++ * *v2++;
127 res += *v1++ * *v2++;
128 res += *v1++ * *v2++;
129 res += *v1++ * *v2++;
130 res += *v1++ * *v2++;
131 res += *v1++ * *v2++;
132 res += *v1++ * *v2++;
133 res += *v1++ * *v2++;
134 res += *v1++ * *v2++;
135 res += *v1++ * *v2++;
136 res += *v1++ * *v2++;
137 res += *v1++ * *v2++;
138 }
139 return res;
140}
diff --git a/apps/codecs/demac/libdemac/vector_math32.h b/apps/codecs/demac/libdemac/vector_math32.h
new file mode 100644
index 0000000000..b32867ca5d
--- /dev/null
+++ b/apps/codecs/demac/libdemac/vector_math32.h
@@ -0,0 +1,54 @@
1/*
2
3libdemac - A Monkey's Audio decoder
4
5$Id:$
6
7Copyright (C) Dave Chapman 2007
8
9This program is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2 of the License, or
12(at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
22
23*/
24
25/* 32-bit vector math functions */
26
27#define scalarproduct4_rev32(x,y) ((x[0] * y[0]) + (x[1] * y[-1]) + \
28 (x[2] * y[-2]) + (x[3] * y[-3]))
29
30#define scalarproduct5_rev32(x,y) ((x[0] * y[0]) + (x[1] * y[-1]) + \
31 (x[2] * y[-2]) + (x[3] * y[-3]) + \
32 (x[4] * y[-4]))
33
34#define vector_sub4_rev32(x, y) { x[0] -= y[0]; \
35 x[1] -= y[-1]; \
36 x[2] -= y[-2]; \
37 x[3] -= y[-3]; }
38
39#define vector_sub5_rev32(x, y) { x[0] -= y[0]; \
40 x[1] -= y[-1]; \
41 x[2] -= y[-2]; \
42 x[3] -= y[-3]; \
43 x[4] -= y[-4]; }
44
45#define vector_add4_rev32(x, y) { x[0] += y[0]; \
46 x[1] += y[-1]; \
47 x[2] += y[-2]; \
48 x[3] += y[-3]; }
49
50#define vector_add5_rev32(x, y) { x[0] += y[0]; \
51 x[1] += y[-1]; \
52 x[2] += y[-2]; \
53 x[3] += y[-3]; \
54 x[4] += y[-4]; }