From 60a617df311fc6e56feced64143d912eb7b7099b Mon Sep 17 00:00:00 2001 From: wm4 Date: Thu, 10 Sep 2015 23:30:51 +0200 Subject: audio/format: add function for determining sample conversion candidates --- audio/format.c | 31 +++++++++++++++++++++++++++++++ audio/format.h | 1 + 2 files changed, 32 insertions(+) (limited to 'audio') diff --git a/audio/format.c b/audio/format.c index ee9d2a1909..e26e5b6f5b 100644 --- a/audio/format.c +++ b/audio/format.c @@ -211,6 +211,37 @@ int af_format_conversion_score(int dst_format, int src_format) return score; } +struct entry { + int fmt; + int score; +}; + +static int cmp_entry(const void *a, const void *b) +{ +#define CMP_INT(a, b) (a > b ? 1 : (a < b ? -1 : 0)) + return -CMP_INT(((struct entry *)a)->score, ((struct entry *)b)->score); +} + +// Return a list of sample format compatible to src_format, sorted by order +// of preference. out_formats[0] will be src_format (as long as it's valid), +// and the list is terminated with 0 (AF_FORMAT_UNKNOWN). +// Keep in mind that this also returns formats with flipped interleaving +// (e.g. for s16, it returns [s16, s16p, ...]). +void af_get_best_sample_formats(int src_format, int out_formats[AF_FORMAT_COUNT]) +{ + int num = 0; + struct entry e[AF_FORMAT_COUNT]; + for (int fmt = 1; fmt < AF_FORMAT_COUNT; fmt++) { + int score = af_format_conversion_score(fmt, src_format); + if (score > INT_MIN) + e[num++] = (struct entry){fmt, score}; + } + qsort(e, num, sizeof(e[0]), cmp_entry); + for (int n = 0; n < num; n++) + out_formats[n] = e[n].fmt; + out_formats[num] = 0; +} + // Return the number of samples that make up one frame in this format. // You get the byte size by multiplying them with sample size and channel count. int af_format_sample_alignment(int format) diff --git a/audio/format.h b/audio/format.h index ea17a44766..831fe0a825 100644 --- a/audio/format.h +++ b/audio/format.h @@ -76,6 +76,7 @@ int af_fmt_seconds_to_bytes(int format, float seconds, int channels, int sampler void af_fill_silence(void *dst, size_t bytes, int format); int af_format_conversion_score(int dst_format, int src_format); +void af_get_best_sample_formats(int src_format, int out_formats[AF_FORMAT_COUNT]); int af_format_sample_alignment(int format); -- cgit v1.2.3