2009-02-11 18:00:41 +01:00
|
|
|
/*
|
2011-01-29 10:13:54 +01:00
|
|
|
* Copyright (C) 2003-2011 The Music Player Daemon Project
|
2009-02-11 18:00:41 +01:00
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2009-02-11 18:00:41 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parser functions for audio related objects.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-01-30 21:47:12 +01:00
|
|
|
#include "AudioParser.hxx"
|
2009-02-11 18:00:41 +01:00
|
|
|
#include "audio_format.h"
|
2009-11-10 19:00:41 +01:00
|
|
|
#include "audio_check.h"
|
2012-06-12 19:21:53 +02:00
|
|
|
#include "gcc.h"
|
2009-02-11 18:00:41 +01:00
|
|
|
|
2009-11-10 17:11:34 +01:00
|
|
|
#include <assert.h>
|
2010-01-15 09:23:36 +01:00
|
|
|
#include <string.h>
|
2009-02-11 18:00:41 +01:00
|
|
|
#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");
|
|
|
|
}
|
|
|
|
|
2009-10-21 23:01:03 +02:00
|
|
|
static bool
|
2009-10-21 23:01:04 +02:00
|
|
|
parse_sample_rate(const char *src, bool mask, uint32_t *sample_rate_r,
|
2009-10-21 23:01:03 +02:00
|
|
|
const char **endptr_r, GError **error_r)
|
2009-02-11 18:00:41 +01:00
|
|
|
{
|
|
|
|
unsigned long value;
|
2009-10-21 23:01:03 +02:00
|
|
|
char *endptr;
|
2009-02-11 18:00:41 +01:00
|
|
|
|
2009-10-21 23:01:04 +02:00
|
|
|
if (mask && *src == '*') {
|
|
|
|
*sample_rate_r = 0;
|
|
|
|
*endptr_r = src + 1;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-02-11 18:00:41 +01:00
|
|
|
value = strtoul(src, &endptr, 10);
|
|
|
|
if (endptr == src) {
|
2009-10-21 22:22:23 +02:00
|
|
|
g_set_error(error_r, audio_parser_quark(), 0,
|
2009-10-21 23:01:03 +02:00
|
|
|
"Failed to parse the sample rate");
|
2009-02-11 18:00:41 +01:00
|
|
|
return false;
|
2009-11-10 19:00:41 +01:00
|
|
|
} else if (!audio_check_sample_rate(value, error_r))
|
2009-02-11 18:00:41 +01:00
|
|
|
return false;
|
|
|
|
|
2009-10-21 23:01:03 +02:00
|
|
|
*sample_rate_r = value;
|
|
|
|
*endptr_r = endptr;
|
|
|
|
return true;
|
|
|
|
}
|
2009-02-11 18:00:41 +01:00
|
|
|
|
2009-10-21 23:01:03 +02:00
|
|
|
static bool
|
2009-11-10 17:11:34 +01:00
|
|
|
parse_sample_format(const char *src, bool mask,
|
|
|
|
enum sample_format *sample_format_r,
|
2009-10-21 23:01:03 +02:00
|
|
|
const char **endptr_r, GError **error_r)
|
|
|
|
{
|
|
|
|
unsigned long value;
|
|
|
|
char *endptr;
|
2009-11-10 17:11:34 +01:00
|
|
|
enum sample_format sample_format;
|
2009-02-11 18:00:41 +01:00
|
|
|
|
2009-10-21 23:01:04 +02:00
|
|
|
if (mask && *src == '*') {
|
2009-11-10 17:11:34 +01:00
|
|
|
*sample_format_r = SAMPLE_FORMAT_UNDEFINED;
|
2009-10-21 23:01:04 +02:00
|
|
|
*endptr_r = src + 1;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-10-08 10:25:06 +02:00
|
|
|
if (*src == 'f') {
|
|
|
|
*sample_format_r = SAMPLE_FORMAT_FLOAT;
|
|
|
|
*endptr_r = src + 1;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-01 00:25:08 +01:00
|
|
|
if (memcmp(src, "dsd", 3) == 0) {
|
2012-03-21 09:06:48 +01:00
|
|
|
*sample_format_r = SAMPLE_FORMAT_DSD;
|
|
|
|
*endptr_r = src + 3;
|
2012-03-01 00:25:08 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-02-11 18:00:41 +01:00
|
|
|
value = strtoul(src, &endptr, 10);
|
|
|
|
if (endptr == src) {
|
2009-10-21 22:22:23 +02:00
|
|
|
g_set_error(error_r, audio_parser_quark(), 0,
|
2009-10-21 23:01:03 +02:00
|
|
|
"Failed to parse the sample format");
|
2009-02-11 18:00:41 +01:00
|
|
|
return false;
|
2009-11-10 17:11:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (value) {
|
|
|
|
case 8:
|
|
|
|
sample_format = SAMPLE_FORMAT_S8;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 16:
|
|
|
|
sample_format = SAMPLE_FORMAT_S16;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 24:
|
2012-03-21 19:37:33 +01:00
|
|
|
if (memcmp(endptr, "_3", 2) == 0)
|
|
|
|
/* for backwards compatibility */
|
2010-01-15 09:23:36 +01:00
|
|
|
endptr += 2;
|
2012-03-21 19:37:33 +01:00
|
|
|
|
|
|
|
sample_format = SAMPLE_FORMAT_S24_P32;
|
2009-11-10 17:11:34 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 32:
|
|
|
|
sample_format = SAMPLE_FORMAT_S32;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
g_set_error(error_r, audio_parser_quark(), 0,
|
|
|
|
"Invalid sample format: %lu", value);
|
2009-02-11 18:00:41 +01:00
|
|
|
return false;
|
2009-11-10 17:11:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(audio_valid_sample_format(sample_format));
|
2009-02-11 18:00:41 +01:00
|
|
|
|
2009-11-10 17:11:34 +01:00
|
|
|
*sample_format_r = sample_format;
|
2009-10-21 23:01:03 +02:00
|
|
|
*endptr_r = endptr;
|
|
|
|
return true;
|
|
|
|
}
|
2009-02-11 18:00:41 +01:00
|
|
|
|
2009-10-21 23:01:03 +02:00
|
|
|
static bool
|
2009-10-21 23:01:04 +02:00
|
|
|
parse_channel_count(const char *src, bool mask, uint8_t *channels_r,
|
2009-10-21 23:01:03 +02:00
|
|
|
const char **endptr_r, GError **error_r)
|
|
|
|
{
|
|
|
|
unsigned long value;
|
|
|
|
char *endptr;
|
2009-02-11 18:00:41 +01:00
|
|
|
|
2009-10-21 23:01:04 +02:00
|
|
|
if (mask && *src == '*') {
|
|
|
|
*channels_r = 0;
|
|
|
|
*endptr_r = src + 1;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-02-11 18:00:41 +01:00
|
|
|
value = strtoul(src, &endptr, 10);
|
2009-10-21 23:01:03 +02:00
|
|
|
if (endptr == src) {
|
|
|
|
g_set_error(error_r, audio_parser_quark(), 0,
|
|
|
|
"Failed to parse the channel count");
|
|
|
|
return false;
|
2009-11-10 19:00:41 +01:00
|
|
|
} else if (!audio_check_channel_count(value, error_r))
|
2009-10-21 23:01:03 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
*channels_r = value;
|
|
|
|
*endptr_r = endptr;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
audio_format_parse(struct audio_format *dest, const char *src,
|
2009-10-21 23:01:04 +02:00
|
|
|
bool mask, GError **error_r)
|
2009-10-21 23:01:03 +02:00
|
|
|
{
|
|
|
|
uint32_t rate;
|
2009-11-10 17:11:34 +01:00
|
|
|
enum sample_format sample_format;
|
|
|
|
uint8_t channels;
|
2009-10-21 23:01:03 +02:00
|
|
|
|
|
|
|
audio_format_clear(dest);
|
|
|
|
|
|
|
|
/* parse sample rate */
|
|
|
|
|
2012-06-12 19:21:53 +02:00
|
|
|
#if GCC_CHECK_VERSION(4,7)
|
|
|
|
/* workaround -Wmaybe-uninitialized false positive */
|
|
|
|
rate = 0;
|
|
|
|
#endif
|
|
|
|
|
2009-10-21 23:01:04 +02:00
|
|
|
if (!parse_sample_rate(src, mask, &rate, &src, error_r))
|
2009-10-21 23:01:03 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (*src++ != ':') {
|
2009-10-21 22:22:23 +02:00
|
|
|
g_set_error(error_r, audio_parser_quark(), 0,
|
2009-10-21 23:01:03 +02:00
|
|
|
"Sample format missing");
|
2009-02-11 18:00:41 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-10-21 23:01:03 +02:00
|
|
|
/* parse sample format */
|
|
|
|
|
2012-06-12 19:21:53 +02:00
|
|
|
#if GCC_CHECK_VERSION(4,7)
|
|
|
|
/* workaround -Wmaybe-uninitialized false positive */
|
|
|
|
sample_format = SAMPLE_FORMAT_UNDEFINED;
|
|
|
|
#endif
|
|
|
|
|
2009-11-10 17:11:34 +01:00
|
|
|
if (!parse_sample_format(src, mask, &sample_format, &src, error_r))
|
2009-10-21 23:01:03 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (*src++ != ':') {
|
|
|
|
g_set_error(error_r, audio_parser_quark(), 0,
|
|
|
|
"Channel count missing");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* parse channel count */
|
|
|
|
|
2009-10-21 23:01:04 +02:00
|
|
|
if (!parse_channel_count(src, mask, &channels, &src, error_r))
|
2009-10-21 23:01:03 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (*src != 0) {
|
|
|
|
g_set_error(error_r, audio_parser_quark(), 0,
|
|
|
|
"Extra data after channel count: %s", src);
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-19 17:24:43 +02:00
|
|
|
|
2009-11-10 17:11:34 +01:00
|
|
|
audio_format_init(dest, rate, sample_format, channels);
|
2011-03-23 22:22:51 +01:00
|
|
|
assert(mask ? audio_format_mask_valid(dest)
|
|
|
|
: audio_format_valid(dest));
|
2009-02-11 18:00:41 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|