2 * Copyright (C) 1999 Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) 2003,2004 David A. Schleef <ds@schleef.org>
4 * Copyright (C) 2007-2008 Sebastian Dröge <sebastian.droege@collabora.co.uk>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 * SECTION:element-audioresample
25 * audioresample resamples raw audio buffers to different sample rates using
26 * a configurable windowing function to enhance quality.
29 * <title>Example launch line</title>
31 * gst-launch -v filesrc location=sine.ogg ! oggdemux ! vorbisdec ! audioconvert ! audioresample ! audio/x-raw-int, rate=8000 ! alsasink
32 * ]| Decode an Ogg/Vorbis downsample to 8Khz and play sound through alsa.
33 * To create the Ogg/Vorbis file refer to the documentation of vorbisenc.
38 * - Enable SSE/ARM optimizations and select at runtime
48 #include "gstaudioresample.h"
49 #include <gst/audio/audio.h>
50 #include <gst/base/gstbasetransform.h>
52 #if defined AUDIORESAMPLE_FORMAT_AUTO
53 #define OIL_ENABLE_UNSTABLE_API
54 #include <liboil/liboilprofile.h>
55 #include <liboil/liboil.h>
58 GST_DEBUG_CATEGORY (audio_resample_debug);
59 #define GST_CAT_DEFAULT audio_resample_debug
68 #define SUPPORTED_CAPS \
70 "audio/x-raw-float, " \
71 "rate = (int) [ 1, MAX ], " \
72 "channels = (int) [ 1, MAX ], " \
73 "endianness = (int) BYTE_ORDER, " \
74 "width = (int) { 32, 64 }; " \
76 "rate = (int) [ 1, MAX ], " \
77 "channels = (int) [ 1, MAX ], " \
78 "endianness = (int) BYTE_ORDER, " \
79 "width = (int) 32, " \
80 "depth = (int) 32, " \
81 "signed = (boolean) true; " \
83 "rate = (int) [ 1, MAX ], " \
84 "channels = (int) [ 1, MAX ], " \
85 "endianness = (int) BYTE_ORDER, " \
86 "width = (int) 24, " \
87 "depth = (int) 24, " \
88 "signed = (boolean) true; " \
90 "rate = (int) [ 1, MAX ], " \
91 "channels = (int) [ 1, MAX ], " \
92 "endianness = (int) BYTE_ORDER, " \
93 "width = (int) 16, " \
94 "depth = (int) 16, " \
95 "signed = (boolean) true; " \
97 "rate = (int) [ 1, MAX ], " \
98 "channels = (int) [ 1, MAX ], " \
99 "endianness = (int) BYTE_ORDER, " \
100 "width = (int) 8, " \
101 "depth = (int) 8, " \
102 "signed = (boolean) true" \
105 /* If TRUE integer arithmetic resampling is faster and will be used if appropiate */
106 #if defined AUDIORESAMPLE_FORMAT_INT
107 static gboolean gst_audio_resample_use_int = TRUE;
108 #elif defined AUDIORESAMPLE_FORMAT_FLOAT
109 static gboolean gst_audio_resample_use_int = FALSE;
111 static gboolean gst_audio_resample_use_int = FALSE;
114 static GstStaticPadTemplate gst_audio_resample_sink_template =
115 GST_STATIC_PAD_TEMPLATE ("sink",
116 GST_PAD_SINK, GST_PAD_ALWAYS, SUPPORTED_CAPS);
118 static GstStaticPadTemplate gst_audio_resample_src_template =
119 GST_STATIC_PAD_TEMPLATE ("src",
120 GST_PAD_SRC, GST_PAD_ALWAYS, SUPPORTED_CAPS);
122 static void gst_audio_resample_set_property (GObject * object,
123 guint prop_id, const GValue * value, GParamSpec * pspec);
124 static void gst_audio_resample_get_property (GObject * object,
125 guint prop_id, GValue * value, GParamSpec * pspec);
128 static gboolean gst_audio_resample_get_unit_size (GstBaseTransform * base,
129 GstCaps * caps, guint * size);
130 static GstCaps *gst_audio_resample_transform_caps (GstBaseTransform * base,
131 GstPadDirection direction, GstCaps * caps);
132 static void gst_audio_resample_fixate_caps (GstBaseTransform * base,
133 GstPadDirection direction, GstCaps * caps, GstCaps * othercaps);
134 static gboolean gst_audio_resample_transform_size (GstBaseTransform * trans,
135 GstPadDirection direction, GstCaps * incaps, guint insize,
136 GstCaps * outcaps, guint * outsize);
137 static gboolean gst_audio_resample_set_caps (GstBaseTransform * base,
138 GstCaps * incaps, GstCaps * outcaps);
139 static GstFlowReturn gst_audio_resample_transform (GstBaseTransform * base,
140 GstBuffer * inbuf, GstBuffer * outbuf);
141 static gboolean gst_audio_resample_event (GstBaseTransform * base,
143 static gboolean gst_audio_resample_start (GstBaseTransform * base);
144 static gboolean gst_audio_resample_stop (GstBaseTransform * base);
145 static gboolean gst_audio_resample_query (GstPad * pad, GstQuery * query);
146 static const GstQueryType *gst_audio_resample_query_type (GstPad * pad);
148 GST_BOILERPLATE (GstAudioResample, gst_audio_resample, GstBaseTransform,
149 GST_TYPE_BASE_TRANSFORM);
152 gst_audio_resample_base_init (gpointer g_class)
154 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
156 gst_element_class_add_pad_template (gstelement_class,
157 gst_static_pad_template_get (&gst_audio_resample_src_template));
158 gst_element_class_add_pad_template (gstelement_class,
159 gst_static_pad_template_get (&gst_audio_resample_sink_template));
161 gst_element_class_set_details_simple (gstelement_class, "Audio resampler",
162 "Filter/Converter/Audio", "Resamples audio",
163 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
167 gst_audio_resample_class_init (GstAudioResampleClass * klass)
169 GObjectClass *gobject_class = (GObjectClass *) klass;
171 gobject_class->set_property = gst_audio_resample_set_property;
172 gobject_class->get_property = gst_audio_resample_get_property;
174 g_object_class_install_property (gobject_class, PROP_QUALITY,
175 g_param_spec_int ("quality", "Quality", "Resample quality with 0 being "
176 "the lowest and 10 being the best",
177 SPEEX_RESAMPLER_QUALITY_MIN, SPEEX_RESAMPLER_QUALITY_MAX,
178 SPEEX_RESAMPLER_QUALITY_DEFAULT,
179 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
181 /* FIXME 0.11: Remove this property, it's just for compatibility
182 * with old audioresample
185 * GstAudioResample:filter-length:
187 * Length of the resample filter
189 * Deprectated: Use #GstAudioResample:quality property instead
191 g_object_class_install_property (gobject_class, PROP_FILTER_LENGTH,
192 g_param_spec_int ("filter-length", "Filter length",
193 "Length of the resample filter", 0, G_MAXINT, 64, G_PARAM_READWRITE));
195 GST_BASE_TRANSFORM_CLASS (klass)->start =
196 GST_DEBUG_FUNCPTR (gst_audio_resample_start);
197 GST_BASE_TRANSFORM_CLASS (klass)->stop =
198 GST_DEBUG_FUNCPTR (gst_audio_resample_stop);
199 GST_BASE_TRANSFORM_CLASS (klass)->transform_size =
200 GST_DEBUG_FUNCPTR (gst_audio_resample_transform_size);
201 GST_BASE_TRANSFORM_CLASS (klass)->get_unit_size =
202 GST_DEBUG_FUNCPTR (gst_audio_resample_get_unit_size);
203 GST_BASE_TRANSFORM_CLASS (klass)->transform_caps =
204 GST_DEBUG_FUNCPTR (gst_audio_resample_transform_caps);
205 GST_BASE_TRANSFORM_CLASS (klass)->fixate_caps =
206 GST_DEBUG_FUNCPTR (gst_audio_resample_fixate_caps);
207 GST_BASE_TRANSFORM_CLASS (klass)->set_caps =
208 GST_DEBUG_FUNCPTR (gst_audio_resample_set_caps);
209 GST_BASE_TRANSFORM_CLASS (klass)->transform =
210 GST_DEBUG_FUNCPTR (gst_audio_resample_transform);
211 GST_BASE_TRANSFORM_CLASS (klass)->event =
212 GST_DEBUG_FUNCPTR (gst_audio_resample_event);
214 GST_BASE_TRANSFORM_CLASS (klass)->passthrough_on_same_caps = TRUE;
218 gst_audio_resample_init (GstAudioResample * resample,
219 GstAudioResampleClass * klass)
221 GstBaseTransform *trans = GST_BASE_TRANSFORM (resample);
223 resample->quality = SPEEX_RESAMPLER_QUALITY_DEFAULT;
225 resample->need_discont = FALSE;
227 gst_pad_set_query_function (trans->srcpad, gst_audio_resample_query);
228 gst_pad_set_query_type_function (trans->srcpad,
229 gst_audio_resample_query_type);
234 gst_audio_resample_start (GstBaseTransform * base)
236 GstAudioResample *resample = GST_AUDIO_RESAMPLE (base);
238 resample->next_offset = -1;
239 resample->next_ts = -1;
240 resample->next_upstream_ts = -1;
246 gst_audio_resample_stop (GstBaseTransform * base)
248 GstAudioResample *resample = GST_AUDIO_RESAMPLE (base);
250 if (resample->state) {
251 resample->funcs->destroy (resample->state);
252 resample->state = NULL;
255 resample->funcs = NULL;
257 g_free (resample->tmp_in);
258 resample->tmp_in = NULL;
259 resample->tmp_in_size = 0;
261 g_free (resample->tmp_out);
262 resample->tmp_out = NULL;
263 resample->tmp_out_size = 0;
265 gst_caps_replace (&resample->sinkcaps, NULL);
266 gst_caps_replace (&resample->srccaps, NULL);
272 gst_audio_resample_get_unit_size (GstBaseTransform * base, GstCaps * caps,
275 gint width, channels;
276 GstStructure *structure;
279 g_return_val_if_fail (size != NULL, FALSE);
281 /* this works for both float and int */
282 structure = gst_caps_get_structure (caps, 0);
283 ret = gst_structure_get_int (structure, "width", &width);
284 ret &= gst_structure_get_int (structure, "channels", &channels);
286 if (G_UNLIKELY (!ret))
289 *size = (width / 8) * channels;
295 gst_audio_resample_transform_caps (GstBaseTransform * base,
296 GstPadDirection direction, GstCaps * caps)
302 /* transform single caps into input_caps + input_caps with the rate
303 * field set to our supported range. This ensures that upstream knows
304 * about downstream's prefered rate(s) and can negotiate accordingly. */
305 res = gst_caps_copy (caps);
307 /* first, however, check if the caps contain a range for the rate field, in
308 * which case that side isn't going to care much about the exact sample rate
309 * chosen and we should just assume things will get fixated to something sane
310 * and we may just as well offer our full range instead of the range in the
311 * caps. If the rate is not an int range value, it's likely to express a
312 * real preference or limitation and we should maintain that structure as
313 * preference by putting it first into the transformed caps, and only add
314 * our full rate range as second option */
315 s = gst_caps_get_structure (res, 0);
316 val = gst_structure_get_value (s, "rate");
317 if (val == NULL || GST_VALUE_HOLDS_INT_RANGE (val)) {
318 /* overwrite existing range, or add field if it doesn't exist yet */
319 gst_structure_set (s, "rate", GST_TYPE_INT_RANGE, 1, G_MAXINT, NULL);
321 /* append caps with full range to existing caps with non-range rate field */
322 s = gst_structure_copy (s);
323 gst_structure_set (s, "rate", GST_TYPE_INT_RANGE, 1, G_MAXINT, NULL);
324 gst_caps_append_structure (res, s);
330 /* Fixate rate to the allowed rate that has the smallest difference */
332 gst_audio_resample_fixate_caps (GstBaseTransform * base,
333 GstPadDirection direction, GstCaps * caps, GstCaps * othercaps)
338 s = gst_caps_get_structure (caps, 0);
339 if (G_UNLIKELY (!gst_structure_get_int (s, "rate", &rate)))
342 s = gst_caps_get_structure (othercaps, 0);
343 gst_structure_fixate_field_nearest_int (s, "rate", rate);
346 static const SpeexResampleFuncs *
347 gst_audio_resample_get_funcs (gint width, gboolean fp)
349 const SpeexResampleFuncs *funcs = NULL;
351 if (gst_audio_resample_use_int && (width == 8 || width == 16) && !fp)
353 else if ((!gst_audio_resample_use_int && (width == 8 || width == 16) && !fp)
354 || (width == 32 && fp))
355 funcs = &float_funcs;
356 else if ((width == 64 && fp) || ((width == 32 || width == 24) && !fp))
357 funcs = &double_funcs;
359 g_assert_not_reached ();
364 static SpeexResamplerState *
365 gst_audio_resample_init_state (GstAudioResample * resample, gint width,
366 gint channels, gint inrate, gint outrate, gint quality, gboolean fp)
368 SpeexResamplerState *ret = NULL;
369 gint err = RESAMPLER_ERR_SUCCESS;
370 const SpeexResampleFuncs *funcs = gst_audio_resample_get_funcs (width, fp);
372 ret = funcs->init (channels, inrate, outrate, quality, &err);
374 if (G_UNLIKELY (err != RESAMPLER_ERR_SUCCESS)) {
375 GST_ERROR_OBJECT (resample, "Failed to create resampler state: %s",
376 funcs->strerror (err));
380 funcs->skip_zeros (ret);
386 gst_audio_resample_update_state (GstAudioResample * resample, gint width,
387 gint channels, gint inrate, gint outrate, gint quality, gboolean fp)
390 gboolean updated_latency = FALSE;
392 updated_latency = (resample->inrate != inrate
393 || quality != resample->quality) && resample->state != NULL;
395 if (resample->state == NULL) {
397 } else if (resample->channels != channels || fp != resample->fp
398 || width != resample->width) {
399 resample->funcs->destroy (resample->state);
401 gst_audio_resample_init_state (resample, width, channels, inrate,
402 outrate, quality, fp);
404 resample->funcs = gst_audio_resample_get_funcs (width, fp);
405 ret = (resample->state != NULL);
406 } else if (resample->inrate != inrate || resample->outrate != outrate) {
407 gint err = RESAMPLER_ERR_SUCCESS;
409 err = resample->funcs->set_rate (resample->state, inrate, outrate);
411 if (G_UNLIKELY (err != RESAMPLER_ERR_SUCCESS))
412 GST_ERROR_OBJECT (resample, "Failed to update rate: %s",
413 resample->funcs->strerror (err));
415 ret = (err == RESAMPLER_ERR_SUCCESS);
416 } else if (quality != resample->quality) {
417 gint err = RESAMPLER_ERR_SUCCESS;
419 err = resample->funcs->set_quality (resample->state, quality);
421 if (G_UNLIKELY (err != RESAMPLER_ERR_SUCCESS))
422 GST_ERROR_OBJECT (resample, "Failed to update quality: %s",
423 resample->funcs->strerror (err));
425 ret = (err == RESAMPLER_ERR_SUCCESS);
428 resample->width = width;
429 resample->channels = channels;
431 resample->quality = quality;
432 resample->inrate = inrate;
433 resample->outrate = outrate;
436 gst_element_post_message (GST_ELEMENT (resample),
437 gst_message_new_latency (GST_OBJECT (resample)));
443 gst_audio_resample_reset_state (GstAudioResample * resample)
446 resample->funcs->reset_mem (resample->state);
450 gst_audio_resample_parse_caps (GstCaps * incaps,
451 GstCaps * outcaps, gint * width, gint * channels, gint * inrate,
452 gint * outrate, gboolean * fp)
454 GstStructure *structure;
456 gint mywidth, myinrate, myoutrate, mychannels;
459 GST_DEBUG ("incaps %" GST_PTR_FORMAT ", outcaps %"
460 GST_PTR_FORMAT, incaps, outcaps);
462 structure = gst_caps_get_structure (incaps, 0);
464 if (g_str_equal (gst_structure_get_name (structure), "audio/x-raw-float"))
469 ret = gst_structure_get_int (structure, "rate", &myinrate);
470 ret &= gst_structure_get_int (structure, "channels", &mychannels);
471 ret &= gst_structure_get_int (structure, "width", &mywidth);
472 if (G_UNLIKELY (!ret))
473 goto no_in_rate_channels;
475 structure = gst_caps_get_structure (outcaps, 0);
476 ret = gst_structure_get_int (structure, "rate", &myoutrate);
477 if (G_UNLIKELY (!ret))
481 *channels = mychannels;
485 *outrate = myoutrate;
496 GST_DEBUG ("could not get input rate and channels");
501 GST_DEBUG ("could not get output rate");
507 _gcd (gint a, gint b)
520 gst_audio_resample_transform_size (GstBaseTransform * base,
521 GstPadDirection direction, GstCaps * caps, guint size, GstCaps * othercaps,
525 guint32 ratio_den, ratio_num;
526 gint inrate, outrate, gcd;
527 gint bytes_per_samp, channels;
529 GST_LOG_OBJECT (base, "asked to transform size %d in direction %s",
530 size, direction == GST_PAD_SINK ? "SINK" : "SRC");
532 /* Get sample width -> bytes_per_samp, channels, inrate, outrate */
534 gst_audio_resample_parse_caps (caps, othercaps, &bytes_per_samp,
535 &channels, &inrate, &outrate, NULL);
536 if (G_UNLIKELY (!ret)) {
537 GST_ERROR_OBJECT (base, "Wrong caps");
540 /* Number of samples in either buffer is size / (width*channels) ->
541 * calculate the factor */
542 bytes_per_samp = bytes_per_samp * channels / 8;
543 /* Convert source buffer size to samples */
544 size /= bytes_per_samp;
546 /* Simplify the conversion ratio factors */
547 gcd = _gcd (inrate, outrate);
548 ratio_num = inrate / gcd;
549 ratio_den = outrate / gcd;
551 if (direction == GST_PAD_SINK) {
552 /* asked to convert size of an incoming buffer. Round up the output size */
553 *othersize = (size * ratio_den + ratio_num - 1) / ratio_num;
554 *othersize *= bytes_per_samp;
556 /* asked to convert size of an outgoing buffer. Round down the input size */
557 *othersize = (size * ratio_num) / ratio_den;
558 *othersize *= bytes_per_samp;
561 GST_LOG_OBJECT (base, "transformed size %d to %d", size * bytes_per_samp,
568 gst_audio_resample_set_caps (GstBaseTransform * base, GstCaps * incaps,
572 gint width = 0, inrate = 0, outrate = 0, channels = 0;
574 GstAudioResample *resample = GST_AUDIO_RESAMPLE (base);
576 GST_LOG ("incaps %" GST_PTR_FORMAT ", outcaps %"
577 GST_PTR_FORMAT, incaps, outcaps);
579 ret = gst_audio_resample_parse_caps (incaps, outcaps,
580 &width, &channels, &inrate, &outrate, &fp);
582 if (G_UNLIKELY (!ret))
586 gst_audio_resample_update_state (resample, width, channels, inrate,
587 outrate, resample->quality, fp);
589 if (G_UNLIKELY (!ret))
592 /* save caps so we can short-circuit in the size_transform if the caps
594 gst_caps_replace (&resample->sinkcaps, incaps);
595 gst_caps_replace (&resample->srccaps, outcaps);
600 #define GST_MAXINT24 (8388607)
601 #define GST_MININT24 (-8388608)
603 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
604 #define GST_READ_UINT24 GST_READ_UINT24_LE
605 #define GST_WRITE_UINT24 GST_WRITE_UINT24_LE
607 #define GST_READ_UINT24 GST_READ_UINT24_BE
608 #define GST_WRITE_UINT24 GST_WRITE_UINT24_BE
612 gst_audio_resample_convert_buffer (GstAudioResample * resample,
613 const guint8 * in, guint8 * out, guint len, gboolean inverse)
615 len *= resample->channels;
618 if (gst_audio_resample_use_int && resample->width == 8 && !resample->fp) {
619 gint8 *o = (gint8 *) out;
620 gint16 *i = (gint16 *) in;
624 tmp = *i + (G_MAXINT8 >> 1);
625 *o = CLAMP (tmp >> 8, G_MININT8, G_MAXINT8);
630 } else if (!gst_audio_resample_use_int && resample->width == 8
632 gint8 *o = (gint8 *) out;
633 gfloat *i = (gfloat *) in;
638 *o = (gint8) CLAMP (tmp * G_MAXINT8 + 0.5, G_MININT8, G_MAXINT8);
643 } else if (!gst_audio_resample_use_int && resample->width == 16
645 gint16 *o = (gint16 *) out;
646 gfloat *i = (gfloat *) in;
651 *o = (gint16) CLAMP (tmp * G_MAXINT16 + 0.5, G_MININT16, G_MAXINT16);
656 } else if (resample->width == 24 && !resample->fp) {
657 guint8 *o = (guint8 *) out;
658 gdouble *i = (gdouble *) in;
663 GST_WRITE_UINT24 (o, (gint32) CLAMP (tmp * GST_MAXINT24 + 0.5,
664 GST_MININT24, GST_MAXINT24));
669 } else if (resample->width == 32 && !resample->fp) {
670 gint32 *o = (gint32 *) out;
671 gdouble *i = (gdouble *) in;
676 *o = (gint32) CLAMP (tmp * G_MAXINT32 + 0.5, G_MININT32, G_MAXINT32);
682 g_assert_not_reached ();
685 if (gst_audio_resample_use_int && resample->width == 8 && !resample->fp) {
686 gint8 *i = (gint8 *) in;
687 gint16 *o = (gint16 *) out;
697 } else if (!gst_audio_resample_use_int && resample->width == 8
699 gint8 *i = (gint8 *) in;
700 gfloat *o = (gfloat *) out;
705 *o = tmp / G_MAXINT8;
710 } else if (!gst_audio_resample_use_int && resample->width == 16
712 gint16 *i = (gint16 *) in;
713 gfloat *o = (gfloat *) out;
718 *o = tmp / G_MAXINT16;
723 } else if (resample->width == 24 && !resample->fp) {
724 guint8 *i = (guint8 *) in;
725 gdouble *o = (gdouble *) out;
730 tmp2 = GST_READ_UINT24 (i);
731 if (tmp2 & 0x00800000)
734 *o = tmp / GST_MAXINT24;
739 } else if (resample->width == 32 && !resample->fp) {
740 gint32 *i = (gint32 *) in;
741 gdouble *o = (gdouble *) out;
746 *o = tmp / G_MAXINT32;
752 g_assert_not_reached ();
758 gst_audio_resample_push_drain (GstAudioResample * resample)
761 GstBaseTransform *trans = GST_BASE_TRANSFORM (resample);
764 guint out_len, out_processed;
767 guint8 *outtmp = NULL;
768 gboolean need_convert = FALSE;
770 if (!resample->state)
773 /* Don't drain samples if we were resetted. */
774 if (resample->next_ts == -1)
777 need_convert = (resample->funcs->width != resample->width);
779 resample->funcs->get_ratio (resample->state, &num, &den);
781 out_len = resample->funcs->get_input_latency (resample->state);
782 out_len = out_processed = (out_len * den + num - 1) / num;
783 outsize = (resample->width / 8) * out_len * resample->channels;
787 (resample->funcs->width / 8) * out_len * resample->channels;
788 if (outsize_tmp <= resample->tmp_out_size) {
789 outtmp = resample->tmp_out;
791 resample->tmp_out_size = outsize_tmp;
792 resample->tmp_out = outtmp = g_realloc (resample->tmp_out, outsize_tmp);
797 gst_pad_alloc_buffer_and_set_caps (trans->srcpad, GST_BUFFER_OFFSET_NONE,
798 outsize, GST_PAD_CAPS (trans->srcpad), &buf);
800 if (G_UNLIKELY (res != GST_FLOW_OK)) {
801 GST_WARNING_OBJECT (resample, "failed allocating buffer of %d bytes",
806 len = resample->funcs->get_input_latency (resample->state);
809 resample->funcs->process (resample->state,
810 NULL, &len, (need_convert) ? outtmp : GST_BUFFER_DATA (buf),
813 if (G_UNLIKELY (err != RESAMPLER_ERR_SUCCESS)) {
814 GST_WARNING_OBJECT (resample, "Failed to process drain: %s",
815 resample->funcs->strerror (err));
816 gst_buffer_unref (buf);
820 if (G_UNLIKELY (out_processed == 0)) {
821 GST_WARNING_OBJECT (resample, "Failed to get drain, dropping buffer");
822 gst_buffer_unref (buf);
826 /* If we wrote more than allocated something is really wrong now
827 * and we should better abort immediately */
828 g_assert (out_len >= out_processed);
831 gst_audio_resample_convert_buffer (resample, outtmp, GST_BUFFER_DATA (buf),
832 out_processed, TRUE);
834 GST_BUFFER_DURATION (buf) =
835 GST_FRAMES_TO_CLOCK_TIME (out_processed, resample->outrate);
836 GST_BUFFER_SIZE (buf) =
837 out_processed * resample->channels * (resample->width / 8);
839 if (GST_CLOCK_TIME_IS_VALID (resample->next_ts)) {
840 GST_BUFFER_TIMESTAMP (buf) = resample->next_ts;
841 resample->next_ts += GST_BUFFER_DURATION (buf);
844 if (resample->next_offset != -1) {
845 GST_BUFFER_OFFSET (buf) = resample->next_offset;
846 GST_BUFFER_OFFSET_END (buf) = resample->next_offset + out_processed;
847 resample->next_offset += out_processed;
849 /* should be impossible, but just incase */
850 GST_BUFFER_OFFSET (buf) = GST_BUFFER_OFFSET_NONE;
851 GST_BUFFER_OFFSET_END (buf) = GST_BUFFER_OFFSET_NONE;
854 GST_LOG_OBJECT (resample,
855 "Pushing drain buffer of %u bytes with timestamp %" GST_TIME_FORMAT
856 " duration %" GST_TIME_FORMAT " offset %" G_GUINT64_FORMAT " offset_end %"
857 G_GUINT64_FORMAT, GST_BUFFER_SIZE (buf),
858 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
859 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)), GST_BUFFER_OFFSET (buf),
860 GST_BUFFER_OFFSET_END (buf));
862 res = gst_pad_push (trans->srcpad, buf);
864 if (G_UNLIKELY (res != GST_FLOW_OK))
865 GST_WARNING_OBJECT (resample, "Failed to push drain: %s",
866 gst_flow_get_name (res));
872 gst_audio_resample_event (GstBaseTransform * base, GstEvent * event)
874 GstAudioResample *resample = GST_AUDIO_RESAMPLE (base);
876 switch (GST_EVENT_TYPE (event)) {
877 case GST_EVENT_FLUSH_STOP:
878 gst_audio_resample_reset_state (resample);
879 resample->next_offset = -1;
880 resample->next_ts = -1;
881 resample->next_upstream_ts = -1;
883 case GST_EVENT_NEWSEGMENT:
884 gst_audio_resample_push_drain (resample);
885 gst_audio_resample_reset_state (resample);
886 resample->next_offset = -1;
887 resample->next_ts = -1;
888 resample->next_upstream_ts = -1;
891 gst_audio_resample_push_drain (resample);
892 gst_audio_resample_reset_state (resample);
898 return parent_class->event (base, event);
902 gst_audio_resample_check_discont (GstAudioResample * resample,
903 GstClockTime timestamp)
905 if (timestamp != GST_CLOCK_TIME_NONE &&
906 resample->next_upstream_ts != GST_CLOCK_TIME_NONE &&
907 timestamp != resample->next_upstream_ts) {
908 /* Potentially a discontinuous buffer. However, it turns out that many
909 * elements generate imperfect streams due to rounding errors, so we permit
910 * a small error (up to one sample) without triggering a filter
911 * flush/restart (if triggered incorrectly, this will be audible) */
912 GstClockTimeDiff diff = timestamp - resample->next_upstream_ts;
914 if (ABS (diff) > (GST_SECOND + resample->inrate - 1) / resample->inrate) {
915 GST_WARNING_OBJECT (resample,
916 "encountered timestamp discontinuity of %s%" GST_TIME_FORMAT,
917 (diff < 0) ? "-" : "", GST_TIME_ARGS ((GstClockTime) ABS (diff)));
926 gst_audio_resample_process (GstAudioResample * resample, GstBuffer * inbuf,
929 guint32 in_len, in_processed;
930 guint32 out_len, out_processed;
931 gint err = RESAMPLER_ERR_SUCCESS;
932 guint8 *in_tmp = NULL, *out_tmp = NULL;
933 gboolean need_convert = (resample->funcs->width != resample->width);
935 in_len = GST_BUFFER_SIZE (inbuf) / resample->channels;
936 out_len = GST_BUFFER_SIZE (outbuf) / resample->channels;
938 in_len /= (resample->width / 8);
939 out_len /= (resample->width / 8);
941 in_processed = in_len;
942 out_processed = out_len;
946 in_len * resample->channels * (resample->funcs->width / 8);
948 out_len * resample->channels * (resample->funcs->width / 8);
950 if (in_size_tmp <= resample->tmp_in_size) {
951 in_tmp = resample->tmp_in;
953 resample->tmp_in = in_tmp = g_realloc (resample->tmp_in, in_size_tmp);
954 resample->tmp_in_size = in_size_tmp;
957 gst_audio_resample_convert_buffer (resample, GST_BUFFER_DATA (inbuf),
958 in_tmp, in_len, FALSE);
960 if (out_size_tmp <= resample->tmp_out_size) {
961 out_tmp = resample->tmp_out;
963 resample->tmp_out = out_tmp = g_realloc (resample->tmp_out, out_size_tmp);
964 resample->tmp_out_size = out_size_tmp;
969 err = resample->funcs->process (resample->state,
970 in_tmp, &in_processed, out_tmp, &out_processed);
972 err = resample->funcs->process (resample->state,
973 (const guint8 *) GST_BUFFER_DATA (inbuf), &in_processed,
974 (guint8 *) GST_BUFFER_DATA (outbuf), &out_processed);
977 if (G_UNLIKELY (in_len != in_processed))
978 GST_WARNING_OBJECT (resample, "Converted %d of %d input samples",
979 in_processed, in_len);
981 if (out_len != out_processed) {
982 if (out_processed == 0) {
983 GST_DEBUG_OBJECT (resample, "Converted to 0 samples, buffer dropped");
985 return GST_BASE_TRANSFORM_FLOW_DROPPED;
988 /* If we wrote more than allocated something is really wrong now
989 * and we should better abort immediately */
990 g_assert (out_len >= out_processed);
993 if (G_UNLIKELY (err != RESAMPLER_ERR_SUCCESS)) {
994 GST_ERROR_OBJECT (resample, "Failed to convert data: %s",
995 resample->funcs->strerror (err));
996 return GST_FLOW_ERROR;
1000 gst_audio_resample_convert_buffer (resample, out_tmp,
1001 GST_BUFFER_DATA (outbuf), out_processed, TRUE);
1003 GST_BUFFER_DURATION (outbuf) =
1004 GST_FRAMES_TO_CLOCK_TIME (out_processed, resample->outrate);
1005 GST_BUFFER_SIZE (outbuf) =
1006 out_processed * resample->channels * (resample->width / 8);
1008 if (GST_CLOCK_TIME_IS_VALID (resample->next_ts)) {
1009 GST_BUFFER_TIMESTAMP (outbuf) = resample->next_ts;
1010 resample->next_ts += GST_BUFFER_DURATION (outbuf);
1013 if (resample->next_offset != -1) {
1014 GST_BUFFER_OFFSET (outbuf) = resample->next_offset;
1015 GST_BUFFER_OFFSET_END (outbuf) = resample->next_offset + out_processed;
1016 resample->next_offset += out_processed;
1018 /* should be impossible, but just incase */
1019 GST_BUFFER_OFFSET (outbuf) = GST_BUFFER_OFFSET_NONE;
1020 GST_BUFFER_OFFSET_END (outbuf) = GST_BUFFER_OFFSET_NONE;
1023 GST_LOG_OBJECT (resample,
1024 "Converted to buffer of %u bytes with timestamp %" GST_TIME_FORMAT
1025 ", duration %" GST_TIME_FORMAT ", offset %" G_GUINT64_FORMAT
1026 ", offset_end %" G_GUINT64_FORMAT, GST_BUFFER_SIZE (outbuf),
1027 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
1028 GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)),
1029 GST_BUFFER_OFFSET (outbuf), GST_BUFFER_OFFSET_END (outbuf));
1035 static GstFlowReturn
1036 gst_audio_resample_transform (GstBaseTransform * base, GstBuffer * inbuf,
1039 GstAudioResample *resample = GST_AUDIO_RESAMPLE (base);
1041 GstClockTime timestamp;
1045 if (resample->state == NULL) {
1046 if (G_UNLIKELY (!(resample->state =
1047 gst_audio_resample_init_state (resample, resample->width,
1048 resample->channels, resample->inrate, resample->outrate,
1049 resample->quality, resample->fp))))
1050 return GST_FLOW_ERROR;
1053 gst_audio_resample_get_funcs (resample->width, resample->fp);
1056 size = GST_BUFFER_SIZE (inbuf);
1057 timestamp = GST_BUFFER_TIMESTAMP (inbuf);
1059 GST_LOG_OBJECT (resample, "transforming buffer of %ld bytes, ts %"
1060 GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT ", offset %"
1061 G_GINT64_FORMAT ", offset_end %" G_GINT64_FORMAT,
1062 size, GST_TIME_ARGS (timestamp),
1063 GST_TIME_ARGS (GST_BUFFER_DURATION (inbuf)),
1064 GST_BUFFER_OFFSET (inbuf), GST_BUFFER_OFFSET_END (inbuf));
1066 /* check for timestamp discontinuities and flush/reset if needed */
1067 if (G_UNLIKELY (gst_audio_resample_check_discont (resample, timestamp)
1068 || GST_BUFFER_IS_DISCONT (inbuf))) {
1069 /* Flush internal samples */
1070 gst_audio_resample_reset_state (resample);
1071 /* Inform downstream element about discontinuity */
1072 resample->need_discont = TRUE;
1073 /* We want to recalculate the timestamps */
1074 resample->next_ts = -1;
1075 resample->next_upstream_ts = -1;
1076 resample->next_offset = -1;
1079 insamples = GST_BUFFER_SIZE (inbuf) / resample->channels;
1080 insamples /= (resample->width / 8);
1082 if (GST_CLOCK_TIME_IS_VALID (timestamp)
1083 && !GST_CLOCK_TIME_IS_VALID (resample->next_ts)) {
1084 resample->next_ts = timestamp;
1087 if (resample->next_offset == -1) {
1088 if (GST_BUFFER_OFFSET_IS_VALID (inbuf)) {
1089 resample->next_offset =
1090 gst_util_uint64_scale_int (GST_BUFFER_OFFSET (inbuf),
1091 resample->outrate, resample->inrate);
1092 } else if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
1093 resample->next_offset =
1094 GST_CLOCK_TIME_TO_FRAMES (timestamp, resample->outrate);
1096 resample->next_offset = 0;
1100 if (G_UNLIKELY (resample->need_discont)) {
1101 GST_DEBUG_OBJECT (resample, "marking this buffer with the DISCONT flag");
1102 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
1103 resample->need_discont = FALSE;
1106 ret = gst_audio_resample_process (resample, inbuf, outbuf);
1107 if (G_UNLIKELY (ret != GST_FLOW_OK))
1110 if (GST_CLOCK_TIME_IS_VALID (timestamp)
1111 && !GST_CLOCK_TIME_IS_VALID (resample->next_upstream_ts))
1112 resample->next_upstream_ts = timestamp;
1114 if (GST_CLOCK_TIME_IS_VALID (resample->next_upstream_ts))
1115 resample->next_upstream_ts +=
1116 GST_FRAMES_TO_CLOCK_TIME (insamples, resample->inrate);
1122 gst_audio_resample_query (GstPad * pad, GstQuery * query)
1124 GstAudioResample *resample = GST_AUDIO_RESAMPLE (gst_pad_get_parent (pad));
1125 GstBaseTransform *trans = GST_BASE_TRANSFORM (resample);
1126 gboolean res = TRUE;
1128 switch (GST_QUERY_TYPE (query)) {
1129 case GST_QUERY_LATENCY:
1131 GstClockTime min, max;
1135 gint rate = resample->inrate;
1136 gint resampler_latency;
1138 if (resample->state)
1140 resample->funcs->get_input_latency (resample->state);
1142 resampler_latency = 0;
1144 if (gst_base_transform_is_passthrough (trans))
1145 resampler_latency = 0;
1147 if ((peer = gst_pad_get_peer (trans->sinkpad))) {
1148 if ((res = gst_pad_query (peer, query))) {
1149 gst_query_parse_latency (query, &live, &min, &max);
1151 GST_DEBUG_OBJECT (resample, "Peer latency: min %"
1152 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
1153 GST_TIME_ARGS (min), GST_TIME_ARGS (max));
1155 /* add our own latency */
1156 if (rate != 0 && resampler_latency != 0)
1158 gst_util_uint64_scale (resampler_latency, GST_SECOND, rate);
1162 GST_DEBUG_OBJECT (resample, "Our latency: %" GST_TIME_FORMAT,
1163 GST_TIME_ARGS (latency));
1166 if (max != GST_CLOCK_TIME_NONE)
1169 GST_DEBUG_OBJECT (resample, "Calculated total latency : min %"
1170 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
1171 GST_TIME_ARGS (min), GST_TIME_ARGS (max));
1173 gst_query_set_latency (query, live, min, max);
1175 gst_object_unref (peer);
1180 res = gst_pad_query_default (pad, query);
1183 gst_object_unref (resample);
1187 static const GstQueryType *
1188 gst_audio_resample_query_type (GstPad * pad)
1190 static const GstQueryType types[] = {
1199 gst_audio_resample_set_property (GObject * object, guint prop_id,
1200 const GValue * value, GParamSpec * pspec)
1202 GstAudioResample *resample;
1204 resample = GST_AUDIO_RESAMPLE (object);
1208 GST_BASE_TRANSFORM_LOCK (resample);
1209 resample->quality = g_value_get_int (value);
1210 GST_DEBUG_OBJECT (resample, "new quality %d", resample->quality);
1212 gst_audio_resample_update_state (resample, resample->width,
1213 resample->channels, resample->inrate, resample->outrate,
1214 resample->quality, resample->fp);
1215 GST_BASE_TRANSFORM_UNLOCK (resample);
1217 case PROP_FILTER_LENGTH:{
1218 gint filter_length = g_value_get_int (value);
1220 GST_BASE_TRANSFORM_LOCK (resample);
1221 if (filter_length <= 8)
1222 resample->quality = 0;
1223 else if (filter_length <= 16)
1224 resample->quality = 1;
1225 else if (filter_length <= 32)
1226 resample->quality = 2;
1227 else if (filter_length <= 48)
1228 resample->quality = 3;
1229 else if (filter_length <= 64)
1230 resample->quality = 4;
1231 else if (filter_length <= 80)
1232 resample->quality = 5;
1233 else if (filter_length <= 96)
1234 resample->quality = 6;
1235 else if (filter_length <= 128)
1236 resample->quality = 7;
1237 else if (filter_length <= 160)
1238 resample->quality = 8;
1239 else if (filter_length <= 192)
1240 resample->quality = 9;
1242 resample->quality = 10;
1244 GST_DEBUG_OBJECT (resample, "new quality %d", resample->quality);
1246 gst_audio_resample_update_state (resample, resample->width,
1247 resample->channels, resample->inrate, resample->outrate,
1248 resample->quality, resample->fp);
1249 GST_BASE_TRANSFORM_UNLOCK (resample);
1253 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1259 gst_audio_resample_get_property (GObject * object, guint prop_id,
1260 GValue * value, GParamSpec * pspec)
1262 GstAudioResample *resample;
1264 resample = GST_AUDIO_RESAMPLE (object);
1268 g_value_set_int (value, resample->quality);
1270 case PROP_FILTER_LENGTH:
1271 switch (resample->quality) {
1273 g_value_set_int (value, 8);
1276 g_value_set_int (value, 16);
1279 g_value_set_int (value, 32);
1282 g_value_set_int (value, 48);
1285 g_value_set_int (value, 64);
1288 g_value_set_int (value, 80);
1291 g_value_set_int (value, 96);
1294 g_value_set_int (value, 128);
1297 g_value_set_int (value, 160);
1300 g_value_set_int (value, 192);
1303 g_value_set_int (value, 256);
1308 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1313 #if defined AUDIORESAMPLE_FORMAT_AUTO
1314 #define BENCHMARK_SIZE 512
1317 _benchmark_int_float (SpeexResamplerState * st)
1319 gint16 in[BENCHMARK_SIZE] = { 0, }, out[BENCHMARK_SIZE / 2];
1320 gfloat in_tmp[BENCHMARK_SIZE], out_tmp[BENCHMARK_SIZE / 2];
1322 guint32 inlen = BENCHMARK_SIZE, outlen = BENCHMARK_SIZE / 2;
1324 for (i = 0; i < BENCHMARK_SIZE; i++) {
1326 in_tmp[i] = tmp / G_MAXINT16;
1329 resample_float_resampler_process_interleaved_float (st,
1330 (const guint8 *) in_tmp, &inlen, (guint8 *) out_tmp, &outlen);
1333 GST_ERROR ("Failed to use float resampler");
1337 for (i = 0; i < outlen; i++) {
1338 gfloat tmp = out_tmp[i];
1339 out[i] = CLAMP (tmp * G_MAXINT16 + 0.5, G_MININT16, G_MAXINT16);
1346 _benchmark_int_int (SpeexResamplerState * st)
1348 gint16 in[BENCHMARK_SIZE] = { 0, }, out[BENCHMARK_SIZE / 2];
1349 guint32 inlen = BENCHMARK_SIZE, outlen = BENCHMARK_SIZE / 2;
1351 resample_int_resampler_process_interleaved_int (st, (const guint8 *) in,
1352 &inlen, (guint8 *) out, &outlen);
1355 GST_ERROR ("Failed to use int resampler");
1363 _benchmark_integer_resampling (void)
1367 SpeexResamplerState *sta, *stb;
1370 oil_profile_init (&a);
1371 oil_profile_init (&b);
1373 sta = resample_float_resampler_init (1, 48000, 24000, 4, NULL);
1375 GST_ERROR ("Failed to create float resampler state");
1379 stb = resample_int_resampler_init (1, 48000, 24000, 4, NULL);
1381 resample_float_resampler_destroy (sta);
1382 GST_ERROR ("Failed to create int resampler state");
1387 for (i = 0; i < 10; i++) {
1388 oil_profile_start (&a);
1389 if (!_benchmark_int_float (sta))
1391 oil_profile_stop (&a);
1395 for (i = 0; i < 10; i++) {
1396 oil_profile_start (&b);
1397 if (!_benchmark_int_int (stb))
1399 oil_profile_stop (&b);
1402 /* Handle results */
1403 oil_profile_get_ave_std (&a, &av, NULL);
1404 oil_profile_get_ave_std (&b, &bv, NULL);
1406 /* Remember benchmark result in global variable */
1407 gst_audio_resample_use_int = (av > bv);
1408 resample_float_resampler_destroy (sta);
1409 resample_int_resampler_destroy (stb);
1412 GST_INFO ("Using integer resampler if appropiate: %lf < %lf", bv, av);
1414 GST_INFO ("Using float resampler for everything: %lf <= %lf", av, bv);
1419 resample_float_resampler_destroy (sta);
1420 resample_int_resampler_destroy (stb);
1427 plugin_init (GstPlugin * plugin)
1429 GST_DEBUG_CATEGORY_INIT (audio_resample_debug, "audioresample", 0,
1430 "audio resampling element");
1431 #if defined AUDIORESAMPLE_FORMAT_AUTO
1434 if (!_benchmark_integer_resampling ())
1438 if (!gst_element_register (plugin, "audioresample", GST_RANK_PRIMARY,
1439 GST_TYPE_AUDIO_RESAMPLE)) {
1446 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1449 "Resamples audio", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,
1450 GST_PACKAGE_ORIGIN);