fixes for event handler changes
[platform/upstream/gstreamer.git] / gst / audiofx / audiofxbasefirfilter.c
1 /* -*- c-basic-offset: 2 -*-
2  * 
3  * GStreamer
4  * Copyright (C) 1999-2001 Erik Walthinsen <omega@cse.ogi.edu>
5  *               2006 Dreamlab Technologies Ltd. <mathis.hofer@dreamlab.net>
6  *               2007-2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  * 
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include <string.h>
30 #include <math.h>
31 #include <gst/gst.h>
32 #include <gst/audio/gstaudiofilter.h>
33 #include <gst/controller/gstcontroller.h>
34
35 /* FIXME: Remove this once we depend on gst-plugins-base 0.10.26 */
36 #ifndef GST_AUDIO_FILTER_CAST
37 #define GST_AUDIO_FILTER_CAST(obj) ((GstAudioFilter *) (obj))
38 #endif
39
40 #include "audiofxbasefirfilter.h"
41
42 #define GST_CAT_DEFAULT gst_audio_fx_base_fir_filter_debug
43 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
44
45 #define ALLOWED_CAPS \
46     "audio/x-raw-float, "                                             \
47     " width = (int) { 32, 64 }, "                                     \
48     " endianness = (int) BYTE_ORDER, "                                \
49     " rate = (int) [ 1, MAX ], "                                      \
50     " channels = (int) [ 1, MAX ]"
51
52 /* Switch from time-domain to FFT convolution for kernels >= this */
53 #define FFT_THRESHOLD 32
54
55 enum
56 {
57   PROP_0 = 0,
58   PROP_LOW_LATENCY,
59   PROP_DRAIN_ON_CHANGES
60 };
61
62 #define DEFAULT_LOW_LATENCY FALSE
63 #define DEFAULT_DRAIN_ON_CHANGES TRUE
64
65 #define gst_audio_fx_base_fir_filter_parent_class parent_class
66 G_DEFINE_TYPE (GstAudioFXBaseFIRFilter, gst_audio_fx_base_fir_filter,
67     GST_TYPE_AUDIO_FILTER);
68
69 static GstFlowReturn gst_audio_fx_base_fir_filter_transform (GstBaseTransform *
70     base, GstBuffer * inbuf, GstBuffer * outbuf);
71 static gboolean gst_audio_fx_base_fir_filter_start (GstBaseTransform * base);
72 static gboolean gst_audio_fx_base_fir_filter_stop (GstBaseTransform * base);
73 static gboolean gst_audio_fx_base_fir_filter_sink_event (GstBaseTransform *
74     base, GstEvent * event);
75 static gboolean gst_audio_fx_base_fir_filter_transform_size (GstBaseTransform *
76     base, GstPadDirection direction, GstCaps * caps, gsize size,
77     GstCaps * othercaps, gsize * othersize);
78 static gboolean gst_audio_fx_base_fir_filter_setup (GstAudioFilter * base,
79     GstRingBufferSpec * format);
80
81 static gboolean gst_audio_fx_base_fir_filter_query (GstPad * pad,
82     GstQuery * query);
83 static const GstQueryType *gst_audio_fx_base_fir_filter_query_type (GstPad *
84     pad);
85
86 /* 
87  * The code below calculates the linear convolution:
88  *
89  * y[t] = \sum_{u=0}^{M-1} x[t - u] * h[u]
90  *
91  * where y is the output, x is the input, M is the length
92  * of the filter kernel and h is the filter kernel. For x
93  * holds: x[t] == 0 \forall t < 0.
94  *
95  * The runtime complexity of this is O (M) per sample.
96  *
97  */
98 #define DEFINE_PROCESS_FUNC(width,ctype) \
99 static guint \
100 process_##width (GstAudioFXBaseFIRFilter * self, const g##ctype * src, g##ctype * dst, guint input_samples) \
101 { \
102   gint channels = GST_AUDIO_FILTER_CAST (self)->format.channels; \
103   TIME_DOMAIN_CONVOLUTION_BODY (channels); \
104 }
105
106 #define DEFINE_PROCESS_FUNC_FIXED_CHANNELS(width,channels,ctype) \
107 static guint \
108 process_##channels##_##width (GstAudioFXBaseFIRFilter * self, const g##ctype * src, g##ctype * dst, guint input_samples) \
109 { \
110   TIME_DOMAIN_CONVOLUTION_BODY (channels); \
111 }
112
113 #define TIME_DOMAIN_CONVOLUTION_BODY(channels) G_STMT_START { \
114   gint kernel_length = self->kernel_length; \
115   gint i, j, k, l; \
116   gint res_start; \
117   gint from_input; \
118   gint off; \
119   gdouble *buffer = self->buffer; \
120   gdouble *kernel = self->kernel; \
121   guint buffer_length = self->buffer_length; \
122   \
123   if (!buffer) { \
124     self->buffer_length = buffer_length = kernel_length * channels; \
125     self->buffer = buffer = g_new0 (gdouble, self->buffer_length); \
126   } \
127   \
128   /* convolution */ \
129   for (i = 0; i < input_samples; i++) { \
130     dst[i] = 0.0; \
131     k = i % channels; \
132     l = i / channels; \
133     from_input = MIN (l, kernel_length-1); \
134     off = l * channels + k; \
135     for (j = 0; j <= from_input; j++) { \
136       dst[i] += src[off] * kernel[j]; \
137       off -= channels; \
138     } \
139     /* j == from_input && off == (l - j) * channels + k */ \
140     off += kernel_length * channels; \
141     for (; j < kernel_length; j++) { \
142       dst[i] += buffer[off] * kernel[j]; \
143       off -= channels; \
144     } \
145   } \
146   \
147   /* copy the tail of the current input buffer to the residue, while \
148    * keeping parts of the residue if the input buffer is smaller than \
149    * the kernel length */ \
150   /* from now on take kernel length as length over all channels */ \
151   kernel_length *= channels; \
152   if (input_samples < kernel_length) \
153     res_start = kernel_length - input_samples; \
154   else \
155     res_start = 0; \
156   \
157   for (i = 0; i < res_start; i++) \
158     buffer[i] = buffer[i + input_samples]; \
159   /* i == res_start */ \
160   for (; i < kernel_length; i++) \
161     buffer[i] = src[input_samples - kernel_length + i]; \
162   \
163   self->buffer_fill += kernel_length - res_start; \
164   if (self->buffer_fill > kernel_length) \
165     self->buffer_fill = kernel_length; \
166   \
167   return input_samples / channels; \
168 } G_STMT_END
169
170 DEFINE_PROCESS_FUNC (32, float);
171 DEFINE_PROCESS_FUNC (64, double);
172
173 DEFINE_PROCESS_FUNC_FIXED_CHANNELS (32, 1, float);
174 DEFINE_PROCESS_FUNC_FIXED_CHANNELS (64, 1, double);
175
176 DEFINE_PROCESS_FUNC_FIXED_CHANNELS (32, 2, float);
177 DEFINE_PROCESS_FUNC_FIXED_CHANNELS (64, 2, double);
178
179 #undef TIME_DOMAIN_CONVOLUTION_BODY
180 #undef DEFINE_PROCESS_FUNC
181 #undef DEFINE_PROCESS_FUNC_FIXED_CHANNELS
182
183 /* This implements FFT convolution and uses the overlap-save algorithm.
184  * See http://cnx.org/content/m12022/latest/ or your favorite
185  * digital signal processing book for details.
186  *
187  * In every pass the following is calculated:
188  *
189  * y = IFFT (FFT(x) * FFT(h))
190  *
191  * where y is the output in the time domain, x the
192  * input and h the filter kernel. * is the multiplication
193  * of complex numbers.
194  *
195  * Due to the circular convolution theorem this
196  * gives in the time domain:
197  *
198  * y[t] = \sum_{u=0}^{M-1} x[t - u] * h[u]
199  *
200  * where y is the output, M is the kernel length,
201  * x the periodically extended[0] input and h the
202  * filter kernel.
203  *
204  * ([0] Periodically extended means:    )
205  * (    x[t] = x[t+kN] \forall k \in Z  )
206  * (    where N is the length of x      )
207  *
208  * This means:
209  * - Obviously x and h need to be of the same size for the FFT
210  * - The first M-1 output values are useless because they're
211  *   built from 1 up to M-1 values from the end of the input
212  *   (circular convolusion!).
213  * - The last M-1 input values are only used for 1 up to M-1
214  *   output values, i.e. they need to be used again in the
215  *   next pass for the first M-1 input values.
216  *
217  * => The first pass needs M-1 zeroes at the beginning of the
218  * input and the last M-1 input values of every pass need to
219  * be used as the first M-1 input values of the next pass.
220  *
221  * => x must be larger than h to give a useful number of output
222  * samples and h needs to be padded by zeroes at the end to give
223  * it virtually the same size as x (by M we denote the number of
224  * non-padding samples of h). If len(x)==len(h)==M only 1 output
225  * sample would be calculated per pass, len(x)==2*len(h) would
226  * give M+1 output samples, etc. Usually a factor between 4 and 8
227  * gives a low number of operations per output samples (see website
228  * given above).
229  *
230  * Overall this gives a runtime complexity per sample of
231  *
232  *   (  N log N  )
233  * O ( --------- ) compared to O (M) for the direct calculation.
234  *   ( N - M + 1 )
235  */
236 #define DEFINE_FFT_PROCESS_FUNC(width,ctype) \
237 static guint \
238 process_fft_##width (GstAudioFXBaseFIRFilter * self, const g##ctype * src, \
239     g##ctype * dst, guint input_samples) \
240 { \
241   gint channels = GST_AUDIO_FILTER_CAST (self)->format.channels; \
242   FFT_CONVOLUTION_BODY (channels); \
243 }
244
245 #define DEFINE_FFT_PROCESS_FUNC_FIXED_CHANNELS(width,channels,ctype) \
246 static guint \
247 process_fft_##channels##_##width (GstAudioFXBaseFIRFilter * self, const g##ctype * src, \
248     g##ctype * dst, guint input_samples) \
249 { \
250   FFT_CONVOLUTION_BODY (channels); \
251 }
252
253 #define FFT_CONVOLUTION_BODY(channels) G_STMT_START { \
254   gint i, j; \
255   guint pass; \
256   guint kernel_length = self->kernel_length; \
257   guint block_length = self->block_length; \
258   guint buffer_length = self->buffer_length; \
259   guint real_buffer_length = buffer_length + kernel_length - 1; \
260   guint buffer_fill = self->buffer_fill; \
261   GstFFTF64 *fft = self->fft; \
262   GstFFTF64 *ifft = self->ifft; \
263   GstFFTF64Complex *frequency_response = self->frequency_response; \
264   GstFFTF64Complex *fft_buffer = self->fft_buffer; \
265   guint frequency_response_length = self->frequency_response_length; \
266   gdouble *buffer = self->buffer; \
267   guint generated = 0; \
268   gdouble re, im; \
269   \
270   if (!fft_buffer) \
271     self->fft_buffer = fft_buffer = \
272         g_new (GstFFTF64Complex, frequency_response_length); \
273   \
274   /* Buffer contains the time domain samples of input data for one chunk \
275    * plus some more space for the inverse FFT below. \
276    * \
277    * The samples are put at offset kernel_length, the inverse FFT \
278    * overwrites everthing from offset 0 to length-kernel_length+1, keeping \
279    * the last kernel_length-1 samples for copying to the next processing \
280    * step. \
281    */ \
282   if (!buffer) { \
283     self->buffer_length = buffer_length = block_length; \
284     real_buffer_length = buffer_length + kernel_length - 1; \
285     \
286     self->buffer = buffer = g_new0 (gdouble, real_buffer_length * channels); \
287     \
288     /* Beginning has kernel_length-1 zeroes at the beginning */ \
289     self->buffer_fill = buffer_fill = kernel_length - 1; \
290   } \
291   \
292   g_assert (self->buffer_length == block_length); \
293   \
294   while (input_samples) { \
295     pass = MIN (buffer_length - buffer_fill, input_samples); \
296     \
297     /* Deinterleave channels */ \
298     for (i = 0; i < pass; i++) { \
299       for (j = 0; j < channels; j++) { \
300         buffer[real_buffer_length * j + buffer_fill + kernel_length - 1 + i] = \
301             src[i * channels + j]; \
302       } \
303     } \
304     buffer_fill += pass; \
305     src += channels * pass; \
306     input_samples -= pass; \
307     \
308     /* If we don't have a complete buffer go out */ \
309     if (buffer_fill < buffer_length) \
310       break; \
311     \
312     for (j = 0; j < channels; j++) { \
313       /* Calculate FFT of input block */ \
314       gst_fft_f64_fft (fft, \
315           buffer + real_buffer_length * j + kernel_length - 1, fft_buffer); \
316       \
317       /* Complex multiplication of input and filter spectrum */ \
318       for (i = 0; i < frequency_response_length; i++) { \
319         re = fft_buffer[i].r; \
320         im = fft_buffer[i].i; \
321         \
322         fft_buffer[i].r = \
323             re * frequency_response[i].r - \
324             im * frequency_response[i].i; \
325         fft_buffer[i].i = \
326             re * frequency_response[i].i + \
327             im * frequency_response[i].r; \
328       } \
329       \
330       /* Calculate inverse FFT of the result */ \
331       gst_fft_f64_inverse_fft (ifft, fft_buffer, \
332           buffer + real_buffer_length * j); \
333       \
334       /* Copy all except the first kernel_length-1 samples to the output */ \
335       for (i = 0; i < buffer_length - kernel_length + 1; i++) { \
336         dst[i * channels + j] = \
337             buffer[real_buffer_length * j + kernel_length - 1 + i]; \
338       } \
339       \
340       /* Copy the last kernel_length-1 samples to the beginning for the next block */ \
341       for (i = 0; i < kernel_length - 1; i++) { \
342         buffer[real_buffer_length * j + kernel_length - 1 + i] = \
343             buffer[real_buffer_length * j + buffer_length + i]; \
344       } \
345     } \
346     \
347     generated += buffer_length - kernel_length + 1; \
348     dst += channels * (buffer_length - kernel_length + 1); \
349     \
350     /* The the first kernel_length-1 samples are there already */ \
351     buffer_fill = kernel_length - 1; \
352   } \
353   \
354   /* Write back cached buffer_fill value */ \
355   self->buffer_fill = buffer_fill; \
356   \
357   return generated; \
358 } G_STMT_END
359
360 DEFINE_FFT_PROCESS_FUNC (32, float);
361 DEFINE_FFT_PROCESS_FUNC (64, double);
362
363 DEFINE_FFT_PROCESS_FUNC_FIXED_CHANNELS (32, 1, float);
364 DEFINE_FFT_PROCESS_FUNC_FIXED_CHANNELS (64, 1, double);
365
366 DEFINE_FFT_PROCESS_FUNC_FIXED_CHANNELS (32, 2, float);
367 DEFINE_FFT_PROCESS_FUNC_FIXED_CHANNELS (64, 2, double);
368
369 #undef FFT_CONVOLUTION_BODY
370 #undef DEFINE_FFT_PROCESS_FUNC
371 #undef DEFINE_FFT_PROCESS_FUNC_FIXED_CHANNELS
372
373 /* Element class */
374 static void
375     gst_audio_fx_base_fir_filter_calculate_frequency_response
376     (GstAudioFXBaseFIRFilter * self)
377 {
378   gst_fft_f64_free (self->fft);
379   self->fft = NULL;
380   gst_fft_f64_free (self->ifft);
381   self->ifft = NULL;
382   g_free (self->frequency_response);
383   self->frequency_response_length = 0;
384   g_free (self->fft_buffer);
385   self->fft_buffer = NULL;
386
387   if (self->kernel && self->kernel_length >= FFT_THRESHOLD
388       && !self->low_latency) {
389     guint block_length, i;
390     gdouble *kernel_tmp, *kernel = self->kernel;
391
392     /* We process 4 * kernel_length samples per pass in FFT mode */
393     block_length = 4 * self->kernel_length;
394     block_length = gst_fft_next_fast_length (block_length);
395     self->block_length = block_length;
396
397     kernel_tmp = g_new0 (gdouble, block_length);
398     memcpy (kernel_tmp, kernel, self->kernel_length * sizeof (gdouble));
399
400     self->fft = gst_fft_f64_new (block_length, FALSE);
401     self->ifft = gst_fft_f64_new (block_length, TRUE);
402     self->frequency_response_length = block_length / 2 + 1;
403     self->frequency_response =
404         g_new (GstFFTF64Complex, self->frequency_response_length);
405     gst_fft_f64_fft (self->fft, kernel_tmp, self->frequency_response);
406     g_free (kernel_tmp);
407
408     /* Normalize to make sure IFFT(FFT(x)) == x */
409     for (i = 0; i < self->frequency_response_length; i++) {
410       self->frequency_response[i].r /= block_length;
411       self->frequency_response[i].i /= block_length;
412     }
413   }
414 }
415
416 /* Must be called with base transform lock! */
417 static void
418 gst_audio_fx_base_fir_filter_select_process_function (GstAudioFXBaseFIRFilter *
419     self, gint width, gint channels)
420 {
421   if (width == 32 && self->fft && !self->low_latency) {
422     if (channels == 1)
423       self->process = (GstAudioFXBaseFIRFilterProcessFunc) process_fft_1_32;
424     else if (channels == 2)
425       self->process = (GstAudioFXBaseFIRFilterProcessFunc) process_fft_2_32;
426     else
427       self->process = (GstAudioFXBaseFIRFilterProcessFunc) process_fft_32;
428   } else if (width == 64 && self->fft && !self->low_latency) {
429     if (channels == 1)
430       self->process = (GstAudioFXBaseFIRFilterProcessFunc) process_fft_1_64;
431     else if (channels == 2)
432       self->process = (GstAudioFXBaseFIRFilterProcessFunc) process_fft_2_64;
433     else
434       self->process = (GstAudioFXBaseFIRFilterProcessFunc) process_fft_64;
435   } else if (width == 32) {
436     if (channels == 1)
437       self->process = (GstAudioFXBaseFIRFilterProcessFunc) process_1_32;
438     else if (channels == 2)
439       self->process = (GstAudioFXBaseFIRFilterProcessFunc) process_2_32;
440     else
441       self->process = (GstAudioFXBaseFIRFilterProcessFunc) process_32;
442   } else if (width == 64) {
443     if (channels == 1)
444       self->process = (GstAudioFXBaseFIRFilterProcessFunc) process_1_64;
445     else if (channels == 2)
446       self->process = (GstAudioFXBaseFIRFilterProcessFunc) process_2_64;
447     else
448       self->process = (GstAudioFXBaseFIRFilterProcessFunc) process_64;
449   } else {
450     self->process = NULL;
451   }
452 }
453
454 static void
455 gst_audio_fx_base_fir_filter_dispose (GObject * object)
456 {
457   GstAudioFXBaseFIRFilter *self = GST_AUDIO_FX_BASE_FIR_FILTER (object);
458
459   g_free (self->buffer);
460   self->buffer = NULL;
461   self->buffer_length = 0;
462
463   g_free (self->kernel);
464   self->kernel = NULL;
465
466   gst_fft_f64_free (self->fft);
467   self->fft = NULL;
468   gst_fft_f64_free (self->ifft);
469   self->ifft = NULL;
470
471   g_free (self->frequency_response);
472   self->frequency_response = NULL;
473
474   g_free (self->fft_buffer);
475   self->fft_buffer = NULL;
476
477   G_OBJECT_CLASS (parent_class)->dispose (object);
478 }
479
480 static void
481 gst_audio_fx_base_fir_filter_set_property (GObject * object, guint prop_id,
482     const GValue * value, GParamSpec * pspec)
483 {
484   GstAudioFXBaseFIRFilter *self = GST_AUDIO_FX_BASE_FIR_FILTER (object);
485
486   switch (prop_id) {
487     case PROP_LOW_LATENCY:{
488       gboolean low_latency;
489
490       if (GST_STATE (self) >= GST_STATE_PAUSED) {
491         g_warning ("Changing the \"low-latency\" property "
492             "is only allowed in states < PAUSED");
493         return;
494       }
495
496       GST_BASE_TRANSFORM_LOCK (self);
497       low_latency = g_value_get_boolean (value);
498
499       if (self->low_latency != low_latency) {
500         self->low_latency = low_latency;
501         gst_audio_fx_base_fir_filter_calculate_frequency_response (self);
502         gst_audio_fx_base_fir_filter_select_process_function (self,
503             GST_AUDIO_FILTER_CAST (self)->format.width,
504             GST_AUDIO_FILTER_CAST (self)->format.channels);
505       }
506       GST_BASE_TRANSFORM_UNLOCK (self);
507       break;
508     }
509     case PROP_DRAIN_ON_CHANGES:{
510       GST_BASE_TRANSFORM_LOCK (self);
511       self->drain_on_changes = g_value_get_boolean (value);
512       GST_BASE_TRANSFORM_UNLOCK (self);
513       break;
514     }
515     default:
516       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
517       break;
518   }
519 }
520
521 static void
522 gst_audio_fx_base_fir_filter_get_property (GObject * object, guint prop_id,
523     GValue * value, GParamSpec * pspec)
524 {
525   GstAudioFXBaseFIRFilter *self = GST_AUDIO_FX_BASE_FIR_FILTER (object);
526
527   switch (prop_id) {
528     case PROP_LOW_LATENCY:
529       g_value_set_boolean (value, self->low_latency);
530       break;
531     case PROP_DRAIN_ON_CHANGES:
532       g_value_set_boolean (value, self->drain_on_changes);
533       break;
534     default:
535       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
536       break;
537   }
538 }
539
540 static void
541 gst_audio_fx_base_fir_filter_class_init (GstAudioFXBaseFIRFilterClass * klass)
542 {
543   GObjectClass *gobject_class = (GObjectClass *) klass;
544   GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
545   GstAudioFilterClass *filter_class = (GstAudioFilterClass *) klass;
546   GstCaps *caps;
547
548   GST_DEBUG_CATEGORY_INIT (gst_audio_fx_base_fir_filter_debug,
549       "audiofxbasefirfilter", 0, "FIR filter base class");
550
551   gobject_class->dispose = gst_audio_fx_base_fir_filter_dispose;
552   gobject_class->set_property = gst_audio_fx_base_fir_filter_set_property;
553   gobject_class->get_property = gst_audio_fx_base_fir_filter_get_property;
554
555   /**
556    * GstAudioFXBaseFIRFilter::low-latency:
557    *
558    * Work in low-latency mode. This mode is much slower for large filter sizes
559    * but the latency is always only the pre-latency of the filter.
560    *
561    * Since: 0.10.18
562    */
563   g_object_class_install_property (gobject_class, PROP_LOW_LATENCY,
564       g_param_spec_boolean ("low-latency", "Low latency",
565           "Operate in low latency mode. This mode is slower but the "
566           "latency will only be the filter pre-latency. "
567           "Can only be changed in states < PAUSED!", DEFAULT_LOW_LATENCY,
568           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
569
570   /**
571    * GstAudioFXBaseFIRFilter::drain-on-changes:
572    *
573    * Whether the filter should be drained when its coeficients change
574    *
575    * Note: Currently this only works if the kernel size is not changed!
576    * Support for drainless kernel size changes will be added in the future.
577    *
578    * Since: 0.10.18
579    */
580   g_object_class_install_property (gobject_class, PROP_DRAIN_ON_CHANGES,
581       g_param_spec_boolean ("drain-on-changes", "Drain on changes",
582           "Drains the filter when its coeficients change",
583           DEFAULT_DRAIN_ON_CHANGES,
584           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
585
586   caps = gst_caps_from_string (ALLOWED_CAPS);
587   gst_audio_filter_class_add_pad_templates (GST_AUDIO_FILTER_CLASS (klass),
588       caps);
589   gst_caps_unref (caps);
590
591   trans_class->transform =
592       GST_DEBUG_FUNCPTR (gst_audio_fx_base_fir_filter_transform);
593   trans_class->start = GST_DEBUG_FUNCPTR (gst_audio_fx_base_fir_filter_start);
594   trans_class->stop = GST_DEBUG_FUNCPTR (gst_audio_fx_base_fir_filter_stop);
595   trans_class->sink_event =
596       GST_DEBUG_FUNCPTR (gst_audio_fx_base_fir_filter_sink_event);
597   trans_class->transform_size =
598       GST_DEBUG_FUNCPTR (gst_audio_fx_base_fir_filter_transform_size);
599   filter_class->setup = GST_DEBUG_FUNCPTR (gst_audio_fx_base_fir_filter_setup);
600 }
601
602 static void
603 gst_audio_fx_base_fir_filter_init (GstAudioFXBaseFIRFilter * self)
604 {
605   self->kernel = NULL;
606   self->buffer = NULL;
607   self->buffer_length = 0;
608
609   self->start_ts = GST_CLOCK_TIME_NONE;
610   self->start_off = GST_BUFFER_OFFSET_NONE;
611   self->nsamples_out = 0;
612   self->nsamples_in = 0;
613
614   self->low_latency = DEFAULT_LOW_LATENCY;
615   self->drain_on_changes = DEFAULT_DRAIN_ON_CHANGES;
616
617   gst_pad_set_query_function (GST_BASE_TRANSFORM (self)->srcpad,
618       gst_audio_fx_base_fir_filter_query);
619   gst_pad_set_query_type_function (GST_BASE_TRANSFORM (self)->srcpad,
620       gst_audio_fx_base_fir_filter_query_type);
621 }
622
623 void
624 gst_audio_fx_base_fir_filter_push_residue (GstAudioFXBaseFIRFilter * self)
625 {
626   GstBuffer *outbuf;
627   GstFlowReturn res;
628   gint rate = GST_AUDIO_FILTER_CAST (self)->format.rate;
629   gint channels = GST_AUDIO_FILTER_CAST (self)->format.channels;
630   gint width = GST_AUDIO_FILTER_CAST (self)->format.width / 8;
631   gint outsize, outsamples;
632   guint8 *in, *out, *data;
633   gsize size;
634
635   if (channels == 0 || rate == 0 || self->nsamples_in == 0) {
636     self->buffer_fill = 0;
637     g_free (self->buffer);
638     self->buffer = NULL;
639     return;
640   }
641
642   /* Calculate the number of samples and their memory size that
643    * should be pushed from the residue */
644   outsamples = self->nsamples_in - (self->nsamples_out - self->latency);
645   if (outsamples <= 0) {
646     self->buffer_fill = 0;
647     g_free (self->buffer);
648     self->buffer = NULL;
649     return;
650   }
651   outsize = outsamples * channels * width;
652
653   if (!self->fft || self->low_latency) {
654     gint64 diffsize, diffsamples;
655
656     /* Process the difference between latency and residue length samples
657      * to start at the actual data instead of starting at the zeros before
658      * when we only got one buffer smaller than latency */
659     diffsamples =
660         ((gint64) self->latency) - ((gint64) self->buffer_fill) / channels;
661     if (diffsamples > 0) {
662       diffsize = diffsamples * channels * width;
663       in = g_new0 (guint8, diffsize);
664       out = g_new0 (guint8, diffsize);
665       self->nsamples_out += self->process (self, in, out, diffsamples);
666       g_free (in);
667       g_free (out);
668     }
669
670     outbuf = gst_buffer_new_and_alloc (outsize);
671
672     /* Convolve the residue with zeros to get the actual remaining data */
673     in = g_new0 (guint8, outsize);
674     data = gst_buffer_map (outbuf, &size, NULL, GST_MAP_READWRITE);
675     self->nsamples_out += self->process (self, in, data, outsamples);
676     gst_buffer_unmap (outbuf, data, size);
677
678     g_free (in);
679   } else {
680     guint gensamples = 0;
681
682     outbuf = gst_buffer_new_and_alloc (outsize);
683     data = gst_buffer_map (outbuf, &size, NULL, GST_MAP_READWRITE);
684
685     while (gensamples < outsamples) {
686       guint step_insamples = self->block_length - self->buffer_fill;
687       guint8 *zeroes = g_new0 (guint8, step_insamples * channels * width);
688       guint8 *out = g_new (guint8, self->block_length * channels * width);
689       guint step_gensamples;
690
691       step_gensamples = self->process (self, zeroes, out, step_insamples);
692       g_free (zeroes);
693
694       memcpy (data + gensamples * width, out, MIN (step_gensamples,
695               outsamples - gensamples) * width);
696       gensamples += MIN (step_gensamples, outsamples - gensamples);
697
698       g_free (out);
699     }
700     self->nsamples_out += gensamples;
701
702     gst_buffer_unmap (outbuf, data, size);
703   }
704
705   /* Set timestamp, offset, etc from the values we
706    * saved when processing the regular buffers */
707   if (GST_CLOCK_TIME_IS_VALID (self->start_ts))
708     GST_BUFFER_TIMESTAMP (outbuf) = self->start_ts;
709   else
710     GST_BUFFER_TIMESTAMP (outbuf) = 0;
711   GST_BUFFER_TIMESTAMP (outbuf) +=
712       gst_util_uint64_scale_int (self->nsamples_out - outsamples -
713       self->latency, GST_SECOND, rate);
714
715   GST_BUFFER_DURATION (outbuf) =
716       gst_util_uint64_scale_int (outsamples, GST_SECOND, rate);
717
718   if (self->start_off != GST_BUFFER_OFFSET_NONE) {
719     GST_BUFFER_OFFSET (outbuf) =
720         self->start_off + self->nsamples_out - outsamples - self->latency;
721     GST_BUFFER_OFFSET_END (outbuf) = GST_BUFFER_OFFSET (outbuf) + outsamples;
722   }
723
724   GST_DEBUG_OBJECT (self, "Pushing residue buffer of size %d with timestamp: %"
725       GST_TIME_FORMAT ", duration: %" GST_TIME_FORMAT ", offset: %"
726       G_GUINT64_FORMAT ", offset_end: %" G_GUINT64_FORMAT ", nsamples_out: %d",
727       gst_buffer_get_size (outbuf),
728       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
729       GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)), GST_BUFFER_OFFSET (outbuf),
730       GST_BUFFER_OFFSET_END (outbuf), outsamples);
731
732   res = gst_pad_push (GST_BASE_TRANSFORM_CAST (self)->srcpad, outbuf);
733
734   if (G_UNLIKELY (res != GST_FLOW_OK)) {
735     GST_WARNING_OBJECT (self, "failed to push residue");
736   }
737
738   self->buffer_fill = 0;
739 }
740
741 /* GstAudioFilter vmethod implementations */
742
743 /* get notified of caps and plug in the correct process function */
744 static gboolean
745 gst_audio_fx_base_fir_filter_setup (GstAudioFilter * base,
746     GstRingBufferSpec * format)
747 {
748   GstAudioFXBaseFIRFilter *self = GST_AUDIO_FX_BASE_FIR_FILTER (base);
749
750   if (self->buffer) {
751     gst_audio_fx_base_fir_filter_push_residue (self);
752     g_free (self->buffer);
753     self->buffer = NULL;
754     self->buffer_fill = 0;
755     self->buffer_length = 0;
756     self->start_ts = GST_CLOCK_TIME_NONE;
757     self->start_off = GST_BUFFER_OFFSET_NONE;
758     self->nsamples_out = 0;
759     self->nsamples_in = 0;
760   }
761
762   gst_audio_fx_base_fir_filter_select_process_function (self, format->width,
763       format->channels);
764
765   return (self->process != NULL);
766 }
767
768 /* GstBaseTransform vmethod implementations */
769
770 static gboolean
771 gst_audio_fx_base_fir_filter_transform_size (GstBaseTransform * base,
772     GstPadDirection direction, GstCaps * caps, gsize size, GstCaps * othercaps,
773     gsize * othersize)
774 {
775   GstAudioFXBaseFIRFilter *self = GST_AUDIO_FX_BASE_FIR_FILTER (base);
776   guint blocklen;
777   GstStructure *s;
778   gint width, channels;
779
780   if (!self->fft || self->low_latency || direction == GST_PAD_SRC) {
781     *othersize = size;
782     return TRUE;
783   }
784
785   s = gst_caps_get_structure (caps, 0);
786   if (!gst_structure_get_int (s, "width", &width) ||
787       !gst_structure_get_int (s, "channels", &channels))
788     return FALSE;
789
790   width /= 8;
791
792   size /= width * channels;
793
794   blocklen = self->block_length - self->kernel_length + 1;
795   *othersize = ((size + blocklen - 1) / blocklen) * blocklen;
796
797   *othersize *= width * channels;
798
799   return TRUE;
800 }
801
802 static GstFlowReturn
803 gst_audio_fx_base_fir_filter_transform (GstBaseTransform * base,
804     GstBuffer * inbuf, GstBuffer * outbuf)
805 {
806   GstAudioFXBaseFIRFilter *self = GST_AUDIO_FX_BASE_FIR_FILTER (base);
807   GstClockTime timestamp, expected_timestamp;
808   gint channels = GST_AUDIO_FILTER_CAST (self)->format.channels;
809   gint rate = GST_AUDIO_FILTER_CAST (self)->format.rate;
810   gint width = GST_AUDIO_FILTER_CAST (self)->format.width / 8;
811   guint8 *indata, *outdata;
812   gsize insize, outsize;
813   guint input_samples;
814   guint output_samples;
815   guint generated_samples;
816   guint64 output_offset;
817   gint64 diff = 0;
818   GstClockTime stream_time;
819
820   timestamp = GST_BUFFER_TIMESTAMP (outbuf);
821
822   if (!GST_CLOCK_TIME_IS_VALID (timestamp)
823       && !GST_CLOCK_TIME_IS_VALID (self->start_ts)) {
824     GST_ERROR_OBJECT (self, "Invalid timestamp");
825     return GST_FLOW_ERROR;
826   }
827
828   stream_time =
829       gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
830
831   GST_DEBUG_OBJECT (self, "sync to %" GST_TIME_FORMAT,
832       GST_TIME_ARGS (timestamp));
833
834   if (GST_CLOCK_TIME_IS_VALID (stream_time))
835     gst_object_sync_values (G_OBJECT (self), stream_time);
836
837   g_return_val_if_fail (self->kernel != NULL, GST_FLOW_ERROR);
838   g_return_val_if_fail (channels != 0, GST_FLOW_ERROR);
839
840   if (GST_CLOCK_TIME_IS_VALID (self->start_ts))
841     expected_timestamp =
842         self->start_ts + gst_util_uint64_scale_int (self->nsamples_in,
843         GST_SECOND, rate);
844   else
845     expected_timestamp = GST_CLOCK_TIME_NONE;
846
847   /* Reset the residue if already existing on discont buffers */
848   if (GST_BUFFER_IS_DISCONT (inbuf)
849       || (GST_CLOCK_TIME_IS_VALID (expected_timestamp)
850           && (ABS (GST_CLOCK_DIFF (timestamp,
851                       expected_timestamp) > 5 * GST_MSECOND)))) {
852     GST_DEBUG_OBJECT (self, "Discontinuity detected - flushing");
853     if (GST_CLOCK_TIME_IS_VALID (expected_timestamp))
854       gst_audio_fx_base_fir_filter_push_residue (self);
855     self->buffer_fill = 0;
856     g_free (self->buffer);
857     self->buffer = NULL;
858     self->start_ts = timestamp;
859     self->start_off = GST_BUFFER_OFFSET (inbuf);
860     self->nsamples_out = 0;
861     self->nsamples_in = 0;
862   } else if (!GST_CLOCK_TIME_IS_VALID (self->start_ts)) {
863     self->start_ts = timestamp;
864     self->start_off = GST_BUFFER_OFFSET (inbuf);
865   }
866
867   indata = gst_buffer_map (inbuf, &insize, NULL, GST_MAP_READ);
868   outdata = gst_buffer_map (outbuf, &outsize, NULL, GST_MAP_WRITE);
869
870   input_samples = (insize / width) / channels;
871   output_samples = (outsize / width) / channels;
872
873   self->nsamples_in += input_samples;
874
875   generated_samples = self->process (self, indata, outdata, input_samples);
876
877   gst_buffer_unmap (inbuf, indata, insize);
878   gst_buffer_unmap (outbuf, outdata, outsize);
879
880   g_assert (generated_samples <= output_samples);
881   self->nsamples_out += generated_samples;
882   if (generated_samples == 0)
883     return GST_BASE_TRANSFORM_FLOW_DROPPED;
884
885   /* Calculate the number of samples we can push out now without outputting
886    * latency zeros in the beginning */
887   diff = ((gint64) self->nsamples_out) - ((gint64) self->latency);
888   if (diff < 0) {
889     return GST_BASE_TRANSFORM_FLOW_DROPPED;
890   } else if (diff < generated_samples) {
891     gint64 tmp = diff;
892     diff = generated_samples - diff;
893     generated_samples = tmp;
894   }
895   gst_buffer_resize (outbuf, diff * width * channels,
896       generated_samples * width * channels);
897
898   output_offset = self->nsamples_out - self->latency - generated_samples;
899   GST_BUFFER_TIMESTAMP (outbuf) =
900       self->start_ts + gst_util_uint64_scale_int (output_offset, GST_SECOND,
901       rate);
902   GST_BUFFER_DURATION (outbuf) =
903       gst_util_uint64_scale_int (output_samples, GST_SECOND, rate);
904   if (self->start_off != GST_BUFFER_OFFSET_NONE) {
905     GST_BUFFER_OFFSET (outbuf) = self->start_off + output_offset;
906     GST_BUFFER_OFFSET_END (outbuf) =
907         GST_BUFFER_OFFSET (outbuf) + generated_samples;
908   } else {
909     GST_BUFFER_OFFSET (outbuf) = GST_BUFFER_OFFSET_NONE;
910     GST_BUFFER_OFFSET_END (outbuf) = GST_BUFFER_OFFSET_NONE;
911   }
912
913   GST_DEBUG_OBJECT (self, "Pushing buffer of size %d with timestamp: %"
914       GST_TIME_FORMAT ", duration: %" GST_TIME_FORMAT ", offset: %"
915       G_GUINT64_FORMAT ", offset_end: %" G_GUINT64_FORMAT ", nsamples_out: %d",
916       gst_buffer_get_size (outbuf),
917       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
918       GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)), GST_BUFFER_OFFSET (outbuf),
919       GST_BUFFER_OFFSET_END (outbuf), generated_samples);
920
921   return GST_FLOW_OK;
922 }
923
924 static gboolean
925 gst_audio_fx_base_fir_filter_start (GstBaseTransform * base)
926 {
927   GstAudioFXBaseFIRFilter *self = GST_AUDIO_FX_BASE_FIR_FILTER (base);
928
929   self->buffer_fill = 0;
930   g_free (self->buffer);
931   self->buffer = NULL;
932   self->start_ts = GST_CLOCK_TIME_NONE;
933   self->start_off = GST_BUFFER_OFFSET_NONE;
934   self->nsamples_out = 0;
935   self->nsamples_in = 0;
936
937   return TRUE;
938 }
939
940 static gboolean
941 gst_audio_fx_base_fir_filter_stop (GstBaseTransform * base)
942 {
943   GstAudioFXBaseFIRFilter *self = GST_AUDIO_FX_BASE_FIR_FILTER (base);
944
945   g_free (self->buffer);
946   self->buffer = NULL;
947   self->buffer_length = 0;
948
949   return TRUE;
950 }
951
952 static gboolean
953 gst_audio_fx_base_fir_filter_query (GstPad * pad, GstQuery * query)
954 {
955   GstAudioFXBaseFIRFilter *self =
956       GST_AUDIO_FX_BASE_FIR_FILTER (gst_pad_get_parent (pad));
957   gboolean res = TRUE;
958
959   switch (GST_QUERY_TYPE (query)) {
960     case GST_QUERY_LATENCY:
961     {
962       GstClockTime min, max;
963       gboolean live;
964       guint64 latency;
965       GstPad *peer;
966       gint rate = GST_AUDIO_FILTER (self)->format.rate;
967
968       if (rate == 0) {
969         res = FALSE;
970       } else if ((peer = gst_pad_get_peer (GST_BASE_TRANSFORM (self)->sinkpad))) {
971         if ((res = gst_pad_query (peer, query))) {
972           gst_query_parse_latency (query, &live, &min, &max);
973
974           GST_DEBUG_OBJECT (self, "Peer latency: min %"
975               GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
976               GST_TIME_ARGS (min), GST_TIME_ARGS (max));
977
978           if (self->fft && !self->low_latency)
979             latency = self->block_length - self->kernel_length + 1;
980           else
981             latency = self->latency;
982
983           /* add our own latency */
984           latency = gst_util_uint64_scale_round (latency, GST_SECOND, rate);
985
986           GST_DEBUG_OBJECT (self, "Our latency: %"
987               GST_TIME_FORMAT, GST_TIME_ARGS (latency));
988
989           min += latency;
990           if (max != GST_CLOCK_TIME_NONE)
991             max += latency;
992
993           GST_DEBUG_OBJECT (self, "Calculated total latency : min %"
994               GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
995               GST_TIME_ARGS (min), GST_TIME_ARGS (max));
996
997           gst_query_set_latency (query, live, min, max);
998         }
999         gst_object_unref (peer);
1000       }
1001       break;
1002     }
1003     default:
1004       res = gst_pad_query_default (pad, query);
1005       break;
1006   }
1007   gst_object_unref (self);
1008   return res;
1009 }
1010
1011 static const GstQueryType *
1012 gst_audio_fx_base_fir_filter_query_type (GstPad * pad)
1013 {
1014   static const GstQueryType types[] = {
1015     GST_QUERY_LATENCY,
1016     0
1017   };
1018
1019   return types;
1020 }
1021
1022 static gboolean
1023 gst_audio_fx_base_fir_filter_sink_event (GstBaseTransform * base,
1024     GstEvent * event)
1025 {
1026   GstAudioFXBaseFIRFilter *self = GST_AUDIO_FX_BASE_FIR_FILTER (base);
1027
1028   switch (GST_EVENT_TYPE (event)) {
1029     case GST_EVENT_EOS:
1030       gst_audio_fx_base_fir_filter_push_residue (self);
1031       self->start_ts = GST_CLOCK_TIME_NONE;
1032       self->start_off = GST_BUFFER_OFFSET_NONE;
1033       self->nsamples_out = 0;
1034       self->nsamples_in = 0;
1035       break;
1036     default:
1037       break;
1038   }
1039
1040   return GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (base, event);
1041 }
1042
1043 void
1044 gst_audio_fx_base_fir_filter_set_kernel (GstAudioFXBaseFIRFilter * self,
1045     gdouble * kernel, guint kernel_length, guint64 latency)
1046 {
1047   gboolean latency_changed;
1048
1049   g_return_if_fail (kernel != NULL);
1050   g_return_if_fail (self != NULL);
1051
1052   GST_BASE_TRANSFORM_LOCK (self);
1053
1054   latency_changed = (self->latency != latency
1055       || (!self->low_latency && self->kernel_length < FFT_THRESHOLD
1056           && kernel_length >= FFT_THRESHOLD)
1057       || (!self->low_latency && self->kernel_length >= FFT_THRESHOLD
1058           && kernel_length < FFT_THRESHOLD));
1059
1060   /* FIXME: If the latency changes, the buffer size changes too and we
1061    * have to drain in any case until this is fixed in the future */
1062   if (self->buffer && (!self->drain_on_changes || latency_changed)) {
1063     gst_audio_fx_base_fir_filter_push_residue (self);
1064     self->start_ts = GST_CLOCK_TIME_NONE;
1065     self->start_off = GST_BUFFER_OFFSET_NONE;
1066     self->nsamples_out = 0;
1067     self->nsamples_in = 0;
1068     self->buffer_fill = 0;
1069   }
1070
1071   g_free (self->kernel);
1072   if (!self->drain_on_changes || latency_changed) {
1073     g_free (self->buffer);
1074     self->buffer = NULL;
1075     self->buffer_fill = 0;
1076     self->buffer_length = 0;
1077   }
1078
1079   self->kernel = kernel;
1080   self->kernel_length = kernel_length;
1081
1082   gst_audio_fx_base_fir_filter_calculate_frequency_response (self);
1083   gst_audio_fx_base_fir_filter_select_process_function (self,
1084       GST_AUDIO_FILTER_CAST (self)->format.width,
1085       GST_AUDIO_FILTER_CAST (self)->format.channels);
1086
1087   if (latency_changed) {
1088     self->latency = latency;
1089     gst_element_post_message (GST_ELEMENT (self),
1090         gst_message_new_latency (GST_OBJECT (self)));
1091   }
1092
1093   GST_BASE_TRANSFORM_UNLOCK (self);
1094 }