From: nu774 Date: Wed, 23 Oct 2013 16:07:06 +0000 (+0900) Subject: refactor pcm io routines X-Git-Tag: v0.4.0~3 X-Git-Url: http://git.ieval.ro/?a=commitdiff_plain;h=29a8f73fafe37beb8048316cca06ac83dfd1f2e1;p=fdkaac.git refactor pcm io routines --- diff --git a/MSVC/fdkaac.vcxproj b/MSVC/fdkaac.vcxproj index 6d71c90..99840f0 100644 --- a/MSVC/fdkaac.vcxproj +++ b/MSVC/fdkaac.vcxproj @@ -102,6 +102,7 @@ copy ..\fdk-aac\libSYS\include\machine_type.h include\fdk-aac\ + @@ -115,6 +116,7 @@ copy ..\fdk-aac\libSYS\include\machine_type.h include\fdk-aac\ + diff --git a/Makefile.am b/Makefile.am index 5f8c678..afe85ed 100644 --- a/Makefile.am +++ b/Makefile.am @@ -10,6 +10,7 @@ fdkaac_SOURCES = \ src/main.c \ src/metadata.c \ src/parson.c \ + src/pcm_readhelper.c \ src/pcm_sint16_converter.c \ src/progress.c \ src/wav_reader.c diff --git a/src/main.c b/src/main.c index 83fb754..a178070 100644 --- a/src/main.c +++ b/src/main.c @@ -643,12 +643,15 @@ int parse_raw_spec(const char *spec, pcm_sample_description_t *desc) 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 pcm_reader_t *open_input(aacenc_param_ex_t *params) { - wav_io_context_t wav_io = { - read_callback, seek_callback, tell_callback - }; + pcm_io_context_t io = { 0 }; pcm_reader_t *reader = 0; struct stat stb = { 0 }; @@ -657,11 +660,13 @@ pcm_reader_t *open_input(aacenc_param_ex_t *params) strerror(errno)); goto END; } - if (fstat(fileno(params->input_fp), &stb) == 0 - && (stb.st_mode & S_IFMT) != S_IFREG) { - wav_io.seek = 0; - wav_io.tell = 0; - } + io.cookie = params->input_fp; + if (fstat(fileno(io.cookie), &stb) == 0 + && (stb.st_mode & S_IFMT) == S_IFREG) + io.vtbl = &pcm_io_vtbl; + else + io.vtbl = &pcm_io_vtbl_noseek; + if (params->is_raw) { int bytes_per_channel; 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; bytes_per_channel = (desc.bits_per_channel + 7) / 8; 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"); goto END; } } else { - if ((reader = wav_open(&wav_io, params->input_fp, - params->ignore_length)) == 0) { + if ((reader = wav_open(&io, params->ignore_length)) == 0) { fprintf(stderr, "ERROR: broken / unsupported input file\n"); goto END; } diff --git a/src/pcm_reader.h b/src/pcm_reader.h index 26e7925..61debad 100644 --- a/src/pcm_reader.h +++ b/src/pcm_reader.h @@ -17,6 +17,21 @@ struct pcm_reader_t { 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 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); +#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 diff --git a/src/pcm_readhelper.c b/src/pcm_readhelper.c new file mode 100644 index 0000000..70b47f5 --- /dev/null +++ b/src/pcm_readhelper.c @@ -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 +#endif + +#include +#include +#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; +} diff --git a/src/wav_reader.c b/src/wav_reader.c index 334910e..133854b 100644 --- a/src/wav_reader.c +++ b/src/wav_reader.c @@ -15,23 +15,12 @@ #include #include #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 { @@ -41,9 +30,7 @@ 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[] = { @@ -74,144 +61,11 @@ static void wav_teardown(pcm_reader_t **reader) *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 @@ -225,7 +79,7 @@ int wav_read_frames(pcm_reader_t *preader, void *buffer, unsigned nframes) 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; @@ -240,11 +94,10 @@ int riff_ds64(wav_reader_t *reader, int64_t *length) 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; @@ -259,41 +112,34 @@ int wav_fmt(wav_reader_t *reader, uint32_t size) 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; @@ -318,12 +164,10 @@ int wav_parse(wav_reader_t *reader, int64_t *data_length) *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')) @@ -338,7 +182,7 @@ int wav_parse(wav_reader_t *reader, int64_t *data_length) 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; } @@ -356,8 +200,7 @@ static pcm_reader_vtbl_t wav_vtable = { 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; @@ -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) 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); @@ -378,39 +220,34 @@ pcm_reader_t *wav_open(wav_io_context_t *io_ctx, void *io_cookie, 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; } - - diff --git a/src/wav_reader.h b/src/wav_reader.h index 62afbc4..225064a 100644 --- a/src/wav_reader.h +++ b/src/wav_reader.h @@ -8,28 +8,10 @@ #include "lpcm.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; -pcm_reader_t *wav_open(wav_io_context_t *io_ctx, void *io_cookie, - int ignore_length); -pcm_reader_t *raw_open(wav_io_context_t *io_ctx, void *io_cookie, +pcm_reader_t *wav_open(pcm_io_context_t *io, int ignore_length); +pcm_reader_t *raw_open(pcm_io_context_t *io, const pcm_sample_description_t *desc); #endif