_flac_common.c 8.53 KB
Newer Older
1
/* the Music Player Daemon (MPD)
2
 * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * This project's homepage is: http://www.musicpd.org
 *
 * Common data structures and functions used by FLAC and OggFLAC
 * (c) 2005 by Eric Wong <normalperson@yhbt.net>
 *
 * 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.
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "_flac_common.h"

24 25
#include <glib.h>

26 27 28
#include <FLAC/format.h>
#include <FLAC/metadata.h>

Max Kellermann's avatar
Max Kellermann committed
29
void init_FlacData(FlacData * data, struct decoder * decoder,
30
		   struct input_stream *inStream)
31 32 33 34
{
	data->time = 0;
	data->position = 0;
	data->bitRate = 0;
Max Kellermann's avatar
Max Kellermann committed
35
	data->decoder = decoder;
36 37 38 39 40 41
	data->inStream = inStream;
	data->replayGainInfo = NULL;
	data->tag = NULL;
}

static int flacFindVorbisCommentFloat(const FLAC__StreamMetadata * block,
42
				      const char *cmnt, float *fl)
43
{
Avuton Olrich's avatar
Avuton Olrich committed
44 45
	int offset =
	    FLAC__metadata_object_vorbiscomment_find_entry_from(block, 0, cmnt);
46

Avuton Olrich's avatar
Avuton Olrich committed
47 48
	if (offset >= 0) {
		size_t pos = strlen(cmnt) + 1;	/* 1 is for '=' */
49
		int len = block->data.vorbis_comment.comments[offset].length
Avuton Olrich's avatar
Avuton Olrich committed
50 51
		    - pos;
		if (len > 0) {
52
			unsigned char tmp;
Max Kellermann's avatar
Max Kellermann committed
53 54 55 56
			unsigned char *p = &(block->data.vorbis_comment.
					     comments[offset].entry[pos]);
			tmp = p[len];
			p[len] = '\0';
Max Kellermann's avatar
Max Kellermann committed
57
			*fl = (float)atof((char *)p);
Max Kellermann's avatar
Max Kellermann committed
58
			p[len] = tmp;
Avuton Olrich's avatar
Avuton Olrich committed
59

60 61 62 63 64 65 66 67
			return 1;
		}
	}

	return 0;
}

/* replaygain stuff by AliasMrJones */
Avuton Olrich's avatar
Avuton Olrich committed
68 69 70
static void flacParseReplayGain(const FLAC__StreamMetadata * block,
				FlacData * data)
{
71
	int found = 0;
72 73

	if (data->replayGainInfo)
74
		replay_gain_info_free(data->replayGainInfo);
75

76
	data->replayGainInfo = replay_gain_info_new();
77

78
	found |= flacFindVorbisCommentFloat(block, "replaygain_album_gain",
79
					    &data->replayGainInfo->tuples[REPLAY_GAIN_ALBUM].gain);
80
	found |= flacFindVorbisCommentFloat(block, "replaygain_album_peak",
81
					    &data->replayGainInfo->tuples[REPLAY_GAIN_ALBUM].peak);
82
	found |= flacFindVorbisCommentFloat(block, "replaygain_track_gain",
83
					    &data->replayGainInfo->tuples[REPLAY_GAIN_TRACK].gain);
84
	found |= flacFindVorbisCommentFloat(block, "replaygain_track_peak",
85
					    &data->replayGainInfo->tuples[REPLAY_GAIN_TRACK].peak);
86 87

	if (!found) {
88
		replay_gain_info_free(data->replayGainInfo);
89 90 91 92 93 94
		data->replayGainInfo = NULL;
	}
}

/* tracknumber is used in VCs, MPD uses "track" ..., all the other
 * tag names match */
Avuton Olrich's avatar
Avuton Olrich committed
95 96
static const char *VORBIS_COMMENT_TRACK_KEY = "tracknumber";
static const char *VORBIS_COMMENT_DISC_KEY = "discnumber";
97

Avuton Olrich's avatar
Avuton Olrich committed
98 99 100
static unsigned int commentMatchesAddToTag(const
					   FLAC__StreamMetadata_VorbisComment_Entry
					   * entry, unsigned int itemType,
101
					   struct tag ** tag)
