--- /dev/null
+/*
+ * Copyright (C) 2013 nu774
+ * For conditions of distribution and use, see copyright notice in COPYING
+ */
+
+#if HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#if HAVE_STDINT_H
+# include <stdint.h>
+#endif
+
+#include <stdio.h>
+#include <stdarg.h>
+#include "pcm_reader.h"
+#include "m4af_endian.h"
+
+int pcm_read(pcm_io_context_t *io, void *buffer, uint32_t size)
+{
+ int rc;
+ uint32_t count = 0;
+
+ do {
+ rc = io->vtbl->read(io->cookie, buffer, size - count);
+ if (rc > 0)
+ count += rc;
+ } while (rc > 0 && count < size);
+ return count > 0 ? count : rc;
+}
+
+int pcm_skip(pcm_io_context_t *io, int64_t count)
+{
+ char buff[8192];
+ int rc;
+ pcm_io_vtbl_t *vp = io->vtbl;
+
+ if (count == 0 || pcm_seek(io, count, SEEK_CUR) >= 0)
+ return 0;
+ do {
+ if ((rc = vp->read(io->cookie, buff, count > 8192 ? 8192 : count)) > 0)
+ count -= rc;
+ } while (rc > 0 && count > 0);
+
+ return count == 0 ? 0 : -1;
+}
+
+int pcm_read16le(pcm_io_context_t *io, uint16_t *value)
+{
+ if (pcm_read(io, value, 2) == 2) {
+ *value = m4af_ltoh16(*value);
+ return 0;
+ }
+ return -1;
+}
+
+int pcm_read16be(pcm_io_context_t *io, uint16_t *value)
+{
+ if (pcm_read(io, value, 2) == 2) {
+ *value = m4af_btoh16(*value);
+ return 0;
+ }
+ return -1;
+}
+
+int pcm_read32le(pcm_io_context_t *io, uint32_t *value)
+{
+ if (pcm_read(io, value, 4) == 4) {
+ *value = m4af_ltoh32(*value);
+ return 0;
+ }
+ return -1;
+}
+
+int pcm_read32be(pcm_io_context_t *io, uint32_t *value)
+{
+ if (pcm_read(io, value, 4) == 4) {
+ *value = m4af_btoh32(*value);
+ return 0;
+ }
+ return -1;
+}
+
+int pcm_read64le(pcm_io_context_t *io, uint64_t *value)
+{
+ if (pcm_read(io, value, 8) == 8) {
+ *value = m4af_ltoh64(*value);
+ return 0;
+ }
+ return -1;
+}
+
+int pcm_read64be(pcm_io_context_t *io, uint64_t *value)
+{
+ if (pcm_read(io, value, 8) == 8) {
+ *value = m4af_btoh64(*value);
+ return 0;
+ }
+ return -1;
+}
+
+int pcm_scanl(pcm_io_context_t *io, const char *fmt, ...)
+{
+ int c, count = 0;
+ va_list ap;
+
+ va_start(ap, fmt);
+ while ((c = *fmt++)) {
+ switch (c) {
+ case 'S':
+ TRY_IO(pcm_read16le(io, va_arg(ap, uint16_t*)));
+ ++count;
+ break;
+ case 'L':
+ TRY_IO(pcm_read32le(io, va_arg(ap, uint32_t*)));
+ ++count;
+ break;
+ case 'Q':
+ TRY_IO(pcm_read64le(io, va_arg(ap, uint64_t*)));
+ ++count;
+ break;
+ }
+ }
+FAIL:
+ va_end(ap);
+ return count;
+}
+
+int pcm_scanb(pcm_io_context_t *io, const char *fmt, ...)
+{
+ int c, count = 0;
+ va_list ap;
+
+ va_start(ap, fmt);
+ while ((c = *fmt++)) {
+ switch (c) {
+ case 'S':
+ TRY_IO(pcm_read16be(io, va_arg(ap, uint16_t*)));
+ ++count;
+ break;
+ case 'L':
+ TRY_IO(pcm_read32be(io, va_arg(ap, uint32_t*)));
+ ++count;
+ break;
+ case 'Q':
+ TRY_IO(pcm_read64be(io, va_arg(ap, uint64_t*)));
+ ++count;
+ break;
+ }
+ }
+FAIL:
+ va_end(ap);
+ return count;
+}
#include <string.h>
#include <stdarg.h>
#include "wav_reader.h"
-#include "m4af_endian.h"
#define RIFF_FOURCC(a,b,c,d) ((a)|((b)<<8)|((c)<<16)|((d)<<24))
-#define TRY_IO(expr) \
+#define ENSURE(expr) \
do { \
- if (expr) \
- goto FAIL; \
- } while (0)
-
-#define ASSERT_FORMAT(ctx, expr) \
- do { \
- if (!expr) { \
- if (!ctx->last_error) \
- ctx->last_error = WAV_INVALID_FORMAT; \
- goto FAIL;\
- } \
+ if (!(expr)) goto FAIL;\
} while (0)
struct wav_reader_t {
int64_t position;
int32_t data_offset;
int ignore_length;
- int last_error;
- wav_io_context_t io;
- void *io_cookie;
+ pcm_io_context_t io;
};
static const uint8_t WAV_GUID_PCM[] = {
*reader = 0;
}
-static
-int riff_read(wav_reader_t *reader, void *buffer, uint32_t size)
-{
- int rc;
- uint32_t count = 0;
-
- if (reader->last_error)
- return -1;
- do {
- rc = reader->io.read(reader->io_cookie, buffer, size - count);
- if (rc > 0)
- count += rc;
- else if (rc < 0)
- reader->last_error = WAV_IO_ERROR;
- } while (rc > 0 && count < size);
- return count > 0 ? count : rc;
-}
-
-static
-int riff_skip(wav_reader_t *reader, int64_t count)
-{
- char buff[8192];
- int rc;
-
- if (reader->last_error)
- return -1;
- if (count == 0)
- return 0;
- if (reader->io.seek &&
- reader->io.seek(reader->io_cookie, count, SEEK_CUR) >= 0)
- return 0;
-
- do {
- if ((rc = riff_read(reader, buff, count > 8192 ? 8192 : count)) > 0)
- count -= rc;
- } while (rc > 0 && count > 0);
-
- if (count > 0)
- reader->last_error = WAV_IO_ERROR;
- return reader->last_error ? -1 : 0;
-}
-
-static
-int riff_seek(wav_reader_t *reader, int64_t off, int whence)
-{
- int rc;
- if (reader->last_error)
- return -1;
- if (!reader->io.seek)
- goto FAIL;
- if ((rc = reader->io.seek(reader->io_cookie, off, whence)) < 0)
- goto FAIL;
- return 0;
-FAIL:
- reader->last_error = WAV_IO_ERROR;
- return -1;
-}
-
-static
-int64_t riff_tell(wav_reader_t *reader)
-{
- int64_t off;
-
- if (reader->last_error || !reader->io.tell)
- return -1;
- off = reader->io.tell(reader->io_cookie);
- if (off < 0) {
- reader->last_error = WAV_IO_ERROR;
- return -1;
- }
- return off;
-}
-
-static
-int riff_read16(wav_reader_t *reader, uint16_t *value)
-{
- TRY_IO(riff_read(reader, value, 2) != 2);
- *value = m4af_ltoh16(*value);
- return 0;
-FAIL:
- return -1;
-}
-
-static
-int riff_read32(wav_reader_t *reader, uint32_t *value)
-{
- TRY_IO(riff_read(reader, value, 4) != 4);
- *value = m4af_ltoh32(*value);
- return 0;
-FAIL:
- return -1;
-}
-
-static
-int riff_read64(wav_reader_t *reader, uint64_t *value)
-{
- TRY_IO(riff_read(reader, value, 8) != 8);
- *value = m4af_ltoh64(*value);
- return 0;
-FAIL:
- return -1;
-}
-
-static
-int riff_scan(wav_reader_t *reader, const char *fmt, ...)
-{
- int c, count = 0;
- va_list ap;
-
- va_start(ap, fmt);
- while ((c = *fmt++)) {
- switch (c) {
- case 'S':
- TRY_IO(riff_read16(reader, va_arg(ap, uint16_t*)));
- ++count;
- break;
- case 'L':
- TRY_IO(riff_read32(reader, va_arg(ap, uint32_t*)));
- ++count;
- break;
- case 'Q':
- TRY_IO(riff_read64(reader, va_arg(ap, uint64_t*)));
- ++count;
- break;
- }
- }
-FAIL:
- va_end(ap);
- return count;
-}
-
static
uint32_t riff_next_chunk(wav_reader_t *reader, uint32_t *chunk_size)
{
uint32_t fcc;
- if (riff_scan(reader, "LL", &fcc, chunk_size) == 2)
- return fcc;
- return 0;
+ return (pcm_scanl(&reader->io, "LL", &fcc, chunk_size) == 2) ? fcc : 0;
}
static
nframes = reader->length - reader->position;
nbytes = nframes * reader->sample_format.bytes_per_frame;
if (nbytes) {
- if ((rc = riff_read(reader, buffer, nbytes)) < 0)
+ if ((rc = pcm_read(&reader->io, buffer, nbytes)) < 0)
return -1;
nframes = rc / reader->sample_format.bytes_per_frame;
reader->position += nframes;
uint64_t riff_size, sample_count;
fcc = riff_next_chunk(reader, &chunk_size);
- ASSERT_FORMAT(reader,
- fcc == RIFF_FOURCC('d','s','6','4') && chunk_size >= 28);
- TRY_IO(riff_scan(reader, "QQQL",
+ ENSURE(fcc == RIFF_FOURCC('d','s','6','4') && chunk_size >= 28);
+ TRY_IO(pcm_scanl(&reader->io, "QQQL",
&riff_size, length, &sample_count, &table_size) != 4);
- TRY_IO(riff_skip(reader, (chunk_size - 27) & ~1));
+ TRY_IO(pcm_skip(&reader->io, (chunk_size - 27) & ~1));
reader->data_offset += (chunk_size + 9) & ~1;
FAIL:
return -1;
uint8_t guid[16];
int is_float = 0;
- ASSERT_FORMAT(reader, size >= 16);
- TRY_IO(riff_scan(reader, "SSLLSS", &wFormatTag, &nChannels,
+ ENSURE(size >= 16);
+ TRY_IO(pcm_scanl(&reader->io, "SSLLSS", &wFormatTag, &nChannels,
&nSamplesPerSec, &nAvgBytesPerSec, &nBlockAlign,
&wBitsPerSample) != 6);
wValidBitsPerSample = wBitsPerSample;
- if (wFormatTag != 1 && wFormatTag != 3 && wFormatTag != 0xfffe) {
- reader->last_error = WAV_UNSUPPORTED_FORMAT;
- goto FAIL;
- }
- ASSERT_FORMAT(reader,
- nChannels && nSamplesPerSec && nAvgBytesPerSec &&
- nBlockAlign && wBitsPerSample && !(wBitsPerSample & 7) &&
- nBlockAlign == nChannels * wBitsPerSample / 8);
+ ENSURE(wFormatTag == 1 || wFormatTag == 3 || wFormatTag == 0xfffe);
+ ENSURE(nChannels && nSamplesPerSec && nAvgBytesPerSec &&
+ nBlockAlign && wBitsPerSample && !(wBitsPerSample & 7) &&
+ nBlockAlign == nChannels * wBitsPerSample / 8);
+
if (wFormatTag == 3)
is_float = 1;
if (wFormatTag != 0xfffe)
- TRY_IO(riff_skip(reader, (size - 15) & ~1));
+ TRY_IO(pcm_skip(&reader->io, (size - 15) & ~1));
else {
- ASSERT_FORMAT(reader, size >= 40);
- TRY_IO(riff_scan(reader, "SSL",
+ ENSURE(size >= 40);
+ TRY_IO(pcm_scanl(&reader->io, "SSL",
&cbSize, &wValidBitsPerSample, &dwChannelMask) != 3);
- TRY_IO(riff_read(reader, guid, 16) != 16);
+ TRY_IO(pcm_read(&reader->io, guid, 16) != 16);
if (memcmp(guid, WAV_GUID_FLOAT, 16) == 0)
is_float = 1;
- else if (memcmp(guid, WAV_GUID_PCM, 16) != 0) {
- reader->last_error = WAV_UNSUPPORTED_FORMAT;
+ else if (memcmp(guid, WAV_GUID_PCM, 16) != 0)
goto FAIL;
- }
- ASSERT_FORMAT(reader,
- wValidBitsPerSample &&
- wValidBitsPerSample <= wBitsPerSample);
- TRY_IO(riff_skip(reader, (size - 39) & ~1));
+ ENSURE(wValidBitsPerSample && wValidBitsPerSample <= wBitsPerSample);
+ TRY_IO(pcm_skip(&reader->io, (size - 39) & ~1));
}
reader->sample_format.sample_rate = nSamplesPerSec;
reader->sample_format.bits_per_channel = wValidBitsPerSample;
*data_length = 0;
container = riff_next_chunk(reader, &chunk_size);
- if (container != RIFF_FOURCC('R','I','F','F') &&
- container != RIFF_FOURCC('R','F','6','4'))
- goto FAIL;
- TRY_IO(riff_read32(reader, &fcc));
- if (fcc != RIFF_FOURCC('W','A','V','E'))
- goto FAIL;
+ ENSURE(container == RIFF_FOURCC('R','I','F','F') ||
+ container == RIFF_FOURCC('R','F','6','4'));
+ TRY_IO(pcm_read32le(&reader->io, &fcc));
+ ENSURE(fcc == RIFF_FOURCC('W','A','V','E'));
reader->data_offset = 12;
if (container == RIFF_FOURCC('R','F','6','4'))
reader->data_offset += 8;
break;
} else {
- TRY_IO(riff_skip(reader, (chunk_size + 1) & ~1));
+ TRY_IO(pcm_skip(&reader->io, (chunk_size + 1) & ~1));
}
reader->data_offset += (chunk_size + 9) & ~1;
}
wav_teardown
};
-pcm_reader_t *wav_open(wav_io_context_t *io_ctx, void *io_cookie,
- int ignore_length)
+pcm_reader_t *wav_open(pcm_io_context_t *io, int ignore_length)
{
wav_reader_t *reader = 0;
int64_t data_length;
if ((reader = calloc(1, sizeof(wav_reader_t))) == 0)
return 0;
- memcpy(&reader->io, io_ctx, sizeof(wav_io_context_t));
- reader->io_cookie = io_cookie;
+ memcpy(&reader->io, io, sizeof(pcm_io_context_t));
reader->ignore_length = ignore_length;
if (wav_parse(reader, &data_length) < 0) {
free(reader);
else
reader->length = data_length / bpf;
- if (reader->length == INT64_MAX && reader->io.seek && reader->io.tell) {
- if (reader->io.seek(reader->io_cookie, 0, SEEK_END) >= 0) {
- int64_t size = reader->io.tell(reader->io_cookie);
+ if (reader->length == INT64_MAX) {
+ if (pcm_seek(&reader->io, 0, SEEK_END) >= 0) {
+ int64_t size = pcm_tell(&reader->io);
if (size > 0)
reader->length = (size - reader->data_offset) / bpf;
- reader->io.seek(reader->io_cookie, reader->data_offset, SEEK_SET);
+ pcm_seek(&reader->io, reader->data_offset, SEEK_SET);
}
}
reader->vtbl = &wav_vtable;
return (pcm_reader_t *)reader;
}
-pcm_reader_t *raw_open(wav_io_context_t *io_ctx, void *io_cookie,
+pcm_reader_t *raw_open(pcm_io_context_t *io,
const pcm_sample_description_t *desc)
{
wav_reader_t *reader = 0;
if ((reader = calloc(1, sizeof(wav_reader_t))) == 0)
return 0;
- memcpy(&reader->io, io_ctx, sizeof(wav_io_context_t));
+ memcpy(&reader->io, io, sizeof(pcm_io_context_t));
memcpy(&reader->sample_format, desc, sizeof(pcm_sample_description_t));
- reader->io_cookie = io_cookie;
- if (io_ctx->seek && io_ctx->tell) {
- if (reader->io.seek(reader->io_cookie, 0, SEEK_END) >= 0) {
- int64_t size = reader->io.tell(reader->io_cookie);
- if (size > 0)
- reader->length = size / desc->bytes_per_frame;
- reader->io.seek(reader->io_cookie, reader->data_offset, SEEK_SET);
- }
+ if (pcm_seek(&reader->io, 0, SEEK_END) >= 0) {
+ int64_t size = pcm_tell(&reader->io);
+ if (size > 0)
+ reader->length = size / reader->sample_format.bytes_per_frame;
+ pcm_seek(&reader->io, 0, SEEK_SET);
} else
reader->length = INT64_MAX;
reader->vtbl = &wav_vtable;
return (pcm_reader_t *)reader;
}
-
-