From d348fbb9b98eaf045604195abe43f85ddb2340a5 Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Fri, 15 Jan 2016 11:20:29 +0100 Subject: [PATCH] audio-converter: make some optimized functions Make an optimized function that just calls the resampler when possible. Optimize the resampler transform_size function a little. --- gst-libs/gst/audio/audio-converter.c | 27 +++++++++++++++++----- gst-libs/gst/audio/audio-resampler.c | 2 -- gst/audioresample/gstaudioresample.c | 45 +++++------------------------------- 3 files changed, 27 insertions(+), 47 deletions(-) diff --git a/gst-libs/gst/audio/audio-converter.c b/gst-libs/gst/audio/audio-converter.c index 8c66b4c..8a8fa4a 100644 --- a/gst-libs/gst/audio/audio-converter.c +++ b/gst-libs/gst/audio/audio-converter.c @@ -513,7 +513,7 @@ do_resample (AudioChain * chain, gpointer user_data) gsize in_frames, out_frames; in = audio_chain_get_samples (chain->prev, &in_frames); - out_frames = convert->out_samples; + out_frames = convert->out_frames; out = (chain->allow_ip ? in : audio_chain_alloc_samples (chain, out_frames)); GST_LOG ("resample %p %p,%" G_GSIZE_FORMAT " %" G_GSIZE_FORMAT, in, @@ -845,7 +845,6 @@ converter_passthrough (GstAudioConverter * convert, for (i = 0; i < chain->blocks; i++) gst_audio_format_fill_silence (convert->in.finfo, out[i], samples); } - return TRUE; } @@ -880,6 +879,17 @@ converter_generic (GstAudioConverter * convert, return TRUE; } +static gboolean +converter_resample (GstAudioConverter * convert, + GstAudioConverterFlags flags, gpointer in[], gsize in_frames, + gpointer out[], gsize out_frames) +{ + gst_audio_resampler_resample (convert->resampler, in, in_frames, out, + out_frames); + + return TRUE; +} + /** * gst_audio_converter_new: (skip) * @flags: extra #GstAudioConverterFlags @@ -942,10 +952,15 @@ gst_audio_converter_new (GstAudioConverterFlags flags, GstAudioInfo * in_info, /* optimize */ if (out_info->finfo->format == in_info->finfo->format - && convert->mix_passthrough && convert->resampler == NULL) { - GST_INFO - ("same formats, no resampler and passthrough mixing -> passthrough"); - convert->convert = converter_passthrough; + && convert->mix_passthrough) { + if (convert->resampler == NULL) { + GST_INFO + ("same formats, no resampler and passthrough mixing -> passthrough"); + convert->convert = converter_passthrough; + } else { + GST_INFO ("same formats, and passthrough mixing -> only resampling"); + convert->convert = converter_resample; + } } else { GST_INFO ("do full conversion"); convert->convert = converter_generic; diff --git a/gst-libs/gst/audio/audio-resampler.c b/gst-libs/gst/audio/audio-resampler.c index 23346dc..5a2f643 100644 --- a/gst-libs/gst/audio/audio-resampler.c +++ b/gst-libs/gst/audio/audio-resampler.c @@ -77,7 +77,6 @@ struct _GstAudioResampler guint blocks; guint inc; - gboolean filling; gint samp_inc; gint samp_frac; gint samp_index; @@ -970,7 +969,6 @@ gst_audio_resampler_update (GstAudioResampler * resampler, resampler_calculate_taps (resampler); resampler_dump (resampler); - resampler->filling = TRUE; resampler->samp_index = 0; resampler->samp_phase = 0; resampler->samples_avail = resampler->n_taps / 2 - 1; diff --git a/gst/audioresample/gstaudioresample.c b/gst/audioresample/gstaudioresample.c index 2faf675..45d4cdf 100644 --- a/gst/audioresample/gstaudioresample.c +++ b/gst/audioresample/gstaudioresample.c @@ -431,61 +431,32 @@ gst_audio_resample_reset_state (GstAudioResample * resample) { } -static gint -_gcd (gint a, gint b) -{ - while (b != 0) { - int temp = a; - - a = b; - b = temp % b; - } - - return ABS (a); -} - static gboolean gst_audio_resample_transform_size (GstBaseTransform * base, GstPadDirection direction, GstCaps * caps, gsize size, GstCaps * othercaps, gsize * othersize) { + GstAudioResample *resample = GST_AUDIO_RESAMPLE (base); gboolean ret = TRUE; - GstAudioInfo in, out; - guint32 ratio_den, ratio_num; - gint inrate, outrate, gcd; gint bpf; GST_LOG_OBJECT (base, "asked to transform size %" G_GSIZE_FORMAT " in direction %s", size, direction == GST_PAD_SINK ? "SINK" : "SRC"); - /* Get sample width -> bytes_per_samp, channels, inrate, outrate */ - ret = gst_audio_info_from_caps (&in, caps); - ret &= gst_audio_info_from_caps (&out, othercaps); - if (G_UNLIKELY (!ret)) { - GST_ERROR_OBJECT (base, "Wrong caps"); - return FALSE; - } /* Number of samples in either buffer is size / (width*channels) -> * calculate the factor */ - bpf = GST_AUDIO_INFO_BPF (&in); - inrate = GST_AUDIO_INFO_RATE (&in); - outrate = GST_AUDIO_INFO_RATE (&out); + bpf = GST_AUDIO_INFO_BPF (&resample->in); /* Convert source buffer size to samples */ size /= bpf; - /* Simplify the conversion ratio factors */ - gcd = _gcd (inrate, outrate); - ratio_num = inrate / gcd; - ratio_den = outrate / gcd; - if (direction == GST_PAD_SINK) { - /* asked to convert size of an incoming buffer. Round up the output size */ - *othersize = gst_util_uint64_scale_int_ceil (size, ratio_den, ratio_num); + /* asked to convert size of an incoming buffer */ + *othersize = gst_audio_converter_get_out_frames (resample->converter, size); *othersize *= bpf; } else { - /* asked to convert size of an outgoing buffer. Round down the input size */ - *othersize = gst_util_uint64_scale_int (size, ratio_num, ratio_den); + /* asked to convert size of an outgoing buffer */ + *othersize = gst_audio_converter_get_in_frames (resample->converter, size); *othersize *= bpf; } @@ -810,9 +781,6 @@ gst_audio_resample_process (GstAudioResample * resample, GstBuffer * inbuf, gpointer in[1], out[1]; GstAudioConverterFlags flags; - out_len = - gst_audio_converter_get_out_frames (resample->converter, in_len); - flags = 0; if (inbuf_writable) flags |= GST_AUDIO_CONVERTER_FLAG_IN_WRITABLE; @@ -853,7 +821,6 @@ gst_audio_resample_process (GstAudioResample * resample, GstBuffer * inbuf, gst_buffer_unmap (outbuf, &out_map); outsize = out_len * resample->in.bpf; - gst_buffer_resize (outbuf, 0, outsize); GST_LOG_OBJECT (resample, "Converted to buffer of %" G_GUINT32_FORMAT -- 2.7.4