102
{
Avuton Olrich's avatar
Avuton Olrich committed
103
	const char *str;
Eric Wong's avatar
Eric Wong committed
104 105 106
	size_t slen;
	int vlen;

107
	switch (itemType) {
Avuton Olrich's avatar
Avuton Olrich committed
108 109 110 111 112 113 114 115
	case TAG_ITEM_TRACK:
		str = VORBIS_COMMENT_TRACK_KEY;
		break;
	case TAG_ITEM_DISC:
		str = VORBIS_COMMENT_DISC_KEY;
		break;
	default:
		str = mpdTagItemKeys[itemType];
116
	}
Eric Wong's avatar
Eric Wong committed
117 118
	slen = strlen(str);
	vlen = entry->length - slen - 1;
119

Avuton Olrich's avatar
Avuton Olrich committed
120 121
	if ((vlen > 0) && (0 == strncasecmp(str, (char *)entry->entry, slen))
	    && (*(entry->entry + slen) == '=')) {
122
		if (!*tag)
123
			*tag = tag_new();
Avuton Olrich's avatar
Avuton Olrich committed
124

125 126
		tag_add_item_n(*tag, itemType,
			       (char *)(entry->entry + slen + 1), vlen);
Avuton Olrich's avatar
Avuton Olrich committed
127

128 129
		return 1;
	}
Avuton Olrich's avatar
Avuton Olrich committed
130

131 132
	return 0;
}
Avuton Olrich's avatar
Avuton Olrich committed
133

134 135
struct tag *copyVorbisCommentBlockToMpdTag(const FLAC__StreamMetadata * block,
					   struct tag * tag)
136 137 138
{
	unsigned int i, j;
	FLAC__StreamMetadata_VorbisComment_Entry *comments;
Avuton Olrich's avatar
Avuton Olrich committed
139

140
	comments = block->data.vorbis_comment.comments;
Avuton Olrich's avatar
Avuton Olrich committed
141

142
	for (i = block->data.vorbis_comment.num_comments; i != 0; --i) {
Avuton Olrich's avatar
Avuton Olrich committed
143
		for (j = TAG_NUM_OF_ITEM_TYPES; j--;) {
144 145 146 147 148 149 150 151 152
			if (commentMatchesAddToTag(comments, j, &tag))
				break;
		}
		comments++;
	}

	return tag;
}

Avuton Olrich's avatar
Avuton Olrich committed
153 154
void flac_metadata_common_cb(const FLAC__StreamMetadata * block,
			     FlacData * data)
155 156 157
{
	const FLAC__StreamMetadata_StreamInfo *si = &(block->data.stream_info);

Avuton Olrich's avatar
Avuton Olrich committed
158
	switch (block->type) {
159
	case FLAC__METADATA_TYPE_STREAMINFO:
160
		data->audio_format.bits = (int8_t)si->bits_per_sample;
161
		data->audio_format.sample_rate = si->sample_rate;
162
		data->audio_format.channels = (int8_t)si->channels;
163
		data->total_time = ((float)si->total_samples) / (si->sample_rate);
164 165
		break;
	case FLAC__METADATA_TYPE_VORBIS_COMMENT:
Avuton Olrich's avatar
Avuton Olrich committed
166 167 168
		flacParseReplayGain(block, data);
	default:
		break;
169 170 171
	}
}

Avuton Olrich's avatar
Avuton Olrich committed
172 173
void flac_error_common_cb(const char *plugin,
			  const FLAC__StreamDecoderErrorStatus status,
174
			  G_GNUC_UNUSED FlacData * data)
175
{
176
	if (decoder_get_command(data->decoder) == DECODE_COMMAND_STOP)
Avuton Olrich's avatar
Avuton Olrich committed
177
		return;
178

Avuton Olrich's avatar
Avuton Olrich committed
179
	switch (status) {
180
	case FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC:
181
		g_warning("%s lost sync\n", plugin);
182 183
		break;
	case FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER:
184
		g_warning("bad %s header\n", plugin);
185 186
		break;
	case FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH:
187
		g_warning("%s crc mismatch\n", plugin);
188 189
		break;
	default:
190
		g_warning("unknown %s error\n", plugin);
191 192 193
	}
}

