InputStream: make Seek() always absolute
Remove the "whence" parameter that is not actually necessary, and only complicates the InputStream implementations.
This commit is contained in:
@@ -92,13 +92,17 @@ audiofile_file_destroy(AFvirtualfile *vfile)
|
||||
}
|
||||
|
||||
static AFfileoffset
|
||||
audiofile_file_seek(AFvirtualfile *vfile, AFfileoffset offset, int is_relative)
|
||||
audiofile_file_seek(AFvirtualfile *vfile, AFfileoffset _offset,
|
||||
int is_relative)
|
||||
{
|
||||
InputStream &is = *(InputStream *)vfile->closure;
|
||||
int whence = (is_relative ? SEEK_CUR : SEEK_SET);
|
||||
|
||||
InputStream::offset_type offset = _offset;
|
||||
if (is_relative)
|
||||
offset += is.GetOffset();
|
||||
|
||||
Error error;
|
||||
if (is.LockSeek(offset, whence, error)) {
|
||||
if (is.LockSeek(offset, IgnoreError())) {
|
||||
return is.GetOffset();
|
||||
} else {
|
||||
return -1;
|
||||
|
@@ -30,9 +30,7 @@
|
||||
#include "tag/TagId3.hxx"
|
||||
#include "util/Error.hxx"
|
||||
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h> /* for SEEK_SET, SEEK_CUR */
|
||||
|
||||
#ifdef HAVE_ID3TAG
|
||||
#include <id3tag.h>
|
||||
@@ -63,7 +61,7 @@ dsdlib_skip_to(Decoder *decoder, InputStream &is,
|
||||
int64_t offset)
|
||||
{
|
||||
if (is.IsSeekable())
|
||||
return is.Seek(offset, SEEK_SET, IgnoreError());
|
||||
return is.Seek(offset, IgnoreError());
|
||||
|
||||
if (is.GetOffset() > offset)
|
||||
return false;
|
||||
@@ -96,7 +94,7 @@ dsdlib_skip(Decoder *decoder, InputStream &is,
|
||||
return true;
|
||||
|
||||
if (is.IsSeekable())
|
||||
return is.Seek(delta, SEEK_CUR, IgnoreError());
|
||||
return is.Seek(is.GetOffset() + delta, IgnoreError());
|
||||
|
||||
char buffer[8192];
|
||||
while (delta > 0) {
|
||||
|
@@ -186,7 +186,7 @@ faad_song_duration(DecoderBuffer *buffer, InputStream &is)
|
||||
/* obtain the duration from the ADTS header */
|
||||
float song_length = adts_song_duration(buffer);
|
||||
|
||||
is.LockSeek(tagsize, SEEK_SET, IgnoreError());
|
||||
is.LockSeek(tagsize, IgnoreError());
|
||||
|
||||
decoder_buffer_clear(buffer);
|
||||
decoder_buffer_fill(buffer);
|
||||
|
@@ -120,10 +120,29 @@ mpd_ffmpeg_stream_seek(void *opaque, int64_t pos, int whence)
|
||||
{
|
||||
AvioStream *stream = (AvioStream *)opaque;
|
||||
|
||||
if (whence == AVSEEK_SIZE)
|
||||
switch (whence) {
|
||||
case SEEK_SET:
|
||||
break;
|
||||
|
||||
case SEEK_CUR:
|
||||
pos += stream->input.GetOffset();
|
||||
break;
|
||||
|
||||
case SEEK_END:
|
||||
if (!stream->input.KnownSize())
|
||||
return -1;
|
||||
|
||||
pos += stream->input.GetSize();
|
||||
break;
|
||||
|
||||
case AVSEEK_SIZE:
|
||||
return stream->input.GetSize();
|
||||
|
||||
if (!stream->input.LockSeek(pos, whence, IgnoreError()))
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!stream->input.LockSeek(pos, IgnoreError()))
|
||||
return -1;
|
||||
|
||||
return stream->input.GetOffset();
|
||||
|
@@ -62,12 +62,31 @@ FlacIORead(void *ptr, size_t size, size_t nmemb, FLAC__IOHandle handle)
|
||||
}
|
||||
|
||||
static int
|
||||
FlacIOSeek(FLAC__IOHandle handle, FLAC__int64 offset, int whence)
|
||||
FlacIOSeek(FLAC__IOHandle handle, FLAC__int64 _offset, int whence)
|
||||
{
|
||||
InputStream *is = (InputStream *)handle;
|
||||
|
||||
Error error;
|
||||
return is->LockSeek(offset, whence, error) ? 0 : -1;
|
||||
InputStream::offset_type offset = _offset;
|
||||
switch (whence) {
|
||||
case SEEK_SET:
|
||||
break;
|
||||
|
||||
case SEEK_CUR:
|
||||
offset += is->GetOffset();
|
||||
break;
|
||||
|
||||
case SEEK_END:
|
||||
if (!is->KnownSize())
|
||||
return -1;
|
||||
|
||||
offset += is->GetSize();
|
||||
break;
|
||||
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
|
||||
return is->LockSeek(offset, IgnoreError()) ? 0 : -1;
|
||||
}
|
||||
|
||||
static FLAC__int64
|
||||
|
@@ -51,7 +51,7 @@ FlacInput::Seek(FLAC__uint64 absolute_byte_offset)
|
||||
return FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED;
|
||||
|
||||
::Error error;
|
||||
if (!input_stream.LockSeek(absolute_byte_offset, SEEK_SET, error)) {
|
||||
if (!input_stream.LockSeek(absolute_byte_offset, error)) {
|
||||
LogError(error);
|
||||
return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
|
||||
}
|
||||
|
@@ -208,7 +208,7 @@ inline bool
|
||||
MadDecoder::Seek(long offset)
|
||||
{
|
||||
Error error;
|
||||
if (!input_stream.LockSeek(offset, SEEK_SET, error))
|
||||
if (!input_stream.LockSeek(offset, error))
|
||||
return false;
|
||||
|
||||
mad_stream_buffer(&stream, input_buffer, 0);
|
||||
|
@@ -57,7 +57,7 @@ mpc_seek_cb(mpc_reader *reader, mpc_int32_t offset)
|
||||
struct mpc_decoder_data *data =
|
||||
(struct mpc_decoder_data *)reader->data;
|
||||
|
||||
return data->is.LockSeek(offset, SEEK_SET, IgnoreError());
|
||||
return data->is.LockSeek(offset, IgnoreError());
|
||||
}
|
||||
|
||||
static mpc_int32_t
|
||||
|
@@ -22,8 +22,6 @@
|
||||
#include "OggSyncState.hxx"
|
||||
#include "util/Error.hxx"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
bool
|
||||
OggFindEOS(OggSyncState &oy, ogg_stream_state &os, ogg_packet &packet)
|
||||
{
|
||||
@@ -41,7 +39,7 @@ OggFindEOS(OggSyncState &oy, ogg_stream_state &os, ogg_packet &packet)
|
||||
|
||||
bool
|
||||
OggSeekPageAtOffset(OggSyncState &oy, ogg_stream_state &os, InputStream &is,
|
||||
InputStream::offset_type offset, int whence)
|
||||
InputStream::offset_type offset)
|
||||
{
|
||||
oy.Reset();
|
||||
|
||||
@@ -49,7 +47,7 @@ OggSeekPageAtOffset(OggSyncState &oy, ogg_stream_state &os, InputStream &is,
|
||||
data */
|
||||
ogg_stream_reset(&os);
|
||||
|
||||
return is.LockSeek(offset, whence, IgnoreError()) &&
|
||||
return is.LockSeek(offset, IgnoreError()) &&
|
||||
oy.ExpectPageSeekIn(os);
|
||||
}
|
||||
|
||||
@@ -57,12 +55,15 @@ bool
|
||||
OggSeekFindEOS(OggSyncState &oy, ogg_stream_state &os, ogg_packet &packet,
|
||||
InputStream &is)
|
||||
{
|
||||
if (is.KnownSize() && is.GetRest() < 65536)
|
||||
if (!is.KnownSize())
|
||||
return false;
|
||||
|
||||
if (is.GetRest() < 65536)
|
||||
return OggFindEOS(oy, os, packet);
|
||||
|
||||
if (!is.CheapSeeking())
|
||||
return false;
|
||||
|
||||
return OggSeekPageAtOffset(oy, os, is, -65536, SEEK_END) &&
|
||||
return OggSeekPageAtOffset(oy, os, is, is.GetSize() - 65536) &&
|
||||
OggFindEOS(oy, os, packet);
|
||||
}
|
||||
|
@@ -41,7 +41,7 @@ OggFindEOS(OggSyncState &oy, ogg_stream_state &os, ogg_packet &packet);
|
||||
*/
|
||||
bool
|
||||
OggSeekPageAtOffset(OggSyncState &oy, ogg_stream_state &os, InputStream &is,
|
||||
InputStream::offset_type offset, int whence);
|
||||
InputStream::offset_type offset);
|
||||
|
||||
/**
|
||||
* Try to find the end-of-stream (EOS) packet. Seek to the end of the
|
||||
|
@@ -199,7 +199,7 @@ LoadEOSPacket(InputStream &is, Decoder *decoder, int serialno,
|
||||
ogg_stream_clear(&os);
|
||||
|
||||
/* restore the previous file position */
|
||||
is.Seek(old_offset, SEEK_SET, IgnoreError());
|
||||
is.Seek(old_offset, IgnoreError());
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -344,7 +344,7 @@ MPDOpusDecoder::Seek(OggSyncState &oy, double where_s)
|
||||
InputStream::offset_type offset(where_granulepos * input_stream.GetSize()
|
||||
/ eos_granulepos);
|
||||
|
||||
if (!OggSeekPageAtOffset(oy, os, input_stream, offset, SEEK_SET))
|
||||
if (!OggSeekPageAtOffset(oy, os, input_stream, offset))
|
||||
return false;
|
||||
|
||||
decoder_timestamp(decoder, where_s);
|
||||
|
@@ -26,7 +26,6 @@
|
||||
#include "Log.hxx"
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h> /* for SEEK_SET */
|
||||
|
||||
static void
|
||||
pcm_stream_decode(Decoder &decoder, InputStream &is)
|
||||
@@ -76,7 +75,7 @@ pcm_stream_decode(Decoder &decoder, InputStream &is)
|
||||
decoder_seek_where(decoder));
|
||||
|
||||
Error error;
|
||||
if (is.LockSeek(offset, SEEK_SET, error)) {
|
||||
if (is.LockSeek(offset, error)) {
|
||||
decoder_command_finished(decoder);
|
||||
} else {
|
||||
LogError(error);
|
||||
|
@@ -41,11 +41,31 @@ sndfile_vio_get_filelen(void *user_data)
|
||||
}
|
||||
|
||||
static sf_count_t
|
||||
sndfile_vio_seek(sf_count_t offset, int whence, void *user_data)
|
||||
sndfile_vio_seek(sf_count_t _offset, int whence, void *user_data)
|
||||
{
|
||||
InputStream &is = *(InputStream *)user_data;
|
||||
|
||||
if (!is.LockSeek(offset, whence, IgnoreError()))
|
||||
InputStream::offset_type offset = _offset;
|
||||
switch (whence) {
|
||||
case SEEK_SET:
|
||||
break;
|
||||
|
||||
case SEEK_CUR:
|
||||
offset += is.GetOffset();
|
||||
break;
|
||||
|
||||
case SEEK_END:
|
||||
if (!is.KnownSize())
|
||||
return -1;
|
||||
|
||||
offset += is.GetSize();
|
||||
break;
|
||||
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!is.LockSeek(offset, IgnoreError()))
|
||||
return -1;
|
||||
|
||||
return is.GetOffset();
|
||||
|
@@ -70,15 +70,37 @@ static size_t ogg_read_cb(void *ptr, size_t size, size_t nmemb, void *data)
|
||||
return ret / size;
|
||||
}
|
||||
|
||||
static int ogg_seek_cb(void *data, ogg_int64_t offset, int whence)
|
||||
static int ogg_seek_cb(void *data, ogg_int64_t _offset, int whence)
|
||||
{
|
||||
VorbisInputStream *vis = (VorbisInputStream *)data;
|
||||
InputStream &is = vis->input_stream;
|
||||
|
||||
Error error;
|
||||
return vis->seekable &&
|
||||
(vis->decoder == nullptr ||
|
||||
decoder_get_command(*vis->decoder) != DecoderCommand::STOP) &&
|
||||
vis->input_stream.LockSeek(offset, whence, error)
|
||||
if (!vis->seekable ||
|
||||
(vis->decoder != nullptr &&
|
||||
decoder_get_command(*vis->decoder) == DecoderCommand::STOP))
|
||||
return -1;
|
||||
|
||||
InputStream::offset_type offset = _offset;
|
||||
switch (whence) {
|
||||
case SEEK_SET:
|
||||
break;
|
||||
|
||||
case SEEK_CUR:
|
||||
offset += is.GetOffset();
|
||||
break;
|
||||
|
||||
case SEEK_END:
|
||||
if (!is.KnownSize())
|
||||
return -1;
|
||||
|
||||
offset += is.GetSize();
|
||||
break;
|
||||
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
|
||||
return is.LockSeek(offset, IgnoreError())
|
||||
? 0 : -1;
|
||||
}
|
||||
|
||||
|
@@ -392,13 +392,35 @@ wavpack_input_get_pos(void *id)
|
||||
static int
|
||||
wavpack_input_set_pos_abs(void *id, uint32_t pos)
|
||||
{
|
||||
return wpin(id)->is.LockSeek(pos, SEEK_SET, IgnoreError()) ? 0 : -1;
|
||||
return wpin(id)->is.LockSeek(pos, IgnoreError()) ? 0 : -1;
|
||||
}
|
||||
|
||||
static int
|
||||
wavpack_input_set_pos_rel(void *id, int32_t delta, int mode)
|
||||
{
|
||||
return wpin(id)->is.LockSeek(delta, mode, IgnoreError()) ? 0 : -1;
|
||||
InputStream &is = wpin(id)->is;
|
||||
|
||||
InputStream::offset_type offset = delta;
|
||||
switch (mode) {
|
||||
case SEEK_SET:
|
||||
break;
|
||||
|
||||
case SEEK_CUR:
|
||||
offset += is.GetOffset();
|
||||
break;
|
||||
|
||||
case SEEK_END:
|
||||
if (!is.KnownSize())
|
||||
return -1;
|
||||
|
||||
offset += is.GetSize();
|
||||
break;
|
||||
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
|
||||
return is.LockSeek(offset, IgnoreError()) ? 0 : -1;
|
||||
}
|
||||
|
||||
static int
|
||||
|
Reference in New Issue
Block a user