aboutsummaryrefslogtreecommitdiffhomepage
path: root/audio
diff options
context:
space:
mode:
authorGravatar wm4 <wm4@nowhere>2015-09-10 23:30:51 +0200
committerGravatar wm4 <wm4@nowhere>2015-09-10 23:30:51 +0200
commit60a617df311fc6e56feced64143d912eb7b7099b (patch)
treedbcf26f8c06596239b7bef93853c95bdd5006452 /audio
parente45f4692804f48c7e8b4a4229bbd51d72a6a6d42 (diff)
audio/format: add function for determining sample conversion candidates
Diffstat (limited to 'audio')
-rw-r--r--audio/format.c31
-rw-r--r--audio/format.h1
2 files changed, 32 insertions, 0 deletions
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);