diff options
Diffstat (limited to '')
-rw-r--r-- | src/audio_format.h | 37 | ||||
-rw-r--r-- | src/audio_parser.c | 6 |
2 files changed, 37 insertions, 6 deletions
diff --git a/src/audio_format.h b/src/audio_format.h index 739fe6c1f..a7ff30830 100644 --- a/src/audio_format.h +++ b/src/audio_format.h @@ -41,14 +41,45 @@ static inline bool audio_format_defined(const struct audio_format *af) } /** + * Checks whether the sample rate is valid. + * + * @param sample_rate the sample rate in Hz + */ +static inline bool +audio_valid_sample_rate(unsigned sample_rate) +{ + return sample_rate > 0 && sample_rate < (1 << 30); +} + +/** + * Checks whether the sample format is valid. + * + * @param bits the number of significant bits per sample + */ +static inline bool +audio_valid_sample_format(unsigned bits) +{ + return bits == 16 || bits == 24 || bits == 8; +} + +/** + * Checks whether the number of channels is valid. + */ +static inline bool +audio_valid_channel_count(unsigned channels) +{ + return channels == 1 || channels == 2; +} + +/** * Returns false if the format is not valid for playback with MPD. * This function performs some basic validity checks. */ static inline bool audio_format_valid(const struct audio_format *af) { - return af->sample_rate > 0 && - (af->bits == 8 || af->bits == 16 || af->bits == 24) && - af->channels >= 1; + return audio_valid_sample_rate(af->sample_rate) && + audio_valid_sample_format(af->bits) && + audio_valid_channel_count(af->channels); } static inline bool audio_format_equals(const struct audio_format *a, diff --git a/src/audio_parser.c b/src/audio_parser.c index 807a279b3..ee43fd24e 100644 --- a/src/audio_parser.c +++ b/src/audio_parser.c @@ -54,7 +54,7 @@ audio_format_parse(struct audio_format *dest, const char *src, GError **error) g_set_error(error, audio_parser_quark(), 0, "Sample format missing"); return false; - } else if (value <= 0 || value > G_MAXINT32) { + } else if (!audio_valid_sample_rate(value)) { g_set_error(error, audio_parser_quark(), 0, "Invalid sample rate: %lu", value); return false; @@ -74,7 +74,7 @@ audio_format_parse(struct audio_format *dest, const char *src, GError **error) g_set_error(error, audio_parser_quark(), 0, "Channel count missing"); return false; - } else if (value != 16 && value != 24 && value != 8) { + } else if (!audio_valid_sample_format(value)) { g_set_error(error, audio_parser_quark(), 0, "Invalid sample format: %lu", value); return false; @@ -86,7 +86,7 @@ audio_format_parse(struct audio_format *dest, const char *src, GError **error) src = endptr + 1; value = strtoul(src, &endptr, 10); - if (*endptr != 0 || (value != 1 && value != 2)) { + if (*endptr != 0 || !audio_valid_channel_count(value)) { g_set_error(error, audio_parser_quark(), 0, "Invalid channel count: %s", src); return false; |