2013-01-04 17:10:05 +01:00
|
|
|
/*
|
|
|
|
* 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 <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
2014-08-12 16:43:19 +02:00
|
|
|
#include "pcm_reader.h"
|
2013-01-04 17:10:05 +01:00
|
|
|
|
|
|
|
#define RIFF_FOURCC(a,b,c,d) ((a)|((b)<<8)|((c)<<16)|((d)<<24))
|
|
|
|
|
2013-10-24 13:09:17 +02:00
|
|
|
typedef struct wav_reader_t {
|
2013-10-20 15:47:40 +02:00
|
|
|
pcm_reader_vtbl_t *vtbl;
|
2013-01-04 17:10:05 +01:00
|
|
|
pcm_sample_description_t sample_format;
|
|
|
|
int64_t length;
|
|
|
|
int64_t position;
|
2013-01-09 03:37:25 +01:00
|
|
|
int32_t data_offset;
|
2013-01-04 17:10:05 +01:00
|
|
|
int ignore_length;
|
2013-10-23 18:07:06 +02:00
|
|
|
pcm_io_context_t io;
|
2013-10-24 13:09:17 +02:00
|
|
|
} wav_reader_t;
|
2013-01-04 17:10:05 +01:00
|
|
|
|
|
|
|
static const uint8_t WAV_GUID_PCM[] = {
|
|
|
|
1, 0, 0, 0, 0, 0, 0x10, 0, 0x80, 0, 0, 0xaa, 0, 0x38, 0x9b, 0x71
|
|
|
|
};
|
|
|
|
static const uint8_t WAV_GUID_FLOAT[] = {
|
|
|
|
3, 0, 0, 0, 0, 0, 0x10, 0, 0x80, 0, 0, 0xaa, 0, 0x38, 0x9b, 0x71
|
|
|
|
};
|
|
|
|
|
2013-10-20 15:47:40 +02:00
|
|
|
static const pcm_sample_description_t *wav_get_format(pcm_reader_t *reader)
|
2013-01-04 17:10:05 +01:00
|
|
|
{
|
2013-10-20 15:47:40 +02:00
|
|
|
return &((wav_reader_t *)reader)->sample_format;
|
2013-01-04 17:10:05 +01:00
|
|
|
}
|
|
|
|
|
2013-10-20 15:47:40 +02:00
|
|
|
static int64_t wav_get_length(pcm_reader_t *reader)
|
2013-01-04 17:10:05 +01:00
|
|
|
{
|
2013-10-20 15:47:40 +02:00
|
|
|
return ((wav_reader_t *)reader)->length;
|
2013-01-04 17:10:05 +01:00
|
|
|
}
|
|
|
|
|
2013-10-20 15:47:40 +02:00
|
|
|
static int64_t wav_get_position(pcm_reader_t *reader)
|
2013-01-04 17:10:05 +01:00
|
|
|
{
|
2013-10-20 15:47:40 +02:00
|
|
|
return ((wav_reader_t *)reader)->position;
|
2013-01-04 17:10:05 +01:00
|
|
|
}
|
|
|
|
|
2013-10-20 15:47:40 +02:00
|
|
|
static void wav_teardown(pcm_reader_t **reader)
|
2013-01-04 17:10:05 +01:00
|
|
|
{
|
|
|
|
free(*reader);
|
|
|
|
*reader = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
uint32_t riff_next_chunk(wav_reader_t *reader, uint32_t *chunk_size)
|
|
|
|
{
|
|
|
|
uint32_t fcc;
|
2013-10-23 18:07:06 +02:00
|
|
|
return (pcm_scanl(&reader->io, "LL", &fcc, chunk_size) == 2) ? fcc : 0;
|
2013-01-04 17:10:05 +01:00
|
|
|
}
|
|
|
|
|
2013-10-20 15:47:40 +02:00
|
|
|
static
|
|
|
|
int wav_read_frames(pcm_reader_t *preader, void *buffer, unsigned nframes)
|
2013-01-04 17:10:05 +01:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
unsigned nbytes;
|
2013-10-20 15:47:40 +02:00
|
|
|
wav_reader_t *reader = (wav_reader_t *)preader;
|
2013-01-04 17:10:05 +01:00
|
|
|
|
|
|
|
if (!reader->ignore_length && nframes > reader->length - reader->position)
|
|
|
|
nframes = reader->length - reader->position;
|
|
|
|
nbytes = nframes * reader->sample_format.bytes_per_frame;
|
|
|
|
if (nbytes) {
|
2013-10-23 18:07:06 +02:00
|
|
|
if ((rc = pcm_read(&reader->io, buffer, nbytes)) < 0)
|
2013-01-04 17:10:05 +01:00
|
|
|
return -1;
|
|
|
|
nframes = rc / reader->sample_format.bytes_per_frame;
|
|
|
|
reader->position += nframes;
|
|
|
|
}
|
|
|
|
return nframes;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
int riff_ds64(wav_reader_t *reader, int64_t *length)
|
|
|
|
{
|
|
|
|
uint32_t fcc, chunk_size, table_size;
|
|
|
|
uint64_t riff_size, sample_count;
|
|
|
|
|
|
|
|
fcc = riff_next_chunk(reader, &chunk_size);
|
2013-10-23 18:07:06 +02:00
|
|
|
ENSURE(fcc == RIFF_FOURCC('d','s','6','4') && chunk_size >= 28);
|
|
|
|
TRY_IO(pcm_scanl(&reader->io, "QQQL",
|
2013-01-04 17:10:05 +01:00
|
|
|
&riff_size, length, &sample_count, &table_size) != 4);
|
2013-10-23 18:07:06 +02:00
|
|
|
TRY_IO(pcm_skip(&reader->io, (chunk_size - 27) & ~1));
|
2013-01-04 17:10:05 +01:00
|
|
|
FAIL:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
int wav_fmt(wav_reader_t *reader, uint32_t size)
|
|
|
|
{
|
|
|
|
uint16_t wFormatTag, nChannels, nBlockAlign, wBitsPerSample, cbSize;
|
|
|
|
uint32_t nSamplesPerSec, nAvgBytesPerSec, dwChannelMask = 0;
|
|
|
|
uint16_t wValidBitsPerSample;
|
|
|
|
uint8_t guid[16];
|
|
|
|
int is_float = 0;
|
|
|
|
|
2013-10-23 18:07:06 +02:00
|
|
|
ENSURE(size >= 16);
|
|
|
|
TRY_IO(pcm_scanl(&reader->io, "SSLLSS", &wFormatTag, &nChannels,
|
2013-01-04 17:10:05 +01:00
|
|
|
&nSamplesPerSec, &nAvgBytesPerSec, &nBlockAlign,
|
|
|
|
&wBitsPerSample) != 6);
|
|
|
|
wValidBitsPerSample = wBitsPerSample;
|
|
|
|
|
2013-10-23 18:07:06 +02:00
|
|
|
ENSURE(wFormatTag == 1 || wFormatTag == 3 || wFormatTag == 0xfffe);
|
2022-08-04 14:06:10 +02:00
|
|
|
ENSURE(nChannels > 0 && nChannels <= 8 &&
|
|
|
|
nSamplesPerSec && nAvgBytesPerSec &&
|
|
|
|
nBlockAlign && nBlockAlign < 256 &&
|
|
|
|
wBitsPerSample && wBitsPerSample < 256 && !(wBitsPerSample & 7) &&
|
2013-10-23 18:07:06 +02:00
|
|
|
nBlockAlign == nChannels * wBitsPerSample / 8);
|
|
|
|
|
2013-01-04 17:10:05 +01:00
|
|
|
if (wFormatTag == 3)
|
|
|
|
is_float = 1;
|
|
|
|
|
|
|
|
if (wFormatTag != 0xfffe)
|
2013-10-23 18:07:06 +02:00
|
|
|
TRY_IO(pcm_skip(&reader->io, (size - 15) & ~1));
|
2013-01-04 17:10:05 +01:00
|
|
|
else {
|
2013-10-23 18:07:06 +02:00
|
|
|
ENSURE(size >= 40);
|
|
|
|
TRY_IO(pcm_scanl(&reader->io, "SSL",
|
2013-01-04 17:10:05 +01:00
|
|
|
&cbSize, &wValidBitsPerSample, &dwChannelMask) != 3);
|
2013-10-23 18:07:06 +02:00
|
|
|
TRY_IO(pcm_read(&reader->io, guid, 16) != 16);
|
2013-01-04 17:10:05 +01:00
|
|
|
|
|
|
|
if (memcmp(guid, WAV_GUID_FLOAT, 16) == 0)
|
|
|
|
is_float = 1;
|
2013-10-23 18:07:06 +02:00
|
|
|
else if (memcmp(guid, WAV_GUID_PCM, 16) != 0)
|
2013-01-04 17:10:05 +01:00
|
|
|
goto FAIL;
|
2013-10-23 18:07:06 +02:00
|
|
|
ENSURE(wValidBitsPerSample && wValidBitsPerSample <= wBitsPerSample);
|
|
|
|
TRY_IO(pcm_skip(&reader->io, (size - 39) & ~1));
|
2013-01-04 17:10:05 +01:00
|
|
|
}
|
|
|
|
reader->sample_format.sample_rate = nSamplesPerSec;
|
|
|
|
reader->sample_format.bits_per_channel = wValidBitsPerSample;
|
|
|
|
reader->sample_format.bytes_per_frame = nBlockAlign;
|
|
|
|
reader->sample_format.channels_per_frame = nChannels;
|
|
|
|
reader->sample_format.channel_mask = dwChannelMask;
|
|
|
|
if (is_float)
|
|
|
|
reader->sample_format.sample_type = PCM_TYPE_FLOAT;
|
|
|
|
else if (wBitsPerSample == 8)
|
|
|
|
reader->sample_format.sample_type = PCM_TYPE_UINT;
|
|
|
|
else
|
|
|
|
reader->sample_format.sample_type = PCM_TYPE_SINT;
|
|
|
|
return 0;
|
|
|
|
FAIL:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
int wav_parse(wav_reader_t *reader, int64_t *data_length)
|
|
|
|
{
|
|
|
|
uint32_t container, fcc, chunk_size;
|
2022-07-13 13:56:49 +02:00
|
|
|
int fmt_seen = 0;
|
2013-01-04 17:10:05 +01:00
|
|
|
|
|
|
|
*data_length = 0;
|
|
|
|
container = riff_next_chunk(reader, &chunk_size);
|
2013-10-23 18:07:06 +02:00
|
|
|
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'));
|
2013-01-09 03:37:25 +01:00
|
|
|
|
2013-01-04 17:10:05 +01:00
|
|
|
if (container == RIFF_FOURCC('R','F','6','4'))
|
|
|
|
riff_ds64(reader, data_length);
|
|
|
|
while ((fcc = riff_next_chunk(reader, &chunk_size)) != 0) {
|
|
|
|
if (fcc == RIFF_FOURCC('f','m','t',' ')) {
|
2022-07-13 13:56:49 +02:00
|
|
|
fmt_seen = 1;
|
2013-01-04 17:10:05 +01:00
|
|
|
if (wav_fmt(reader, chunk_size) < 0)
|
|
|
|
goto FAIL;
|
|
|
|
} else if (fcc == RIFF_FOURCC('d','a','t','a')) {
|
|
|
|
if (container == RIFF_FOURCC('R','I','F','F'))
|
|
|
|
*data_length = chunk_size;
|
2013-10-30 06:27:28 +01:00
|
|
|
reader->data_offset = pcm_tell(&reader->io);
|
2013-01-04 17:10:05 +01:00
|
|
|
break;
|
2013-01-09 03:37:25 +01:00
|
|
|
} else {
|
2013-10-23 18:07:06 +02:00
|
|
|
TRY_IO(pcm_skip(&reader->io, (chunk_size + 1) & ~1));
|
2013-01-09 03:37:25 +01:00
|
|
|
}
|
2013-01-04 17:10:05 +01:00
|
|
|
}
|
2022-07-13 13:56:49 +02:00
|
|
|
ENSURE(fmt_seen && fcc == RIFF_FOURCC('d', 'a', 't', 'a'));
|
|
|
|
return 0;
|
2013-01-04 17:10:05 +01:00
|
|
|
FAIL:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-10-20 15:47:40 +02:00
|
|
|
static pcm_reader_vtbl_t wav_vtable = {
|
|
|
|
wav_get_format,
|
|
|
|
wav_get_length,
|
|
|
|
wav_get_position,
|
|
|
|
wav_read_frames,
|
|
|
|
wav_teardown
|
|
|
|
};
|
|
|
|
|
2013-10-23 18:07:06 +02:00
|
|
|
pcm_reader_t *wav_open(pcm_io_context_t *io, int ignore_length)
|
2013-01-04 17:10:05 +01:00
|
|
|
{
|
2013-01-09 03:37:25 +01:00
|
|
|
wav_reader_t *reader = 0;
|
2013-01-04 17:10:05 +01:00
|
|
|
int64_t data_length;
|
2013-01-09 03:37:25 +01:00
|
|
|
unsigned bpf;
|
2013-01-04 17:10:05 +01:00
|
|
|
|
|
|
|
if ((reader = calloc(1, sizeof(wav_reader_t))) == 0)
|
|
|
|
return 0;
|
2013-10-23 18:07:06 +02:00
|
|
|
memcpy(&reader->io, io, sizeof(pcm_io_context_t));
|
2013-01-04 17:10:05 +01:00
|
|
|
reader->ignore_length = ignore_length;
|
|
|
|
if (wav_parse(reader, &data_length) < 0) {
|
|
|
|
free(reader);
|
|
|
|
return 0;
|
|
|
|
}
|
2013-01-09 03:37:25 +01:00
|
|
|
bpf = reader->sample_format.bytes_per_frame;
|
|
|
|
if (ignore_length || !data_length || data_length % bpf)
|
2013-01-04 17:10:05 +01:00
|
|
|
reader->length = INT64_MAX;
|
|
|
|
else
|
2013-01-09 03:37:25 +01:00
|
|
|
reader->length = data_length / bpf;
|
|
|
|
|
2013-10-23 18:07:06 +02:00
|
|
|
if (reader->length == INT64_MAX) {
|
|
|
|
if (pcm_seek(&reader->io, 0, SEEK_END) >= 0) {
|
|
|
|
int64_t size = pcm_tell(&reader->io);
|
2013-01-09 03:37:25 +01:00
|
|
|
if (size > 0)
|
|
|
|
reader->length = (size - reader->data_offset) / bpf;
|
2013-10-23 18:07:06 +02:00
|
|
|
pcm_seek(&reader->io, reader->data_offset, SEEK_SET);
|
2013-01-09 03:37:25 +01:00
|
|
|
}
|
|
|
|
}
|
2013-10-20 15:47:40 +02:00
|
|
|
reader->vtbl = &wav_vtable;
|
|
|
|
return (pcm_reader_t *)reader;
|
2013-01-04 17:10:05 +01:00
|
|
|
}
|
2013-01-08 16:12:55 +01:00
|
|
|
|
2013-10-23 18:07:06 +02:00
|
|
|
pcm_reader_t *raw_open(pcm_io_context_t *io,
|
2013-01-08 16:12:55 +01:00
|
|
|
const pcm_sample_description_t *desc)
|
|
|
|
{
|
|
|
|
wav_reader_t *reader = 0;
|
|
|
|
|
|
|
|
if ((reader = calloc(1, sizeof(wav_reader_t))) == 0)
|
|
|
|
return 0;
|
2013-10-23 18:07:06 +02:00
|
|
|
memcpy(&reader->io, io, sizeof(pcm_io_context_t));
|
2013-01-08 16:12:55 +01:00
|
|
|
memcpy(&reader->sample_format, desc, sizeof(pcm_sample_description_t));
|
2013-10-23 18:07:06 +02:00
|
|
|
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);
|
2013-01-08 16:12:55 +01:00
|
|
|
} else
|
|
|
|
reader->length = INT64_MAX;
|
2013-10-20 15:47:40 +02:00
|
|
|
reader->vtbl = &wav_vtable;
|
|
|
|
return (pcm_reader_t *)reader;
|
2013-01-08 16:12:55 +01:00
|
|
|
}
|