FlacCommon.cxx 3.53 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 The Music Player Daemon Project
3
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
14 15 16 17 18 19 20 21
 *
 * 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.
 */

/*
 * Common data structures and functions used by FLAC and OggFLAC
22 23
 */

24
#include "FlacCommon.hxx"
25
#include "lib/xiph/FlacStreamMetadata.hxx"
26
#include "Log.hxx"
27
#include "input/InputStream.hxx"
28

29
#include <exception>
30

31
bool
32 33
FlacDecoder::Initialize(unsigned sample_rate, unsigned bits_per_sample,
			unsigned channels, FLAC__uint64 total_frames)
34 35 36 37
{
	assert(!initialized);
	assert(!unsupported);

38 39 40
	try {
		pcm_import.Open(sample_rate, bits_per_sample,
				channels);
41 42
	} catch (...) {
		LogError(std::current_exception());
43 44 45 46
		unsupported = true;
		return false;
	}

47
	const auto audio_format = pcm_import.GetAudioFormat();
48 49 50 51 52 53

	const auto duration = total_frames > 0
		? SignedSongTime::FromScale<uint64_t>(total_frames,
						      audio_format.sample_rate)
		: SignedSongTime::Negative();

54 55 56
	GetClient()->Ready(audio_format,
			   GetInputStream().IsSeekable(),
			   duration);
57 58 59 60 61

	initialized = true;
	return true;
}

62 63
inline void
FlacDecoder::OnStreamInfo(const FLAC__StreamMetadata_StreamInfo &stream_info)
64
{
65
	if (initialized)
66
		return;
67

68 69 70 71
	Initialize(stream_info.sample_rate,
		   stream_info.bits_per_sample,
		   stream_info.channels,
		   stream_info.total_samples);
72 73
}

74 75
inline void
FlacDecoder::OnVorbisComment(const FLAC__StreamMetadata_VorbisComment &vc)
76
{
77
	ReplayGainInfo rgi;
78
	if (flac_parse_replay_gain(rgi, vc))
79
		GetClient()->SubmitReplayGain(&rgi);
80

81
	GetClient()->SubmitMixRamp(flac_parse_mixramp(vc));
82 83 84 85 86 87 88 89 90

	tag = flac_vorbis_comments_to_tag(&vc);
}

void
FlacDecoder::OnMetadata(const FLAC__StreamMetadata &metadata)
{
	if (unsupported)
		return;
91

92
	switch (metadata.type) {
93
	case FLAC__METADATA_TYPE_STREAMINFO:
94
		OnStreamInfo(metadata.data.stream_info);
95
		break;
96

97
	case FLAC__METADATA_TYPE_VORBIS_COMMENT:
98
		OnVorbisComment(metadata.data.vorbis_comment);
99
		break;
Max Kellermann's avatar
Max Kellermann committed
100

Avuton Olrich's avatar
Avuton Olrich committed
101 102
	default:
		break;
103 104 105
	}
}

106 107
inline bool
FlacDecoder::OnFirstFrame(const FLAC__FrameHeader &header)
108
{
109
	if (unsupported)
110 111
		return false;

112 113 114 115 116
	return Initialize(header.sample_rate,
			  header.bits_per_sample,
			  header.channels,
			  /* unknown duration */
			  0);
117 118
}

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
FLAC__uint64
FlacDecoder::GetDeltaPosition(const FLAC__StreamDecoder &sd)
{
	FLAC__uint64 nbytes;
	if (!FLAC__stream_decoder_get_decode_position(&sd, &nbytes))
		return 0;

	if (position > 0 && nbytes > position) {
		nbytes -= position;
		position += nbytes;
	} else {
		position = nbytes;
		nbytes = 0;
	}

	return nbytes;
}

137
FLAC__StreamDecoderWriteStatus
138 139 140
FlacDecoder::OnWrite(const FLAC__Frame &frame,
		     const FLAC__int32 *const buf[],
		     FLAC__uint64 nbytes)
141
{
142
	if (!initialized && !OnFirstFrame(frame.header))
143 144
		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;

145
	chunk = pcm_import.Import(buf, frame.header.blocksize);
146

147
	kbit_rate = nbytes * 8 * frame.header.sample_rate /
148
		(1000 * frame.header.blocksize);
149

150 151
	return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
}