194
static void flac_convert_stereo16(int16_t *dest,
195 196 197 198
				  const FLAC__int32 * const buf[],
				  unsigned int position, unsigned int end)
{
	for (; position < end; ++position) {
199 200
		*dest++ = buf[0][position];
		*dest++ = buf[1][position];
201 202 203
	}
}

204 205 206 207 208 209 210 211 212 213 214 215 216
static void
flac_convert_16(int16_t *dest,
		unsigned int num_channels,
		const FLAC__int32 * const buf[],
		unsigned int position, unsigned int end)
{
	unsigned int c_chan;

	for (; position < end; ++position)
		for (c_chan = 0; c_chan < num_channels; c_chan++)
			*dest++ = buf[c_chan][position];
}

217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
/**
 * Note: this function also handles 24 bit files!
 */
static void
flac_convert_32(int32_t *dest,
		unsigned int num_channels,
		const FLAC__int32 * const buf[],
		unsigned int position, unsigned int end)
{
	unsigned int c_chan;

	for (; position < end; ++position)
		for (c_chan = 0; c_chan < num_channels; c_chan++)
			*dest++ = buf[c_chan][position];
}

static void
flac_convert_8(int8_t *dest,
	       unsigned int num_channels,
	       const FLAC__int32 * const buf[],
	       unsigned int position, unsigned int end)
{
	unsigned int c_chan;

	for (; position < end; ++position)
		for (c_chan = 0; c_chan < num_channels; c_chan++)
			*dest++ = buf[c_chan][position];
}

246 247 248 249 250 251
static void flac_convert(unsigned char *dest,
			 unsigned int num_channels,
			 unsigned int bytes_per_sample,
			 const FLAC__int32 * const buf[],
			 unsigned int position, unsigned int end)
{
252 253 254 255 256 257 258 259 260
	switch (bytes_per_sample) {
	case 2:
		if (num_channels == 2)
			flac_convert_stereo16((int16_t*)dest, buf,
					      position, end);
		else
			flac_convert_16((int16_t*)dest, num_channels, buf,
					position, end);
		break;
261

262 263 264 265 266 267 268 269 270
	case 4:
		flac_convert_32((int32_t*)dest, num_channels, buf,
				position, end);
		break;

	case 1:
		flac_convert_8((int8_t*)dest, num_channels, buf,
			       position, end);
		break;
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
	}
}

FLAC__StreamDecoderWriteStatus
flac_common_write(FlacData *data, const FLAC__Frame * frame,
		  const FLAC__int32 *const buf[])
{
	unsigned int c_samp;
	const unsigned int num_channels = frame->header.channels;
	const unsigned int bytes_per_sample =
		audio_format_sample_size(&data->audio_format);
	const unsigned int bytes_per_channel =
		bytes_per_sample * frame->header.channels;
	const unsigned int max_samples = FLAC_CHUNK_SIZE / bytes_per_channel;
	unsigned int num_samples;
286
	enum decoder_command cmd;
287

288 289 290 291
	if (bytes_per_sample != 1 && bytes_per_sample != 2 &&
	    bytes_per_sample != 4)
		/* exotic unsupported bit rate */
		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
292 293 294 295 296 297 298

	for (c_samp = 0; c_samp < frame->header.blocksize;
	     c_samp += num_samples) {
		num_samples = frame->header.blocksize - c_samp;
		if (num_samples > max_samples)
			num_samples = max_samples;

299 300 301 302
		flac_convert(data->chunk,
			     num_channels, bytes_per_sample, buf,
			     c_samp, c_samp + num_samples);

303
		cmd = decoder_data(data->decoder, data->inStream,
304
				   data->chunk,
305 306
				   num_samples * bytes_per_channel,
				   data->time, data->bitRate,
307 308 309 310 311 312 313
				   data->replayGainInfo);
		switch (cmd) {
		case DECODE_COMMAND_NONE:
		case DECODE_COMMAND_START:
			break;

		case DECODE_COMMAND_STOP:
314 315
			return
				FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
316 317

		case DECODE_COMMAND_SEEK:
318 319 320 321 322 323 324
			return
				FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
		}
	}

	return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
}