summaryrefslogtreecommitdiff
path: root/plugins/alac/stream.c
diff options
context:
space:
mode:
authorGravatar waker <wakeroid@gmail.com>2012-08-01 22:27:36 +0200
committerGravatar waker <wakeroid@gmail.com>2012-08-01 22:27:36 +0200
commit002d8c063496bbaa4d781c2812b1f28d28902c23 (patch)
treeb8bb1c1d201f810edccc2045cdfd4c487866abb0 /plugins/alac/stream.c
parent397e22ae9d585b39d8f60a91e273bf1c31d7895e (diff)
added new ALAC plugin
Diffstat (limited to 'plugins/alac/stream.c')
-rw-r--r--plugins/alac/stream.c179
1 files changed, 179 insertions, 0 deletions
diff --git a/plugins/alac/stream.c b/plugins/alac/stream.c
new file mode 100644
index 00000000..4976a539
--- /dev/null
+++ b/plugins/alac/stream.c
@@ -0,0 +1,179 @@
+/*
+ * ALAC (Apple Lossless Audio Codec) decoder
+ * Copyright (c) 2005 David Hammerton
+ * All rights reserved.
+ *
+ * Basic stream reading
+ *
+ * http://crazney.net/programs/itunes/alac.html
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+
+#include <stdlib.h>
+#include <errno.h>
+#ifdef _WIN32
+ #include "stdint_win.h"
+#else
+ #include <stdint.h>
+#endif
+
+#include "stream.h"
+
+extern DB_functions_t *deadbeef;
+
+#define _Swap32(v) do { \
+ v = (((v) & 0x000000FF) << 0x18) | \
+ (((v) & 0x0000FF00) << 0x08) | \
+ (((v) & 0x00FF0000) >> 0x08) | \
+ (((v) & 0xFF000000) >> 0x18); } while(0)
+
+#define _Swap16(v) do { \
+ v = (((v) & 0x00FF) << 0x08) | \
+ (((v) & 0xFF00) >> 0x08); } while (0)
+
+extern int host_bigendian;
+
+struct stream_tTAG {
+ DB_FILE *f;
+ int bigendian;
+ int eof;
+};
+
+void stream_read(stream_t *stream, size_t size, void *buf)
+{
+ size_t ret;
+
+ ret = deadbeef->fread(buf, 4, size >> 2, stream->f) * 4;
+ ret += deadbeef->fread((char*)buf + ret, 1, size - ret, stream->f);
+
+ if (ret == 0 && size != 0) stream->eof = 1;
+}
+
+int32_t stream_read_int32(stream_t *stream)
+{
+ int32_t v;
+ stream_read(stream, 4, &v);
+ if ((stream->bigendian && !host_bigendian) ||
+ (!stream->bigendian && host_bigendian))
+ {
+ _Swap32(v);
+ }
+ return v;
+}
+
+uint32_t stream_read_uint32(stream_t *stream)
+{
+ uint32_t v;
+ stream_read(stream, 4, &v);
+ if ((stream->bigendian && !host_bigendian) ||
+ (!stream->bigendian && host_bigendian))
+ {
+ _Swap32(v);
+ }
+ return v;
+}
+
+int16_t stream_read_int16(stream_t *stream)
+{
+ int16_t v;
+ stream_read(stream, 2, &v);
+ if ((stream->bigendian && !host_bigendian) ||
+ (!stream->bigendian && host_bigendian))
+ {
+ _Swap16(v);
+ }
+ return v;
+}
+
+uint16_t stream_read_uint16(stream_t *stream)
+{
+ uint16_t v;
+ stream_read(stream, 2, &v);
+ if ((stream->bigendian && !host_bigendian) ||
+ (!stream->bigendian && host_bigendian))
+ {
+ _Swap16(v);
+ }
+ return v;
+}
+
+int8_t stream_read_int8(stream_t *stream)
+{
+ int8_t v;
+ stream_read(stream, 1, &v);
+ return v;
+}
+
+uint8_t stream_read_uint8(stream_t *stream)
+{
+ uint8_t v;
+ stream_read(stream, 1, &v);
+ return v;
+}
+
+
+void stream_skip(stream_t *stream, size_t skip)
+{
+ if (deadbeef->fseek(stream->f, (long)skip, SEEK_CUR) == 0) return;
+ if (errno == ESPIPE)
+ {
+ char *buffer = malloc(skip);
+ stream_read(stream, skip, buffer);
+ free(buffer);
+ }
+}
+
+int stream_eof(stream_t *stream)
+{
+ return stream->eof;
+}
+
+long stream_tell(stream_t *stream)
+{
+ return deadbeef->ftell(stream->f); /* returns -1 on error */
+}
+
+int stream_setpos(stream_t *stream, long pos)
+{
+ return deadbeef->fseek(stream->f, pos, SEEK_SET);
+}
+
+stream_t *stream_create_file(DB_FILE *file,
+ int bigendian)
+{
+ stream_t *new_stream;
+
+ new_stream = (stream_t*)malloc(sizeof(stream_t));
+ new_stream->f = file;
+ new_stream->bigendian = bigendian;
+ new_stream->eof = 0;
+
+ return new_stream;
+}
+
+void stream_destroy(stream_t *stream)
+{
+ free(stream);
+}
+