FlacCommon.cxx 3.6 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 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 82 83
	if (auto mix_ramp = flac_parse_mixramp(vc);
	    mix_ramp.IsDefined())
		GetClient()->SubmitMixRamp(std::move(mix_ramp));
84 85 86 87 88 89 90 91 92

	tag = flac_vorbis_comments_to_tag(&vc);
}

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

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

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

Avuton Olrich's avatar
Avuton Olrich committed
103 104
	default:
		break;
105 106 107
	}
}

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

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

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
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;
}

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

147
	chunk = pcm_import.Import(buf, frame.header.blocksize);
148

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

152 153
	return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
}