audioresample: Fix unused variable in compilation with --disable-gst-debug
[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   GstCaps *srccaps, *sinkcaps;
525   gboolean ret = TRUE;
526   guint32 ratio_den, ratio_num;
527   gint inrate, outrate, gcd;
528   gint width;
529
530   GST_LOG_OBJECT (base, "asked to transform size %d in direction %s",
531       size, direction == GST_PAD_SINK ? "SINK" : "SRC");
532   if (direction == GST_PAD_SINK) {
533     sinkcaps = caps;
534     srccaps = othercaps;
535   } else {
536     sinkcaps = othercaps;
537     srccaps = caps;
538   }
539
540   ret =
541       gst_audio_resample_parse_caps (caps, othercaps, &width, NULL, &inrate,
542       &outrate, NULL);
543   if (G_UNLIKELY (!ret)) {
544     GST_ERROR_OBJECT (base, "Wrong caps");
545     return FALSE;
546   }
547
548   gcd = _gcd (inrate, outrate);
549   ratio_num = inrate / gcd;
550   ratio_den = outrate / gcd;
551
552   if (direction == GST_PAD_SINK) {
553     gint fac = width / 8;
554
555     /* asked to convert size of an incoming buffer */
556     size /= fac;
557     *othersize = (size * ratio_den + ratio_num - 1) / ratio_num;
558     *othersize *= fac;
559     size *= fac;
560   } else {
561     gint fac = width / 8;
562
563     /* asked to convert size of an outgoing buffer */
564     size /= fac;
565     *othersize = (size * ratio_num + ratio_den - 1) / ratio_den;
566     *othersize *= fac;
567     size *= fac;
568   }
569
570   GST_LOG_OBJECT (base, "transformed size %d to %d", size, *othersize);
571
572   return ret;
573 }
574
575 static gboolean
576 gst_audio_resample_set_caps (GstBaseTransform * base, GstCaps * incaps,
577     GstCaps * outcaps)
578 {
579   gboolean ret;
580   gint width = 0, inrate = 0, outrate = 0, channels = 0;
581   gboolean fp;
582   GstAudioResample *resample = GST_AUDIO_RESAMPLE (base);
583
584   GST_LOG ("incaps %" GST_PTR_FORMAT ", outcaps %"
585       GST_PTR_FORMAT, incaps, outcaps);
586
587   ret = gst_audio_resample_parse_caps (incaps, outcaps,
588       &width, &channels, &inrate, &outrate, &fp);
589
590   if (G_UNLIKELY (!ret))
591     return FALSE;
592
593   ret =
594       gst_audio_resample_update_state (resample, width, channels, inrate,
595       outrate, resample->quality, fp);
596
597   if (G_UNLIKELY (!ret))
598     return FALSE;
599
600   /* save caps so we can short-circuit in the size_transform if the caps
601    * are the same */
602   gst_caps_replace (&resample->sinkcaps, incaps);
603   gst_caps_replace (&resample->srccaps, outcaps);
604
605   return TRUE;
606 }
607
608 #define GST_MAXINT24 (8388607)
609 #define GST_MININT24 (-8388608)
610
611 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
612 #define GST_READ_UINT24 GST_READ_UINT24_LE
613 #define GST_WRITE_UINT24 GST_WRITE_UINT24_LE
614 #else
615 #define GST_READ_UINT24 GST_READ_UINT24_BE
616 #define GST_WRITE_UINT24 GST_WRITE_UINT24_BE
617 #endif
618
619 static void
620 gst_audio_resample_convert_buffer (GstAudioResample * resample,
621     const guint8 * in, guint8 * out, guint len, gboolean inverse)
622 {
623   len *= resample->channels;
624
625   if (inverse) {
626     if (gst_audio_resample_use_int && resample->width == 8 && !resample->fp) {
627       gint8 *o = (gint8 *) out;
628       gint16 *i = (gint16 *) in;
629       gint32 tmp;
630
631       while (len) {
632         tmp = *i + (G_MAXINT8 >> 1);
633         *o = CLAMP (tmp >> 8, G_MININT8, G_MAXINT8);
634         o++;
635         i++;
636         len--;
637       }
638     } else if (!gst_audio_resample_use_int && resample->width == 8
639         && !resample->fp) {
640       gint8 *o = (gint8 *) out;
641       gfloat *i = (gfloat *) in;
642       gfloat tmp;
643
644       while (len) {
645         tmp = *i;
646         *o = (gint8) CLAMP (tmp * G_MAXINT8 + 0.5, G_MININT8, G_MAXINT8);
647         o++;
648         i++;
649         len--;
650       }
651     } else if (!gst_audio_resample_use_int && resample->width == 16
652         && !resample->fp) {
653       gint16 *o = (gint16 *) out;
654       gfloat *i = (gfloat *) in;
655       gfloat tmp;
656
657       while (len) {
658         tmp = *i;
659         *o = (gint16) CLAMP (tmp * G_MAXINT16 + 0.5, G_MININT16, G_MAXINT16);
660         o++;
661         i++;
662         len--;
663       }
664     } else if (resample->width == 24 && !resample->fp) {
665       guint8 *o = (guint8 *) out;
666       gdouble *i = (gdouble *) in;
667       gdouble tmp;
668
669       while (len) {
670         tmp = *i;
671         GST_WRITE_UINT24 (o, (gint32) CLAMP (tmp * GST_MAXINT24 + 0.5,
672                 GST_MININT24, GST_MAXINT24));
673         o += 3;
674         i++;
675         len--;
676       }
677     } else if (resample->width == 32 && !resample->fp) {
678       gint32 *o = (gint32 *) out;
679       gdouble *i = (gdouble *) in;
680       gdouble tmp;
681
682       while (len) {
683         tmp = *i;
684         *o = (gint32) CLAMP (tmp * G_MAXINT32 + 0.5, G_MININT32, G_MAXINT32);
685         o++;
686         i++;
687         len--;
688       }
689     } else {
690       g_assert_not_reached ();
691     }
692   } else {
693     if (gst_audio_resample_use_int && resample->width == 8 && !resample->fp) {
694       gint8 *i = (gint8 *) in;
695       gint16 *o = (gint16 *) out;
696       gint32 tmp;
697
698       while (len) {
699         tmp = *i;
700         *o = tmp << 8;
701         o++;
702         i++;
703         len--;
704       }
705     } else if (!gst_audio_resample_use_int && resample->width == 8
706         && !resample->fp) {
707       gint8 *i = (gint8 *) in;
708       gfloat *o = (gfloat *) out;
709       gfloat tmp;
710
711       while (len) {
712         tmp = *i;
713         *o = tmp / G_MAXINT8;
714         o++;
715         i++;
716         len--;
717       }
718     } else if (!gst_audio_resample_use_int && resample->width == 16
719         && !resample->fp) {
720       gint16 *i = (gint16 *) in;
721       gfloat *o = (gfloat *) out;
722       gfloat tmp;
723
724       while (len) {
725         tmp = *i;
726         *o = tmp / G_MAXINT16;
727         o++;
728         i++;
729         len--;
730       }
731     } else if (resample->width == 24 && !resample->fp) {
732       guint8 *i = (guint8 *) in;
733       gdouble *o = (gdouble *) out;
734       gdouble tmp;
735       guint32 tmp2;
736
737       while (len) {
738         tmp2 = GST_READ_UINT24 (i);
739         if (tmp2 & 0x00800000)
740           tmp2 |= 0xff000000;
741         tmp = (gint32) tmp2;
742         *o = tmp / GST_MAXINT24;
743         o++;
744         i += 3;
745         len--;
746       }
747     } else if (resample->width == 32 && !resample->fp) {
748       gint32 *i = (gint32 *) in;
749       gdouble *o = (gdouble *) out;
750       gdouble tmp;
751
752       while (len) {
753         tmp = *i;
754         *o = tmp / G_MAXINT32;
755         o++;
756         i++;
757         len--;
758       }
759     } else {
760       g_assert_not_reached ();
761     }
762   }
763 }
764
765 static void
766 gst_audio_resample_push_drain (GstAudioResample * resample)
767 {
768   GstBuffer *buf;
769   GstBaseTransform *trans = GST_BASE_TRANSFORM (resample);
770   GstFlowReturn res;
771   gint outsize;
772   guint out_len, out_processed;
773   gint err;
774   guint num, den, len;
775   guint8 *outtmp = NULL;
776   gboolean need_convert = FALSE;
777
778   if (!resample->state)
779     return;
780
781   need_convert = (resample->funcs->width != resample->width);
782
783   resample->funcs->get_ratio (resample->state, &num, &den);
784
785   out_len = resample->funcs->get_input_latency (resample->state);
786   out_len = out_processed = (out_len * den + num - 1) / num;
787   outsize = (resample->width / 8) * out_len * resample->channels;
788
789   if (need_convert) {
790     guint outsize_tmp =
791         (resample->funcs->width / 8) * out_len * resample->channels;
792     if (outsize_tmp <= resample->tmp_out_size) {
793       outtmp = resample->tmp_out;
794     } else {
795       resample->tmp_out_size = outsize_tmp;
796       resample->tmp_out = outtmp = g_realloc (resample->tmp_out, outsize_tmp);
797     }
798   }
799
800   res =
801       gst_pad_alloc_buffer_and_set_caps (trans->srcpad, GST_BUFFER_OFFSET_NONE,
802       outsize, GST_PAD_CAPS (trans->srcpad), &buf);
803
804   if (G_UNLIKELY (res != GST_FLOW_OK)) {
805     GST_WARNING_OBJECT (resample, "failed allocating buffer of %d bytes",
806         outsize);
807     return;
808   }
809
810   len = resample->funcs->get_input_latency (resample->state);
811
812   err =
813       resample->funcs->process (resample->state,
814       NULL, &len, (need_convert) ? outtmp : GST_BUFFER_DATA (buf),
815       &out_processed);
816
817   if (G_UNLIKELY (err != RESAMPLER_ERR_SUCCESS)) {
818     GST_WARNING_OBJECT (resample, "Failed to process drain: %s",
819         resample->funcs->strerror (err));
820     gst_buffer_unref (buf);
821     return;
822   }
823
824   if (G_UNLIKELY (out_processed == 0)) {
825     GST_WARNING_OBJECT (resample, "Failed to get drain, dropping buffer");
826     gst_buffer_unref (buf);
827     return;
828   }
829
830   /* If we wrote more than allocated something is really wrong now
831    * and we should better abort immediately */
832   g_assert (out_len >= out_processed);
833
834   if (need_convert)
835     gst_audio_resample_convert_buffer (resample, outtmp, GST_BUFFER_DATA (buf),
836         out_processed, TRUE);
837
838   GST_BUFFER_DURATION (buf) =
839       GST_FRAMES_TO_CLOCK_TIME (out_processed, resample->outrate);
840   GST_BUFFER_SIZE (buf) =
841       out_processed * resample->channels * (resample->width / 8);
842
843   if (GST_CLOCK_TIME_IS_VALID (resample->next_ts)) {
844     GST_BUFFER_OFFSET (buf) = resample->next_offset;
845     GST_BUFFER_OFFSET_END (buf) = resample->next_offset + out_processed;
846     GST_BUFFER_TIMESTAMP (buf) = resample->next_ts;
847
848     resample->next_ts += GST_BUFFER_DURATION (buf);
849     resample->next_offset += out_processed;
850   }
851
852   GST_LOG_OBJECT (resample,
853       "Pushing drain buffer of %u bytes with timestamp %" GST_TIME_FORMAT
854       " duration %" GST_TIME_FORMAT " offset %" G_GUINT64_FORMAT " offset_end %"
855       G_GUINT64_FORMAT, GST_BUFFER_SIZE (buf),
856       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
857       GST_TIME_ARGS (GST_BUFFER_DURATION (buf)), GST_BUFFER_OFFSET (buf),
858       GST_BUFFER_OFFSET_END (buf));
859
860   res = gst_pad_push (trans->srcpad, buf);
861
862   if (G_UNLIKELY (res != GST_FLOW_OK))
863     GST_WARNING_OBJECT (resample, "Failed to push drain: %s",
864         gst_flow_get_name (res));
865
866   return;
867 }
868
869 static gboolean
870 gst_audio_resample_event (GstBaseTransform * base, GstEvent * event)
871 {
872   GstAudioResample *resample = GST_AUDIO_RESAMPLE (base);
873
874   switch (GST_EVENT_TYPE (event)) {
875     case GST_EVENT_FLUSH_STOP:
876       gst_audio_resample_reset_state (resample);
877       resample->next_offset = -1;
878       resample->next_ts = -1;
879       resample->next_upstream_ts = -1;
880       break;
881     case GST_EVENT_NEWSEGMENT:
882       gst_audio_resample_push_drain (resample);
883       gst_audio_resample_reset_state (resample);
884       resample->next_offset = -1;
885       resample->next_ts = -1;
886       resample->next_upstream_ts = -1;
887       break;
888     case GST_EVENT_EOS:
889       gst_audio_resample_push_drain (resample);
890       gst_audio_resample_reset_state (resample);
891       break;
892     default:
893       break;
894   }
895
896   return parent_class->event (base, event);
897 }
898
899 static gboolean
900 gst_audio_resample_check_discont (GstAudioResample * resample,
901     GstClockTime timestamp)
902 {
903   if (timestamp != GST_CLOCK_TIME_NONE &&
904       resample->next_upstream_ts != GST_CLOCK_TIME_NONE &&
905       timestamp != resample->next_upstream_ts) {
906     /* Potentially a discontinuous buffer. However, it turns out that many
907      * elements generate imperfect streams due to rounding errors, so we permit
908      * a small error (up to one sample) without triggering a filter 
909      * flush/restart (if triggered incorrectly, this will be audible) */
910     GstClockTimeDiff diff = timestamp - resample->next_upstream_ts;
911
912     if (ABS (diff) > (GST_SECOND + resample->inrate - 1) / resample->inrate) {
913       GST_WARNING_OBJECT (resample,
914           "encountered timestamp discontinuity of %s%" GST_TIME_FORMAT,
915           (diff < 0) ? "-" : "", GST_TIME_ARGS ((GstClockTime) ABS (diff)));
916       return TRUE;
917     }
918   }
919
920   return FALSE;
921 }
922
923 static GstFlowReturn
924 gst_audio_resample_process (GstAudioResample * resample, GstBuffer * inbuf,
925     GstBuffer * outbuf)
926 {
927   guint32 in_len, in_processed;
928   guint32 out_len, out_processed;
929   gint err = RESAMPLER_ERR_SUCCESS;
930   guint8 *in_tmp = NULL, *out_tmp = NULL;
931   gboolean need_convert = (resample->funcs->width != resample->width);
932
933   in_len = GST_BUFFER_SIZE (inbuf) / resample->channels;
934   out_len = GST_BUFFER_SIZE (outbuf) / resample->channels;
935
936   in_len /= (resample->width / 8);
937   out_len /= (resample->width / 8);
938
939   in_processed = in_len;
940   out_processed = out_len;
941
942   if (need_convert) {
943     guint in_size_tmp =
944         in_len * resample->channels * (resample->funcs->width / 8);
945     guint out_size_tmp =
946         out_len * resample->channels * (resample->funcs->width / 8);
947
948     if (in_size_tmp <= resample->tmp_in_size) {
949       in_tmp = resample->tmp_in;
950     } else {
951       resample->tmp_in = in_tmp = g_realloc (resample->tmp_in, in_size_tmp);
952       resample->tmp_in_size = in_size_tmp;
953     }
954
955     gst_audio_resample_convert_buffer (resample, GST_BUFFER_DATA (inbuf),
956         in_tmp, in_len, FALSE);
957
958     if (out_size_tmp <= resample->tmp_out_size) {
959       out_tmp = resample->tmp_out;
960     } else {
961       resample->tmp_out = out_tmp = g_realloc (resample->tmp_out, out_size_tmp);
962       resample->tmp_out_size = out_size_tmp;
963     }
964   }
965
966   if (need_convert) {
967     err = resample->funcs->process (resample->state,
968         in_tmp, &in_processed, out_tmp, &out_processed);
969   } else {
970     err = resample->funcs->process (resample->state,
971         (const guint8 *) GST_BUFFER_DATA (inbuf), &in_processed,
972         (guint8 *) GST_BUFFER_DATA (outbuf), &out_processed);
973   }
974
975   if (G_UNLIKELY (in_len != in_processed))
976     GST_WARNING_OBJECT (resample, "Converted %d of %d input samples",
977         in_processed, in_len);
978
979   if (out_len != out_processed) {
980     if (out_processed == 0) {
981       GST_DEBUG_OBJECT (resample, "Converted to 0 samples, buffer dropped");
982
983       return GST_BASE_TRANSFORM_FLOW_DROPPED;
984     }
985
986     /* If we wrote more than allocated something is really wrong now
987      * and we should better abort immediately */
988     g_assert (out_len >= out_processed);
989   }
990
991   if (G_UNLIKELY (err != RESAMPLER_ERR_SUCCESS)) {
992     GST_ERROR_OBJECT (resample, "Failed to convert data: %s",
993         resample->funcs->strerror (err));
994     return GST_FLOW_ERROR;
995   } else {
996
997     if (need_convert)
998       gst_audio_resample_convert_buffer (resample, out_tmp,
999           GST_BUFFER_DATA (outbuf), out_processed, TRUE);
1000
1001     GST_BUFFER_DURATION (outbuf) =
1002         GST_FRAMES_TO_CLOCK_TIME (out_processed, resample->outrate);
1003     GST_BUFFER_SIZE (outbuf) =
1004         out_processed * resample->channels * (resample->width / 8);
1005
1006     if (GST_CLOCK_TIME_IS_VALID (resample->next_ts)) {
1007       GST_BUFFER_TIMESTAMP (outbuf) = resample->next_ts;
1008       GST_BUFFER_OFFSET (outbuf) = resample->next_offset;
1009       GST_BUFFER_OFFSET_END (outbuf) = resample->next_offset + out_processed;
1010
1011       resample->next_ts += GST_BUFFER_DURATION (outbuf);
1012       resample->next_offset += out_processed;
1013     }
1014
1015     GST_LOG_OBJECT (resample,
1016         "Converted to buffer of %u bytes with timestamp %" GST_TIME_FORMAT
1017         ", duration %" GST_TIME_FORMAT ", offset %" G_GUINT64_FORMAT
1018         ", offset_end %" G_GUINT64_FORMAT, GST_BUFFER_SIZE (outbuf),
1019         GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
1020         GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)),
1021         GST_BUFFER_OFFSET (outbuf), GST_BUFFER_OFFSET_END (outbuf));
1022
1023     return GST_FLOW_OK;
1024   }
1025 }
1026
1027 static GstFlowReturn
1028 gst_audio_resample_transform (GstBaseTransform * base, GstBuffer * inbuf,
1029     GstBuffer * outbuf)
1030 {
1031   GstAudioResample *resample = GST_AUDIO_RESAMPLE (base);
1032   guint8 *data;
1033   gulong size;
1034   GstClockTime timestamp;
1035   guint outsamples, insamples;
1036   GstFlowReturn ret;
1037
1038   if (resample->state == NULL) {
1039     if (G_UNLIKELY (!(resample->state =
1040                 gst_audio_resample_init_state (resample, resample->width,
1041                     resample->channels, resample->inrate, resample->outrate,
1042                     resample->quality, resample->fp))))
1043       return GST_FLOW_ERROR;
1044
1045     resample->funcs =
1046         gst_audio_resample_get_funcs (resample->width, resample->fp);
1047   }
1048
1049   data = GST_BUFFER_DATA (inbuf);
1050   size = GST_BUFFER_SIZE (inbuf);
1051   timestamp = GST_BUFFER_TIMESTAMP (inbuf);
1052
1053   GST_LOG_OBJECT (resample, "transforming buffer of %ld bytes, ts %"
1054       GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT ", offset %"
1055       G_GINT64_FORMAT ", offset_end %" G_GINT64_FORMAT,
1056       size, GST_TIME_ARGS (timestamp),
1057       GST_TIME_ARGS (GST_BUFFER_DURATION (inbuf)),
1058       GST_BUFFER_OFFSET (inbuf), GST_BUFFER_OFFSET_END (inbuf));
1059
1060   /* check for timestamp discontinuities and flush/reset if needed */
1061   if (G_UNLIKELY (gst_audio_resample_check_discont (resample, timestamp)
1062           || GST_BUFFER_IS_DISCONT (inbuf))) {
1063     /* Flush internal samples */
1064     gst_audio_resample_reset_state (resample);
1065     /* Inform downstream element about discontinuity */
1066     resample->need_discont = TRUE;
1067     /* We want to recalculate the timestamps */
1068     resample->next_ts = -1;
1069     resample->next_upstream_ts = -1;
1070     resample->next_offset = -1;
1071   }
1072
1073   insamples = GST_BUFFER_SIZE (inbuf) / resample->channels;
1074   insamples /= (resample->width / 8);
1075
1076   outsamples = GST_BUFFER_SIZE (outbuf) / resample->channels;
1077   outsamples /= (resample->width / 8);
1078
1079   if (GST_CLOCK_TIME_IS_VALID (timestamp)
1080       && !GST_CLOCK_TIME_IS_VALID (resample->next_ts)) {
1081     resample->next_ts = timestamp;
1082     resample->next_offset =
1083         GST_CLOCK_TIME_TO_FRAMES (timestamp, resample->outrate);
1084   }
1085
1086   if (G_UNLIKELY (resample->need_discont)) {
1087     GST_DEBUG_OBJECT (resample, "marking this buffer with the DISCONT flag");
1088     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
1089     resample->need_discont = FALSE;
1090   }
1091
1092   ret = gst_audio_resample_process (resample, inbuf, outbuf);
1093   if (G_UNLIKELY (ret != GST_FLOW_OK))
1094     return ret;
1095
1096   if (GST_CLOCK_TIME_IS_VALID (timestamp)
1097       && !GST_CLOCK_TIME_IS_VALID (resample->next_upstream_ts))
1098     resample->next_upstream_ts = timestamp;
1099
1100   if (GST_CLOCK_TIME_IS_VALID (resample->next_upstream_ts))
1101     resample->next_upstream_ts +=
1102         GST_FRAMES_TO_CLOCK_TIME (insamples, resample->inrate);
1103
1104   return GST_FLOW_OK;
1105 }
1106
1107 static gboolean
1108 gst_audio_resample_query (GstPad * pad, GstQuery * query)
1109 {
1110   GstAudioResample *resample = GST_AUDIO_RESAMPLE (gst_pad_get_parent (pad));
1111   GstBaseTransform *trans = GST_BASE_TRANSFORM (resample);
1112   gboolean res = TRUE;
1113
1114   switch (GST_QUERY_TYPE (query)) {
1115     case GST_QUERY_LATENCY:
1116     {
1117       GstClockTime min, max;
1118       gboolean live;
1119       guint64 latency;
1120       GstPad *peer;
1121       gint rate = resample->inrate;
1122       gint resampler_latency;
1123
1124       if (resample->state)
1125         resampler_latency =
1126             resample->funcs->get_input_latency (resample->state);
1127       else
1128         resampler_latency = 0;
1129
1130       if (gst_base_transform_is_passthrough (trans))
1131         resampler_latency = 0;
1132
1133       if ((peer = gst_pad_get_peer (trans->sinkpad))) {
1134         if ((res = gst_pad_query (peer, query))) {
1135           gst_query_parse_latency (query, &live, &min, &max);
1136
1137           GST_DEBUG_OBJECT (resample, "Peer latency: min %"
1138               GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
1139               GST_TIME_ARGS (min), GST_TIME_ARGS (max));
1140
1141           /* add our own latency */
1142           if (rate != 0 && resampler_latency != 0)
1143             latency =
1144                 gst_util_uint64_scale (resampler_latency, GST_SECOND, rate);
1145           else
1146             latency = 0;
1147
1148           GST_DEBUG_OBJECT (resample, "Our latency: %" GST_TIME_FORMAT,
1149               GST_TIME_ARGS (latency));
1150
1151           min += latency;
1152           if (max != GST_CLOCK_TIME_NONE)
1153             max += latency;
1154
1155           GST_DEBUG_OBJECT (resample, "Calculated total latency : min %"
1156               GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
1157               GST_TIME_ARGS (min), GST_TIME_ARGS (max));
1158
1159           gst_query_set_latency (query, live, min, max);
1160         }
1161         gst_object_unref (peer);
1162       }
1163       break;
1164     }
1165     default:
1166       res = gst_pad_query_default (pad, query);
1167       break;
1168   }
1169   gst_object_unref (resample);
1170   return res;
1171 }
1172
1173 static const GstQueryType *
1174 gst_audio_resample_query_type (GstPad * pad)
1175 {
1176   static const GstQueryType types[] = {
1177     GST_QUERY_LATENCY,
1178     0
1179   };
1180
1181   return types;
1182 }
1183
1184 static void
1185 gst_audio_resample_set_property (GObject * object, guint prop_id,
1186     const GValue * value, GParamSpec * pspec)
1187 {
1188   GstAudioResample *resample;
1189
1190   resample = GST_AUDIO_RESAMPLE (object);
1191
1192   switch (prop_id) {
1193     case PROP_QUALITY:
1194       GST_BASE_TRANSFORM_LOCK (resample);
1195       resample->quality = g_value_get_int (value);
1196       GST_DEBUG_OBJECT (resample, "new quality %d", resample->quality);
1197
1198       gst_audio_resample_update_state (resample, resample->width,
1199           resample->channels, resample->inrate, resample->outrate,
1200           resample->quality, resample->fp);
1201       GST_BASE_TRANSFORM_UNLOCK (resample);
1202       break;
1203     case PROP_FILTER_LENGTH:{
1204       gint filter_length = g_value_get_int (value);
1205
1206       GST_BASE_TRANSFORM_LOCK (resample);
1207       if (filter_length <= 8)
1208         resample->quality = 0;
1209       else if (filter_length <= 16)
1210         resample->quality = 1;
1211       else if (filter_length <= 32)
1212         resample->quality = 2;
1213       else if (filter_length <= 48)
1214         resample->quality = 3;
1215       else if (filter_length <= 64)
1216         resample->quality = 4;
1217       else if (filter_length <= 80)
1218         resample->quality = 5;
1219       else if (filter_length <= 96)
1220         resample->quality = 6;
1221       else if (filter_length <= 128)
1222         resample->quality = 7;
1223       else if (filter_length <= 160)
1224         resample->quality = 8;
1225       else if (filter_length <= 192)
1226         resample->quality = 9;
1227       else
1228         resample->quality = 10;
1229
1230       GST_DEBUG_OBJECT (resample, "new quality %d", resample->quality);
1231
1232       gst_audio_resample_update_state (resample, resample->width,
1233           resample->channels, resample->inrate, resample->outrate,
1234           resample->quality, resample->fp);
1235       GST_BASE_TRANSFORM_UNLOCK (resample);
1236       break;
1237     }
1238     default:
1239       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1240       break;
1241   }
1242 }
1243
1244 static void
1245 gst_audio_resample_get_property (GObject * object, guint prop_id,
1246     GValue * value, GParamSpec * pspec)
1247 {
1248   GstAudioResample *resample;
1249
1250   resample = GST_AUDIO_RESAMPLE (object);
1251
1252   switch (prop_id) {
1253     case PROP_QUALITY:
1254       g_value_set_int (value, resample->quality);
1255       break;
1256     case PROP_FILTER_LENGTH:
1257       switch (resample->quality) {
1258         case 0:
1259           g_value_set_int (value, 8);
1260           break;
1261         case 1:
1262           g_value_set_int (value, 16);
1263           break;
1264         case 2:
1265           g_value_set_int (value, 32);
1266           break;
1267         case 3:
1268           g_value_set_int (value, 48);
1269           break;
1270         case 4:
1271           g_value_set_int (value, 64);
1272           break;
1273         case 5:
1274           g_value_set_int (value, 80);
1275           break;
1276         case 6:
1277           g_value_set_int (value, 96);
1278           break;
1279         case 7:
1280           g_value_set_int (value, 128);
1281           break;
1282         case 8:
1283           g_value_set_int (value, 160);
1284           break;
1285         case 9:
1286           g_value_set_int (value, 192);
1287           break;
1288         case 10:
1289           g_value_set_int (value, 256);
1290           break;
1291       }
1292       break;
1293     default:
1294       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1295       break;
1296   }
1297 }
1298
1299 #if defined AUDIORESAMPLE_FORMAT_AUTO
1300 #define BENCHMARK_SIZE 512
1301
1302 static gboolean
1303 _benchmark_int_float (SpeexResamplerState * st)
1304 {
1305   gint16 in[BENCHMARK_SIZE] = { 0, }, out[BENCHMARK_SIZE / 2];
1306   gfloat in_tmp[BENCHMARK_SIZE], out_tmp[BENCHMARK_SIZE / 2];
1307   gint i;
1308   guint32 inlen = BENCHMARK_SIZE, outlen = BENCHMARK_SIZE / 2;
1309
1310   for (i = 0; i < BENCHMARK_SIZE; i++) {
1311     gfloat tmp = in[i];
1312     in_tmp[i] = tmp / G_MAXINT16;
1313   }
1314
1315   resample_float_resampler_process_interleaved_float (st,
1316       (const guint8 *) in_tmp, &inlen, (guint8 *) out_tmp, &outlen);
1317
1318   if (outlen == 0) {
1319     GST_ERROR ("Failed to use float resampler");
1320     return FALSE;
1321   }
1322
1323   for (i = 0; i < outlen; i++) {
1324     gfloat tmp = out_tmp[i];
1325     out[i] = CLAMP (tmp * G_MAXINT16 + 0.5, G_MININT16, G_MAXINT16);
1326   }
1327
1328   return TRUE;
1329 }
1330
1331 static gboolean
1332 _benchmark_int_int (SpeexResamplerState * st)
1333 {
1334   gint16 in[BENCHMARK_SIZE] = { 0, }, out[BENCHMARK_SIZE / 2];
1335   guint32 inlen = BENCHMARK_SIZE, outlen = BENCHMARK_SIZE / 2;
1336
1337   resample_int_resampler_process_interleaved_int (st, (const guint8 *) in,
1338       &inlen, (guint8 *) out, &outlen);
1339
1340   if (outlen == 0) {
1341     GST_ERROR ("Failed to use int resampler");
1342     return FALSE;
1343   }
1344
1345   return TRUE;
1346 }
1347
1348 static gboolean
1349 _benchmark_integer_resampling (void)
1350 {
1351   OilProfile a, b;
1352   gdouble av, bv;
1353   SpeexResamplerState *sta, *stb;
1354
1355   oil_profile_init (&a);
1356   oil_profile_init (&b);
1357
1358   sta = resample_float_resampler_init (1, 48000, 24000, 4, NULL);
1359   if (sta == NULL) {
1360     GST_ERROR ("Failed to create float resampler state");
1361     return FALSE;
1362   }
1363
1364   stb = resample_int_resampler_init (1, 48000, 24000, 4, NULL);
1365   if (stb == NULL) {
1366     resample_float_resampler_destroy (sta);
1367     GST_ERROR ("Failed to create int resampler state");
1368     return FALSE;
1369   }
1370
1371   /* Warm up cache */
1372   if (!_benchmark_int_float (sta))
1373     goto error;
1374   if (!_benchmark_int_float (sta))
1375     goto error;
1376
1377   /* Benchmark */
1378   oil_profile_start (&a);
1379   if (!_benchmark_int_float (sta))
1380     goto error;
1381   oil_profile_stop (&a);
1382
1383   /* Warm up cache */
1384   if (!_benchmark_int_int (stb))
1385     goto error;
1386   if (!_benchmark_int_int (stb))
1387     goto error;
1388
1389   /* Benchmark */
1390   oil_profile_start (&b);
1391   if (!_benchmark_int_int (stb))
1392     goto error;
1393   oil_profile_stop (&b);
1394
1395   /* Handle results */
1396   oil_profile_get_ave_std (&a, &av, NULL);
1397   oil_profile_get_ave_std (&b, &bv, NULL);
1398
1399   /* Remember benchmark result in global variable */
1400   gst_audio_resample_use_int = (av > bv);
1401   resample_float_resampler_destroy (sta);
1402   resample_int_resampler_destroy (stb);
1403
1404   if (av > bv)
1405     GST_INFO ("Using integer resampler if appropiate: %lf < %lf", bv, av);
1406   else
1407     GST_INFO ("Using float resampler for everything: %lf <= %lf", av, bv);
1408
1409   return TRUE;
1410
1411 error:
1412   resample_float_resampler_destroy (sta);
1413   resample_int_resampler_destroy (stb);
1414
1415   return FALSE;
1416 }
1417 #endif
1418
1419 static gboolean
1420 plugin_init (GstPlugin * plugin)
1421 {
1422   GST_DEBUG_CATEGORY_INIT (audio_resample_debug, "audioresample", 0,
1423       "audio resampling element");
1424 #if defined AUDIORESAMPLE_FORMAT_AUTO
1425   oil_init ();
1426
1427   if (!_benchmark_integer_resampling ())
1428     return FALSE;
1429 #endif
1430
1431   if (!gst_element_register (plugin, "audioresample", GST_RANK_PRIMARY,
1432           GST_TYPE_AUDIO_RESAMPLE)) {
1433     return FALSE;
1434   }
1435
1436   return TRUE;
1437 }
1438
1439 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1440     GST_VERSION_MINOR,
1441     "audioresample",
1442     "Resamples audio", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,
1443     GST_PACKAGE_ORIGIN);