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.
This commit is contained in:
Wim Taymans 2016-01-15 11:20:29 +01:00
parent 23531bdc93
commit d348fbb9b9
3 changed files with 27 additions and 47 deletions

View file

@ -513,7 +513,7 @@ do_resample (AudioChain * chain, gpointer user_data)
gsize in_frames, out_frames; gsize in_frames, out_frames;
in = audio_chain_get_samples (chain->prev, &in_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)); out = (chain->allow_ip ? in : audio_chain_alloc_samples (chain, out_frames));
GST_LOG ("resample %p %p,%" G_GSIZE_FORMAT " %" G_GSIZE_FORMAT, in, 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++) for (i = 0; i < chain->blocks; i++)
gst_audio_format_fill_silence (convert->in.finfo, out[i], samples); gst_audio_format_fill_silence (convert->in.finfo, out[i], samples);
} }
return TRUE; return TRUE;
} }
@ -880,6 +879,17 @@ converter_generic (GstAudioConverter * convert,
return TRUE; 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) * gst_audio_converter_new: (skip)
* @flags: extra #GstAudioConverterFlags * @flags: extra #GstAudioConverterFlags
@ -942,10 +952,15 @@ gst_audio_converter_new (GstAudioConverterFlags flags, GstAudioInfo * in_info,
/* optimize */ /* optimize */
if (out_info->finfo->format == in_info->finfo->format if (out_info->finfo->format == in_info->finfo->format
&& convert->mix_passthrough && convert->resampler == NULL) { && convert->mix_passthrough) {
GST_INFO if (convert->resampler == NULL) {
("same formats, no resampler and passthrough mixing -> passthrough"); GST_INFO
convert->convert = converter_passthrough; ("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 { } else {
GST_INFO ("do full conversion"); GST_INFO ("do full conversion");
convert->convert = converter_generic; convert->convert = converter_generic;

View file

@ -77,7 +77,6 @@ struct _GstAudioResampler
guint blocks; guint blocks;
guint inc; guint inc;
gboolean filling;
gint samp_inc; gint samp_inc;
gint samp_frac; gint samp_frac;
gint samp_index; gint samp_index;
@ -970,7 +969,6 @@ gst_audio_resampler_update (GstAudioResampler * resampler,
resampler_calculate_taps (resampler); resampler_calculate_taps (resampler);
resampler_dump (resampler); resampler_dump (resampler);
resampler->filling = TRUE;
resampler->samp_index = 0; resampler->samp_index = 0;
resampler->samp_phase = 0; resampler->samp_phase = 0;
resampler->samples_avail = resampler->n_taps / 2 - 1; resampler->samples_avail = resampler->n_taps / 2 - 1;

View file

@ -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 static gboolean
gst_audio_resample_transform_size (GstBaseTransform * base, gst_audio_resample_transform_size (GstBaseTransform * base,
GstPadDirection direction, GstCaps * caps, gsize size, GstCaps * othercaps, GstPadDirection direction, GstCaps * caps, gsize size, GstCaps * othercaps,
gsize * othersize) gsize * othersize)
{ {
GstAudioResample *resample = GST_AUDIO_RESAMPLE (base);
gboolean ret = TRUE; gboolean ret = TRUE;
GstAudioInfo in, out;
guint32 ratio_den, ratio_num;
gint inrate, outrate, gcd;
gint bpf; gint bpf;
GST_LOG_OBJECT (base, "asked to transform size %" G_GSIZE_FORMAT GST_LOG_OBJECT (base, "asked to transform size %" G_GSIZE_FORMAT
" in direction %s", size, direction == GST_PAD_SINK ? "SINK" : "SRC"); " 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) -> /* Number of samples in either buffer is size / (width*channels) ->
* calculate the factor */ * calculate the factor */
bpf = GST_AUDIO_INFO_BPF (&in); bpf = GST_AUDIO_INFO_BPF (&resample->in);
inrate = GST_AUDIO_INFO_RATE (&in);
outrate = GST_AUDIO_INFO_RATE (&out);
/* Convert source buffer size to samples */ /* Convert source buffer size to samples */
size /= bpf; size /= bpf;
/* Simplify the conversion ratio factors */
gcd = _gcd (inrate, outrate);
ratio_num = inrate / gcd;
ratio_den = outrate / gcd;
if (direction == GST_PAD_SINK) { if (direction == GST_PAD_SINK) {
/* asked to convert size of an incoming buffer. Round up the output size */ /* asked to convert size of an incoming buffer */
*othersize = gst_util_uint64_scale_int_ceil (size, ratio_den, ratio_num); *othersize = gst_audio_converter_get_out_frames (resample->converter, size);
*othersize *= bpf; *othersize *= bpf;
} else { } else {
/* asked to convert size of an outgoing buffer. Round down the input size */ /* asked to convert size of an outgoing buffer */
*othersize = gst_util_uint64_scale_int (size, ratio_num, ratio_den); *othersize = gst_audio_converter_get_in_frames (resample->converter, size);
*othersize *= bpf; *othersize *= bpf;
} }
@ -810,9 +781,6 @@ gst_audio_resample_process (GstAudioResample * resample, GstBuffer * inbuf,
gpointer in[1], out[1]; gpointer in[1], out[1];
GstAudioConverterFlags flags; GstAudioConverterFlags flags;
out_len =
gst_audio_converter_get_out_frames (resample->converter, in_len);
flags = 0; flags = 0;
if (inbuf_writable) if (inbuf_writable)
flags |= GST_AUDIO_CONVERTER_FLAG_IN_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); gst_buffer_unmap (outbuf, &out_map);
outsize = out_len * resample->in.bpf; outsize = out_len * resample->in.bpf;
gst_buffer_resize (outbuf, 0, outsize);
GST_LOG_OBJECT (resample, GST_LOG_OBJECT (resample,
"Converted to buffer of %" G_GUINT32_FORMAT "Converted to buffer of %" G_GUINT32_FORMAT