From c6758ecfa9fdfb3d06d3c2a3652e2c7dc14dbbd1 Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Mon, 22 Aug 2011 16:11:27 +0200 Subject: [PATCH] audio: move function to convert --- gst-libs/gst/audio/audio.c | 93 ++++++++++++++++++++++++++++++ gst-libs/gst/audio/audio.h | 5 ++ gst-libs/gst/audio/gstringbuffer.c | 73 ++--------------------- 3 files changed, 102 insertions(+), 69 deletions(-) diff --git a/gst-libs/gst/audio/audio.c b/gst-libs/gst/audio/audio.c index 0f647c6724..2753a62eed 100644 --- a/gst-libs/gst/audio/audio.c +++ b/gst-libs/gst/audio/audio.c @@ -410,6 +410,99 @@ gst_audio_info_to_caps (GstAudioInfo * info) return caps; } +/** + * gst_audio_format_convert: + * @info: a #GstAudioInfo + * @src_format: #GstFormat of the @src_value + * @src_value: value to convert + * @dest_format: #GstFormat of the @dest_value + * @dest_value: pointer to destination value + * + * Converts among various #GstFormat types. This function handles + * GST_FORMAT_BYTES, GST_FORMAT_TIME, and GST_FORMAT_DEFAULT. For + * raw audio, GST_FORMAT_DEFAULT corresponds to audio frames. This + * function can be used to handle pad queries of the type GST_QUERY_CONVERT. + * + * Returns: TRUE if the conversion was successful. + */ +gboolean +gst_audio_info_convert (GstAudioInfo * info, + GstFormat src_fmt, gint64 src_val, GstFormat dest_fmt, gint64 * dest_val) +{ + gboolean res = TRUE; + gint bpf, rate; + + GST_DEBUG ("converting value %" G_GINT64_FORMAT " from %s (%d) to %s (%d)", + src_val, gst_format_get_name (src_fmt), src_fmt, + gst_format_get_name (dest_fmt), dest_fmt); + + if (src_fmt == dest_fmt || src_val == -1) { + *dest_val = src_val; + goto done; + } + + /* get important info */ + bpf = GST_AUDIO_INFO_BPF (info); + rate = GST_AUDIO_INFO_RATE (info); + + if (bpf == 0 || rate == 0) { + GST_DEBUG ("no rate or bpf configured"); + res = FALSE; + goto done; + } + + switch (src_fmt) { + case GST_FORMAT_BYTES: + switch (dest_fmt) { + case GST_FORMAT_TIME: + *dest_val = GST_FRAMES_TO_CLOCK_TIME (src_val / bpf, rate); + break; + case GST_FORMAT_DEFAULT: + *dest_val = src_val / bpf; + break; + default: + res = FALSE; + break; + } + break; + case GST_FORMAT_DEFAULT: + switch (dest_fmt) { + case GST_FORMAT_TIME: + *dest_val = GST_FRAMES_TO_CLOCK_TIME (src_val, rate); + break; + case GST_FORMAT_BYTES: + *dest_val = src_val * bpf; + break; + default: + res = FALSE; + break; + } + break; + case GST_FORMAT_TIME: + switch (dest_fmt) { + case GST_FORMAT_DEFAULT: + *dest_val = GST_CLOCK_TIME_TO_FRAMES (src_val, rate); + break; + case GST_FORMAT_BYTES: + *dest_val = GST_CLOCK_TIME_TO_FRAMES (src_val, rate); + *dest_val *= bpf; + break; + default: + res = FALSE; + break; + } + break; + default: + res = FALSE; + break; + } +done: + GST_DEBUG ("ret=%d result %" G_GINT64_FORMAT, res, *dest_val); + + return res; +} + + /** * gst_audio_buffer_clip: * @buffer: The buffer to clip. diff --git a/gst-libs/gst/audio/audio.h b/gst-libs/gst/audio/audio.h index e60312dab2..f5a3d1e639 100644 --- a/gst-libs/gst/audio/audio.h +++ b/gst-libs/gst/audio/audio.h @@ -297,6 +297,11 @@ void gst_audio_info_set_format (GstAudioInfo *info, GstAudioFormat form gboolean gst_audio_info_from_caps (GstAudioInfo *info, const GstCaps *caps); GstCaps * gst_audio_info_to_caps (GstAudioInfo *info); +gboolean gst_audio_info_convert (GstAudioInfo * info, + GstFormat src_fmt, gint64 src_val, + GstFormat dest_fmt, gint64 * dest_val); + + #define GST_AUDIO_RATE_RANGE "(int) [ 1, max ]" #define GST_AUDIO_CHANNELS_RANGE "(int) [ 1, max ]" diff --git a/gst-libs/gst/audio/gstringbuffer.c b/gst-libs/gst/audio/gstringbuffer.c index cb18f764ef..53dfa71456 100644 --- a/gst-libs/gst/audio/gstringbuffer.c +++ b/gst-libs/gst/audio/gstringbuffer.c @@ -316,79 +316,14 @@ gboolean gst_ring_buffer_convert (GstRingBuffer * buf, GstFormat src_fmt, gint64 src_val, GstFormat dest_fmt, gint64 * dest_val) { - gboolean res = TRUE; - gint bpf, rate; + gboolean res; - GST_DEBUG ("converting value %" G_GINT64_FORMAT " from %s (%d) to %s (%d)", - src_val, gst_format_get_name (src_fmt), src_fmt, - gst_format_get_name (dest_fmt), dest_fmt); - - if (src_fmt == dest_fmt || src_val == -1) { - *dest_val = src_val; - goto done; - } - - /* get important info */ GST_OBJECT_LOCK (buf); - bpf = GST_AUDIO_INFO_BPF (&buf->spec.info); - rate = GST_AUDIO_INFO_RATE (&buf->spec.info); + res = + gst_audio_info_convert (&buf->spec.info, src_fmt, src_val, dest_fmt, + dest_val); GST_OBJECT_UNLOCK (buf); - if (bpf == 0 || rate == 0) { - GST_DEBUG ("no rate or bpf configured"); - res = FALSE; - goto done; - } - - switch (src_fmt) { - case GST_FORMAT_BYTES: - switch (dest_fmt) { - case GST_FORMAT_TIME: - *dest_val = gst_util_uint64_scale_int (src_val / bpf, GST_SECOND, - rate); - break; - case GST_FORMAT_DEFAULT: - *dest_val = src_val / bpf; - break; - default: - res = FALSE; - break; - } - break; - case GST_FORMAT_DEFAULT: - switch (dest_fmt) { - case GST_FORMAT_TIME: - *dest_val = gst_util_uint64_scale_int (src_val, GST_SECOND, rate); - break; - case GST_FORMAT_BYTES: - *dest_val = src_val * bpf; - break; - default: - res = FALSE; - break; - } - break; - case GST_FORMAT_TIME: - switch (dest_fmt) { - case GST_FORMAT_DEFAULT: - *dest_val = gst_util_uint64_scale_int (src_val, rate, GST_SECOND); - break; - case GST_FORMAT_BYTES: - *dest_val = gst_util_uint64_scale_int (src_val, rate, GST_SECOND); - *dest_val *= bpf; - break; - default: - res = FALSE; - break; - } - break; - default: - res = FALSE; - break; - } -done: - GST_DEBUG ("ret=%d result %" G_GINT64_FORMAT, res, *dest_val); - return res; }