2016-05-09 16:46:08 +02:00
|
|
|
/*
|
2017-01-03 20:48:59 +01:00
|
|
|
* Copyright 2003-2017 The Music Player Daemon Project
|
2016-05-09 16:46:08 +02:00
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h" /* must be first for large file support */
|
|
|
|
#include "OggDecoder.hxx"
|
|
|
|
#include "lib/xiph/OggFind.hxx"
|
|
|
|
#include "input/InputStream.hxx"
|
2016-09-09 18:47:42 +02:00
|
|
|
|
|
|
|
#include <stdexcept>
|
2016-05-09 16:46:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Load the end-of-stream packet and restore the previous file
|
|
|
|
* position.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
OggDecoder::LoadEndPacket(ogg_packet &packet) const
|
|
|
|
{
|
|
|
|
if (!input_stream.CheapSeeking())
|
|
|
|
/* we do this for local files only, because seeking
|
|
|
|
around remote files is expensive and not worth the
|
|
|
|
trouble */
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const auto old_offset = input_stream.GetOffset();
|
|
|
|
|
|
|
|
/* create temporary Ogg objects for seeking and parsing the
|
|
|
|
EOS packet */
|
|
|
|
|
|
|
|
bool result;
|
|
|
|
|
|
|
|
{
|
2016-11-18 07:13:35 +01:00
|
|
|
DecoderReader reader(client, input_stream);
|
2016-05-09 16:46:08 +02:00
|
|
|
OggSyncState sync2(reader);
|
|
|
|
OggStreamState stream2(GetSerialNo());
|
|
|
|
result = OggSeekFindEOS(sync2, stream2, packet,
|
|
|
|
input_stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* restore the previous file position */
|
2016-09-09 18:47:42 +02:00
|
|
|
try {
|
|
|
|
input_stream.LockSeek(old_offset);
|
|
|
|
} catch (const std::runtime_error &) {
|
|
|
|
}
|
2016-05-09 16:46:08 +02:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
ogg_int64_t
|
|
|
|
OggDecoder::LoadEndGranulePos() const
|
|
|
|
{
|
|
|
|
ogg_packet packet;
|
|
|
|
if (!LoadEndPacket(packet))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return packet.granulepos;
|
|
|
|
}
|
|
|
|
|
2016-09-09 18:47:42 +02:00
|
|
|
void
|
|
|
|
OggDecoder::SeekGranulePos(ogg_int64_t where_granulepos)
|
2016-05-09 16:46:08 +02:00
|
|
|
{
|
|
|
|
assert(IsSeekable());
|
|
|
|
|
|
|
|
/* interpolate the file offset where we expect to find the
|
|
|
|
given granule position */
|
|
|
|
/* TODO: implement binary search */
|
|
|
|
offset_type offset(where_granulepos * input_stream.GetSize()
|
|
|
|
/ end_granulepos);
|
|
|
|
|
2016-09-09 18:47:42 +02:00
|
|
|
input_stream.LockSeek(offset);
|
2016-05-09 16:46:08 +02:00
|
|
|
PostSeek();
|
|
|
|
}
|
|
|
|
|