audio: move function to convert

This commit is contained in:
Wim Taymans 2011-08-22 16:11:27 +02:00
parent 00a30b5cfd
commit c6758ecfa9
3 changed files with 102 additions and 69 deletions

View file

@ -410,6 +410,99 @@ gst_audio_info_to_caps (GstAudioInfo * info)
return caps; 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: * gst_audio_buffer_clip:
* @buffer: The buffer to clip. * @buffer: The buffer to clip.

View file

@ -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); gboolean gst_audio_info_from_caps (GstAudioInfo *info, const GstCaps *caps);
GstCaps * gst_audio_info_to_caps (GstAudioInfo *info); 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_RATE_RANGE "(int) [ 1, max ]"
#define GST_AUDIO_CHANNELS_RANGE "(int) [ 1, max ]" #define GST_AUDIO_CHANNELS_RANGE "(int) [ 1, max ]"

View file

@ -316,79 +316,14 @@ gboolean
gst_ring_buffer_convert (GstRingBuffer * buf, gst_ring_buffer_convert (GstRingBuffer * buf,
GstFormat src_fmt, gint64 src_val, GstFormat dest_fmt, gint64 * dest_val) GstFormat src_fmt, gint64 src_val, GstFormat dest_fmt, gint64 * dest_val)
{ {
gboolean res = TRUE; gboolean res;
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 */
GST_OBJECT_LOCK (buf); GST_OBJECT_LOCK (buf);
bpf = GST_AUDIO_INFO_BPF (&buf->spec.info); res =
rate = GST_AUDIO_INFO_RATE (&buf->spec.info); gst_audio_info_convert (&buf->spec.info, src_fmt, src_val, dest_fmt,
dest_val);
GST_OBJECT_UNLOCK (buf); 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; return res;
} }