FlacCommon.cxx 4.79 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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 27
#include "FlacCommon.hxx"
#include "FlacMetadata.hxx"
#include "FlacPcm.hxx"
28
#include "CheckAudioFormat.hxx"
29
#include "util/Error.hxx"
30
#include "Log.hxx"
31

32
flac_data::flac_data(Decoder &_decoder,
33
		     InputStream &_input_stream)
34
	:FlacInput(_input_stream, &_decoder),
35
	 initialized(false), unsupported(false),
36
	 position(0),
Max Kellermann's avatar
Max Kellermann committed
37
	 decoder(_decoder), input_stream(_input_stream)
38 39 40
{
}

41
static SampleFormat
42
flac_sample_format(unsigned bits_per_sample)
43
{
44
	switch (bits_per_sample) {
45
	case 8:
46
		return SampleFormat::S8;
47 48

	case 16:
49
		return SampleFormat::S16;
50 51

	case 24:
52
		return SampleFormat::S24_P32;
53 54

	case 32:
55
		return SampleFormat::S32;
56 57

	default:
58
		return SampleFormat::UNDEFINED;
59 60 61
	}
}

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
bool
flac_data::Initialize(unsigned sample_rate, unsigned bits_per_sample,
		      unsigned channels, FLAC__uint64 total_frames)
{
	assert(!initialized);
	assert(!unsupported);

	::Error error;
	if (!audio_format_init_checked(audio_format,
				       sample_rate,
				       flac_sample_format(bits_per_sample),
				       channels, error)) {
		LogError(error);
		unsupported = true;
		return false;
	}

	frame_size = audio_format.GetFrameSize();

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

	decoder_initialized(decoder, audio_format,
			    input_stream.IsSeekable(),
			    duration);

	initialized = true;
	return true;
}

94 95 96 97
static void
flac_got_stream_info(struct flac_data *data,
		     const FLAC__StreamMetadata_StreamInfo *stream_info)
{
98
	if (data->initialized || data->unsupported)
99
		return;
100

101 102 103 104
	data->Initialize(stream_info->sample_rate,
			 stream_info->bits_per_sample,
			 stream_info->channels,
			 stream_info->total_samples);
105 106
}

Avuton Olrich's avatar
Avuton Olrich committed
107
void flac_metadata_common_cb(const FLAC__StreamMetadata * block,
Max Kellermann's avatar
Max Kellermann committed
108
			     struct flac_data *data)
109
{
110 111 112
	if (data->unsupported)
		return;

113
	ReplayGainInfo rgi;
114

Avuton Olrich's avatar
Avuton Olrich committed
115
	switch (block->type) {
116
	case FLAC__METADATA_TYPE_STREAMINFO:
117
		flac_got_stream_info(data, &block->data.stream_info);
118
		break;
119

120
	case FLAC__METADATA_TYPE_VORBIS_COMMENT:
121
		if (flac_parse_replay_gain(rgi, block->data.vorbis_comment))
122
			decoder_replay_gain(data->decoder, &rgi);
123

124 125
		decoder_mixramp(data->decoder,
				flac_parse_mixramp(block->data.vorbis_comment));
Max Kellermann's avatar
Max Kellermann committed
126

127 128
		data->tag = flac_vorbis_comments_to_tag(&block->data.vorbis_comment);
		break;
Max Kellermann's avatar
Max Kellermann committed
129

Avuton Olrich's avatar
Avuton Olrich committed
130 131
	default:
		break;
132 133 134
	}
}

135 136 137 138 139 140 141 142 143 144 145 146 147
/**
 * This function attempts to call decoder_initialized() in case there
 * was no STREAMINFO block.  This is allowed for nonseekable streams,
 * where the server sends us only a part of the file, without
 * providing the STREAMINFO block from the beginning of the file
 * (e.g. when seeking with SqueezeBox Server).
 */
static bool
flac_got_first_frame(struct flac_data *data, const FLAC__FrameHeader *header)
{
	if (data->unsupported)
		return false;

148 149 150 151 152
	return data->Initialize(header->sample_rate,
				header->bits_per_sample,
				header->channels,
				/* unknown duration */
				0);
153 154
}

155
FLAC__StreamDecoderWriteStatus
Max Kellermann's avatar
Max Kellermann committed
156
flac_common_write(struct flac_data *data, const FLAC__Frame * frame,
157 158
		  const FLAC__int32 *const buf[],
		  FLAC__uint64 nbytes)
159
{
160 161
	void *buffer;

162 163 164 165
	if (!data->initialized && !flac_got_first_frame(data, &frame->header))
		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;

	size_t buffer_size = frame->header.blocksize * data->frame_size;
166
	buffer = data->buffer.Get(buffer_size);
167

168
	flac_convert(buffer, frame->header.channels,
169
		     data->audio_format.format, buf,
170 171
		     0, frame->header.blocksize);

172 173
	unsigned bit_rate = nbytes * 8 * frame->header.sample_rate /
		(1000 * frame->header.blocksize);
174

175 176 177
	auto cmd = decoder_data(data->decoder, data->input_stream,
				buffer, buffer_size,
				bit_rate);
178
	switch (cmd) {
179 180
	case DecoderCommand::NONE:
	case DecoderCommand::START:
181 182
		break;

183
	case DecoderCommand::STOP:
184
		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
185

186
	case DecoderCommand::SEEK:
187
		return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
188 189 190 191
	}

	return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
}