AudioParser.cxx 4.18 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
		throw std::invalid_argument("Failed to parse the sample rate");
48
	} else if (!audio_valid_sample_rate(value))
49
		throw FormatInvalidArgument("Invalid sample rate: %lu", value);
50

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

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

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

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

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

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

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

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

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

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

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

	default:
103 104
		throw FormatInvalidArgument("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
	if (endptr == src)
126
		throw std::invalid_argument("Failed to parse the channel count");
127
	else if (!audio_valid_channel_count(value))
128 129
		throw FormatInvalidArgument("Invalid channel count: %u",
					    value);
130 131

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

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

141 142 143 144 145 146 147 148 149 150 151 152 153 154
	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)
155 156
				throw FormatInvalidArgument("Extra data after channel count: %s",
							    src);
157 158 159 160 161

			return dest;
		}
	}

162 163
	/* parse sample rate */

164
	dest.sample_rate = ParseSampleRate(src, mask, &src);
165

166
	if (*src++ != ':')
167
		throw std::invalid_argument("Sample format missing");
168

169 170
	/* parse sample format */

171
	dest.format = ParseSampleFormat(src, mask, &src);
172

173
	if (*src++ != ':')
174
		throw std::invalid_argument("Channel count missing");
175 176 177

	/* parse channel count */

178
	dest.channels = ParseChannelCount(src, mask, &src);
179

180
	if (*src != 0)
181 182
		throw FormatInvalidArgument("Extra data after channel count: %s",
					    src);
183

184 185 186
	assert(mask
	       ? dest.IsMaskValid()
	       : dest.IsValid());
187
	return dest;
188
}