FlacCommon.cxx 3.89 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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 "config.h"
25 26
#include "FlacCommon.hxx"
#include "FlacMetadata.hxx"
27
#include "util/ConstBuffer.hxx"
28
#include "Log.hxx"
29

30 31
#include <stdexcept>

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

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

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

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

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

	initialized = true;
	return true;
}

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

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

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

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

	tag = flac_vorbis_comments_to_tag(&vc);
}

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

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

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

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

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

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

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

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

146
	const auto data = pcm_import.Import(buf, frame.header.blocksize);
147

148 149
	unsigned bit_rate = nbytes * 8 * frame.header.sample_rate /
		(1000 * frame.header.blocksize);
150

151 152 153
	auto cmd = GetClient()->SubmitData(GetInputStream(),
					   data.data, data.size,
					   bit_rate);
154
	switch (cmd) {
155 156
	case DecoderCommand::NONE:
	case DecoderCommand::START:
157 158
		break;

159
	case DecoderCommand::STOP:
160
		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
161

162
	case DecoderCommand::SEEK:
163
		return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
164 165 166 167
	}

	return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
}