gst/filter/: Post a GST_MESSAGE_LATENCY if the latency changes.
[platform/upstream/gst-plugins-good.git] / gst / audiofx / audiowsincband.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 Sebastian Dröge <slomo@circular-chaos.org>
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  * this windowed sinc filter is taken from the freely downloadable DSP book,
25  * "The Scientist and Engineer's Guide to Digital Signal Processing",
26  * chapter 16
27  * available at http://www.dspguide.com/
28  *
29  * TODO:  - Implement the convolution in place, probably only makes sense
30  *          when using FFT convolution as currently the convolution itself
31  *          is probably the bottleneck
32  *        - Maybe allow cascading the filter to get a better stopband attenuation.
33  *          Can be done by convolving a filter kernel with itself
34  *        - Drop the first kernel_length/2 samples and append the same number of
35  *          samples on EOS as the first few samples are essentialy zero.
36  */
37
38 /**
39  * SECTION:element-bpwsinc
40  * @short_description: Windowed Sinc band pass and band reject filter
41  *
42  * <refsect2>
43  * <para>
44  * Attenuates all frequencies outside (bandpass) or inside (bandreject) of a frequency
45  * band. The length parameter controls the rolloff, the window parameter
46  * controls rolloff and stopband attenuation. The Hamming window provides a faster rolloff but a bit
47  * worse stopband attenuation, the other way around for the Blackman window.
48  * </para>
49  * <para>
50  * This element has the advantage over the Chebyshev bandpass and bandreject filter that it has
51  * a much better rolloff when using a larger kernel size and almost linear phase. The only
52  * disadvantage is the much slower execution time with larger kernels.
53  * </para>
54  * <title>Example launch line</title>
55  * <para>
56  * <programlisting>
57  * gst-launch audiotestsrc freq=1500 ! audioconvert ! bpwsinc mode=band-pass lower-frequency=3000 upper-frequency=10000 length=501 window=blackman ! audioconvert ! alsasink
58  * gst-launch filesrc location="melo1.ogg" ! oggdemux ! vorbisdec ! audioconvert ! bpwsinc mode=band-reject lower-frequency=59 upper-frequency=61 length=10001 window=hamming ! audioconvert ! alsasink
59  * gst-launch audiotestsrc wave=white-noise ! audioconvert ! bpwsinc mode=band-pass lower-frequency=1000 upper-frequency=2000 length=31 ! audioconvert ! alsasink
60  * </programlisting>
61  * </para>
62  * </refsect2>
63  */
64
65 #ifdef HAVE_CONFIG_H
66 #include "config.h"
67 #endif
68
69 #include <string.h>
70 #include <math.h>
71 #include <gst/gst.h>
72 #include <gst/audio/gstaudiofilter.h>
73 #include <gst/controller/gstcontroller.h>
74
75 #include "gstbpwsinc.h"
76
77 #define GST_CAT_DEFAULT gst_bpwsinc_debug
78 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
79
80 static const GstElementDetails bpwsinc_details =
81 GST_ELEMENT_DETAILS ("Band-pass and Band-reject Windowed sinc filter",
82     "Filter/Effect/Audio",
83     "Band-pass Windowed sinc filter",
84     "Thomas <thomas@apestaart.org>, "
85     "Steven W. Smith, "
86     "Dreamlab Technologies Ltd. <mathis.hofer@dreamlab.net>, "
87     "Sebastian Dröge <slomo@circular-chaos.org>");
88
89 /* Filter signals and args */
90 enum
91 {
92   /* FILL ME */
93   LAST_SIGNAL
94 };
95
96 enum
97 {
98   PROP_0,
99   PROP_LENGTH,
100   PROP_LOWER_FREQUENCY,
101   PROP_UPPER_FREQUENCY,
102   PROP_MODE,
103   PROP_WINDOW
104 };
105
106 enum
107 {
108   MODE_BAND_PASS = 0,
109   MODE_BAND_REJECT
110 };
111
112 #define GST_TYPE_BPWSINC_MODE (gst_bpwsinc_mode_get_type ())
113 static GType
114 gst_bpwsinc_mode_get_type (void)
115 {
116   static GType gtype = 0;
117
118   if (gtype == 0) {
119     static const GEnumValue values[] = {
120       {MODE_BAND_PASS, "Band pass (default)",
121           "band-pass"},
122       {MODE_BAND_REJECT, "Band reject",
123           "band-reject"},
124       {0, NULL, NULL}
125     };
126
127     gtype = g_enum_register_static ("GstBPWSincMode", values);
128   }
129   return gtype;
130 }
131
132 enum
133 {
134   WINDOW_HAMMING = 0,
135   WINDOW_BLACKMAN
136 };
137
138 #define GST_TYPE_BPWSINC_WINDOW (gst_bpwsinc_window_get_type ())
139 static GType
140 gst_bpwsinc_window_get_type (void)
141 {
142   static GType gtype = 0;
143
144   if (gtype == 0) {
145     static const GEnumValue values[] = {
146       {WINDOW_HAMMING, "Hamming window (default)",
147           "hamming"},
148       {WINDOW_BLACKMAN, "Blackman window",
149           "blackman"},
150       {0, NULL, NULL}
151     };
152
153     gtype = g_enum_register_static ("GstBPWSincWindow", values);
154   }
155   return gtype;
156 }
157
158 #define ALLOWED_CAPS \
159     "audio/x-raw-float, "                                             \
160     " width = (int) { 32, 64 }, "                                     \
161     " endianness = (int) BYTE_ORDER, "                                \
162     " rate = (int) [ 1, MAX ], "                                      \
163     " channels = (int) [ 1, MAX ] "
164
165 #define DEBUG_INIT(bla) \
166   GST_DEBUG_CATEGORY_INIT (gst_bpwsinc_debug, "bpwsinc", 0, "Band-pass and Band-reject Windowed sinc filter plugin");
167
168 GST_BOILERPLATE_FULL (GstBPWSinc, gst_bpwsinc, GstAudioFilter,
169     GST_TYPE_AUDIO_FILTER, DEBUG_INIT);
170
171 static void bpwsinc_set_property (GObject * object, guint prop_id,
172     const GValue * value, GParamSpec * pspec);
173 static void bpwsinc_get_property (GObject * object, guint prop_id,
174     GValue * value, GParamSpec * pspec);
175
176 static GstFlowReturn bpwsinc_transform (GstBaseTransform * base,
177     GstBuffer * inbuf, GstBuffer * outbuf);
178 static gboolean bpwsinc_get_unit_size (GstBaseTransform * base, GstCaps * caps,
179     guint * size);
180 static gboolean bpwsinc_start (GstBaseTransform * base);
181 static gboolean bpwsinc_event (GstBaseTransform * base, GstEvent * event);
182
183 static gboolean bpwsinc_setup (GstAudioFilter * base,
184     GstRingBufferSpec * format);
185
186 static gboolean bpwsinc_query (GstPad * pad, GstQuery * query);
187 static const GstQueryType *bpwsinc_query_type (GstPad * pad);
188
189 /* Element class */
190
191 static void
192 gst_bpwsinc_dispose (GObject * object)
193 {
194   GstBPWSinc *self = GST_BPWSINC (object);
195
196   if (self->residue) {
197     g_free (self->residue);
198     self->residue = NULL;
199   }
200
201   if (self->kernel) {
202     g_free (self->kernel);
203     self->kernel = NULL;
204   }
205
206   G_OBJECT_CLASS (parent_class)->dispose (object);
207 }
208
209 static void
210 gst_bpwsinc_base_init (gpointer g_class)
211 {
212   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
213   GstCaps *caps;
214
215   gst_element_class_set_details (element_class, &bpwsinc_details);
216
217   caps = gst_caps_from_string (ALLOWED_CAPS);
218   gst_audio_filter_class_add_pad_templates (GST_AUDIO_FILTER_CLASS (g_class),
219       caps);
220   gst_caps_unref (caps);
221 }
222
223 static void
224 gst_bpwsinc_class_init (GstBPWSincClass * klass)
225 {
226   GObjectClass *gobject_class;
227   GstBaseTransformClass *trans_class;
228   GstAudioFilterClass *filter_class;
229
230   gobject_class = (GObjectClass *) klass;
231   trans_class = (GstBaseTransformClass *) klass;
232   filter_class = (GstAudioFilterClass *) klass;
233
234   gobject_class->set_property = bpwsinc_set_property;
235   gobject_class->get_property = bpwsinc_get_property;
236   gobject_class->dispose = gst_bpwsinc_dispose;
237
238   /* FIXME: Don't use the complete possible range but restrict the upper boundary
239    * so automatically generated UIs can use a slider */
240   g_object_class_install_property (gobject_class, PROP_LOWER_FREQUENCY,
241       g_param_spec_float ("lower-frequency", "Lower Frequency",
242           "Cut-off lower frequency (Hz)", 0.0, 100000.0, 0, G_PARAM_READWRITE));
243   g_object_class_install_property (gobject_class, PROP_UPPER_FREQUENCY,
244       g_param_spec_float ("upper-frequency", "Upper Frequency",
245           "Cut-off upper frequency (Hz)", 0.0, 100000.0, 0, G_PARAM_READWRITE));
246   g_object_class_install_property (gobject_class, PROP_LENGTH,
247       g_param_spec_int ("length", "Length",
248           "Filter kernel length, will be rounded to the next odd number",
249           3, 50000, 101, G_PARAM_READWRITE));
250
251   g_object_class_install_property (gobject_class, PROP_MODE,
252       g_param_spec_enum ("mode", "Mode",
253           "Band pass or band reject mode", GST_TYPE_BPWSINC_MODE,
254           MODE_BAND_PASS, G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));
255
256   g_object_class_install_property (gobject_class, PROP_WINDOW,
257       g_param_spec_enum ("window", "Window",
258           "Window function to use", GST_TYPE_BPWSINC_WINDOW,
259           WINDOW_HAMMING, G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));
260
261   trans_class->transform = GST_DEBUG_FUNCPTR (bpwsinc_transform);
262   trans_class->get_unit_size = GST_DEBUG_FUNCPTR (bpwsinc_get_unit_size);
263   trans_class->start = GST_DEBUG_FUNCPTR (bpwsinc_start);
264   trans_class->event = GST_DEBUG_FUNCPTR (bpwsinc_event);
265   filter_class->setup = GST_DEBUG_FUNCPTR (bpwsinc_setup);
266 }
267
268 static void
269 gst_bpwsinc_init (GstBPWSinc * self, GstBPWSincClass * g_class)
270 {
271   self->kernel_length = 101;
272   self->latency = 50;
273   self->lower_frequency = 0.0;
274   self->upper_frequency = 0.0;
275   self->mode = MODE_BAND_PASS;
276   self->window = WINDOW_HAMMING;
277   self->kernel = NULL;
278   self->have_kernel = FALSE;
279   self->residue = NULL;
280
281   self->residue_length = 0;
282   self->next_ts = GST_CLOCK_TIME_NONE;
283   self->next_off = GST_BUFFER_OFFSET_NONE;
284
285   gst_pad_set_query_function (GST_BASE_TRANSFORM (self)->srcpad, bpwsinc_query);
286   gst_pad_set_query_type_function (GST_BASE_TRANSFORM (self)->srcpad,
287       bpwsinc_query_type);
288 }
289
290 #define DEFINE_PROCESS_FUNC(width,ctype) \
291 static void \
292 process_##width (GstBPWSinc * self, g##ctype * src, g##ctype * dst, guint input_samples) \
293 { \
294   gint kernel_length = self->kernel_length; \
295   gint i, j, k, l; \
296   gint channels = GST_AUDIO_FILTER (self)->format.channels; \
297   gint res_start; \
298   \
299   /* convolution */ \
300   for (i = 0; i < input_samples; i++) { \
301     dst[i] = 0.0; \
302     k = i % channels; \
303     l = i / channels; \
304     for (j = 0; j < kernel_length; j++) \
305       if (l < j) \
306         dst[i] += \
307             self->residue[(kernel_length + l - j) * channels + \
308             k] * self->kernel[j]; \
309       else \
310         dst[i] += src[(l - j) * channels + k] * self->kernel[j]; \
311   } \
312   \
313   /* copy the tail of the current input buffer to the residue, while \
314    * keeping parts of the residue if the input buffer is smaller than \
315    * the kernel length */ \
316   if (input_samples < kernel_length * channels) \
317     res_start = kernel_length * channels - input_samples; \
318   else \
319     res_start = 0; \
320   \
321   for (i = 0; i < res_start; i++) \
322     self->residue[i] = self->residue[i + input_samples]; \
323   for (i = res_start; i < kernel_length * channels; i++) \
324     self->residue[i] = src[input_samples - kernel_length * channels + i]; \
325   \
326   self->residue_length += kernel_length * channels - res_start; \
327   if (self->residue_length > kernel_length * channels) \
328     self->residue_length = kernel_length * channels; \
329 }
330
331 DEFINE_PROCESS_FUNC (32, float);
332 DEFINE_PROCESS_FUNC (64, double);
333
334 #undef DEFINE_PROCESS_FUNC
335
336 static void
337 bpwsinc_build_kernel (GstBPWSinc * self)
338 {
339   gint i = 0;
340   gdouble sum = 0.0;
341   gint len = 0;
342   gdouble *kernel_lp, *kernel_hp;
343   gdouble w;
344
345   len = self->kernel_length;
346
347   if (GST_AUDIO_FILTER (self)->format.rate == 0) {
348     GST_DEBUG ("rate not set yet");
349     return;
350   }
351
352   if (GST_AUDIO_FILTER (self)->format.channels == 0) {
353     GST_DEBUG ("channels not set yet");
354     return;
355   }
356
357   /* Clamp frequencies */
358   self->lower_frequency =
359       CLAMP (self->lower_frequency, 0.0,
360       GST_AUDIO_FILTER (self)->format.rate / 2);
361   self->upper_frequency =
362       CLAMP (self->upper_frequency, 0.0,
363       GST_AUDIO_FILTER (self)->format.rate / 2);
364   if (self->lower_frequency > self->upper_frequency) {
365     gint tmp = self->lower_frequency;
366
367     self->lower_frequency = self->upper_frequency;
368     self->upper_frequency = tmp;
369   }
370
371   GST_DEBUG ("bpwsinc: initializing filter kernel of length %d "
372       "with lower frequency %.2lf Hz "
373       ", upper frequency %.2lf Hz for mode %s",
374       len, self->lower_frequency, self->upper_frequency,
375       (self->mode == MODE_BAND_PASS) ? "band-pass" : "band-reject");
376
377   /* fill the lp kernel */
378   w = 2 * M_PI * (self->lower_frequency / GST_AUDIO_FILTER (self)->format.rate);
379   kernel_lp = g_new (gdouble, len);
380   for (i = 0; i < len; ++i) {
381     if (i == len / 2)
382       kernel_lp[i] = w;
383     else
384       kernel_lp[i] = sin (w * (i - len / 2))
385           / (i - len / 2);
386     /* Windowing */
387     if (self->window == WINDOW_HAMMING)
388       kernel_lp[i] *= (0.54 - 0.46 * cos (2 * M_PI * i / len));
389     else
390       kernel_lp[i] *=
391           (0.42 - 0.5 * cos (2 * M_PI * i / len) +
392           0.08 * cos (4 * M_PI * i / len));
393   }
394
395   /* normalize for unity gain at DC */
396   sum = 0.0;
397   for (i = 0; i < len; ++i)
398     sum += kernel_lp[i];
399   for (i = 0; i < len; ++i)
400     kernel_lp[i] /= sum;
401
402   /* fill the hp kernel */
403   w = 2 * M_PI * (self->upper_frequency / GST_AUDIO_FILTER (self)->format.rate);
404   kernel_hp = g_new (gdouble, len);
405   for (i = 0; i < len; ++i) {
406     if (i == len / 2)
407       kernel_hp[i] = w;
408     else
409       kernel_hp[i] = sin (w * (i - len / 2))
410           / (i - len / 2);
411     /* Windowing */
412     if (self->window == WINDOW_HAMMING)
413       kernel_hp[i] *= (0.54 - 0.46 * cos (2 * M_PI * i / len));
414     else
415       kernel_hp[i] *=
416           (0.42 - 0.5 * cos (2 * M_PI * i / len) +
417           0.08 * cos (4 * M_PI * i / len));
418   }
419
420   /* normalize for unity gain at DC */
421   sum = 0.0;
422   for (i = 0; i < len; ++i)
423     sum += kernel_hp[i];
424   for (i = 0; i < len; ++i)
425     kernel_hp[i] /= sum;
426
427   /* do spectral inversion to go from lowpass to highpass */
428   for (i = 0; i < len; ++i)
429     kernel_hp[i] = -kernel_hp[i];
430   kernel_hp[len / 2] += 1;
431
432   /* combine the two kernels */
433   if (self->kernel)
434     g_free (self->kernel);
435   self->kernel = g_new (gdouble, len);
436
437   for (i = 0; i < len; ++i)
438     self->kernel[i] = kernel_lp[i] + kernel_hp[i];
439
440   /* free the helper kernels */
441   g_free (kernel_lp);
442   g_free (kernel_hp);
443
444   /* do spectral inversion to go from bandreject to bandpass
445    * if specified */
446   if (self->mode == MODE_BAND_PASS) {
447     for (i = 0; i < len; ++i)
448       self->kernel[i] = -self->kernel[i];
449     self->kernel[len / 2] += 1;
450   }
451
452   /* set up the residue memory space */
453   if (!self->residue) {
454     self->residue =
455         g_new0 (gdouble, len * GST_AUDIO_FILTER (self)->format.channels);
456     self->residue_length = 0;
457   }
458
459   self->have_kernel = TRUE;
460 }
461
462 static void
463 bpwsinc_push_residue (GstBPWSinc * self)
464 {
465   GstBuffer *outbuf;
466   GstFlowReturn res;
467   gint rate = GST_AUDIO_FILTER (self)->format.rate;
468   gint channels = GST_AUDIO_FILTER (self)->format.channels;
469   gint outsize, outsamples;
470   gint diffsize, diffsamples;
471   guint8 *in, *out;
472
473   /* Calculate the number of samples and their memory size that
474    * should be pushed from the residue */
475   outsamples = MIN (self->latency, self->residue_length / channels);
476   outsize = outsamples * channels * (GST_AUDIO_FILTER (self)->format.width / 8);
477   if (outsize == 0)
478     return;
479
480   /* Process the difference between latency and residue_length samples
481    * to start at the actual data instead of starting at the zeros before
482    * when we only got one buffer smaller than latency */
483   diffsamples = self->latency - self->residue_length / channels;
484   diffsize =
485       diffsamples * channels * (GST_AUDIO_FILTER (self)->format.width / 8);
486   if (diffsize > 0) {
487     in = g_new0 (guint8, diffsize);
488     out = g_new0 (guint8, diffsize);
489     self->process (self, in, out, diffsamples * channels);
490     g_free (in);
491     g_free (out);
492   }
493
494   res = gst_pad_alloc_buffer (GST_BASE_TRANSFORM (self)->srcpad,
495       GST_BUFFER_OFFSET_NONE, outsize,
496       GST_PAD_CAPS (GST_BASE_TRANSFORM (self)->srcpad), &outbuf);
497
498   if (G_UNLIKELY (res != GST_FLOW_OK)) {
499     GST_WARNING_OBJECT (self, "failed allocating buffer of %d bytes", outsize);
500     return;
501   }
502
503   /* Convolve the residue with zeros to get the actual remaining data */
504   in = g_new0 (guint8, outsize);
505   self->process (self, in, GST_BUFFER_DATA (outbuf), outsamples * channels);
506   g_free (in);
507
508   /* Set timestamp, offset, etc from the values we
509    * saved when processing the regular buffers */
510   if (GST_CLOCK_TIME_IS_VALID (self->next_ts))
511     GST_BUFFER_TIMESTAMP (outbuf) = self->next_ts;
512   else
513     GST_BUFFER_TIMESTAMP (outbuf) = 0;
514   GST_BUFFER_DURATION (outbuf) =
515       gst_util_uint64_scale (outsamples, GST_SECOND, rate);
516   self->next_ts += gst_util_uint64_scale (outsamples, GST_SECOND, rate);
517
518   if (self->next_off != GST_BUFFER_OFFSET_NONE) {
519     GST_BUFFER_OFFSET (outbuf) = self->next_off;
520     GST_BUFFER_OFFSET_END (outbuf) = self->next_off + outsamples;
521   }
522
523   GST_DEBUG_OBJECT (self, "Pushing residue buffer of size %d with timestamp: %"
524       GST_TIME_FORMAT ", duration: %" GST_TIME_FORMAT ", offset: %lld,"
525       " offset_end: %lld, nsamples: %d", GST_BUFFER_SIZE (outbuf),
526       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
527       GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)), GST_BUFFER_OFFSET (outbuf),
528       GST_BUFFER_OFFSET_END (outbuf), outsamples);
529
530   res = gst_pad_push (GST_BASE_TRANSFORM (self)->srcpad, outbuf);
531
532   if (G_UNLIKELY (res != GST_FLOW_OK)) {
533     GST_WARNING_OBJECT (self, "failed to push residue");
534   }
535
536 }
537
538 /* GstAudioFilter vmethod implementations */
539
540 /* get notified of caps and plug in the correct process function */
541 static gboolean
542 bpwsinc_setup (GstAudioFilter * base, GstRingBufferSpec * format)
543 {
544   GstBPWSinc *self = GST_BPWSINC (base);
545
546   gboolean ret = TRUE;
547
548   if (format->width == 32)
549     self->process = (GstBPWSincProcessFunc) process_32;
550   else if (format->width == 64)
551     self->process = (GstBPWSincProcessFunc) process_64;
552   else
553     ret = FALSE;
554
555   self->have_kernel = FALSE;
556
557   return TRUE;
558 }
559
560 /* GstBaseTransform vmethod implementations */
561
562 static gboolean
563 bpwsinc_get_unit_size (GstBaseTransform * base, GstCaps * caps, guint * size)
564 {
565   gint width, channels;
566   GstStructure *structure;
567   gboolean ret;
568
569   g_assert (size);
570
571   structure = gst_caps_get_structure (caps, 0);
572   ret = gst_structure_get_int (structure, "width", &width);
573   ret &= gst_structure_get_int (structure, "channels", &channels);
574
575   *size = width * channels / 8;
576
577   return ret;
578 }
579
580 static GstFlowReturn
581 bpwsinc_transform (GstBaseTransform * base, GstBuffer * inbuf,
582     GstBuffer * outbuf)
583 {
584   GstBPWSinc *self = GST_BPWSINC (base);
585   GstClockTime timestamp;
586   gint channels = GST_AUDIO_FILTER (self)->format.channels;
587   gint rate = GST_AUDIO_FILTER (self)->format.rate;
588   gint input_samples =
589       GST_BUFFER_SIZE (outbuf) / (GST_AUDIO_FILTER (self)->format.width / 8);
590   gint output_samples = input_samples;
591   gint diff;
592
593   /* don't process data in passthrough-mode */
594   if (gst_base_transform_is_passthrough (base))
595     return GST_FLOW_OK;
596
597   /* FIXME: subdivide GST_BUFFER_SIZE into small chunks for smooth fades */
598   timestamp = GST_BUFFER_TIMESTAMP (outbuf);
599
600   if (GST_CLOCK_TIME_IS_VALID (timestamp))
601     gst_object_sync_values (G_OBJECT (self), timestamp);
602
603   if (!self->have_kernel)
604     bpwsinc_build_kernel (self);
605
606   /* Reset the residue if already existing on discont buffers */
607   if (GST_BUFFER_IS_DISCONT (inbuf)) {
608     if (channels && self->residue)
609       memset (self->residue, 0, channels *
610           self->kernel_length * sizeof (gdouble));
611     self->residue_length = 0;
612     self->next_ts = GST_CLOCK_TIME_NONE;
613     self->next_off = GST_BUFFER_OFFSET_NONE;
614   }
615
616   /* Calculate the number of samples we can push out now without outputting
617    * kernel_length/2 zeros in the beginning */
618   diff = (self->kernel_length / 2) * channels - self->residue_length;
619   if (diff > 0)
620     output_samples -= diff;
621
622   self->process (self, GST_BUFFER_DATA (inbuf), GST_BUFFER_DATA (outbuf),
623       input_samples);
624
625   if (output_samples <= 0) {
626     /* Drop buffer and save original timestamp/offset for later use */
627     if (!GST_CLOCK_TIME_IS_VALID (self->next_ts)
628         && GST_BUFFER_TIMESTAMP_IS_VALID (outbuf))
629       self->next_ts = GST_BUFFER_TIMESTAMP (outbuf);
630     if (self->next_off == GST_BUFFER_OFFSET_NONE
631         && GST_BUFFER_OFFSET_IS_VALID (outbuf))
632       self->next_off = GST_BUFFER_OFFSET (outbuf);
633     return GST_BASE_TRANSFORM_FLOW_DROPPED;
634   } else if (output_samples < input_samples) {
635     /* First (probably partial) buffer after starting from
636      * a clean residue. Use stored timestamp/offset here */
637     if (GST_CLOCK_TIME_IS_VALID (self->next_ts))
638       GST_BUFFER_TIMESTAMP (outbuf) = self->next_ts;
639
640     if (self->next_off != GST_BUFFER_OFFSET_NONE) {
641       GST_BUFFER_OFFSET (outbuf) = self->next_off;
642       if (GST_BUFFER_OFFSET_END_IS_VALID (outbuf))
643         GST_BUFFER_OFFSET_END (outbuf) =
644             self->next_off + output_samples / channels;
645     } else {
646       /* We dropped no buffer, offset is valid, offset_end must be adjusted by diff */
647       if (GST_BUFFER_OFFSET_END_IS_VALID (outbuf))
648         GST_BUFFER_OFFSET_END (outbuf) -= diff / channels;
649     }
650
651     if (GST_BUFFER_DURATION_IS_VALID (outbuf))
652       GST_BUFFER_DURATION (outbuf) -=
653           gst_util_uint64_scale (diff, GST_SECOND, channels * rate);
654
655     GST_BUFFER_DATA (outbuf) +=
656         diff * (GST_AUDIO_FILTER (self)->format.width / 8);
657     GST_BUFFER_SIZE (outbuf) -=
658         diff * (GST_AUDIO_FILTER (self)->format.width / 8);
659   } else {
660     GstClockTime ts_latency =
661         gst_util_uint64_scale (self->latency, GST_SECOND, rate);
662
663     /* Normal buffer, adjust timestamp/offset/etc by latency */
664     if (GST_BUFFER_TIMESTAMP (outbuf) < ts_latency) {
665       GST_WARNING_OBJECT (self, "GST_BUFFER_TIMESTAMP (outbuf) < latency");
666       GST_BUFFER_TIMESTAMP (outbuf) = 0;
667     } else {
668       GST_BUFFER_TIMESTAMP (outbuf) -= ts_latency;
669     }
670
671     if (GST_BUFFER_OFFSET_IS_VALID (outbuf)) {
672       if (GST_BUFFER_OFFSET (outbuf) > self->latency) {
673         GST_BUFFER_OFFSET (outbuf) -= self->latency;
674       } else {
675         GST_WARNING_OBJECT (self, "GST_BUFFER_OFFSET (outbuf) < latency");
676         GST_BUFFER_OFFSET (outbuf) = 0;
677       }
678     }
679
680     if (GST_BUFFER_OFFSET_END_IS_VALID (outbuf)) {
681       if (GST_BUFFER_OFFSET_END (outbuf) > self->latency) {
682         GST_BUFFER_OFFSET_END (outbuf) -= self->latency;
683       } else {
684         GST_WARNING_OBJECT (self, "GST_BUFFER_OFFSET_END (outbuf) < latency");
685         GST_BUFFER_OFFSET_END (outbuf) = 0;
686       }
687     }
688   }
689
690   GST_DEBUG_OBJECT (self, "Pushing buffer of size %d with timestamp: %"
691       GST_TIME_FORMAT ", duration: %" GST_TIME_FORMAT ", offset: %lld,"
692       " offset_end: %lld, nsamples: %d", GST_BUFFER_SIZE (outbuf),
693       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
694       GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)), GST_BUFFER_OFFSET (outbuf),
695       GST_BUFFER_OFFSET_END (outbuf), output_samples / channels);
696
697   self->next_ts = GST_BUFFER_TIMESTAMP (outbuf) + GST_BUFFER_DURATION (outbuf);
698   self->next_off = GST_BUFFER_OFFSET_END (outbuf);
699
700   return GST_FLOW_OK;
701 }
702
703 static gboolean
704 bpwsinc_start (GstBaseTransform * base)
705 {
706   GstBPWSinc *self = GST_BPWSINC (base);
707   gint channels = GST_AUDIO_FILTER (self)->format.channels;
708
709   /* Reset the residue if already existing */
710   if (channels && self->residue)
711     memset (self->residue, 0, channels *
712         self->kernel_length * sizeof (gdouble));
713
714   self->residue_length = 0;
715   self->next_ts = GST_CLOCK_TIME_NONE;
716   self->next_off = GST_BUFFER_OFFSET_NONE;
717
718   return TRUE;
719 }
720
721 static gboolean
722 bpwsinc_query (GstPad * pad, GstQuery * query)
723 {
724   GstBPWSinc *self = GST_BPWSINC (gst_pad_get_parent (pad));
725   gboolean res = TRUE;
726
727   switch (GST_QUERY_TYPE (query)) {
728     case GST_QUERY_LATENCY:
729     {
730       GstClockTime min, max;
731       gboolean live;
732       guint64 latency;
733       GstPad *peer;
734       gint rate = GST_AUDIO_FILTER (self)->format.rate;
735
736       if ((peer = gst_pad_get_peer (GST_BASE_TRANSFORM (self)->sinkpad))) {
737         if ((res = gst_pad_query (peer, query))) {
738           gst_query_parse_latency (query, &live, &min, &max);
739
740           GST_DEBUG_OBJECT (self, "Peer latency: min %"
741               GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
742               GST_TIME_ARGS (min), GST_TIME_ARGS (max));
743
744           /* add our own latency */
745           latency =
746               (rate != 0) ? gst_util_uint64_scale (self->latency, GST_SECOND,
747               rate) : 0;
748
749           GST_DEBUG_OBJECT (self, "Our latency: %"
750               GST_TIME_FORMAT, GST_TIME_ARGS (latency));
751
752           min += latency;
753           if (max != GST_CLOCK_TIME_NONE)
754             max += latency;
755
756           GST_DEBUG_OBJECT (self, "Calculated total latency : min %"
757               GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
758               GST_TIME_ARGS (min), GST_TIME_ARGS (max));
759
760           gst_query_set_latency (query, live, min, max);
761         }
762         gst_object_unref (peer);
763       }
764       break;
765     }
766     default:
767       res = gst_pad_query_default (pad, query);
768       break;
769   }
770   gst_object_unref (self);
771   return res;
772 }
773
774 static const GstQueryType *
775 bpwsinc_query_type (GstPad * pad)
776 {
777   static const GstQueryType types[] = {
778     GST_QUERY_LATENCY,
779     0
780   };
781
782   return types;
783 }
784
785 static gboolean
786 bpwsinc_event (GstBaseTransform * base, GstEvent * event)
787 {
788   GstBPWSinc *self = GST_BPWSINC (base);
789
790   switch (GST_EVENT_TYPE (event)) {
791     case GST_EVENT_EOS:
792       bpwsinc_push_residue (self);
793       break;
794     default:
795       break;
796   }
797
798   return GST_BASE_TRANSFORM_CLASS (parent_class)->event (base, event);
799 }
800
801 static void
802 bpwsinc_set_property (GObject * object, guint prop_id, const GValue * value,
803     GParamSpec * pspec)
804 {
805   GstBPWSinc *self = GST_BPWSINC (object);
806
807   g_return_if_fail (GST_IS_BPWSINC (self));
808
809   switch (prop_id) {
810     case PROP_LENGTH:{
811       gint val;
812
813       GST_BASE_TRANSFORM_LOCK (self);
814       val = g_value_get_int (value);
815       if (val % 2 == 0)
816         val++;
817
818       if (val != self->kernel_length) {
819         if (self->residue) {
820           bpwsinc_push_residue (self);
821           g_free (self->residue);
822           self->residue = NULL;
823         }
824         self->kernel_length = val;
825         self->latency = val / 2;
826         bpwsinc_build_kernel (self);
827         gst_element_post_message (GST_ELEMENT (self),
828             gst_message_new_latency (GST_OBJECT (self)));
829       }
830       GST_BASE_TRANSFORM_UNLOCK (self);
831       break;
832     }
833     case PROP_LOWER_FREQUENCY:
834       GST_BASE_TRANSFORM_LOCK (self);
835       self->lower_frequency = g_value_get_float (value);
836       bpwsinc_build_kernel (self);
837       GST_BASE_TRANSFORM_UNLOCK (self);
838       break;
839     case PROP_UPPER_FREQUENCY:
840       GST_BASE_TRANSFORM_LOCK (self);
841       self->upper_frequency = g_value_get_float (value);
842       bpwsinc_build_kernel (self);
843       GST_BASE_TRANSFORM_UNLOCK (self);
844       break;
845     case PROP_MODE:
846       GST_BASE_TRANSFORM_LOCK (self);
847       self->mode = g_value_get_enum (value);
848       bpwsinc_build_kernel (self);
849       GST_BASE_TRANSFORM_UNLOCK (self);
850       break;
851     case PROP_WINDOW:
852       GST_BASE_TRANSFORM_LOCK (self);
853       self->window = g_value_get_enum (value);
854       bpwsinc_build_kernel (self);
855       GST_BASE_TRANSFORM_UNLOCK (self);
856       break;
857     default:
858       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
859       break;
860   }
861 }
862
863 static void
864 bpwsinc_get_property (GObject * object, guint prop_id, GValue * value,
865     GParamSpec * pspec)
866 {
867   GstBPWSinc *self = GST_BPWSINC (object);
868
869   switch (prop_id) {
870     case PROP_LENGTH:
871       g_value_set_int (value, self->kernel_length);
872       break;
873     case PROP_LOWER_FREQUENCY:
874       g_value_set_float (value, self->lower_frequency);
875       break;
876     case PROP_UPPER_FREQUENCY:
877       g_value_set_float (value, self->upper_frequency);
878       break;
879     case PROP_MODE:
880       g_value_set_enum (value, self->mode);
881       break;
882     case PROP_WINDOW:
883       g_value_set_enum (value, self->window);
884       break;
885     default:
886       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
887       break;
888   }
889 }