FlacCommon.cxx 4.94 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
	 total_frames(0), first_frame(0), next_frame(0), 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
static void
flac_got_stream_info(struct flac_data *data,
		     const FLAC__StreamMetadata_StreamInfo *stream_info)
{
66
	if (data->initialized || data->unsupported)
67
		return;
68

69
	Error error;
70
	if (!audio_format_init_checked(data->audio_format,
71
				       stream_info->sample_rate,
72
				       flac_sample_format(stream_info->bits_per_sample),
73
				       stream_info->channels, error)) {
74
		LogError(error);
75 76
		data->unsupported = true;
		return;
77 78
	}

79
	data->frame_size = data->audio_format.GetFrameSize();
80

81 82 83 84
	if (data->total_frames == 0)
		data->total_frames = stream_info->total_samples;

	data->initialized = true;
85 86
}

Avuton Olrich's avatar
Avuton Olrich committed
87
void flac_metadata_common_cb(const FLAC__StreamMetadata * block,
Max Kellermann's avatar
Max Kellermann committed
88
			     struct flac_data *data)
89
{
90 91 92
	if (data->unsupported)
		return;

93
	ReplayGainInfo rgi;
94

Avuton Olrich's avatar
Avuton Olrich committed
95
	switch (block->type) {
96
	case FLAC__METADATA_TYPE_STREAMINFO:
97
		flac_got_stream_info(data, &block->data.stream_info);
98
		break;
99

100
	case FLAC__METADATA_TYPE_VORBIS_COMMENT:
101
		if (flac_parse_replay_gain(rgi, block->data.vorbis_comment))
102
			decoder_replay_gain(data->decoder, &rgi);
103

104 105
		decoder_mixramp(data->decoder,
				flac_parse_mixramp(block->data.vorbis_comment));
Max Kellermann's avatar
Max Kellermann committed
106

107 108
		data->tag = flac_vorbis_comments_to_tag(&block->data.vorbis_comment);
		break;
Max Kellermann's avatar
Max Kellermann committed
109

Avuton Olrich's avatar
Avuton Olrich committed
110 111
	default:
		break;
112 113 114
	}
}

115 116 117 118 119 120 121 122 123 124 125 126 127
/**
 * 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;

128
	Error error;
129
	if (!audio_format_init_checked(data->audio_format,
130 131
				       header->sample_rate,
				       flac_sample_format(header->bits_per_sample),
132
				       header->channels, error)) {
133
		LogError(error);
134 135 136 137
		data->unsupported = true;
		return false;
	}

138
	data->frame_size = data->audio_format.GetFrameSize();
139

140 141 142
	const auto duration = SongTime::FromScale<uint64_t>(data->total_frames,
							    data->audio_format.sample_rate);

143
	decoder_initialized(data->decoder, data->audio_format,
144
			    data->input_stream.IsSeekable(),
145
			    duration);
146 147 148 149 150 151

	data->initialized = true;

	return true;
}

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

160 161 162 163
	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;
164
	buffer = data->buffer.Get(buffer_size);
165

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

170 171 172 173 174 175
	if (nbytes > 0)
		bit_rate = nbytes * 8 * frame->header.sample_rate /
			(1000 * frame->header.blocksize);
	else
		bit_rate = 0;

176 177 178
	auto cmd = decoder_data(data->decoder, data->input_stream,
				buffer, buffer_size,
				bit_rate);
179
	data->next_frame += frame->header.blocksize;
180
	switch (cmd) {
181 182
	case DecoderCommand::NONE:
	case DecoderCommand::START:
183 184
		break;

185
	case DecoderCommand::STOP:
186
		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
187

188
	case DecoderCommand::SEEK:
189
		return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
190 191 192 193
	}

	return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
}