1 /*
2  * Copyright 2003-2021 The Music Player Daemon Project
3  * http://www.musicpd.org
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 /*
21  * Parser functions for audio related objects.
22  *
23  */
24 
25 #include "AudioParser.hxx"
26 #include "AudioFormat.hxx"
27 #include "util/RuntimeError.hxx"
28 
29 #include <cassert>
30 
31 #include <string.h>
32 #include <stdlib.h>
33 
34 static uint32_t
ParseSampleRate(const char * src,bool mask,const char ** endptr_r)35 ParseSampleRate(const char *src, bool mask, const char **endptr_r)
36 {
37 	unsigned long value;
38 	char *endptr;
39 
40 	if (mask && *src == '*') {
41 		*endptr_r = src + 1;
42 		return 0;
43 	}
44 
45 	value = strtoul(src, &endptr, 10);
46 	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 static SampleFormat
ParseSampleFormat(const char * src,bool mask,const char ** endptr_r)56 ParseSampleFormat(const char *src, bool mask, const char **endptr_r)
57 {
58 	unsigned long value;
59 	char *endptr;
60 	SampleFormat sample_format;
61 
62 	if (mask && *src == '*') {
63 		*endptr_r = src + 1;
64 		return SampleFormat::UNDEFINED;
65 	}
66 
67 	if (*src == 'f') {
68 		*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 	switch (value) {
82 	case 8:
83 		sample_format = SampleFormat::S8;
84 		break;
85 
86 	case 16:
87 		sample_format = SampleFormat::S16;
88 		break;
89 
90 	case 24:
91 		if (memcmp(endptr, "_3", 2) == 0)
92 			/* for backwards compatibility */
93 			endptr += 2;
94 
95 		sample_format = SampleFormat::S24_P32;
96 		break;
97 
98 	case 32:
99 		sample_format = SampleFormat::S32;
100 		break;
101 
102 	default:
103 		throw FormatInvalidArgument("Invalid sample format: %lu",
104 					    value);
105 	}
106 
107 	assert(audio_valid_sample_format(sample_format));
108 
109 	*endptr_r = endptr;
110 	return sample_format;
111 }
112 
113 static uint8_t
ParseChannelCount(const char * src,bool mask,const char ** endptr_r)114 ParseChannelCount(const char *src, bool mask, const char **endptr_r)
115 {
116 	unsigned long value;
117 	char *endptr;
118 
119 	if (mask && *src == '*') {
120 		*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 		throw FormatInvalidArgument("Invalid channel count: %u",
129 					    value);
130 
131 	*endptr_r = endptr;
132 	return value;
133 }
134 
135 AudioFormat
ParseAudioFormat(const char * src,bool mask)136 ParseAudioFormat(const char *src, bool mask)
137 {
138 	AudioFormat dest;
139 	dest.Clear();
140 
141 	if (strncmp(src, "dsd", 3) == 0) {
142 		/* allow format specifications such as "dsd64" which
143 		   implies the sample rate */
144 
145 		char *endptr;
146 		auto dsd = strtoul(src + 3, &endptr, 10);
147 		if (endptr > src + 3 && *endptr == ':' &&
148 		    dsd >= 32 && dsd <= 4096 && dsd % 2 == 0) {
149 			dest.sample_rate = dsd * 44100 / 8;
150 			dest.format = SampleFormat::DSD;
151 
152 			src = endptr + 1;
153 			dest.channels = ParseChannelCount(src, mask, &src);
154 			if (*src != 0)
155 				throw FormatInvalidArgument("Extra data after channel count: %s",
156 							    src);
157 
158 			return dest;
159 		}
160 	}
161 
162 	/* parse sample rate */
163 
164 	dest.sample_rate = ParseSampleRate(src, mask, &src);
165 
166 	if (*src++ != ':')
167 		throw std::invalid_argument("Sample format missing");
168 
169 	/* parse sample format */
170 
171 	dest.format = ParseSampleFormat(src, mask, &src);
172 
173 	if (*src++ != ':')
174 		throw std::invalid_argument("Channel count missing");
175 
176 	/* parse channel count */
177 
178 	dest.channels = ParseChannelCount(src, mask, &src);
179 
180 	if (*src != 0)
181 		throw FormatInvalidArgument("Extra data after channel count: %s",
182 					    src);
183 
184 	assert(mask
185 	       ? dest.IsMaskValid()
186 	       : dest.IsValid());
187 	return dest;
188 }
189