gst: Remove dead assignments and resulting unused variables
[platform/upstream/gstreamer.git] / gst / audioresample / gstaudioresample.c
1 /* GStreamer
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>
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 /**
23  * SECTION:element-audioresample
24  *
25  * audioresample resamples raw audio buffers to different sample rates using
26  * a configurable windowing function to enhance quality.
27  *
28  * <refsect2>
29  * <title>Example launch line</title>
30  * |[
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.
34  * </refsect2>
35  */
36
37 /* TODO:
38  *  - Enable SSE/ARM optimizations and select at runtime
39  */
40
41 #ifdef HAVE_CONFIG_H
42 #include "config.h"
43 #endif
44
45 #include <string.h>
46 #include <math.h>
47
48 #include "gstaudioresample.h"
49 #include <gst/audio/audio.h>
50 #include <gst/base/gstbasetransform.h>
51
52 #if defined AUDIORESAMPLE_FORMAT_AUTO
53 #define OIL_ENABLE_UNSTABLE_API
54 #include <liboil/liboilprofile.h>
55 #include <liboil/liboil.h>
56 #endif
57
58 GST_DEBUG_CATEGORY (audio_resample_debug);
59 #define GST_CAT_DEFAULT audio_resample_debug
60
61 enum
62 {
63   PROP_0,
64   PROP_QUALITY,
65   PROP_FILTER_LENGTH
66 };
67
68 #define SUPPORTED_CAPS \
69 GST_STATIC_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 }; " \
75     "audio/x-raw-int, " \
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; " \
82     "audio/x-raw-int, " \
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; " \
89     "audio/x-raw-int, " \
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; " \
96     "audio/x-raw-int, " \
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" \
103 )
104
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;
110 #else
111 static gboolean gst_audio_resample_use_int = FALSE;
112 #endif
113
114 static GstStaticPadTemplate gst_audio_resample_sink_template =
115 GST_STATIC_PAD_TEMPLATE ("sink",
116     GST_PAD_SINK, GST_PAD_ALWAYS, SUPPORTED_CAPS);
117
118 static GstStaticPadTemplate gst_audio_resample_src_template =
119 GST_STATIC_PAD_TEMPLATE ("src",
120     GST_PAD_SRC, GST_PAD_ALWAYS, SUPPORTED_CAPS);
121
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);
126
127 /* vmethods */
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,
142     GstEvent * event);
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);
147
148 GST_BOILERPLATE (GstAudioResample, gst_audio_resample, GstBaseTransform,
149     GST_TYPE_BASE_TRANSFORM);
150
151 static void
152 gst_audio_resample_base_init (gpointer g_class)
153 {
154   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
155
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));
160
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>");
164 }
165
166 static void
167 gst_audio_resample_class_init (GstAudioResampleClass * klass)
168 {
169   GObjectClass *gobject_class = (GObjectClass *) klass;
170
171   gobject_class->set_property = gst_audio_resample_set_property;
172   gobject_class->get_property = gst_audio_resample_get_property;
173
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));
180
181   /* FIXME 0.11: Remove this property, it's just for compatibility
182    * with old audioresample
183    */
184   /**
185    * GstAudioResample:filter-length:
186    *
187    * Length of the resample filter
188    *
189    * Deprectated: Use #GstAudioResample:quality property instead
190    */
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));
194
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);
213
214   GST_BASE_TRANSFORM_CLASS (klass)->passthrough_on_same_caps = TRUE;
215 }
216
217 static void
218 gst_audio_resample_init (GstAudioResample * resample,
219     GstAudioResampleClass * klass)
220 {
221   GstBaseTransform *trans = GST_BASE_TRANSFORM (resample);
222
223   resample->quality = SPEEX_RESAMPLER_QUALITY_DEFAULT;
224
225   resample->need_discont = FALSE;
226
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);
230 }
231
232 /* vmethods */
233 static gboolean
234 gst_audio_resample_start (GstBaseTransform * base)
235 {
236   GstAudioResample *resample = GST_AUDIO_RESAMPLE (base);
237
238   resample->next_offset = -1;
239   resample->next_ts = -1;
240   resample->next_upstream_ts = -1;
241
242   return TRUE;
243 }
244
245 static gboolean
246 gst_audio_resample_stop (GstBaseTransform * base)
247 {
248   GstAudioResample *resample = GST_AUDIO_RESAMPLE (base);
249
250   if (resample->state) {
251     resample->funcs->destroy (resample->state);
252     resample->state = NULL;
253   }
254
255   resample->funcs = NULL;
256
257   g_free (resample->tmp_in);
258   resample->tmp_in = NULL;
259   resample->tmp_in_size = 0;
260
261   g_free (resample->tmp_out);
262   resample->tmp_out = NULL;
263   resample->tmp_out_size = 0;
264
265   gst_caps_replace (&resample->sinkcaps, NULL);
266   gst_caps_replace (&resample->srccaps, NULL);
267
268   return TRUE;
269 }
270
271 static gboolean
272 gst_audio_resample_get_unit_size (GstBaseTransform * base, GstCaps * caps,
273     guint * size)
274 {
275   gint width, channels;
276   GstStructure *structure;
277   gboolean ret;
278
279   g_return_val_if_fail (size != NULL, FALSE);
280
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);
285
286   if (G_UNLIKELY (!ret))
287     return FALSE;
288
289   *size = (width / 8) * channels;
290
291   return TRUE;
292 }
293
294 static GstCaps *
295 gst_audio_resample_transform_caps (GstBaseTransform * base,
296     GstPadDirection direction, GstCaps * caps)
297 {
298   const GValue *val;
299   GstStructure *s;
300   GstCaps *res;
301
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);
306
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);
320   } else {
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);
325   }
326
327   return res;
328 }
329
330 /* Fixate rate to the allowed rate that has the smallest difference */
331 static void
332 gst_audio_resample_fixate_caps (GstBaseTransform * base,
333     GstPadDirection direction, GstCaps * caps, GstCaps * othercaps)
334 {
335   GstStructure *s;
336   gint rate;
337
338   s = gst_caps_get_structure (caps, 0);
339   if (G_UNLIKELY (!gst_structure_get_int (s, "rate", &rate)))
340     return;
341
342   s = gst_caps_get_structure (othercaps, 0);
343   gst_structure_fixate_field_nearest_int (s, "rate", rate);
344 }
345
346 static const SpeexResampleFuncs *
347 gst_audio_resample_get_funcs (gint width, gboolean fp)
348 {
349   const SpeexResampleFuncs *funcs = NULL;
350
351   if (gst_audio_resample_use_int && (width == 8 || width == 16) && !fp)
352     funcs = &int_funcs;
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;
358   else
359     g_assert_not_reached ();
360
361   return funcs;
362 }
363
364 static SpeexResamplerState *
365 gst_audio_resample_init_state (GstAudioResample * resample, gint width,
366     gint channels, gint inrate, gint outrate, gint quality, gboolean fp)
367 {
368   SpeexResamplerState *ret = NULL;
369   gint err = RESAMPLER_ERR_SUCCESS;
370   const SpeexResampleFuncs *funcs = gst_audio_resample_get_funcs (width, fp);
371
372   ret = funcs->init (channels, inrate, outrate, quality, &err);
373
374   if (G_UNLIKELY (err != RESAMPLER_ERR_SUCCESS)) {
375     GST_ERROR_OBJECT (resample, "Failed to create resampler state: %s",
376         funcs->strerror (err));
377     return NULL;
378   }
379
380   funcs->skip_zeros (ret);
381
382   return ret;
383 }
384
385 static gboolean
386 gst_audio_resample_update_state (GstAudioResample * resample, gint width,
387     gint channels, gint inrate, gint outrate, gint quality, gboolean fp)
388 {
389   gboolean ret = TRUE;
390   gboolean updated_latency = FALSE;
391
392   updated_latency = (resample->inrate != inrate
393       || quality != resample->quality) && resample->state != NULL;
394
395   if (resample->state == NULL) {
396     ret = TRUE;
397   } else if (resample->channels != channels || fp != resample->fp
398       || width != resample->width) {
399     resample->funcs->destroy (resample->state);
400     resample->state =
401         gst_audio_resample_init_state (resample, width, channels, inrate,
402         outrate, quality, fp);
403
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;
408
409     err = resample->funcs->set_rate (resample->state, inrate, outrate);
410
411     if (G_UNLIKELY (err != RESAMPLER_ERR_SUCCESS))
412       GST_ERROR_OBJECT (resample, "Failed to update rate: %s",
413           resample->funcs->strerror (err));
414
415     ret = (err == RESAMPLER_ERR_SUCCESS);
416   } else if (quality != resample->quality) {
417     gint err = RESAMPLER_ERR_SUCCESS;
418
419     err = resample->funcs->set_quality (resample->state, quality);
420
421     if (G_UNLIKELY (err != RESAMPLER_ERR_SUCCESS))
422       GST_ERROR_OBJECT (resample, "Failed to update quality: %s",
423           resample->funcs->strerror (err));
424
425     ret = (err == RESAMPLER_ERR_SUCCESS);
426   }
427
428   resample->width = width;
429   resample->channels = channels;
430   resample->fp = fp;
431   resample->quality = quality;
432   resample->inrate = inrate;
433   resample->outrate = outrate;
434
435   if (updated_latency)
436     gst_element_post_message (GST_ELEMENT (resample),
437         gst_message_new_latency (GST_OBJECT (resample)));
438
439   return ret;
440 }
441
442 static void
443 gst_audio_resample_reset_state (GstAudioResample * resample)
444 {
445   if (resample->state)
446     resample->funcs->reset_mem (resample->state);
447 }
448
449 static gboolean
450 gst_audio_resample_parse_caps (GstCaps * incaps,
451     GstCaps * outcaps, gint * width, gint * channels, gint * inrate,
452     gint * outrate, gboolean * fp)
453 {
454   GstStructure *structure;
455   gboolean ret;
456   gint mywidth, myinrate, myoutrate, mychannels;
457   gboolean myfp;
458
459   GST_DEBUG ("incaps %" GST_PTR_FORMAT ", outcaps %"
460       GST_PTR_FORMAT, incaps, outcaps);
461
462   structure = gst_caps_get_structure (incaps, 0);
463
464   if (g_str_equal (gst_structure_get_name (structure), "audio/x-raw-float"))
465     myfp = TRUE;
466   else
467     myfp = FALSE;
468
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;
474
475   structure = gst_caps_get_structure (outcaps, 0);
476   ret = gst_structure_get_int (structure, "rate", &myoutrate);
477   if (G_UNLIKELY (!ret))
478     goto no_out_rate;
479
480   if (channels)
481     *channels = mychannels;
482   if (inrate)
483     *inrate = myinrate;
484   if (outrate)
485     *outrate = myoutrate;
486   if (width)
487     *width = mywidth;
488   if (fp)
489     *fp = myfp;
490
491   return TRUE;
492
493   /* ERRORS */
494 no_in_rate_channels:
495   {
496     GST_DEBUG ("could not get input rate and channels");
497     return FALSE;
498   }
499 no_out_rate:
500   {
501     GST_DEBUG ("could not get output rate");
502     return FALSE;
503   }
504 }
505
506 static gint
507 _gcd (gint a, gint b)
508 {
509   while (b != 0) {
510     int temp = a;
511
512     a = b;
513     b = temp % b;
514   }
515
516   return ABS (a);
517 }
518
519 static gboolean
520 gst_audio_resample_transform_size (GstBaseTransform * base,
521     GstPadDirection direction, GstCaps * caps, guint size, GstCaps * othercaps,
522     guint * othersize)
523 {
524   gboolean ret = TRUE;
525   guint32 ratio_den, ratio_num;
526   gint inrate, outrate, gcd;
527   gint bytes_per_samp, channels;
528
529   GST_LOG_OBJECT (base, "asked to transform size %d in direction %s",
530       size, direction == GST_PAD_SINK ? "SINK" : "SRC");
531
532   /* Get sample width -> bytes_per_samp, channels, inrate, outrate */
533   ret =
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");
538     return FALSE;
539   }
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;
545
546   /* Simplify the conversion ratio factors */
547   gcd = _gcd (inrate, outrate);
548   ratio_num = inrate / gcd;
549   ratio_den = outrate / gcd;
550
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;
555   } else {
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;
559   }
560
561   GST_LOG_OBJECT (base, "transformed size %d to %d", size * bytes_per_samp,
562       *othersize);
563
564   return ret;
565 }
566
567 static gboolean
568 gst_audio_resample_set_caps (GstBaseTransform * base, GstCaps * incaps,
569     GstCaps * outcaps)
570 {
571   gboolean ret;
572   gint width = 0, inrate = 0, outrate = 0, channels = 0;
573   gboolean fp;
574   GstAudioResample *resample = GST_AUDIO_RESAMPLE (base);
575
576   GST_LOG ("incaps %" GST_PTR_FORMAT ", outcaps %"
577       GST_PTR_FORMAT, incaps, outcaps);
578
579   ret = gst_audio_resample_parse_caps (incaps, outcaps,
580       &width, &channels, &inrate, &outrate, &fp);
581
582   if (G_UNLIKELY (!ret))
583     return FALSE;
584
585   ret =
586       gst_audio_resample_update_state (resample, width, channels, inrate,
587       outrate, resample->quality, fp);
588
589   if (G_UNLIKELY (!ret))
590     return FALSE;
591
592   /* save caps so we can short-circuit in the size_transform if the caps
593    * are the same */
594   gst_caps_replace (&resample->sinkcaps, incaps);
595   gst_caps_replace (&resample->srccaps, outcaps);
596
597   return TRUE;
598 }
599
600 #define GST_MAXINT24 (8388607)
601 #define GST_MININT24 (-8388608)
602
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
606 #else
607 #define GST_READ_UINT24 GST_READ_UINT24_BE
608 #define GST_WRITE_UINT24 GST_WRITE_UINT24_BE
609 #endif
610
611 static void
612 gst_audio_resample_convert_buffer (GstAudioResample * resample,
613     const guint8 * in, guint8 * out, guint len, gboolean inverse)
614 {
615   len *= resample->channels;
616
617   if (inverse) {
618     if (gst_audio_resample_use_int && resample->width == 8 && !resample->fp) {
619       gint8 *o = (gint8 *) out;
620       gint16 *i = (gint16 *) in;
621       gint32 tmp;
622
623       while (len) {
624         tmp = *i + (G_MAXINT8 >> 1);
625         *o = CLAMP (tmp >> 8, G_MININT8, G_MAXINT8);
626         o++;
627         i++;
628         len--;
629       }
630     } else if (!gst_audio_resample_use_int && resample->width == 8
631         && !resample->fp) {
632       gint8 *o = (gint8 *) out;
633       gfloat *i = (gfloat *) in;
634       gfloat tmp;
635
636       while (len) {
637         tmp = *i;
638         *o = (gint8) CLAMP (tmp * G_MAXINT8 + 0.5, G_MININT8, G_MAXINT8);
639         o++;
640         i++;
641         len--;
642       }
643     } else if (!gst_audio_resample_use_int && resample->width == 16
644         && !resample->fp) {
645       gint16 *o = (gint16 *) out;
646       gfloat *i = (gfloat *) in;
647       gfloat tmp;
648
649       while (len) {
650         tmp = *i;
651         *o = (gint16) CLAMP (tmp * G_MAXINT16 + 0.5, G_MININT16, G_MAXINT16);
652         o++;
653         i++;
654         len--;
655       }
656     } else if (resample->width == 24 && !resample->fp) {
657       guint8 *o = (guint8 *) out;
658       gdouble *i = (gdouble *) in;
659       gdouble tmp;
660
661       while (len) {
662         tmp = *i;
663         GST_WRITE_UINT24 (o, (gint32) CLAMP (tmp * GST_MAXINT24 + 0.5,
664                 GST_MININT24, GST_MAXINT24));
665         o += 3;
666         i++;
667         len--;
668       }
669     } else if (resample->width == 32 && !resample->fp) {
670       gint32 *o = (gint32 *) out;
671       gdouble *i = (gdouble *) in;
672       gdouble tmp;
673
674       while (len) {
675         tmp = *i;
676         *o = (gint32) CLAMP (tmp * G_MAXINT32 + 0.5, G_MININT32, G_MAXINT32);
677         o++;
678         i++;
679         len--;
680       }
681     } else {
682       g_assert_not_reached ();
683     }
684   } else {
685     if (gst_audio_resample_use_int && resample->width == 8 && !resample->fp) {
686       gint8 *i = (gint8 *) in;
687       gint16 *o = (gint16 *) out;
688       gint32 tmp;
689
690       while (len) {
691         tmp = *i;
692         *o = tmp << 8;
693         o++;
694         i++;
695         len--;
696       }
697     } else if (!gst_audio_resample_use_int && resample->width == 8
698         && !resample->fp) {
699       gint8 *i = (gint8 *) in;
700       gfloat *o = (gfloat *) out;
701       gfloat tmp;
702
703       while (len) {
704         tmp = *i;
705         *o = tmp / G_MAXINT8;
706         o++;
707         i++;
708         len--;
709       }
710     } else if (!gst_audio_resample_use_int && resample->width == 16
711         && !resample->fp) {
712       gint16 *i = (gint16 *) in;
713       gfloat *o = (gfloat *) out;
714       gfloat tmp;
715
716       while (len) {
717         tmp = *i;
718         *o = tmp / G_MAXINT16;
719         o++;
720         i++;
721         len--;
722       }
723     } else if (resample->width == 24 && !resample->fp) {
724       guint8 *i = (guint8 *) in;
725       gdouble *o = (gdouble *) out;
726       gdouble tmp;
727       guint32 tmp2;
728
729       while (len) {
730         tmp2 = GST_READ_UINT24 (i);
731         if (tmp2 & 0x00800000)
732           tmp2 |= 0xff000000;
733         tmp = (gint32) tmp2;
734         *o = tmp / GST_MAXINT24;
735         o++;
736         i += 3;
737         len--;
738       }
739     } else if (resample->width == 32 && !resample->fp) {
740       gint32 *i = (gint32 *) in;
741       gdouble *o = (gdouble *) out;
742       gdouble tmp;
743
744       while (len) {
745         tmp = *i;
746         *o = tmp / G_MAXINT32;
747         o++;
748         i++;
749         len--;
750       }
751     } else {
752       g_assert_not_reached ();
753     }
754   }
755 }
756
757 static void
758 gst_audio_resample_push_drain (GstAudioResample * resample)
759 {
760   GstBuffer *buf;
761   GstBaseTransform *trans = GST_BASE_TRANSFORM (resample);
762   GstFlowReturn res;
763   gint outsize;
764   guint out_len, out_processed;
765   gint err;
766   guint num, den, len;
767   guint8 *outtmp = NULL;
768   gboolean need_convert = FALSE;
769
770   if (!resample->state)
771     return;
772
773   /* Don't drain samples if we were resetted. */
774   if (resample->next_ts == -1)
775     return;
776
777   need_convert = (resample->funcs->width != resample->width);
778
779   resample->funcs->get_ratio (resample->state, &num, &den);
780
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;
784
785   if (need_convert) {
786     guint outsize_tmp =
787         (resample->funcs->width / 8) * out_len * resample->channels;
788     if (outsize_tmp <= resample->tmp_out_size) {
789       outtmp = resample->tmp_out;
790     } else {
791       resample->tmp_out_size = outsize_tmp;
792       resample->tmp_out = outtmp = g_realloc (resample->tmp_out, outsize_tmp);
793     }
794   }
795
796   res =
797       gst_pad_alloc_buffer_and_set_caps (trans->srcpad, GST_BUFFER_OFFSET_NONE,
798       outsize, GST_PAD_CAPS (trans->srcpad), &buf);
799
800   if (G_UNLIKELY (res != GST_FLOW_OK)) {
801     GST_WARNING_OBJECT (resample, "failed allocating buffer of %d bytes",
802         outsize);
803     return;
804   }
805
806   len = resample->funcs->get_input_latency (resample->state);
807
808   err =
809       resample->funcs->process (resample->state,
810       NULL, &len, (need_convert) ? outtmp : GST_BUFFER_DATA (buf),
811       &out_processed);
812
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);
817     return;
818   }
819
820   if (G_UNLIKELY (out_processed == 0)) {
821     GST_WARNING_OBJECT (resample, "Failed to get drain, dropping buffer");
822     gst_buffer_unref (buf);
823     return;
824   }
825
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);
829
830   if (need_convert)
831     gst_audio_resample_convert_buffer (resample, outtmp, GST_BUFFER_DATA (buf),
832         out_processed, TRUE);
833
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);
838
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);
842   }
843
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;
848   } else {
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;
852   }
853
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));
861
862   res = gst_pad_push (trans->srcpad, buf);
863
864   if (G_UNLIKELY (res != GST_FLOW_OK))
865     GST_WARNING_OBJECT (resample, "Failed to push drain: %s",
866         gst_flow_get_name (res));
867
868   return;
869 }
870
871 static gboolean
872 gst_audio_resample_event (GstBaseTransform * base, GstEvent * event)
873 {
874   GstAudioResample *resample = GST_AUDIO_RESAMPLE (base);
875
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;
882       break;
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;
889       break;
890     case GST_EVENT_EOS:
891       gst_audio_resample_push_drain (resample);
892       gst_audio_resample_reset_state (resample);
893       break;
894     default:
895       break;
896   }
897
898   return parent_class->event (base, event);
899 }
900
901 static gboolean
902 gst_audio_resample_check_discont (GstAudioResample * resample,
903     GstClockTime timestamp)
904 {
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;
913
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)));
918       return TRUE;
919     }
920   }
921
922   return FALSE;
923 }
924
925 static GstFlowReturn
926 gst_audio_resample_process (GstAudioResample * resample, GstBuffer * inbuf,
927     GstBuffer * outbuf)
928 {
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);
934
935   in_len = GST_BUFFER_SIZE (inbuf) / resample->channels;
936   out_len = GST_BUFFER_SIZE (outbuf) / resample->channels;
937
938   in_len /= (resample->width / 8);
939   out_len /= (resample->width / 8);
940
941   in_processed = in_len;
942   out_processed = out_len;
943
944   if (need_convert) {
945     guint in_size_tmp =
946         in_len * resample->channels * (resample->funcs->width / 8);
947     guint out_size_tmp =
948         out_len * resample->channels * (resample->funcs->width / 8);
949
950     if (in_size_tmp <= resample->tmp_in_size) {
951       in_tmp = resample->tmp_in;
952     } else {
953       resample->tmp_in = in_tmp = g_realloc (resample->tmp_in, in_size_tmp);
954       resample->tmp_in_size = in_size_tmp;
955     }
956
957     gst_audio_resample_convert_buffer (resample, GST_BUFFER_DATA (inbuf),
958         in_tmp, in_len, FALSE);
959
960     if (out_size_tmp <= resample->tmp_out_size) {
961       out_tmp = resample->tmp_out;
962     } else {
963       resample->tmp_out = out_tmp = g_realloc (resample->tmp_out, out_size_tmp);
964       resample->tmp_out_size = out_size_tmp;
965     }
966   }
967
968   if (need_convert) {
969     err = resample->funcs->process (resample->state,
970         in_tmp, &in_processed, out_tmp, &out_processed);
971   } else {
972     err = resample->funcs->process (resample->state,
973         (const guint8 *) GST_BUFFER_DATA (inbuf), &in_processed,
974         (guint8 *) GST_BUFFER_DATA (outbuf), &out_processed);
975   }
976
977   if (G_UNLIKELY (in_len != in_processed))
978     GST_WARNING_OBJECT (resample, "Converted %d of %d input samples",
979         in_processed, in_len);
980
981   if (out_len != out_processed) {
982     if (out_processed == 0) {
983       GST_DEBUG_OBJECT (resample, "Converted to 0 samples, buffer dropped");
984
985       return GST_BASE_TRANSFORM_FLOW_DROPPED;
986     }
987
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);
991   }
992
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;
997   } else {
998
999     if (need_convert)
1000       gst_audio_resample_convert_buffer (resample, out_tmp,
1001           GST_BUFFER_DATA (outbuf), out_processed, TRUE);
1002
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);
1007
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);
1011     }
1012
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;
1017     } else {
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;
1021     }
1022
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));
1030
1031     return GST_FLOW_OK;
1032   }
1033 }
1034
1035 static GstFlowReturn
1036 gst_audio_resample_transform (GstBaseTransform * base, GstBuffer * inbuf,
1037     GstBuffer * outbuf)
1038 {
1039   GstAudioResample *resample = GST_AUDIO_RESAMPLE (base);
1040   gulong size;
1041   GstClockTime timestamp;
1042   guint insamples;
1043   GstFlowReturn ret;
1044
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;
1051
1052     resample->funcs =
1053         gst_audio_resample_get_funcs (resample->width, resample->fp);
1054   }
1055
1056   size = GST_BUFFER_SIZE (inbuf);
1057   timestamp = GST_BUFFER_TIMESTAMP (inbuf);
1058
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));
1065
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;
1077   }
1078
1079   insamples = GST_BUFFER_SIZE (inbuf) / resample->channels;
1080   insamples /= (resample->width / 8);
1081
1082   if (GST_CLOCK_TIME_IS_VALID (timestamp)
1083       && !GST_CLOCK_TIME_IS_VALID (resample->next_ts)) {
1084     resample->next_ts = timestamp;
1085   }
1086
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);
1095     } else {
1096       resample->next_offset = 0;
1097     }
1098   }
1099
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;
1104   }
1105
1106   ret = gst_audio_resample_process (resample, inbuf, outbuf);
1107   if (G_UNLIKELY (ret != GST_FLOW_OK))
1108     return ret;
1109
1110   if (GST_CLOCK_TIME_IS_VALID (timestamp)
1111       && !GST_CLOCK_TIME_IS_VALID (resample->next_upstream_ts))
1112     resample->next_upstream_ts = timestamp;
1113
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);
1117
1118   return GST_FLOW_OK;
1119 }
1120
1121 static gboolean
1122 gst_audio_resample_query (GstPad * pad, GstQuery * query)
1123 {
1124   GstAudioResample *resample = GST_AUDIO_RESAMPLE (gst_pad_get_parent (pad));
1125   GstBaseTransform *trans = GST_BASE_TRANSFORM (resample);
1126   gboolean res = TRUE;
1127
1128   switch (GST_QUERY_TYPE (query)) {
1129     case GST_QUERY_LATENCY:
1130     {
1131       GstClockTime min, max;
1132       gboolean live;
1133       guint64 latency;
1134       GstPad *peer;
1135       gint rate = resample->inrate;
1136       gint resampler_latency;
1137
1138       if (resample->state)
1139         resampler_latency =
1140             resample->funcs->get_input_latency (resample->state);
1141       else
1142         resampler_latency = 0;
1143
1144       if (gst_base_transform_is_passthrough (trans))
1145         resampler_latency = 0;
1146
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);
1150
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));
1154
1155           /* add our own latency */
1156           if (rate != 0 && resampler_latency != 0)
1157             latency =
1158                 gst_util_uint64_scale (resampler_latency, GST_SECOND, rate);
1159           else
1160             latency = 0;
1161
1162           GST_DEBUG_OBJECT (resample, "Our latency: %" GST_TIME_FORMAT,
1163               GST_TIME_ARGS (latency));
1164
1165           min += latency;
1166           if (max != GST_CLOCK_TIME_NONE)
1167             max += latency;
1168
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));
1172
1173           gst_query_set_latency (query, live, min, max);
1174         }
1175         gst_object_unref (peer);
1176       }
1177       break;
1178     }
1179     default:
1180       res = gst_pad_query_default (pad, query);
1181       break;
1182   }
1183   gst_object_unref (resample);
1184   return res;
1185 }
1186
1187 static const GstQueryType *
1188 gst_audio_resample_query_type (GstPad * pad)
1189 {
1190   static const GstQueryType types[] = {
1191     GST_QUERY_LATENCY,
1192     0
1193   };
1194
1195   return types;
1196 }
1197
1198 static void
1199 gst_audio_resample_set_property (GObject * object, guint prop_id,
1200     const GValue * value, GParamSpec * pspec)
1201 {
1202   GstAudioResample *resample;
1203
1204   resample = GST_AUDIO_RESAMPLE (object);
1205
1206   switch (prop_id) {
1207     case PROP_QUALITY:
1208       GST_BASE_TRANSFORM_LOCK (resample);
1209       resample->quality = g_value_get_int (value);
1210       GST_DEBUG_OBJECT (resample, "new quality %d", resample->quality);
1211
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);
1216       break;
1217     case PROP_FILTER_LENGTH:{
1218       gint filter_length = g_value_get_int (value);
1219
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;
1241       else
1242         resample->quality = 10;
1243
1244       GST_DEBUG_OBJECT (resample, "new quality %d", resample->quality);
1245
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);
1250       break;
1251     }
1252     default:
1253       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1254       break;
1255   }
1256 }
1257
1258 static void
1259 gst_audio_resample_get_property (GObject * object, guint prop_id,
1260     GValue * value, GParamSpec * pspec)
1261 {
1262   GstAudioResample *resample;
1263
1264   resample = GST_AUDIO_RESAMPLE (object);
1265
1266   switch (prop_id) {
1267     case PROP_QUALITY:
1268       g_value_set_int (value, resample->quality);
1269       break;
1270     case PROP_FILTER_LENGTH:
1271       switch (resample->quality) {
1272         case 0:
1273           g_value_set_int (value, 8);
1274           break;
1275         case 1:
1276           g_value_set_int (value, 16);
1277           break;
1278         case 2:
1279           g_value_set_int (value, 32);
1280           break;
1281         case 3:
1282           g_value_set_int (value, 48);
1283           break;
1284         case 4:
1285           g_value_set_int (value, 64);
1286           break;
1287         case 5:
1288           g_value_set_int (value, 80);
1289           break;
1290         case 6:
1291           g_value_set_int (value, 96);
1292           break;
1293         case 7:
1294           g_value_set_int (value, 128);
1295           break;
1296         case 8:
1297           g_value_set_int (value, 160);
1298           break;
1299         case 9:
1300           g_value_set_int (value, 192);
1301           break;
1302         case 10:
1303           g_value_set_int (value, 256);
1304           break;
1305       }
1306       break;
1307     default:
1308       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1309       break;
1310   }
1311 }
1312
1313 #if defined AUDIORESAMPLE_FORMAT_AUTO
1314 #define BENCHMARK_SIZE 512
1315
1316 static gboolean
1317 _benchmark_int_float (SpeexResamplerState * st)
1318 {
1319   gint16 in[BENCHMARK_SIZE] = { 0, }, out[BENCHMARK_SIZE / 2];
1320   gfloat in_tmp[BENCHMARK_SIZE], out_tmp[BENCHMARK_SIZE / 2];
1321   gint i;
1322   guint32 inlen = BENCHMARK_SIZE, outlen = BENCHMARK_SIZE / 2;
1323
1324   for (i = 0; i < BENCHMARK_SIZE; i++) {
1325     gfloat tmp = in[i];
1326     in_tmp[i] = tmp / G_MAXINT16;
1327   }
1328
1329   resample_float_resampler_process_interleaved_float (st,
1330       (const guint8 *) in_tmp, &inlen, (guint8 *) out_tmp, &outlen);
1331
1332   if (outlen == 0) {
1333     GST_ERROR ("Failed to use float resampler");
1334     return FALSE;
1335   }
1336
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);
1340   }
1341
1342   return TRUE;
1343 }
1344
1345 static gboolean
1346 _benchmark_int_int (SpeexResamplerState * st)
1347 {
1348   gint16 in[BENCHMARK_SIZE] = { 0, }, out[BENCHMARK_SIZE / 2];
1349   guint32 inlen = BENCHMARK_SIZE, outlen = BENCHMARK_SIZE / 2;
1350
1351   resample_int_resampler_process_interleaved_int (st, (const guint8 *) in,
1352       &inlen, (guint8 *) out, &outlen);
1353
1354   if (outlen == 0) {
1355     GST_ERROR ("Failed to use int resampler");
1356     return FALSE;
1357   }
1358
1359   return TRUE;
1360 }
1361
1362 static gboolean
1363 _benchmark_integer_resampling (void)
1364 {
1365   OilProfile a, b;
1366   gdouble av, bv;
1367   SpeexResamplerState *sta, *stb;
1368   int i;
1369
1370   oil_profile_init (&a);
1371   oil_profile_init (&b);
1372
1373   sta = resample_float_resampler_init (1, 48000, 24000, 4, NULL);
1374   if (sta == NULL) {
1375     GST_ERROR ("Failed to create float resampler state");
1376     return FALSE;
1377   }
1378
1379   stb = resample_int_resampler_init (1, 48000, 24000, 4, NULL);
1380   if (stb == NULL) {
1381     resample_float_resampler_destroy (sta);
1382     GST_ERROR ("Failed to create int resampler state");
1383     return FALSE;
1384   }
1385
1386   /* Benchmark */
1387   for (i = 0; i < 10; i++) {
1388     oil_profile_start (&a);
1389     if (!_benchmark_int_float (sta))
1390       goto error;
1391     oil_profile_stop (&a);
1392   }
1393
1394   /* Benchmark */
1395   for (i = 0; i < 10; i++) {
1396     oil_profile_start (&b);
1397     if (!_benchmark_int_int (stb))
1398       goto error;
1399     oil_profile_stop (&b);
1400   }
1401
1402   /* Handle results */
1403   oil_profile_get_ave_std (&a, &av, NULL);
1404   oil_profile_get_ave_std (&b, &bv, NULL);
1405
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);
1410
1411   if (av > bv)
1412     GST_INFO ("Using integer resampler if appropiate: %lf < %lf", bv, av);
1413   else
1414     GST_INFO ("Using float resampler for everything: %lf <= %lf", av, bv);
1415
1416   return TRUE;
1417
1418 error:
1419   resample_float_resampler_destroy (sta);
1420   resample_int_resampler_destroy (stb);
1421
1422   return FALSE;
1423 }
1424 #endif
1425
1426 static gboolean
1427 plugin_init (GstPlugin * plugin)
1428 {
1429   GST_DEBUG_CATEGORY_INIT (audio_resample_debug, "audioresample", 0,
1430       "audio resampling element");
1431 #if defined AUDIORESAMPLE_FORMAT_AUTO
1432   oil_init ();
1433
1434   if (!_benchmark_integer_resampling ())
1435     return FALSE;
1436 #endif
1437
1438   if (!gst_element_register (plugin, "audioresample", GST_RANK_PRIMARY,
1439           GST_TYPE_AUDIO_RESAMPLE)) {
1440     return FALSE;
1441   }
1442
1443   return TRUE;
1444 }
1445
1446 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1447     GST_VERSION_MINOR,
1448     "audioresample",
1449     "Resamples audio", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,
1450     GST_PACKAGE_ORIGIN);