diff options
author | Sebastian Dröge <slomo@circular-chaos.org> | 2007-08-16 19:22:48 +0000 |
---|---|---|
committer | Sebastian Dröge <slomo@circular-chaos.org> | 2007-08-16 19:22:48 +0000 |
commit | e221638fdd642bdf8b4a86365d8a873e89ca9e46 (patch) | |
tree | a9a8aeaf955e416d5b38c032db00d0b3ef0ee2d4 /gst | |
parent | 1df9f40304a2b03667dd88fe3b41d75204986335 (diff) | |
download | gst-plugins-bad-e221638fdd642bdf8b4a86365d8a873e89ca9e46.tar.gz gst-plugins-bad-e221638fdd642bdf8b4a86365d8a873e89ca9e46.tar.bz2 gst-plugins-bad-e221638fdd642bdf8b4a86365d8a873e89ca9e46.zip |
gst/filter/gstbpwsinc.*: Implement latency query and only forward those samples downstream that actually contain the ...
Original commit message from CVS:
* gst/filter/gstbpwsinc.c: (gst_bpwsinc_class_init),
(gst_bpwsinc_init), (process_32), (process_64),
(bpwsinc_build_kernel), (bpwsinc_push_residue),
(bpwsinc_transform), (bpwsinc_start), (bpwsinc_query),
(bpwsinc_query_type), (bpwsinc_event), (bpwsinc_set_property):
* gst/filter/gstbpwsinc.h:
Implement latency query and only forward those samples downstream
that actually contain the data we want, i.e. drop kernel_length/2
in the beginning and append kernel_length/2 (created by convolving
the filter kernel with zeroes) to the end.
* tests/check/elements/bpwsinc.c: (GST_START_TEST):
Adjust the unit test for this slightly changed behaviour.
* gst/filter/gstlpwsinc.c: (lpwsinc_build_kernel):
Reset residue length only when actually creating a residue.
Diffstat (limited to 'gst')
-rw-r--r-- | gst/filter/gstbpwsinc.c | 299 | ||||
-rw-r--r-- | gst/filter/gstbpwsinc.h | 4 | ||||
-rw-r--r-- | gst/filter/gstlpwsinc.c | 5 |
3 files changed, 300 insertions, 8 deletions
diff --git a/gst/filter/gstbpwsinc.c b/gst/filter/gstbpwsinc.c index f86de8d0..2304ac68 100644 --- a/gst/filter/gstbpwsinc.c +++ b/gst/filter/gstbpwsinc.c @@ -173,10 +173,14 @@ static GstFlowReturn bpwsinc_transform (GstBaseTransform * base, static gboolean bpwsinc_get_unit_size (GstBaseTransform * base, GstCaps * caps, guint * size); static gboolean bpwsinc_start (GstBaseTransform * base); +static gboolean bpwsinc_event (GstBaseTransform * base, GstEvent * event); static gboolean bpwsinc_setup (GstAudioFilter * base, GstRingBufferSpec * format); +static gboolean bpwsinc_query (GstPad * pad, GstQuery * query); +static const GstQueryType *bpwsinc_query_type (GstPad * pad); + /* Element class */ static void @@ -252,6 +256,7 @@ gst_bpwsinc_class_init (GstBPWSincClass * klass) trans_class->transform = GST_DEBUG_FUNCPTR (bpwsinc_transform); trans_class->get_unit_size = GST_DEBUG_FUNCPTR (bpwsinc_get_unit_size); trans_class->start = GST_DEBUG_FUNCPTR (bpwsinc_start); + trans_class->event = GST_DEBUG_FUNCPTR (bpwsinc_event); filter_class->setup = GST_DEBUG_FUNCPTR (bpwsinc_setup); } @@ -259,6 +264,7 @@ static void gst_bpwsinc_init (GstBPWSinc * self, GstBPWSincClass * g_class) { self->kernel_length = 101; + self->latency = 50; self->lower_frequency = 0.0; self->upper_frequency = 0.0; self->mode = MODE_BAND_PASS; @@ -266,6 +272,14 @@ gst_bpwsinc_init (GstBPWSinc * self, GstBPWSincClass * g_class) self->kernel = NULL; self->have_kernel = FALSE; self->residue = NULL; + + self->residue_length = 0; + self->next_ts = GST_CLOCK_TIME_NONE; + self->next_off = GST_BUFFER_OFFSET_NONE; + + gst_pad_set_query_function (GST_BASE_TRANSFORM (self)->srcpad, bpwsinc_query); + gst_pad_set_query_type_function (GST_BASE_TRANSFORM (self)->srcpad, + bpwsinc_query_type); } static void @@ -302,6 +316,10 @@ process_32 (GstBPWSinc * self, gfloat * src, gfloat * dst, guint input_samples) self->residue[i] = self->residue[i + input_samples]; for (i = res_start; i < kernel_length * channels; i++) self->residue[i] = src[input_samples - kernel_length * channels + i]; + + self->residue_length += kernel_length * channels - res_start; + if (self->residue_length > kernel_length * channels) + self->residue_length = kernel_length * channels; } static void @@ -339,6 +357,10 @@ process_64 (GstBPWSinc * self, gdouble * src, gdouble * dst, self->residue[i] = self->residue[i + input_samples]; for (i = res_start; i < kernel_length * channels; i++) self->residue[i] = src[input_samples - kernel_length * channels + i]; + + self->residue_length += kernel_length * channels - res_start; + if (self->residue_length > kernel_length * channels) + self->residue_length = kernel_length * channels; } static void @@ -458,14 +480,91 @@ bpwsinc_build_kernel (GstBPWSinc * self) } /* set up the residue memory space */ - if (self->residue) - g_free (self->residue); - self->residue = - g_new0 (gdouble, len * GST_AUDIO_FILTER (self)->format.channels); + if (!self->residue) { + self->residue = + g_new0 (gdouble, len * GST_AUDIO_FILTER (self)->format.channels); + self->residue_length = 0; + } self->have_kernel = TRUE; } +static void +bpwsinc_push_residue (GstBPWSinc * self) +{ + GstBuffer *outbuf; + GstFlowReturn res; + gint rate = GST_AUDIO_FILTER (self)->format.rate; + gint channels = GST_AUDIO_FILTER (self)->format.channels; + gint outsize, outsamples; + gint diffsize, diffsamples; + guint8 *in, *out; + + /* Calculate the number of samples and their memory size that + * should be pushed from the residue */ + outsamples = MIN (self->latency, self->residue_length / channels); + outsize = outsamples * channels * (GST_AUDIO_FILTER (self)->format.width / 8); + if (outsize == 0) + return; + + /* Process the difference between latency and residue_length samples + * to start at the actual data instead of starting at the zeros before + * when we only got one buffer smaller than latency */ + diffsamples = self->latency - self->residue_length / channels; + diffsize = + diffsamples * channels * (GST_AUDIO_FILTER (self)->format.width / 8); + if (diffsize > 0) { + in = g_new0 (guint8, diffsize); + out = g_new0 (guint8, diffsize); + self->process (self, in, out, diffsamples * channels); + g_free (in); + g_free (out); + } + + res = gst_pad_alloc_buffer (GST_BASE_TRANSFORM (self)->srcpad, + GST_BUFFER_OFFSET_NONE, outsize, + GST_PAD_CAPS (GST_BASE_TRANSFORM (self)->srcpad), &outbuf); + + if (G_UNLIKELY (res != GST_FLOW_OK)) { + GST_WARNING_OBJECT (self, "failed allocating buffer of %d bytes", outsize); + return; + } + + /* Convolve the residue with zeros to get the actual remaining data */ + in = g_new0 (guint8, outsize); + self->process (self, in, GST_BUFFER_DATA (outbuf), outsamples * channels); + g_free (in); + + /* Set timestamp, offset, etc from the values we + * saved when processing the regular buffers */ + if (GST_CLOCK_TIME_IS_VALID (self->next_ts)) + GST_BUFFER_TIMESTAMP (outbuf) = self->next_ts; + else + GST_BUFFER_TIMESTAMP (outbuf) = 0; + GST_BUFFER_DURATION (outbuf) = + gst_util_uint64_scale (outsamples, GST_SECOND, rate); + self->next_ts += gst_util_uint64_scale (outsamples, GST_SECOND, rate); + + if (self->next_off != GST_BUFFER_OFFSET_NONE) { + GST_BUFFER_OFFSET (outbuf) = self->next_off; + GST_BUFFER_OFFSET_END (outbuf) = self->next_off + outsamples; + } + + GST_DEBUG_OBJECT (self, "Pushing residue buffer of size %d with timestamp: %" + GST_TIME_FORMAT ", duration: %" GST_TIME_FORMAT ", offset: %lld," + " offset_end: %lld, nsamples: %d", GST_BUFFER_SIZE (outbuf), + GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)), + GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)), GST_BUFFER_OFFSET (outbuf), + GST_BUFFER_OFFSET_END (outbuf), outsamples); + + res = gst_pad_push (GST_BASE_TRANSFORM (self)->srcpad, outbuf); + + if (G_UNLIKELY (res != GST_FLOW_OK)) { + GST_WARNING_OBJECT (self, "failed to push residue"); + } + +} + /* GstAudioFilter vmethod implementations */ /* get notified of caps and plug in the correct process function */ @@ -514,8 +613,12 @@ bpwsinc_transform (GstBaseTransform * base, GstBuffer * inbuf, { GstBPWSinc *self = GST_BPWSINC (base); GstClockTime timestamp; + gint channels = GST_AUDIO_FILTER (self)->format.channels; + gint rate = GST_AUDIO_FILTER (self)->format.rate; gint input_samples = GST_BUFFER_SIZE (outbuf) / (GST_AUDIO_FILTER (self)->format.width / 8); + gint output_samples = input_samples; + gint diff; /* don't process data in passthrough-mode */ if (gst_base_transform_is_passthrough (base)) @@ -530,9 +633,100 @@ bpwsinc_transform (GstBaseTransform * base, GstBuffer * inbuf, if (!self->have_kernel) bpwsinc_build_kernel (self); + /* Reset the residue if already existing on discont buffers */ + if (GST_BUFFER_IS_DISCONT (inbuf)) { + if (channels && self->residue) + memset (self->residue, 0, channels * + self->kernel_length * sizeof (gdouble)); + self->residue_length = 0; + self->next_ts = GST_CLOCK_TIME_NONE; + self->next_off = GST_BUFFER_OFFSET_NONE; + } + + /* Calculate the number of samples we can push out now without outputting + * kernel_length/2 zeros in the beginning */ + diff = (self->kernel_length / 2) * channels - self->residue_length; + if (diff > 0) + output_samples -= diff; + self->process (self, GST_BUFFER_DATA (inbuf), GST_BUFFER_DATA (outbuf), input_samples); + if (output_samples <= 0) { + /* Drop buffer and save original timestamp/offset for later use */ + if (!GST_CLOCK_TIME_IS_VALID (self->next_ts) + && GST_BUFFER_TIMESTAMP_IS_VALID (outbuf)) + self->next_ts = GST_BUFFER_TIMESTAMP (outbuf); + if (self->next_off == GST_BUFFER_OFFSET_NONE + && GST_BUFFER_OFFSET_IS_VALID (outbuf)) + self->next_off = GST_BUFFER_OFFSET (outbuf); + return GST_BASE_TRANSFORM_FLOW_DROPPED; + } else if (output_samples < input_samples) { + /* First (probably partial) buffer after starting from + * a clean residue. Use stored timestamp/offset here */ + if (GST_CLOCK_TIME_IS_VALID (self->next_ts)) + GST_BUFFER_TIMESTAMP (outbuf) = self->next_ts; + + if (self->next_off != GST_BUFFER_OFFSET_NONE) { + GST_BUFFER_OFFSET (outbuf) = self->next_off; + if (GST_BUFFER_OFFSET_END_IS_VALID (outbuf)) + GST_BUFFER_OFFSET_END (outbuf) = + self->next_off + output_samples / channels; + } else { + /* We dropped no buffer, offset is valid, offset_end must be adjusted by diff */ + if (GST_BUFFER_OFFSET_END_IS_VALID (outbuf)) + GST_BUFFER_OFFSET_END (outbuf) -= diff / channels; + } + + if (GST_BUFFER_DURATION_IS_VALID (outbuf)) + GST_BUFFER_DURATION (outbuf) -= + gst_util_uint64_scale (diff, GST_SECOND, channels * rate); + + GST_BUFFER_DATA (outbuf) += + diff * (GST_AUDIO_FILTER (self)->format.width / 8); + GST_BUFFER_SIZE (outbuf) -= + diff * (GST_AUDIO_FILTER (self)->format.width / 8); + } else { + GstClockTime ts_latency = + gst_util_uint64_scale (self->latency, GST_SECOND, rate); + + /* Normal buffer, adjust timestamp/offset/etc by latency */ + if (GST_BUFFER_TIMESTAMP (outbuf) < ts_latency) { + GST_WARNING_OBJECT (self, "GST_BUFFER_TIMESTAMP (outbuf) < latency"); + GST_BUFFER_TIMESTAMP (outbuf) = 0; + } else { + GST_BUFFER_TIMESTAMP (outbuf) -= ts_latency; + } + + if (GST_BUFFER_OFFSET_IS_VALID (outbuf)) { + if (GST_BUFFER_OFFSET (outbuf) > self->latency) { + GST_BUFFER_OFFSET (outbuf) -= self->latency; + } else { + GST_WARNING_OBJECT (self, "GST_BUFFER_OFFSET (outbuf) < latency"); + GST_BUFFER_OFFSET (outbuf) = 0; + } + } + + if (GST_BUFFER_OFFSET_END_IS_VALID (outbuf)) { + if (GST_BUFFER_OFFSET_END (outbuf) > self->latency) { + GST_BUFFER_OFFSET_END (outbuf) -= self->latency; + } else { + GST_WARNING_OBJECT (self, "GST_BUFFER_OFFSET_END (outbuf) < latency"); + GST_BUFFER_OFFSET_END (outbuf) = 0; + } + } + } + + GST_DEBUG_OBJECT (self, "Pushing buffer of size %d with timestamp: %" + GST_TIME_FORMAT ", duration: %" GST_TIME_FORMAT ", offset: %lld," + " offset_end: %lld, nsamples: %d", GST_BUFFER_SIZE (outbuf), + GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)), + GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)), GST_BUFFER_OFFSET (outbuf), + GST_BUFFER_OFFSET_END (outbuf), output_samples / channels); + + self->next_ts = GST_BUFFER_TIMESTAMP (outbuf) + GST_BUFFER_DURATION (outbuf); + self->next_off = GST_BUFFER_OFFSET_END (outbuf); + return GST_FLOW_OK; } @@ -547,9 +741,93 @@ bpwsinc_start (GstBaseTransform * base) memset (self->residue, 0, channels * self->kernel_length * sizeof (gdouble)); + self->residue_length = 0; + self->next_ts = GST_CLOCK_TIME_NONE; + self->next_off = GST_BUFFER_OFFSET_NONE; + return TRUE; } +static gboolean +bpwsinc_query (GstPad * pad, GstQuery * query) +{ + GstBPWSinc *self = GST_BPWSINC (gst_pad_get_parent (pad)); + gboolean res = TRUE; + + switch (GST_QUERY_TYPE (query)) { + case GST_QUERY_LATENCY: + { + GstClockTime min, max; + gboolean live; + guint64 latency; + GstPad *peer; + gint rate = GST_AUDIO_FILTER (self)->format.rate; + + if ((peer = gst_pad_get_peer (GST_BASE_TRANSFORM (self)->sinkpad))) { + if ((res = gst_pad_query (peer, query))) { + gst_query_parse_latency (query, &live, &min, &max); + + GST_DEBUG_OBJECT (self, "Peer latency: min %" + GST_TIME_FORMAT " max %" GST_TIME_FORMAT, + GST_TIME_ARGS (min), GST_TIME_ARGS (max)); + + /* add our own latency */ + latency = + (rate != 0) ? gst_util_uint64_scale (self->latency, GST_SECOND, + rate) : 0; + + GST_DEBUG_OBJECT (self, "Our latency: %" + GST_TIME_FORMAT, GST_TIME_ARGS (latency)); + + min += latency; + if (max != GST_CLOCK_TIME_NONE) + max += latency; + + GST_DEBUG_OBJECT (self, "Calculated total latency : min %" + GST_TIME_FORMAT " max %" GST_TIME_FORMAT, + GST_TIME_ARGS (min), GST_TIME_ARGS (max)); + + gst_query_set_latency (query, live, min, max); + } + gst_object_unref (peer); + } + break; + } + default: + res = gst_pad_query_default (pad, query); + break; + } + gst_object_unref (self); + return res; +} + +static const GstQueryType * +bpwsinc_query_type (GstPad * pad) +{ + static const GstQueryType types[] = { + GST_QUERY_LATENCY, + 0 + }; + + return types; +} + +static gboolean +bpwsinc_event (GstBaseTransform * base, GstEvent * event) +{ + GstBPWSinc *self = GST_BPWSINC (base); + + switch (GST_EVENT_TYPE (event)) { + case GST_EVENT_EOS: + bpwsinc_push_residue (self); + break; + default: + break; + } + + return GST_BASE_TRANSFORM_CLASS (parent_class)->event (base, event); +} + static void bpwsinc_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec) @@ -566,8 +844,17 @@ bpwsinc_set_property (GObject * object, guint prop_id, const GValue * value, val = g_value_get_int (value); if (val % 2 == 0) val++; - self->kernel_length = val; - bpwsinc_build_kernel (self); + + if (val != self->kernel_length) { + if (self->residue) { + bpwsinc_push_residue (self); + g_free (self->residue); + self->residue = NULL; + } + self->kernel_length = val; + self->latency = val / 2; + bpwsinc_build_kernel (self); + } GST_BASE_TRANSFORM_UNLOCK (self); break; } diff --git a/gst/filter/gstbpwsinc.h b/gst/filter/gstbpwsinc.h index 7d9c06ba..d790d040 100644 --- a/gst/filter/gstbpwsinc.h +++ b/gst/filter/gstbpwsinc.h @@ -71,6 +71,10 @@ struct _GstBPWSinc { gdouble *residue; /* buffer for left-over samples from previous buffer */ gdouble *kernel; gboolean have_kernel; + gint residue_length; + guint64 latency; + GstClockTime next_ts; + guint64 next_off; }; struct _GstBPWSincClass { diff --git a/gst/filter/gstlpwsinc.c b/gst/filter/gstlpwsinc.c index d86aace1..3869553b 100644 --- a/gst/filter/gstlpwsinc.c +++ b/gst/filter/gstlpwsinc.c @@ -421,12 +421,13 @@ lpwsinc_build_kernel (GstLPWSinc * self) } /* set up the residue memory space */ - if (!self->residue) + if (!self->residue) { self->residue = g_new0 (gdouble, len * GST_AUDIO_FILTER (self)->format.channels); + self->residue_length = 0; + } self->have_kernel = TRUE; - self->residue_length = 0; } static void |