OggDecoder.cxx 3.91 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * 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 "OggDecoder.hxx"
#include "lib/xiph/OggFind.hxx"
#include "input/InputStream.hxx"
23

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/**
 * 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;

	{
45
		DecoderReader reader(client, input_stream);
46 47
		OggSyncState sync2(reader);
		OggStreamState stream2(GetSerialNo());
48 49 50 51

		/* passing synced=false because we're inside an
		   OggVisitor callback, and our InputStream may be in
		   the middle of an Ogg packet */
52
		result = OggSeekFindEOS(sync2, stream2, packet,
53
					input_stream, false);
54 55 56
	}

	/* restore the previous file position */
57 58
	try {
		input_stream.LockSeek(old_offset);
59
	} catch (...) {
60
	}
61 62 63 64 65 66 67 68 69 70 71 72 73 74

	return result;
}

ogg_int64_t
OggDecoder::LoadEndGranulePos() const
{
	ogg_packet packet;
	if (!LoadEndPacket(packet))
		return -1;

	return packet.granulepos;
}

75 76 77 78
inline void
OggDecoder::SeekByte(offset_type offset)
{
	input_stream.LockSeek(offset);
79
	PostSeek(offset);
80 81
}

82 83
void
OggDecoder::SeekGranulePos(ogg_int64_t where_granulepos)
84 85 86
{
	assert(IsSeekable());

87 88 89
	/* binary search: interpolate the file offset where we expect
	   to find the given granule position, and repeat until we're
	   close enough */
90

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
	static const ogg_int64_t MARGIN_BEFORE = 44100 / 3;
	static const ogg_int64_t MARGIN_AFTER = 44100 / 10;

	offset_type min_offset = 0, max_offset = input_stream.GetSize();
	ogg_int64_t min_granule = 0, max_granule = end_granulepos;

	while (true) {
		const offset_type delta_offset = max_offset - min_offset;
		const ogg_int64_t delta_granule = max_granule - min_granule;
		const ogg_int64_t relative_granule = where_granulepos - min_granule;

		const offset_type offset = min_offset + relative_granule * delta_offset
			/ delta_granule;

		SeekByte(offset);

		const auto new_granule = ReadGranulepos();
		if (new_granule < 0)
			/* no granulepos here, which shouldn't happen
			   - we can't improve, so stop */
			return;

		if (new_granule > where_granulepos + MARGIN_AFTER) {
			if (new_granule > max_granule)
				/* something went wrong */
				return;
117

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
			if (max_granule == new_granule)
				/* break out of the infinite loop, we
				   can't get any closer */
				break;

			/* reduce the max bounds and interpolate again */
			max_granule = new_granule;
			max_offset = GetStartOffset();
		} else if (new_granule + MARGIN_BEFORE < where_granulepos) {
			if (new_granule < min_granule)
				/* something went wrong */
				return;

			if (min_granule == new_granule)
				/* break out of the infinite loop, we
				   can't get any closer */
				break;

			/* increase the min bounds and interpolate
			   again */
			min_granule = new_granule;
			min_offset = GetStartOffset();
		} else {
			break;
		}
	}

	/* go back to the last page start so OggVisitor can start
	   visiting from here (we have consumed a few pages
	   already) */
	SeekByte(GetStartOffset());
}