Merge branch 'master' into 0.11
[platform/upstream/gst-plugins-good.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_event (GstBaseTransform * base,
74     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->event = GST_DEBUG_FUNCPTR (gst_audio_fx_base_fir_filter_event);
596   trans_class->transform_size =
597       GST_DEBUG_FUNCPTR (gst_audio_fx_base_fir_filter_transform_size);
598   filter_class->setup = GST_DEBUG_FUNCPTR (gst_audio_fx_base_fir_filter_setup);
599 }
600
601 static void
602 gst_audio_fx_base_fir_filter_init (GstAudioFXBaseFIRFilter * self)
603 {
604   self->kernel = NULL;
605   self->buffer = NULL;
606   self->buffer_length = 0;
607
608   self->start_ts = GST_CLOCK_TIME_NONE;
609   self->start_off = GST_BUFFER_OFFSET_NONE;
610   self->nsamples_out = 0;
611   self->nsamples_in = 0;
612
613   self->low_latency = DEFAULT_LOW_LATENCY;
614   self->drain_on_changes = DEFAULT_DRAIN_ON_CHANGES;
615
616   gst_pad_set_query_function (GST_BASE_TRANSFORM (self)->srcpad,
617       gst_audio_fx_base_fir_filter_query);
618   gst_pad_set_query_type_function (GST_BASE_TRANSFORM (self)->srcpad,
619       gst_audio_fx_base_fir_filter_query_type);
620 }
621
622 void
623 gst_audio_fx_base_fir_filter_push_residue (GstAudioFXBaseFIRFilter * self)
624 {
625   GstBuffer *outbuf;
626   GstFlowReturn res;
627   gint rate = GST_AUDIO_FILTER_CAST (self)->format.rate;
628   gint channels = GST_AUDIO_FILTER_CAST (self)->format.channels;
629   gint width = GST_AUDIO_FILTER_CAST (self)->format.width / 8;
630   gint outsize, outsamples;
631   guint8 *in, *out, *data;
632   gsize size;
633
634   if (channels == 0 || rate == 0 || self->nsamples_in == 0) {
635     self->buffer_fill = 0;
636     g_free (self->buffer);
637     self->buffer = NULL;
638     return;
639   }
640
641   /* Calculate the number of samples and their memory size that
642    * should be pushed from the residue */
643   outsamples = self->nsamples_in - (self->nsamples_out - self->latency);
644   if (outsamples <= 0) {
645     self->buffer_fill = 0;
646     g_free (self->buffer);
647     self->buffer = NULL;
648     return;
649   }
650   outsize = outsamples * channels * width;
651
652   if (!self->fft || self->low_latency) {
653     gint64 diffsize, diffsamples;
654
655     /* Process the difference between latency and residue length samples
656      * to start at the actual data instead of starting at the zeros before
657      * when we only got one buffer smaller than latency */
658     diffsamples =
659         ((gint64) self->latency) - ((gint64) self->buffer_fill) / channels;
660     if (diffsamples > 0) {
661       diffsize = diffsamples * channels * width;
662       in = g_new0 (guint8, diffsize);
663       out = g_new0 (guint8, diffsize);
664       self->nsamples_out += self->process (self, in, out, diffsamples);
665       g_free (in);
666       g_free (out);
667     }
668
669     outbuf = gst_buffer_new_and_alloc (outsize);
670
671     /* Convolve the residue with zeros to get the actual remaining data */
672     in = g_new0 (guint8, outsize);
673     data = gst_buffer_map (outbuf, &size, NULL, GST_MAP_READWRITE);
674     self->nsamples_out += self->process (self, in, data, outsamples);
675     gst_buffer_unmap (outbuf, data, size);
676
677     g_free (in);
678   } else {
679     guint gensamples = 0;
680
681     outbuf = gst_buffer_new_and_alloc (outsize);
682     data = gst_buffer_map (outbuf, &size, NULL, GST_MAP_READWRITE);
683
684     while (gensamples < outsamples) {
685       guint step_insamples = self->block_length - self->buffer_fill;
686       guint8 *zeroes = g_new0 (guint8, step_insamples * channels * width);
687       guint8 *out = g_new (guint8, self->block_length * channels * width);
688       guint step_gensamples;
689
690       step_gensamples = self->process (self, zeroes, out, step_insamples);
691       g_free (zeroes);
692
693       memcpy (data + gensamples * width, out, MIN (step_gensamples,
694               outsamples - gensamples) * width);
695       gensamples += MIN (step_gensamples, outsamples - gensamples);
696
697       g_free (out);
698     }
699     self->nsamples_out += gensamples;
700
701     gst_buffer_unmap (outbuf, data, size);
702   }
703
704   /* Set timestamp, offset, etc from the values we
705    * saved when processing the regular buffers */
706   if (GST_CLOCK_TIME_IS_VALID (self->start_ts))
707     GST_BUFFER_TIMESTAMP (outbuf) = self->start_ts;
708   else
709     GST_BUFFER_TIMESTAMP (outbuf) = 0;
710   GST_BUFFER_TIMESTAMP (outbuf) +=
711       gst_util_uint64_scale_int (self->nsamples_out - outsamples -
712       self->latency, GST_SECOND, rate);
713
714   GST_BUFFER_DURATION (outbuf) =
715       gst_util_uint64_scale_int (outsamples, GST_SECOND, rate);
716
717   if (self->start_off != GST_BUFFER_OFFSET_NONE) {
718     GST_BUFFER_OFFSET (outbuf) =
719         self->start_off + self->nsamples_out - outsamples - self->latency;
720     GST_BUFFER_OFFSET_END (outbuf) = GST_BUFFER_OFFSET (outbuf) + outsamples;
721   }
722
723   GST_DEBUG_OBJECT (self, "Pushing residue buffer of size %d with timestamp: %"
724       GST_TIME_FORMAT ", duration: %" GST_TIME_FORMAT ", offset: %"
725       G_GUINT64_FORMAT ", offset_end: %" G_GUINT64_FORMAT ", nsamples_out: %d",
726       gst_buffer_get_size (outbuf),
727       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
728       GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)), GST_BUFFER_OFFSET (outbuf),
729       GST_BUFFER_OFFSET_END (outbuf), outsamples);
730
731   res = gst_pad_push (GST_BASE_TRANSFORM_CAST (self)->srcpad, outbuf);
732
733   if (G_UNLIKELY (res != GST_FLOW_OK)) {
734     GST_WARNING_OBJECT (self, "failed to push residue");
735   }
736
737   self->buffer_fill = 0;
738 }
739
740 /* GstAudioFilter vmethod implementations */
741
742 /* get notified of caps and plug in the correct process function */
743 static gboolean
744 gst_audio_fx_base_fir_filter_setup (GstAudioFilter * base,
745     GstRingBufferSpec * format)
746 {
747   GstAudioFXBaseFIRFilter *self = GST_AUDIO_FX_BASE_FIR_FILTER (base);
748
749   if (self->buffer) {
750     gst_audio_fx_base_fir_filter_push_residue (self);
751     g_free (self->buffer);
752     self->buffer = NULL;
753     self->buffer_fill = 0;
754     self->buffer_length = 0;
755     self->start_ts = GST_CLOCK_TIME_NONE;
756     self->start_off = GST_BUFFER_OFFSET_NONE;
757     self->nsamples_out = 0;
758     self->nsamples_in = 0;
759   }
760
761   gst_audio_fx_base_fir_filter_select_process_function (self, format->width,
762       format->channels);
763
764   return (self->process != NULL);
765 }
766
767 /* GstBaseTransform vmethod implementations */
768
769 static gboolean
770 gst_audio_fx_base_fir_filter_transform_size (GstBaseTransform * base,
771     GstPadDirection direction, GstCaps * caps, gsize size, GstCaps * othercaps,
772     gsize * othersize)
773 {
774   GstAudioFXBaseFIRFilter *self = GST_AUDIO_FX_BASE_FIR_FILTER (base);
775   guint blocklen;
776   GstStructure *s;
777   gint width, channels;
778
779   if (!self->fft || self->low_latency || direction == GST_PAD_SRC) {
780     *othersize = size;
781     return TRUE;
782   }
783
784   s = gst_caps_get_structure (caps, 0);
785   if (!gst_structure_get_int (s, "width", &width) ||
786       !gst_structure_get_int (s, "channels", &channels))
787     return FALSE;
788
789   width /= 8;
790
791   size /= width * channels;
792
793   blocklen = self->block_length - self->kernel_length + 1;
794   *othersize = ((size + blocklen - 1) / blocklen) * blocklen;
795
796   *othersize *= width * channels;
797
798   return TRUE;
799 }
800
801 static GstFlowReturn
802 gst_audio_fx_base_fir_filter_transform (GstBaseTransform * base,
803     GstBuffer * inbuf, GstBuffer * outbuf)
804 {
805   GstAudioFXBaseFIRFilter *self = GST_AUDIO_FX_BASE_FIR_FILTER (base);
806   GstClockTime timestamp, expected_timestamp;
807   gint channels = GST_AUDIO_FILTER_CAST (self)->format.channels;
808   gint rate = GST_AUDIO_FILTER_CAST (self)->format.rate;
809   gint width = GST_AUDIO_FILTER_CAST (self)->format.width / 8;
810   guint8 *indata, *outdata;
811   gsize insize, outsize;
812   guint input_samples;
813   guint output_samples;
814   guint generated_samples;
815   guint64 output_offset;
816   gint64 diff = 0;
817   GstClockTime stream_time;
818
819   timestamp = GST_BUFFER_TIMESTAMP (outbuf);
820
821   if (!GST_CLOCK_TIME_IS_VALID (timestamp)
822       && !GST_CLOCK_TIME_IS_VALID (self->start_ts)) {
823     GST_ERROR_OBJECT (self, "Invalid timestamp");
824     return GST_FLOW_ERROR;
825   }
826
827   stream_time =
828       gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
829
830   GST_DEBUG_OBJECT (self, "sync to %" GST_TIME_FORMAT,
831       GST_TIME_ARGS (timestamp));
832
833   if (GST_CLOCK_TIME_IS_VALID (stream_time))
834     gst_object_sync_values (G_OBJECT (self), stream_time);
835
836   g_return_val_if_fail (self->kernel != NULL, GST_FLOW_ERROR);
837   g_return_val_if_fail (channels != 0, GST_FLOW_ERROR);
838
839   if (GST_CLOCK_TIME_IS_VALID (self->start_ts))
840     expected_timestamp =
841         self->start_ts + gst_util_uint64_scale_int (self->nsamples_in,
842         GST_SECOND, rate);
843   else
844     expected_timestamp = GST_CLOCK_TIME_NONE;
845
846   /* Reset the residue if already existing on discont buffers */
847   if (GST_BUFFER_IS_DISCONT (inbuf)
848       || (GST_CLOCK_TIME_IS_VALID (expected_timestamp)
849           && (ABS (GST_CLOCK_DIFF (timestamp,
850                       expected_timestamp) > 5 * GST_MSECOND)))) {
851     GST_DEBUG_OBJECT (self, "Discontinuity detected - flushing");
852     if (GST_CLOCK_TIME_IS_VALID (expected_timestamp))
853       gst_audio_fx_base_fir_filter_push_residue (self);
854     self->buffer_fill = 0;
855     g_free (self->buffer);
856     self->buffer = NULL;
857     self->start_ts = timestamp;
858     self->start_off = GST_BUFFER_OFFSET (inbuf);
859     self->nsamples_out = 0;
860     self->nsamples_in = 0;
861   } else if (!GST_CLOCK_TIME_IS_VALID (self->start_ts)) {
862     self->start_ts = timestamp;
863     self->start_off = GST_BUFFER_OFFSET (inbuf);
864   }
865
866   indata = gst_buffer_map (inbuf, &insize, NULL, GST_MAP_READ);
867   outdata = gst_buffer_map (outbuf, &outsize, NULL, GST_MAP_WRITE);
868
869   input_samples = (insize / width) / channels;
870   output_samples = (outsize / width) / channels;
871
872   self->nsamples_in += input_samples;
873
874   generated_samples = self->process (self, indata, outdata, input_samples);
875
876   gst_buffer_unmap (inbuf, indata, insize);
877   gst_buffer_unmap (outbuf, outdata, outsize);
878
879   g_assert (generated_samples <= output_samples);
880   self->nsamples_out += generated_samples;
881   if (generated_samples == 0)
882     return GST_BASE_TRANSFORM_FLOW_DROPPED;
883
884   /* Calculate the number of samples we can push out now without outputting
885    * latency zeros in the beginning */
886   diff = ((gint64) self->nsamples_out) - ((gint64) self->latency);
887   if (diff < 0) {
888     return GST_BASE_TRANSFORM_FLOW_DROPPED;
889   } else if (diff < generated_samples) {
890     gint64 tmp = diff;
891     diff = generated_samples - diff;
892     generated_samples = tmp;
893   }
894   gst_buffer_resize (outbuf, diff * width * channels,
895       generated_samples * width * channels);
896
897   output_offset = self->nsamples_out - self->latency - generated_samples;
898   GST_BUFFER_TIMESTAMP (outbuf) =
899       self->start_ts + gst_util_uint64_scale_int (output_offset, GST_SECOND,
900       rate);
901   GST_BUFFER_DURATION (outbuf) =
902       gst_util_uint64_scale_int (output_samples, GST_SECOND, rate);
903   if (self->start_off != GST_BUFFER_OFFSET_NONE) {
904     GST_BUFFER_OFFSET (outbuf) = self->start_off + output_offset;
905     GST_BUFFER_OFFSET_END (outbuf) =
906         GST_BUFFER_OFFSET (outbuf) + generated_samples;
907   } else {
908     GST_BUFFER_OFFSET (outbuf) = GST_BUFFER_OFFSET_NONE;
909     GST_BUFFER_OFFSET_END (outbuf) = GST_BUFFER_OFFSET_NONE;
910   }
911
912   GST_DEBUG_OBJECT (self, "Pushing buffer of size %d with timestamp: %"
913       GST_TIME_FORMAT ", duration: %" GST_TIME_FORMAT ", offset: %"
914       G_GUINT64_FORMAT ", offset_end: %" G_GUINT64_FORMAT ", nsamples_out: %d",
915       gst_buffer_get_size (outbuf),
916       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
917       GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)), GST_BUFFER_OFFSET (outbuf),
918       GST_BUFFER_OFFSET_END (outbuf), generated_samples);
919
920   return GST_FLOW_OK;
921 }
922
923 static gboolean
924 gst_audio_fx_base_fir_filter_start (GstBaseTransform * base)
925 {
926   GstAudioFXBaseFIRFilter *self = GST_AUDIO_FX_BASE_FIR_FILTER (base);
927
928   self->buffer_fill = 0;
929   g_free (self->buffer);
930   self->buffer = NULL;
931   self->start_ts = GST_CLOCK_TIME_NONE;
932   self->start_off = GST_BUFFER_OFFSET_NONE;
933   self->nsamples_out = 0;
934   self->nsamples_in = 0;
935
936   return TRUE;
937 }
938
939 static gboolean
940 gst_audio_fx_base_fir_filter_stop (GstBaseTransform * base)
941 {
942   GstAudioFXBaseFIRFilter *self = GST_AUDIO_FX_BASE_FIR_FILTER (base);
943
944   g_free (self->buffer);
945   self->buffer = NULL;
946   self->buffer_length = 0;
947
948   return TRUE;
949 }
950
951 static gboolean
952 gst_audio_fx_base_fir_filter_query (GstPad * pad, GstQuery * query)
953 {
954   GstAudioFXBaseFIRFilter *self =
955       GST_AUDIO_FX_BASE_FIR_FILTER (gst_pad_get_parent (pad));
956   gboolean res = TRUE;
957
958   switch (GST_QUERY_TYPE (query)) {
959     case GST_QUERY_LATENCY:
960     {
961       GstClockTime min, max;
962       gboolean live;
963       guint64 latency;
964       GstPad *peer;
965       gint rate = GST_AUDIO_FILTER (self)->format.rate;
966
967       if (rate == 0) {
968         res = FALSE;
969       } else if ((peer = gst_pad_get_peer (GST_BASE_TRANSFORM (self)->sinkpad))) {
970         if ((res = gst_pad_query (peer, query))) {
971           gst_query_parse_latency (query, &live, &min, &max);
972
973           GST_DEBUG_OBJECT (self, "Peer latency: min %"
974               GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
975               GST_TIME_ARGS (min), GST_TIME_ARGS (max));
976
977           if (self->fft && !self->low_latency)
978             latency = self->block_length - self->kernel_length + 1;
979           else
980             latency = self->latency;
981
982           /* add our own latency */
983           latency = gst_util_uint64_scale_round (latency, GST_SECOND, rate);
984
985           GST_DEBUG_OBJECT (self, "Our latency: %"
986               GST_TIME_FORMAT, GST_TIME_ARGS (latency));
987
988           min += latency;
989           if (max != GST_CLOCK_TIME_NONE)
990             max += latency;
991
992           GST_DEBUG_OBJECT (self, "Calculated total latency : min %"
993               GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
994               GST_TIME_ARGS (min), GST_TIME_ARGS (max));
995
996           gst_query_set_latency (query, live, min, max);
997         }
998         gst_object_unref (peer);
999       }
1000       break;
1001     }
1002     default:
1003       res = gst_pad_query_default (pad, query);
1004       break;
1005   }
1006   gst_object_unref (self);
1007   return res;
1008 }
1009
1010 static const GstQueryType *
1011 gst_audio_fx_base_fir_filter_query_type (GstPad * pad)
1012 {
1013   static const GstQueryType types[] = {
1014     GST_QUERY_LATENCY,
1015     0
1016   };
1017
1018   return types;
1019 }
1020
1021 static gboolean
1022 gst_audio_fx_base_fir_filter_event (GstBaseTransform * base, GstEvent * event)
1023 {
1024   GstAudioFXBaseFIRFilter *self = GST_AUDIO_FX_BASE_FIR_FILTER (base);
1025
1026   switch (GST_EVENT_TYPE (event)) {
1027     case GST_EVENT_EOS:
1028       gst_audio_fx_base_fir_filter_push_residue (self);
1029       self->start_ts = GST_CLOCK_TIME_NONE;
1030       self->start_off = GST_BUFFER_OFFSET_NONE;
1031       self->nsamples_out = 0;
1032       self->nsamples_in = 0;
1033       break;
1034     default:
1035       break;
1036   }
1037
1038   return GST_BASE_TRANSFORM_CLASS (parent_class)->event (base, event);
1039 }
1040
1041 void
1042 gst_audio_fx_base_fir_filter_set_kernel (GstAudioFXBaseFIRFilter * self,
1043     gdouble * kernel, guint kernel_length, guint64 latency)
1044 {
1045   gboolean latency_changed;
1046
1047   g_return_if_fail (kernel != NULL);
1048   g_return_if_fail (self != NULL);
1049
1050   GST_BASE_TRANSFORM_LOCK (self);
1051
1052   latency_changed = (self->latency != latency
1053       || (!self->low_latency && self->kernel_length < FFT_THRESHOLD
1054           && kernel_length >= FFT_THRESHOLD)
1055       || (!self->low_latency && self->kernel_length >= FFT_THRESHOLD
1056           && kernel_length < FFT_THRESHOLD));
1057
1058   /* FIXME: If the latency changes, the buffer size changes too and we
1059    * have to drain in any case until this is fixed in the future */
1060   if (self->buffer && (!self->drain_on_changes || latency_changed)) {
1061     gst_audio_fx_base_fir_filter_push_residue (self);
1062     self->start_ts = GST_CLOCK_TIME_NONE;
1063     self->start_off = GST_BUFFER_OFFSET_NONE;
1064     self->nsamples_out = 0;
1065     self->nsamples_in = 0;
1066     self->buffer_fill = 0;
1067   }
1068
1069   g_free (self->kernel);
1070   if (!self->drain_on_changes || latency_changed) {
1071     g_free (self->buffer);
1072     self->buffer = NULL;
1073     self->buffer_fill = 0;
1074     self->buffer_length = 0;
1075   }
1076
1077   self->kernel = kernel;
1078   self->kernel_length = kernel_length;
1079
1080   gst_audio_fx_base_fir_filter_calculate_frequency_response (self);
1081   gst_audio_fx_base_fir_filter_select_process_function (self,
1082       GST_AUDIO_FILTER_CAST (self)->format.width,
1083       GST_AUDIO_FILTER_CAST (self)->format.channels);
1084
1085   if (latency_changed) {
1086     self->latency = latency;
1087     gst_element_post_message (GST_ELEMENT (self),
1088         gst_message_new_latency (GST_OBJECT (self)));
1089   }
1090
1091   GST_BASE_TRANSFORM_UNLOCK (self);
1092 }