AudioParser.cxx 4.79 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2011 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 "gcc.h"
30

31
#include <assert.h>
32
#include <string.h>
33 34 35 36 37 38 39 40 41 42 43
#include <stdlib.h>

/**
 * The GLib quark used for errors reported by this library.
 */
static inline GQuark
audio_parser_quark(void)
{
	return g_quark_from_static_string("audio_parser");
}

44
static bool
45
parse_sample_rate(const char *src, bool mask, uint32_t *sample_rate_r,
46
		  const char **endptr_r, GError **error_r)
47 48
{
	unsigned long value;
49
	char *endptr;
50

51 52 53 54 55 56
	if (mask && *src == '*') {
		*sample_rate_r = 0;
		*endptr_r = src + 1;
		return true;
	}

57 58
	value = strtoul(src, &endptr, 10);
	if (endptr == src) {
59
		g_set_error(error_r, audio_parser_quark(), 0,
60
			    "Failed to parse the sample rate");
61
		return false;
62
	} else if (!audio_check_sample_rate(value, error_r))
63 64
		return false;

65 66 67 68
	*sample_rate_r = value;
	*endptr_r = endptr;
	return true;
}
69

70
static bool
71
parse_sample_format(const char *src, bool mask,
72
		    SampleFormat *sample_format_r,
73 74 75 76
		    const char **endptr_r, GError **error_r)
{
	unsigned long value;
	char *endptr;
77
	SampleFormat sample_format;
78

79
	if (mask && *src == '*') {
80
		*sample_format_r = SampleFormat::UNDEFINED;
81 82 83 84
		*endptr_r = src + 1;
		return true;
	}

85
	if (*src == 'f') {
86
		*sample_format_r = SampleFormat::FLOAT;
87 88 89 90
		*endptr_r = src + 1;
		return true;
	}

91
	if (memcmp(src, "dsd", 3) == 0) {
92
		*sample_format_r = SampleFormat::DSD;
93
		*endptr_r = src + 3;
94 95 96
		return true;
	}

97 98
	value = strtoul(src, &endptr, 10);
	if (endptr == src) {
99
		g_set_error(error_r, audio_parser_quark(), 0,
100
			    "Failed to parse the sample format");
101
		return false;
102 103 104 105
	}

	switch (value) {
	case 8:
106
		sample_format = SampleFormat::S8;
107 108 109
		break;

	case 16:
110
		sample_format = SampleFormat::S16;
111 112 113
		break;

	case 24:
114 115
		if (memcmp(endptr, "_3", 2) == 0)
			/* for backwards compatibility */
116
			endptr += 2;
117

118
		sample_format = SampleFormat::S24_P32;
119 120 121
		break;

	case 32:
122
		sample_format = SampleFormat::S32;
123 124 125 126 127
		break;

	default:
		g_set_error(error_r, audio_parser_quark(), 0,
			    "Invalid sample format: %lu", value);
128
		return false;
129 130 131
	}

	assert(audio_valid_sample_format(sample_format));
132

133
	*sample_format_r = sample_format;
134 135 136
	*endptr_r = endptr;
	return true;
}
137

138
static bool
139
parse_channel_count(const char *src, bool mask, uint8_t *channels_r,
140 141 142 143
		    const char **endptr_r, GError **error_r)
{
	unsigned long value;
	char *endptr;
144

145 146 147 148 149 150
	if (mask && *src == '*') {
		*channels_r = 0;
		*endptr_r = src + 1;
		return true;
	}

151
	value = strtoul(src, &endptr, 10);
152 153 154 155
	if (endptr == src) {
		g_set_error(error_r, audio_parser_quark(), 0,
			    "Failed to parse the channel count");
		return false;
156
	} else if (!audio_check_channel_count(value, error_r))
157 158 159 160 161 162 163 164
		return false;

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

bool
165
audio_format_parse(AudioFormat &dest, const char *src,
166
		   bool mask, GError **error_r)
167 168
{
	uint32_t rate;
169
	SampleFormat sample_format;
170
	uint8_t channels;
171

172
	dest.Clear();
173 174 175

	/* parse sample rate */

176 177 178 179 180
#if GCC_CHECK_VERSION(4,7)
	/* workaround -Wmaybe-uninitialized false positive */
	rate = 0;
#endif

181
	if (!parse_sample_rate(src, mask, &rate, &src, error_r))
182 183 184
		return false;

	if (*src++ != ':') {
185
		g_set_error(error_r, audio_parser_quark(), 0,
186
			    "Sample format missing");
187 188 189
		return false;
	}

190 191
	/* parse sample format */

192 193
#if GCC_CHECK_VERSION(4,7)
	/* workaround -Wmaybe-uninitialized false positive */
194
	sample_format = SampleFormat::UNDEFINED;
195 196
#endif

197
	if (!parse_sample_format(src, mask, &sample_format, &src, error_r))
198 199 200 201 202 203 204 205 206 207
		return false;

	if (*src++ != ':') {
		g_set_error(error_r, audio_parser_quark(), 0,
			    "Channel count missing");
		return false;
	}

	/* parse channel count */

208
	if (!parse_channel_count(src, mask, &channels, &src, error_r))
209 210 211 212 213 214 215
		return false;

	if (*src != 0) {
		g_set_error(error_r, audio_parser_quark(), 0,
			    "Extra data after channel count: %s", src);
		return false;
	}
216

217 218 219 220
	dest = AudioFormat(rate, sample_format, channels);
	assert(mask
	       ? dest.IsMaskValid()
	       : dest.IsValid());
221 222 223

	return true;
}