FLACCommon.cxx 5.33 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2012 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 28 29
#include "FLACCommon.hxx"
#include "FLACMetaData.hxx"
#include "FLAC_PCM.hxx"

extern "C" {
30
#include "audio_check.h"
31
}
32

33 34
#include <glib.h>

35 36
#include <assert.h>

37 38
flac_data::flac_data(struct decoder *_decoder,
		     struct input_stream *_input_stream)
39 40
	:FLACInput(_input_stream, _decoder),
	 initialized(false), unsupported(false),
41 42 43
	 total_frames(0), first_frame(0), next_frame(0), position(0),
	 decoder(_decoder), input_stream(_input_stream),
	 tag(nullptr)
44
{
45
	pcm_buffer_init(&buffer);
46 47
}

48
flac_data::~flac_data()
49
{
50
	pcm_buffer_deinit(&buffer);
51

52 53
	if (tag != nullptr)
		tag_free(tag);
54 55
}

56
static enum sample_format
57
flac_sample_format(unsigned bits_per_sample)
58
{
59
	switch (bits_per_sample) {
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
	case 8:
		return SAMPLE_FORMAT_S8;

	case 16:
		return SAMPLE_FORMAT_S16;

	case 24:
		return SAMPLE_FORMAT_S24_P32;

	case 32:
		return SAMPLE_FORMAT_S32;

	default:
		return SAMPLE_FORMAT_UNDEFINED;
	}
}

77 78 79 80
static void
flac_got_stream_info(struct flac_data *data,
		     const FLAC__StreamMetadata_StreamInfo *stream_info)
{
81
	if (data->initialized || data->unsupported)
82
		return;
83

84
	GError *error = nullptr;
85 86
	if (!audio_format_init_checked(&data->audio_format,
				       stream_info->sample_rate,
87
				       flac_sample_format(stream_info->bits_per_sample),
88
				       stream_info->channels, &error)) {
89 90
		g_warning("%s", error->message);
		g_error_free(error);
91 92
		data->unsupported = true;
		return;
93 94
	}

95
	data->frame_size = audio_format_frame_size(&data->audio_format);
96

97 98 99 100
	if (data->total_frames == 0)
		data->total_frames = stream_info->total_samples;

	data->initialized = true;
101 102
}

Avuton Olrich's avatar
Avuton Olrich committed
103
void flac_metadata_common_cb(const FLAC__StreamMetadata * block,
Max Kellermann's avatar
Max Kellermann committed
104
			     struct flac_data *data)
105
{
106 107 108
	if (data->unsupported)
		return;

109
	struct replay_gain_info rgi;
110 111
	char *mixramp_start;
	char *mixramp_end;
112

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

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

		if (flac_parse_mixramp(&mixramp_start, &mixramp_end, block))
123
			decoder_mixramp(data->decoder,
124
					mixramp_start, mixramp_end);
Max Kellermann's avatar
Max Kellermann committed
125

126
		if (data->tag != nullptr)
127
			flac_vorbis_comments_to_tag(data->tag,
128
						    &block->data.vorbis_comment);
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
	GError *error = nullptr;
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
	if (!audio_format_init_checked(&data->audio_format,
				       header->sample_rate,
				       flac_sample_format(header->bits_per_sample),
				       header->channels, &error)) {
		g_warning("%s", error->message);
		g_error_free(error);
		data->unsupported = true;
		return false;
	}

	data->frame_size = audio_format_frame_size(&data->audio_format);

	decoder_initialized(data->decoder, &data->audio_format,
			    data->input_stream->seekable,
			    (float)data->total_frames /
			    (float)data->audio_format.sample_rate);

	data->initialized = true;

	return true;
}

171
FLAC__StreamDecoderWriteStatus
Max Kellermann's avatar
Max Kellermann committed
172
flac_common_write(struct flac_data *data, const FLAC__Frame * frame,
173 174
		  const FLAC__int32 *const buf[],
		  FLAC__uint64 nbytes)
175
{
176
	enum decoder_command cmd;
177
	void *buffer;
178
	unsigned bit_rate;
179

180 181 182 183
	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;
184 185
	buffer = pcm_buffer_get(&data->buffer, buffer_size);

186
	flac_convert(buffer, frame->header.channels,
187
		     (enum sample_format)data->audio_format.format, buf,
188 189
		     0, frame->header.blocksize);

190 191 192 193 194 195
	if (nbytes > 0)
		bit_rate = nbytes * 8 * frame->header.sample_rate /
			(1000 * frame->header.blocksize);
	else
		bit_rate = 0;

196 197
	cmd = decoder_data(data->decoder, data->input_stream,
			   buffer, buffer_size,
198
			   bit_rate);
199
	data->next_frame += frame->header.blocksize;
200 201 202 203 204 205 206
	switch (cmd) {
	case DECODE_COMMAND_NONE:
	case DECODE_COMMAND_START:
		break;

	case DECODE_COMMAND_STOP:
		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
207

208 209
	case DECODE_COMMAND_SEEK:
		return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
210 211 212 213
	}

	return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
}