AudioParser.cxx 4.12 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13
 * http://www.musicpd.org
 *
 * 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
 *
 * 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.
18 19 20 21 22 23 24
 */

/*
 * Parser functions for audio related objects.
 *
 */

25
#include "config.h"
26
#include "AudioParser.hxx"
27
#include "AudioFormat.hxx"
28
#include "util/RuntimeError.hxx"
29

30
#include <assert.h>
31
#include <string.h>
32 33
#include <stdlib.h>

34 35
static uint32_t
ParseSampleRate(const char *src, bool mask, const char **endptr_r)
36 37
{
	unsigned long value;
38
	char *endptr;
39

40 41
	if (mask && *src == '*') {
		*endptr_r = src + 1;
42
		return 0;
43 44
	}

45 46
	value = strtoul(src, &endptr, 10);
	if (endptr == src) {
47 48 49 50
		throw std::runtime_error("Failed to parse the sample rate");
	} else if (!audio_valid_sample_rate(value))
		throw FormatRuntimeError("Invalid sample rate: %lu",
					 value);
51

52
	*endptr_r = endptr;
53
	return value;
54
}
55

56 57
static SampleFormat
ParseSampleFormat(const char *src, bool mask, const char **endptr_r)
58 59 60
{
	unsigned long value;
	char *endptr;
61
	SampleFormat sample_format;
62

63 64
	if (mask && *src == '*') {
		*endptr_r = src + 1;
65
		return SampleFormat::UNDEFINED;
66 67
	}

68 69
	if (*src == 'f') {
		*endptr_r = src + 1;
70
		return SampleFormat::FLOAT;
71 72
	}

73
	if (memcmp(src, "dsd", 3) == 0) {
74
		*endptr_r = src + 3;
75
		return SampleFormat::DSD;
76 77
	}

78
	value = strtoul(src, &endptr, 10);
79 80
	if (endptr == src)
		throw std::runtime_error("Failed to parse the sample format");
81 82 83

	switch (value) {
	case 8:
84
		sample_format = SampleFormat::S8;
85 86 87
		break;

	case 16:
88
		sample_format = SampleFormat::S16;
89 90 91
		break;

	case 24:
92 93
		if (memcmp(endptr, "_3", 2) == 0)
			/* for backwards compatibility */
94
			endptr += 2;
95

96
		sample_format = SampleFormat::S24_P32;
97 98 99
		break;

	case 32:
100
		sample_format = SampleFormat::S32;
101 102 103
		break;

	default:
104
		throw FormatRuntimeError("Invalid sample format: %lu", value);
105 106 107
	}

	assert(audio_valid_sample_format(sample_format));
108

109
	*endptr_r = endptr;
110
	return sample_format;
111
}
112

113 114
static uint8_t
ParseChannelCount(const char *src, bool mask, const char **endptr_r)
115 116 117
{
	unsigned long value;
	char *endptr;
118

119 120
	if (mask && *src == '*') {
		*endptr_r = src + 1;
121
		return 0;
122 123
	}

124
	value = strtoul(src, &endptr, 10);
125 126 127 128
	if (endptr == src)
		throw std::runtime_error("Failed to parse the channel count");
	else if (!audio_valid_channel_count(value))
		throw FormatRuntimeError("Invalid channel count: %u", value);
129 130

	*endptr_r = endptr;
131
	return value;
132 133
}

134 135
AudioFormat
ParseAudioFormat(const char *src, bool mask)
136
{
137
	AudioFormat dest;
138
	dest.Clear();
139

140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
	if (strncmp(src, "dsd", 3) == 0) {
		/* allow format specifications such as "dsd64" which
		   implies the sample rate */

		char *endptr;
		auto dsd = strtoul(src + 3, &endptr, 10);
		if (endptr > src + 3 && *endptr == ':' &&
		    dsd >= 32 && dsd <= 4096 && dsd % 2 == 0) {
			dest.sample_rate = dsd * 44100 / 8;
			dest.format = SampleFormat::DSD;

			src = endptr + 1;
			dest.channels = ParseChannelCount(src, mask, &src);
			if (*src != 0)
				throw FormatRuntimeError("Extra data after channel count: %s", src);

			return dest;
		}
	}

160 161
	/* parse sample rate */

162
	dest.sample_rate = ParseSampleRate(src, mask, &src);
163

164 165
	if (*src++ != ':')
		throw std::runtime_error("Sample format missing");
166

167 168
	/* parse sample format */

169
	dest.format = ParseSampleFormat(src, mask, &src);
170

171 172
	if (*src++ != ':')
		throw std::runtime_error("Channel count missing");
173 174 175

	/* parse channel count */

176
	dest.channels = ParseChannelCount(src, mask, &src);
177

178 179
	if (*src != 0)
		throw FormatRuntimeError("Extra data after channel count: %s", src);
180

181 182 183
	assert(mask
	       ? dest.IsMaskValid()
	       : dest.IsValid());
184
	return dest;
185
}