refactor pcm io routines

This commit is contained in:
nu774 2013-10-24 01:07:06 +09:00
parent 3b666b7546
commit 29a8f73faf
7 changed files with 256 additions and 234 deletions

View File

@ -102,6 +102,7 @@ copy ..\fdk-aac\libSYS\include\machine_type.h include\fdk-aac\ </Command>
<ClCompile Include="..\src\main.c" /> <ClCompile Include="..\src\main.c" />
<ClCompile Include="..\src\metadata.c" /> <ClCompile Include="..\src\metadata.c" />
<ClCompile Include="..\src\parson.c" /> <ClCompile Include="..\src\parson.c" />
<ClCompile Include="..\src\pcm_readhelper.c" />
<ClCompile Include="..\src\pcm_sint16_converter.c" /> <ClCompile Include="..\src\pcm_sint16_converter.c" />
<ClCompile Include="..\src\progress.c" /> <ClCompile Include="..\src\progress.c" />
<ClCompile Include="..\src\wav_reader.c" /> <ClCompile Include="..\src\wav_reader.c" />
@ -115,6 +116,7 @@ copy ..\fdk-aac\libSYS\include\machine_type.h include\fdk-aac\ </Command>
<ClInclude Include="..\src\m4af_endian.h" /> <ClInclude Include="..\src\m4af_endian.h" />
<ClInclude Include="..\src\metadata.h" /> <ClInclude Include="..\src\metadata.h" />
<ClInclude Include="..\src\parson.h" /> <ClInclude Include="..\src\parson.h" />
<ClInclude Include="..\src\pcm_reader.h" />
<ClInclude Include="..\src\progress.h" /> <ClInclude Include="..\src\progress.h" />
<ClInclude Include="..\src\wav_reader.h" /> <ClInclude Include="..\src\wav_reader.h" />
</ItemGroup> </ItemGroup>

View File

@ -10,6 +10,7 @@ fdkaac_SOURCES = \
src/main.c \ src/main.c \
src/metadata.c \ src/metadata.c \
src/parson.c \ src/parson.c \
src/pcm_readhelper.c \
src/pcm_sint16_converter.c \ src/pcm_sint16_converter.c \
src/progress.c \ src/progress.c \
src/wav_reader.c src/wav_reader.c

View File

