aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2012-03-21 08:44:43 +0100
committerMax Kellermann <max@duempel.org>2012-03-21 08:44:43 +0100
commitc1d0a8b5ce93c4b70b00b14f2015fb6a57681dbe (patch)
treeacae564b851d4c799dd3bd635e25a18531770b94
parent08ce24ec3fb094f653b3f3421388edec02f1dced (diff)
downloadmpd-c1d0a8b5ce93c4b70b00b14f2015fb6a57681dbe.tar.gz
mpd-c1d0a8b5ce93c4b70b00b14f2015fb6a57681dbe.tar.xz
mpd-c1d0a8b5ce93c4b70b00b14f2015fb6a57681dbe.zip
dsd2pcm: move the bit reversing code to a generic library
Instead of doing run-time initialisation, use a constant lookup table.
-rw-r--r--Makefile.am3
-rw-r--r--src/dsd2pcm/dsd2pcm.c12
-rw-r--r--src/util/bit_reverse.c31
-rw-r--r--src/util/bit_reverse.h35
4 files changed, 73 insertions, 8 deletions
diff --git a/Makefile.am b/Makefile.am
index c7b110354..d7f7f2314 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -227,6 +227,7 @@ src_mpd_SOURCES = \
$(MIXER_API_SRC) \
src/util/list.h \
src/util/list_sort.c src/util/list_sort.h \
+ src/util/bit_reverse.c src/util/bit_reverse.h \
src/glib_socket.h \
src/notify.c \
src/audio_config.c src/audio_config.h \
@@ -1174,6 +1175,7 @@ test_run_normalize_LDADD = \
$(GLIB_LIBS)
test_run_convert_SOURCES = test/run_convert.c \
+ src/util/bit_reverse.c \
src/dsd2pcm/dsd2pcm.c \
src/fifo_buffer.c \
src/audio_format.c \
@@ -1272,6 +1274,7 @@ if HAVE_CXX
noinst_PROGRAMS += src/dsd2pcm/dsd2pcm
src_dsd2pcm_dsd2pcm_SOURCES = \
+ src/util/bit_reverse.c \
src/dsd2pcm/dsd2pcm.c src/dsd2pcm/dsd2pcm.h \
src/dsd2pcm/noiseshape.c src/dsd2pcm/noiseshape.h \
src/dsd2pcm/main.cpp
diff --git a/src/dsd2pcm/dsd2pcm.c b/src/dsd2pcm/dsd2pcm.c
index 315820f60..4c7640853 100644
--- a/src/dsd2pcm/dsd2pcm.c
+++ b/src/dsd2pcm/dsd2pcm.c
@@ -1,3 +1,5 @@
+#include "util/bit_reverse.h"
+
#include <stdlib.h>
#include <string.h>
@@ -84,7 +86,6 @@ static const double htaps[HTAPS] = {
};
static float ctables[CTABLES][256];
-static unsigned char bitreverse[256];
static int precalculated = 0;
static void precalc(void)
@@ -92,11 +93,6 @@ static void precalc(void)
int t, e, m, k;
double acc;
if (precalculated) return;
- for (t=0, e=0; t<256; ++t) {
- bitreverse[t] = e;
- for (m=128; m && !((e^=m)&m); m>>=1)
- ;
- }
for (t=0; t<CTABLES; ++t) {
k = HTAPS - t*8;
if (k>8) k=8;
@@ -170,10 +166,10 @@ extern void dsd2pcm_translate(
lsbf = lsbf ? 1 : 0;
while (samples-- > 0) {
bite1 = *src & 0xFFu;
- if (lsbf) bite1 = bitreverse[bite1];
+ if (lsbf) bite1 = bit_reverse(bite1);
ptr->fifo[ffp] = bite1; src += src_stride;
p = ptr->fifo + ((ffp-CTABLES) & FIFOMASK);
- *p = bitreverse[*p & 0xFF];
+ *p = bit_reverse(*p);
acc = 0;
for (i=0; i<CTABLES; ++i) {
bite1 = ptr->fifo[(ffp -i) & FIFOMASK] & 0xFF;
diff --git a/src/util/bit_reverse.c b/src/util/bit_reverse.c
new file mode 100644
index 000000000..ba8a23ef1
--- /dev/null
+++ b/src/util/bit_reverse.c
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2003-2012 The Music Player Daemon Project
+ * http://www.musicpd.org
+ *
+ * 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 Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "bit_reverse.h"
+
+/**
+ * @see http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable
+ */
+const uint8_t bit_reverse_table[256] =
+{
+#define R2(n) n, n + 2*64, n + 1*64, n + 3*64
+#define R4(n) R2(n), R2(n + 2*16), R2(n + 1*16), R2(n + 3*16)
+#define R6(n) R4(n), R4(n + 2*4 ), R4(n + 1*4 ), R4(n + 3*4 )
+ R6(0), R6(2), R6(1), R6(3)
+};
diff --git a/src/util/bit_reverse.h b/src/util/bit_reverse.h
new file mode 100644
index 000000000..e44693b1d
--- /dev/null
+++ b/src/util/bit_reverse.h
@@ -0,0 +1,35 @@
+/*
+ * Copyright (C) 2003-2012 The Music Player Daemon Project
+ * http://www.musicpd.org
+ *
+ * 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 Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef MPD_BIT_REVERSE_H
+#define MPD_BIT_REVERSE_H
+
+#include <glib.h>
+#include <stdint.h>
+
+extern const uint8_t bit_reverse_table[256];
+
+G_GNUC_CONST
+static inline uint8_t
+bit_reverse(uint8_t x)
+{
+ return bit_reverse_table[x];
+}
+
+#endif