AudioParser.cxx 4.51 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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 "CheckAudioFormat.hxx"
29
#include "util/Error.hxx"
30
#include "Compiler.h"
31

32
#include <assert.h>
33
#include <string.h>
34 35
#include <stdlib.h>

36
static bool
37
parse_sample_rate(const char *src, bool mask, uint32_t *sample_rate_r,
38
		  const char **endptr_r, Error &error)
39 40
{
	unsigned long value;
41
	char *endptr;
42

43 44 45 46 47 48
	if (mask && *src == '*') {
		*sample_rate_r = 0;
		*endptr_r = src + 1;
		return true;
	}

49 50
	value = strtoul(src, &endptr, 10);
	if (endptr == src) {
51 52
		error.Set(audio_format_domain,
			  "Failed to parse the sample rate");
53
		return false;
54
	} else if (!audio_check_sample_rate(value, error))
55 56
		return false;

57 58 59 60
	*sample_rate_r = value;
	*endptr_r = endptr;
	return true;
}
61

62
static bool
63
parse_sample_format(const char *src, bool mask,
64
		    SampleFormat *sample_format_r,
65
		    const char **endptr_r, Error &error)
66 67 68
{
	unsigned long value;
	char *endptr;
69
	SampleFormat sample_format;
70

71
	if (mask && *src == '*') {
72
		*sample_format_r = SampleFormat::UNDEFINED;
73 74 75 76
		*endptr_r = src + 1;
		return true;
	}

77
	if (*src == 'f') {
78
		*sample_format_r = SampleFormat::FLOAT;
79 80 81 82
		*endptr_r = src + 1;
		return true;
	}

83
	if (memcmp(src, "dsd", 3) == 0) {
84
		*sample_format_r = SampleFormat::DSD;
85
		*endptr_r = src + 3;
86 87 88
		return true;
	}

89 90
	value = strtoul(src, &endptr, 10);
	if (endptr == src) {
91 92
		error.Set(audio_format_domain,
			  "Failed to parse the sample format");
93
		return false;
94 95 96 97
	}

	switch (value) {
	case 8:
98
		sample_format = SampleFormat::S8;
99 100 101
		break;

	case 16:
102
		sample_format = SampleFormat::S16;
103 104 105
		break;

	case 24:
106 107
		if (memcmp(endptr, "_3", 2) == 0)
			/* for backwards compatibility */
108
			endptr += 2;
109

110
		sample_format = SampleFormat::S24_P32;
111 112 113
		break;

	case 32:
114
		sample_format = SampleFormat::S32;
115 116 117
		break;

	default:
118 119
		error.Format(audio_format_domain,
			     "Invalid sample format: %lu", value);
120
		return false;
121 122 123
	}

	assert(audio_valid_sample_format(sample_format));
124

125
	*sample_format_r = sample_format;
126 127 128
	*endptr_r = endptr;
	return true;
}
129

130
static bool
131
parse_channel_count(const char *src, bool mask, uint8_t *channels_r,
132
		    const char **endptr_r, Error &error)
133 134 135
{
	unsigned long value;
	char *endptr;
136

137 138 139 140 141 142
	if (mask && *src == '*') {
		*channels_r = 0;
		*endptr_r = src + 1;
		return true;
	}

143
	value = strtoul(src, &endptr, 10);
144
	if (endptr == src) {
145 146
		error.Set(audio_format_domain,
			  "Failed to parse the channel count");
147
		return false;
148
	} else if (!audio_check_channel_count(value, error))
149 150 151 152 153 154 155 156
		return false;

	*channels_r = value;
	*endptr_r = endptr;
	return true;
}

bool
157
audio_format_parse(AudioFormat &dest, const char *src,
158
		   bool mask, Error &error)
159 160
{
	uint32_t rate;
161
	SampleFormat sample_format;
162
	uint8_t channels;
163

164
	dest.Clear();
165 166 167

	/* parse sample rate */

168 169 170 171 172
#if GCC_CHECK_VERSION(4,7)
	/* workaround -Wmaybe-uninitialized false positive */
	rate = 0;
#endif

173
	if (!parse_sample_rate(src, mask, &rate, &src, error))
174 175 176
		return false;

	if (*src++ != ':') {
177
		error.Set(audio_format_domain, "Sample format missing");
178 179 180
		return false;
	}

181 182
	/* parse sample format */

183 184
#if GCC_CHECK_VERSION(4,7)
	/* workaround -Wmaybe-uninitialized false positive */
185
	sample_format = SampleFormat::UNDEFINED;
186 187
#endif

188
	if (!parse_sample_format(src, mask, &sample_format, &src, error))
189 190 191
		return false;

	if (*src++ != ':') {
192
		error.Set(audio_format_domain, "Channel count missing");
193 194 195 196 197
		return false;
	}

	/* parse channel count */

198
	if (!parse_channel_count(src, mask, &channels, &src, error))
199 200 201
		return false;

	if (*src != 0) {
202
		error.Format(audio_format_domain,
203 204 205
			    "Extra data after channel count: %s", src);
		return false;
	}
206

207 208 209 210
	dest = AudioFormat(rate, sample_format, channels);
	assert(mask
	       ? dest.IsMaskValid()
	       : dest.IsValid());
211 212 213

	return true;
}