@ -643,12 +643,15 @@ int parse_raw_spec(const char *spec, pcm_sample_description_t *desc)
return 0; return 0;
} }
static pcm_io_vtbl_t pcm_io_vtbl = {
read_callback, seek_callback, tell_callback
};
static pcm_io_vtbl_t pcm_io_vtbl_noseek = { read_callback, 0, 0 };
static static
pcm_reader_t *open_input(aacenc_param_ex_t *params) pcm_reader_t *open_input(aacenc_param_ex_t *params)
{ {
wav_io_context_t wav_io = { pcm_io_context_t io = { 0 };
read_callback, seek_callback, tell_callback
};
pcm_reader_t *reader = 0; pcm_reader_t *reader = 0;
struct stat stb = { 0 }; struct stat stb = { 0 };
@ -657,11 +660,13 @@ pcm_reader_t *open_input(aacenc_param_ex_t *params)
strerror(errno)); strerror(errno));
goto END; goto END;
} }
if (fstat(fileno(params->input_fp), &stb) == 0 io.cookie = params->input_fp;
&& (stb.st_mode & S_IFMT) != S_IFREG) { if (fstat(fileno(io.cookie), &stb) == 0
wav_io.seek = 0; && (stb.st_mode & S_IFMT) == S_IFREG)
wav_io.tell = 0; io.vtbl = &pcm_io_vtbl;
} else
io.vtbl = &pcm_io_vtbl_noseek;
if (params->is_raw) { if (params->is_raw) {
int bytes_per_channel; int bytes_per_channel;
pcm_sample_description_t desc = { 0 }; pcm_sample_description_t desc = { 0 };
@ -673,13 +678,12 @@ pcm_reader_t *open_input(aacenc_param_ex_t *params)
desc.channels_per_frame = params->raw_channels; desc.channels_per_frame = params->raw_channels;
bytes_per_channel = (desc.bits_per_channel + 7) / 8; bytes_per_channel = (desc.bits_per_channel + 7) / 8;
desc.bytes_per_frame = params->raw_channels * bytes_per_channel; desc.bytes_per_frame = params->raw_channels * bytes_per_channel;
if ((reader = raw_open(&wav_io, params->input_fp, &desc)) == 0) { if ((reader = raw_open(&io, &desc)) == 0) {
fprintf(stderr, "ERROR: failed to open raw input\n"); fprintf(stderr, "ERROR: failed to open raw input\n");
goto END; goto END;
} }
} else { } else {
if ((reader = wav_open(&wav_io, params->input_fp, if ((reader = wav_open(&io, params->ignore_length)) == 0) {
params->ignore_length)) == 0) {
fprintf(stderr, "ERROR: broken / unsupported input file\n"); fprintf(stderr, "ERROR: broken / unsupported input file\n");
goto END; goto END;
} }

View File

@ -17,6 +17,21 @@ struct pcm_reader_t {
pcm_reader_vtbl_t *vtbl; pcm_reader_vtbl_t *vtbl;
}; };
typedef int (*pcm_read_callback)(void *cookie, void *data, uint32_t count);
typedef int (*pcm_seek_callback)(void *cookie, int64_t off, int whence);
typedef int64_t (*pcm_tell_callback)(void *cookie);
typedef struct pcm_io_vtbl_t {
pcm_read_callback read;
pcm_seek_callback seek;
pcm_tell_callback tell;
} pcm_io_vtbl_t;
typedef struct pcm_io_context_t {
pcm_io_vtbl_t *vtbl;
void *cookie;
} pcm_io_context_t;
static inline static inline
const pcm_sample_description_t *pcm_get_format(pcm_reader_t *r) const pcm_sample_description_t *pcm_get_format(pcm_reader_t *r)
{ {
@ -49,4 +64,31 @@ void pcm_teardown(pcm_reader_t **r)
pcm_reader_t *pcm_open_sint16_converter(pcm_reader_t *reader); pcm_reader_t *pcm_open_sint16_converter(pcm_reader_t *reader);
#define TRY_IO(expr) \
do { \
if ((expr)) goto FAIL; \
} while (0)
int pcm_read(pcm_io_context_t *io, void *buffer, uint32_t size);
int pcm_skip(pcm_io_context_t *io, int64_t count);
static int pcm_seek(pcm_io_context_t *io, int64_t off, int whence)
{
return io->vtbl->seek ? io->vtbl->seek(io->cookie, off, whence) : -1;
}
static inline int64_t pcm_tell(pcm_io_context_t *io)
{
return io->vtbl->tell ? io->vtbl->tell(io->cookie) : -1;
}
int pcm_read16le(pcm_io_context_t *io, uint16_t *value);
int pcm_read16be(pcm_io_context_t *io, uint16_t *value);
int pcm_read32le(pcm_io_context_t *io, uint32_t *value);
int pcm_read32be(pcm_io_context_t *io, uint32_t *value);
int pcm_read64le(pcm_io_context_t *io, uint64_t *value);
int pcm_read64be(pcm_io_context_t *io, uint64_t *value);
int pcm_scanl(pcm_io_context_t *io, const char *fmt, ...);
int pcm_scanb(pcm_io_context_t *io, const char *fmt, ...);
#endif #endif

154
src/pcm_readhelper.c Normal file
View File

@ -0,0 +1,154 @@
/*
* 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;
}

View File

@ -15,23 +15,12 @@
#include <string.h> #include <string.h>
#include <stdarg.h> #include <stdarg.h>
#include "wav_reader.h" #include "wav_reader.h"
#include "m4af_endian.h"
#define RIFF_FOURCC(a,b,c,d) ((a)|((b)<<8)|((c)<<16)|((d)<<24)) #define RIFF_FOURCC(a,b,c,d) ((a)|((b)<<8)|((c)<<16)|((d)<<24))
#define TRY_IO(expr) \ #define ENSURE(expr) \
do { \ do { \
if (expr) \ if (!(expr)) goto FAIL;\
goto FAIL; \
} while (0)
#define ASSERT_FORMAT(ctx, expr) \
do { \
if (!expr) { \
if (!ctx->last_error) \
ctx->last_error = WAV_INVALID_FORMAT; \
goto FAIL;\
} \
} while (0) } while (0)
struct wav_reader_t { struct wav_reader_t {
@ -41,9 +30,7 @@ struct wav_reader_t {
int64_t position; int64_t position;
int32_t data_offset; int32_t data_offset;
int ignore_length; int ignore_length;
int last_error; pcm_io_context_t io;
wav_io_context_t io;
void *io_cookie;
}; };
static const uint8_t WAV_GUID_PCM[] = { static const uint8_t WAV_GUID_PCM[] = {
@ -74,144 +61,11 @@ static void wav_teardown(pcm_reader_t **reader)
*reader = 0; *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 static
uint32_t riff_next_chunk(wav_reader_t *reader, uint32_t *chunk_size) uint32_t riff_next_chunk(wav_reader_t *reader, uint32_t *chunk_size)
{ {
uint32_t fcc; uint32_t fcc;
if (riff_scan(reader, "LL", &fcc, chunk_size) == 2) return (pcm_scanl(&reader->io, "LL", &fcc, chunk_size) == 2) ? fcc : 0;
return fcc;
return 0;
} }
static static
@ -225,7 +79,7 @@ int wav_read_frames(pcm_reader_t *preader, void *buffer, unsigned nframes)
nframes = reader->length - reader->position; nframes = reader->length - reader->position;
nbytes = nframes * reader->sample_format.bytes_per_frame; nbytes = nframes * reader->sample_format.bytes_per_frame;
if (nbytes) { if (nbytes) {
if ((rc = riff_read(reader, buffer, nbytes)) < 0) if ((rc = pcm_read(&reader->io, buffer, nbytes)) < 0)
return -1; return -1;
nframes = rc / reader->sample_format.bytes_per_frame; nframes = rc / reader->sample_format.bytes_per_frame;
reader->position += nframes; reader->position += nframes;
@ -240,11 +94,10 @@ int riff_ds64(wav_reader_t *reader, int64_t *length)
uint64_t riff_size, sample_count; uint64_t riff_size, sample_count;
fcc = riff_next_chunk(reader, &chunk_size); fcc = riff_next_chunk(reader, &chunk_size);
ASSERT_FORMAT(reader, ENSURE(fcc == RIFF_FOURCC('d','s','6','4') && chunk_size >= 28);
fcc == RIFF_FOURCC('d','s','6','4') && chunk_size >= 28); TRY_IO(pcm_scanl(&reader->io, "QQQL",
TRY_IO(riff_scan(reader, "QQQL",
&riff_size, length, &sample_count, &table_size) != 4); &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; reader->data_offset += (chunk_size + 9) & ~1;
FAIL: FAIL:
return -1; return -1;
@ -259,41 +112,34 @@ int wav_fmt(wav_reader_t *reader, uint32_t size)
uint8_t guid[16]; uint8_t guid[16];
int is_float = 0; int is_float = 0;
ASSERT_FORMAT(reader, size >= 16); ENSURE(size >= 16);
TRY_IO(riff_scan(reader, "SSLLSS", &wFormatTag, &nChannels, TRY_IO(pcm_scanl(&reader->io, "SSLLSS", &wFormatTag, &nChannels,
&nSamplesPerSec, &nAvgBytesPerSec, &nBlockAlign, &nSamplesPerSec, &nAvgBytesPerSec, &nBlockAlign,
&wBitsPerSample) != 6); &wBitsPerSample) != 6);
wValidBitsPerSample = wBitsPerSample; wValidBitsPerSample = wBitsPerSample;
if (wFormatTag != 1 && wFormatTag != 3 && wFormatTag != 0xfffe) { ENSURE(wFormatTag == 1 || wFormatTag == 3 || wFormatTag == 0xfffe);
reader->last_error = WAV_UNSUPPORTED_FORMAT; ENSURE(nChannels && nSamplesPerSec && nAvgBytesPerSec &&
goto FAIL;
}
ASSERT_FORMAT(reader,
nChannels && nSamplesPerSec && nAvgBytesPerSec &&
nBlockAlign && wBitsPerSample && !(wBitsPerSample & 7) && nBlockAlign && wBitsPerSample && !(wBitsPerSample & 7) &&
nBlockAlign == nChannels * wBitsPerSample / 8); nBlockAlign == nChannels * wBitsPerSample / 8);
if (wFormatTag == 3) if (wFormatTag == 3)
is_float = 1; is_float = 1;
if (wFormatTag != 0xfffe) if (wFormatTag != 0xfffe)
TRY_IO(riff_skip(reader, (size - 15) & ~1)); TRY_IO(pcm_skip(&reader->io, (size - 15) & ~1));
else { else {
ASSERT_FORMAT(reader, size >= 40); ENSURE(size >= 40);
TRY_IO(riff_scan(reader, "SSL", TRY_IO(pcm_scanl(&reader->io, "SSL",
&cbSize, &wValidBitsPerSample, &dwChannelMask) != 3); &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) if (memcmp(guid, WAV_GUID_FLOAT, 16) == 0)
is_float = 1; is_float = 1;
else if (memcmp(guid, WAV_GUID_PCM, 16) != 0) { else if (memcmp(guid, WAV_GUID_PCM, 16) != 0)
reader->last_error = WAV_UNSUPPORTED_FORMAT;
goto FAIL; goto FAIL;
} ENSURE(wValidBitsPerSample && wValidBitsPerSample <= wBitsPerSample);
ASSERT_FORMAT(reader, TRY_IO(pcm_skip(&reader->io, (size - 39) & ~1));
wValidBitsPerSample &&
wValidBitsPerSample <= wBitsPerSample);
TRY_IO(riff_skip(reader, (size - 39) & ~1));
} }
reader->sample_format.sample_rate = nSamplesPerSec; reader->sample_format.sample_rate = nSamplesPerSec;
reader->sample_format.bits_per_channel = wValidBitsPerSample; reader->sample_format.bits_per_channel = wValidBitsPerSample;
@ -318,12 +164,10 @@ int wav_parse(wav_reader_t *reader, int64_t *data_length)
*data_length = 0; *data_length = 0;
container = riff_next_chunk(reader, &chunk_size); container = riff_next_chunk(reader, &chunk_size);
if (container != RIFF_FOURCC('R','I','F','F') && ENSURE(container == RIFF_FOURCC('R','I','F','F') ||
container != RIFF_FOURCC('R','F','6','4')) container == RIFF_FOURCC('R','F','6','4'));
goto FAIL; TRY_IO(pcm_read32le(&reader->io, &fcc));
TRY_IO(riff_read32(reader, &fcc)); ENSURE(fcc == RIFF_FOURCC('W','A','V','E'));
if (fcc != RIFF_FOURCC('W','A','V','E'))
goto FAIL;
reader->data_offset = 12; reader->data_offset = 12;
if (container == RIFF_FOURCC('R','F','6','4')) if (container == RIFF_FOURCC('R','F','6','4'))
@ -338,7 +182,7 @@ int wav_parse(wav_reader_t *reader, int64_t *data_length)
reader->data_offset += 8; reader->data_offset += 8;
break; break;
} else { } 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; reader->data_offset += (chunk_size + 9) & ~1;
} }
@ -356,8 +200,7 @@ static pcm_reader_vtbl_t wav_vtable = {
wav_teardown wav_teardown
}; };
pcm_reader_t *wav_open(wav_io_context_t *io_ctx, void *io_cookie, pcm_reader_t *wav_open(pcm_io_context_t *io, int ignore_length)
int ignore_length)
{ {
wav_reader_t *reader = 0; wav_reader_t *reader = 0;
int64_t data_length; int64_t data_length;
@ -365,8 +208,7 @@ pcm_reader_t *wav_open(wav_io_context_t *io_ctx, void *io_cookie,
if ((reader = calloc(1, sizeof(wav_reader_t))) == 0) if ((reader = calloc(1, sizeof(wav_reader_t))) == 0)
return 0; return 0;
memcpy(&reader->io, io_ctx, sizeof(wav_io_context_t)); memcpy(&reader->io, io, sizeof(pcm_io_context_t));
reader->io_cookie = io_cookie;
reader->ignore_length = ignore_length; reader->ignore_length = ignore_length;
if (wav_parse(reader, &data_length) < 0) { if (wav_parse(reader, &data_length) < 0) {
free(reader); free(reader);
@ -378,39 +220,34 @@ pcm_reader_t *wav_open(wav_io_context_t *io_ctx, void *io_cookie,
else else
reader->length = data_length / bpf; reader->length = data_length / bpf;
if (reader->length == INT64_MAX && reader->io.seek && reader->io.tell) { if (reader->length == INT64_MAX) {
if (reader->io.seek(reader->io_cookie, 0, SEEK_END) >= 0) { if (pcm_seek(&reader->io, 0, SEEK_END) >= 0) {
int64_t size = reader->io.tell(reader->io_cookie); int64_t size = pcm_tell(&reader->io);
if (size > 0) if (size > 0)
reader->length = (size - reader->data_offset) / bpf; 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; reader->vtbl = &wav_vtable;
return (pcm_reader_t *)reader; 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) const pcm_sample_description_t *desc)
{ {
wav_reader_t *reader = 0; wav_reader_t *reader = 0;
if ((reader = calloc(1, sizeof(wav_reader_t))) == 0) if ((reader = calloc(1, sizeof(wav_reader_t))) == 0)
return 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)); memcpy(&reader->sample_format, desc, sizeof(pcm_sample_description_t));
reader->io_cookie = io_cookie; if (pcm_seek(&reader->io, 0, SEEK_END) >= 0) {
if (io_ctx->seek && io_ctx->tell) { int64_t size = pcm_tell(&reader->io);
if (reader->io.seek(reader->io_cookie, 0, SEEK_END) >= 0) {
int64_t size = reader->io.tell(reader->io_cookie);
if (size > 0) if (size > 0)
reader->length = size / desc->bytes_per_frame; reader->length = size / reader->sample_format.bytes_per_frame;
reader->io.seek(reader->io_cookie, reader->data_offset, SEEK_SET); pcm_seek(&reader->io, 0, SEEK_SET);
}
} else } else
reader->length = INT64_MAX; reader->length = INT64_MAX;
reader->vtbl = &wav_vtable; reader->vtbl = &wav_vtable;
return (pcm_reader_t *)reader; return (pcm_reader_t *)reader;
} }

View File

@ -8,28 +8,10 @@
#include "lpcm.h" #include "lpcm.h"
#include "pcm_reader.h" #include "pcm_reader.h"
enum wav_error_code {
WAV_IO_ERROR = 1,
WAV_NO_MEMORY,
WAV_INVALID_FORMAT,
WAV_UNSUPPORTED_FORMAT
};
typedef int (*wav_read_callback)(void *cookie, void *data, uint32_t size);
typedef int (*wav_seek_callback)(void *cookie, int64_t off, int whence);
typedef int64_t (*wav_tell_callback)(void *cookie);
typedef struct wav_io_context_t {
wav_read_callback read;
wav_seek_callback seek;
wav_tell_callback tell;
} wav_io_context_t;
typedef struct wav_reader_t wav_reader_t; typedef struct wav_reader_t wav_reader_t;
pcm_reader_t *wav_open(wav_io_context_t *io_ctx, void *io_cookie, pcm_reader_t *wav_open(pcm_io_context_t *io, int ignore_length);
int ignore_length); pcm_reader_t *raw_open(pcm_io_context_t *io,
pcm_reader_t *raw_open(wav_io_context_t *io_ctx, void *io_cookie,
const pcm_sample_description_t *desc); const pcm_sample_description_t *desc);
#endif #endif