summaryrefslogtreecommitdiffstats
path: root/gst/audioresample
diff options
context:
space:
mode:
authorWim Taymans <wim.taymans@gmail.com>2005-12-02 11:34:50 +0000
committerWim Taymans <wim.taymans@gmail.com>2005-12-02 11:34:50 +0000
commitc962e657c3ce4ef1ba270b9c298d07eb9e3b1c09 (patch)
tree44e5b1068699db69a59d2772d4930723eca0acad /gst/audioresample
parent130b68aeff56298ade65ce96fb4966356afb5cd5 (diff)
downloadgst-plugins-bad-c962e657c3ce4ef1ba270b9c298d07eb9e3b1c09.tar.gz
gst-plugins-bad-c962e657c3ce4ef1ba270b9c298d07eb9e3b1c09.tar.bz2
gst-plugins-bad-c962e657c3ce4ef1ba270b9c298d07eb9e3b1c09.zip
gst/audioresample/: Fix audioresample, seek torture, new segments, reverse negotiation etc.. work fine.
Original commit message from CVS: * gst/audioresample/buffer.c: (audioresample_buffer_queue_flush): * gst/audioresample/buffer.h: * gst/audioresample/gstaudioresample.c: * gst/audioresample/gstaudioresample.h: * gst/audioresample/resample.c: (resample_input_flush), (resample_input_pushthrough), (resample_input_eos), (resample_get_output_size_for_input), (resample_get_input_size_for_output), (resample_get_output_size), (resample_get_output_data): * gst/audioresample/resample.h: * gst/audioresample/resample_ref.c: (resample_scale_ref): Fix audioresample, seek torture, new segments, reverse negotiation etc.. work fine.
Diffstat (limited to 'gst/audioresample')
-rw-r--r--gst/audioresample/buffer.c14
-rw-r--r--gst/audioresample/buffer.h35
-rw-r--r--gst/audioresample/gstaudioresample.c215
-rw-r--r--gst/audioresample/gstaudioresample.h2
-rw-r--r--gst/audioresample/resample.c95
-rw-r--r--gst/audioresample/resample.h5
-rw-r--r--gst/audioresample/resample_ref.c3
7 files changed, 293 insertions, 76 deletions
diff --git a/gst/audioresample/buffer.c b/gst/audioresample/buffer.c
index 679fa020..442b4f8c 100644
--- a/gst/audioresample/buffer.c
+++ b/gst/audioresample/buffer.c
@@ -237,3 +237,17 @@ audioresample_buffer_queue_peek (AudioresampleBufferQueue * queue, int length)
return newbuffer;
}
+
+void
+audioresample_buffer_queue_flush (AudioresampleBufferQueue * queue)
+{
+ GList *g;
+
+ for (g = g_list_first (queue->buffers); g; g = g_list_next (g)) {
+ audioresample_buffer_unref ((AudioresampleBuffer *) g->data);
+ }
+ g_list_free (queue->buffers);
+ queue->buffers = NULL;
+ queue->depth = 0;
+ queue->offset = 0;
+}
diff --git a/gst/audioresample/buffer.h b/gst/audioresample/buffer.h
index 17fb5f90..35b15e7c 100644
--- a/gst/audioresample/buffer.h
+++ b/gst/audioresample/buffer.h
@@ -28,21 +28,24 @@ struct _AudioresampleBufferQueue
int offset;
};
-AudioresampleBuffer *audioresample_buffer_new (void);
-AudioresampleBuffer *audioresample_buffer_new_and_alloc (int size);
-AudioresampleBuffer *audioresample_buffer_new_with_data (void *data, int size);
-AudioresampleBuffer *audioresample_buffer_new_subbuffer (AudioresampleBuffer * buffer, int offset,
- int length);
-void audioresample_buffer_ref (AudioresampleBuffer * buffer);
-void audioresample_buffer_unref (AudioresampleBuffer * buffer);
-
-AudioresampleBufferQueue *audioresample_buffer_queue_new (void);
-void audioresample_buffer_queue_free (AudioresampleBufferQueue * queue);
-int audioresample_buffer_queue_get_depth (AudioresampleBufferQueue * queue);
-int audioresample_buffer_queue_get_offset (AudioresampleBufferQueue * queue);
-void audioresample_buffer_queue_push (AudioresampleBufferQueue * queue,
- AudioresampleBuffer * buffer);
-AudioresampleBuffer *audioresample_buffer_queue_pull (AudioresampleBufferQueue * queue, int len);
-AudioresampleBuffer *audioresample_buffer_queue_peek (AudioresampleBufferQueue * queue, int len);
+AudioresampleBuffer * audioresample_buffer_new (void);
+AudioresampleBuffer * audioresample_buffer_new_and_alloc (int size);
+AudioresampleBuffer * audioresample_buffer_new_with_data (void *data, int size);
+AudioresampleBuffer * audioresample_buffer_new_subbuffer (AudioresampleBuffer * buffer,
+ int offset,
+ int length);
+void audioresample_buffer_ref (AudioresampleBuffer * buffer);
+void audioresample_buffer_unref (AudioresampleBuffer * buffer);
+
+AudioresampleBufferQueue *
+ audioresample_buffer_queue_new (void);
+void audioresample_buffer_queue_free (AudioresampleBufferQueue * queue);
+int audioresample_buffer_queue_get_depth (AudioresampleBufferQueue * queue);
+int audioresample_buffer_queue_get_offset (AudioresampleBufferQueue * queue);
+void audioresample_buffer_queue_push (AudioresampleBufferQueue * queue,
+ AudioresampleBuffer * buffer);
+AudioresampleBuffer * audioresample_buffer_queue_pull (AudioresampleBufferQueue * queue, int len);
+AudioresampleBuffer * audioresample_buffer_queue_peek (AudioresampleBufferQueue * queue, int len);
+void audioresample_buffer_queue_flush (AudioresampleBufferQueue * queue);
#endif
diff --git a/gst/audioresample/gstaudioresample.c b/gst/audioresample/gstaudioresample.c
index 6077a610..3fbff60e 100644
--- a/gst/audioresample/gstaudioresample.c
+++ b/gst/audioresample/gstaudioresample.c
@@ -48,6 +48,8 @@ enum
LAST_SIGNAL
};
+#define DEFAULT_FILTERLEN 16
+
enum
{
ARG_0,
@@ -97,8 +99,12 @@ GST_STATIC_CAPS ( \
GstCaps * outcaps, guint * outsize);
gboolean audioresample_set_caps (GstBaseTransform * base, GstCaps * incaps,
GstCaps * outcaps);
+ static GstFlowReturn audioresample_pushthrough (GstAudioresample *
+ audioresample);
static GstFlowReturn audioresample_transform (GstBaseTransform * base,
GstBuffer * inbuf, GstBuffer * outbuf);
+ static gboolean audioresample_event (GstBaseTransform * base,
+ GstEvent * event);
/*static guint gst_audioresample_signals[LAST_SIGNAL] = { 0 }; */
@@ -133,7 +139,8 @@ static void gst_audioresample_class_init (GstAudioresampleClass * klass)
g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FILTERLEN,
g_param_spec_int ("filter_length", "filter_length", "filter_length",
- 0, G_MAXINT, 16, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
+ 0, G_MAXINT, DEFAULT_FILTERLEN,
+ G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
GST_BASE_TRANSFORM_CLASS (klass)->transform_size =
GST_DEBUG_FUNCPTR (audioresample_transform_size);
@@ -145,19 +152,32 @@ static void gst_audioresample_class_init (GstAudioresampleClass * klass)
GST_DEBUG_FUNCPTR (audioresample_set_caps);
GST_BASE_TRANSFORM_CLASS (klass)->transform =
GST_DEBUG_FUNCPTR (audioresample_transform);
+ GST_BASE_TRANSFORM_CLASS (klass)->event =
+ GST_DEBUG_FUNCPTR (audioresample_event);
GST_BASE_TRANSFORM_CLASS (klass)->passthrough_on_same_caps = TRUE;
}
-static void gst_audioresample_init (GstAudioresample * audioresample,
+static void
+ gst_audioresample_init (GstAudioresample * audioresample,
GstAudioresampleClass * klass)
{
ResampleState *r;
+ GstBaseTransform *trans;
+
+ trans = GST_BASE_TRANSFORM (audioresample);
+
+ /* buffer alloc passthrough is too impossible. FIXME, it
+ * is trivial in the passtrough case. */
+ gst_pad_set_bufferalloc_function (trans->sinkpad, NULL);
r = resample_new ();
audioresample->resample = r;
+ audioresample->ts_offset = -1;
+ audioresample->offset = -1;
+ audioresample->next_ts = -1;
- resample_set_filter_length (r, 64);
+ resample_set_filter_length (r, DEFAULT_FILTERLEN);
resample_set_format (r, RESAMPLE_FORMAT_S16);
}
@@ -197,16 +217,14 @@ gboolean
GstCaps *audioresample_transform_caps (GstBaseTransform * base,
GstPadDirection direction, GstCaps * caps)
{
- GstCaps *temp, *res;
- const GstCaps *templcaps;
+ GstCaps *res;
GstStructure *structure;
- temp = gst_caps_copy (caps);
- structure = gst_caps_get_structure (temp, 0);
- gst_structure_remove_field (structure, "rate");
- templcaps = gst_pad_get_pad_template_caps (base->srcpad);
- res = gst_caps_intersect (templcaps, temp);
- gst_caps_unref (temp);
+ /* transform caps gives one single caps so we can just replace
+ * the rate property with our range. */
+ res = gst_caps_copy (caps);
+ structure = gst_caps_get_structure (res, 0);
+ gst_structure_set (structure, "rate", GST_TYPE_INT_RANGE, 1, G_MAXINT, NULL);
return res;
}
@@ -286,6 +304,7 @@ gboolean
GST_DEBUG_OBJECT (audioresample,
"caps are not the set caps, creating state");
state = resample_new ();
+ resample_set_filter_length (state, audioresample->filter_length);
resample_set_state_from_caps (state, sinkcaps, srccaps, NULL, NULL, NULL);
}
@@ -293,12 +312,9 @@ gboolean
/* asked to convert size of an incoming buffer */
*othersize = resample_get_output_size_for_input (state, size);
} else {
- /* take a best guess, this is called cheating */
- *othersize = floor (size * state->i_rate / state->o_rate);
- *othersize -= *othersize % state->sample_size;
+ /* asked to convert size of an outgoing buffer */
+ *othersize = resample_get_input_size_for_output (state, size);
}
- *othersize += state->sample_size;
-
g_assert (*othersize % state->sample_size == 0);
/* we make room for one extra sample, given that the resampling filter
@@ -346,35 +362,50 @@ gboolean
return TRUE;
}
+static gboolean audioresample_event (GstBaseTransform * base, GstEvent * event)
+{
+ GstAudioresample *audioresample;
+
+ audioresample = GST_AUDIORESAMPLE (base);
+
+ switch (GST_EVENT_TYPE (event)) {
+ case GST_EVENT_FLUSH_START:
+ break;
+ case GST_EVENT_FLUSH_STOP:
+ resample_input_flush (audioresample->resample);
+ audioresample->ts_offset = -1;
+ audioresample->next_ts = -1;
+ audioresample->offset = -1;
+ break;
+ case GST_EVENT_NEWSEGMENT:
+ resample_input_pushthrough (audioresample->resample);
+ audioresample_pushthrough (audioresample);
+ audioresample->ts_offset = -1;
+ audioresample->next_ts = -1;
+ audioresample->offset = -1;
+ break;
+ case GST_EVENT_EOS:
+ resample_input_eos (audioresample->resample);
+ audioresample_pushthrough (audioresample);
+ break;
+ default:
+ break;
+ }
+ parent_class->event (base, event);
+
+ return TRUE;
+}
+
static GstFlowReturn
- audioresample_transform (GstBaseTransform * base, GstBuffer * inbuf,
+ audioresample_do_output (GstAudioresample * audioresample,
GstBuffer * outbuf)
{
- /* FIXME: this-> */
- GstAudioresample *audioresample = GST_AUDIORESAMPLE (base);
- ResampleState *r;
- guchar *data;
- gulong size;
int outsize;
int outsamples;
-
- /* FIXME: move to _inplace */
-#if 0
- if (audioresample->passthru) {
- gst_pad_push (audioresample->srcpad, GST_DATA (buf));
- return;
- }
-#endif
+ ResampleState *r;
r = audioresample->resample;
- data = GST_BUFFER_DATA (inbuf);
- size = GST_BUFFER_SIZE (inbuf);
-
- GST_DEBUG_OBJECT (audioresample, "got buffer of %ld bytes", size);
-
- resample_add_input_data (r, data, size, NULL, NULL);
-
outsize = resample_get_output_size (r);
GST_DEBUG_OBJECT (audioresample, "audioresample can give me %d bytes",
outsize);
@@ -399,18 +430,27 @@ static GstFlowReturn
outsize, outsamples);
GST_BUFFER_OFFSET (outbuf) = audioresample->offset;
- GST_BUFFER_TIMESTAMP (outbuf) = base->segment.start +
- audioresample->offset * GST_SECOND / audioresample->o_rate;
-
- audioresample->offset += outsamples;
- GST_BUFFER_OFFSET_END (outbuf) = audioresample->offset;
-
- /* we calculate DURATION as the difference between "next" timestamp
- * and current timestamp so we ensure a contiguous stream, instead of
- * having rounding errors. */
- GST_BUFFER_DURATION (outbuf) = base->segment.start +
- audioresample->offset * GST_SECOND / audioresample->o_rate -
- GST_BUFFER_TIMESTAMP (outbuf);
+ GST_BUFFER_TIMESTAMP (outbuf) = audioresample->next_ts;
+
+ if (audioresample->ts_offset != -1) {
+ audioresample->offset += outsamples;
+ audioresample->ts_offset += outsamples;
+ audioresample->next_ts =
+ gst_util_uint64_scale_int (audioresample->ts_offset, GST_SECOND,
+ audioresample->o_rate);
+ GST_BUFFER_OFFSET_END (outbuf) = audioresample->offset;
+
+ /* we calculate DURATION as the difference between "next" timestamp
+ * and current timestamp so we ensure a contiguous stream, instead of
+ * having rounding errors. */
+ GST_BUFFER_DURATION (outbuf) = audioresample->next_ts -
+ GST_BUFFER_TIMESTAMP (outbuf);
+ } else {
+ /* no valid offset know, we can still sortof calculate the duration though */
+ GST_BUFFER_DURATION (outbuf) =
+ gst_util_uint64_scale_int (outsamples, GST_SECOND,
+ audioresample->o_rate);
+ }
/* check for possible mem corruption */
if (outsize > GST_BUFFER_SIZE (outbuf)) {
@@ -429,10 +469,87 @@ static GstFlowReturn
"audioresample's written outsize %d too far from outbuffer's size %d",
outsize, GST_BUFFER_SIZE (outbuf));
}
+ GST_BUFFER_SIZE (outbuf) = outsize;
return GST_FLOW_OK;
}
+static GstFlowReturn
+ audioresample_transform (GstBaseTransform * base, GstBuffer * inbuf,
+ GstBuffer * outbuf)
+{
+ GstAudioresample *audioresample;
+ ResampleState *r;
+ guchar *data;
+ gulong size;
+ GstClockTime timestamp;
+
+ audioresample = GST_AUDIORESAMPLE (base);
+ r = audioresample->resample;
+
+ data = GST_BUFFER_DATA (inbuf);
+ size = GST_BUFFER_SIZE (inbuf);
+ timestamp = GST_BUFFER_TIMESTAMP (inbuf);
+
+ GST_DEBUG_OBJECT (audioresample, "got buffer of %ld bytes", size);
+
+ if (audioresample->ts_offset == -1) {
+ /* if we don't know the initial offset yet, calculate it based on the
+ * input timestamp. */
+ if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
+ GstClockTime stime;
+
+ /* offset used to calculate the timestamps. We use the sample offset for this
+ * to make it more accurate. We want the first buffer to have the same timestamp
+ * as the incomming timestamp. */
+ audioresample->next_ts = timestamp;
+ audioresample->ts_offset =
+ gst_util_uint64_scale_int (timestamp, r->o_rate, GST_SECOND);
+ /* offset used to set as the buffer offset, this offset is always relative
+ * to the stream time, note that timestamp is not... */
+ stime = (timestamp - base->segment.start) + base->segment.time;
+ audioresample->offset =
+ gst_util_uint64_scale_int (stime, r->o_rate, GST_SECOND);
+ }
+ }
+
+ /* need to memdup, resample takes ownership. */
+ resample_add_input_data (r, g_memdup (data, size), size, NULL, NULL);
+
+ return audioresample_do_output (audioresample, outbuf);
+}
+
+/* push remaining data in the buffers out */
+static GstFlowReturn
+ audioresample_pushthrough (GstAudioresample * audioresample)
+{
+ int outsize;
+ ResampleState *r;
+ GstBuffer *outbuf;
+ GstFlowReturn res = GST_FLOW_OK;
+ GstBaseTransform *trans;
+
+ r = audioresample->resample;
+
+ outsize = resample_get_output_size (r);
+ if (outsize == 0)
+ goto done;
+
+ outbuf = gst_buffer_new_and_alloc (outsize);
+
+ res = audioresample_do_output (audioresample, outbuf);
+ if (res != GST_FLOW_OK)
+ goto done;
+
+ trans = GST_BASE_TRANSFORM (audioresample);
+
+ res = gst_pad_push (trans->srcpad, outbuf);
+
+done:
+ return res;
+}
+
+
static void
gst_audioresample_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec)
diff --git a/gst/audioresample/gstaudioresample.h b/gst/audioresample/gstaudioresample.h
index 943bc8d8..9bf18c9f 100644
--- a/gst/audioresample/gstaudioresample.h
+++ b/gst/audioresample/gstaudioresample.h
@@ -53,6 +53,8 @@ struct _GstAudioresample {
gboolean passthru;
guint64 offset;
+ guint64 ts_offset;
+ GstClockTime next_ts;
int channels;
int i_rate;
diff --git a/gst/audioresample/resample.c b/gst/audioresample/resample.c
index 1f793639..7571ff15 100644
--- a/gst/audioresample/resample.c
+++ b/gst/audioresample/resample.c
@@ -121,19 +121,50 @@ resample_add_input_data (ResampleState * r, void *data, int size,
}
void
-resample_input_eos (ResampleState * r)
+resample_input_flush (ResampleState * r)
+{
+ RESAMPLE_DEBUG ("flush");
+
+ audioresample_buffer_queue_flush (r->queue);
+ r->buffer_filled = 0;
+ r->need_reinit = 1;
+}
+
+void
+resample_input_pushthrough (ResampleState * r)
{
AudioresampleBuffer *buffer;
- int sample_size;
+ int filter_bytes;
+ int buffer_filled;
+
+ if (r->sample_size == 0)
+ return;
+
+ filter_bytes = r->filter_length * r->sample_size;
+ buffer_filled = r->buffer_filled;
- sample_size = r->n_channels * resample_format_size (r->format);
+ RESAMPLE_DEBUG ("pushthrough filter_bytes %d, filled %d",
+ filter_bytes, buffer_filled);
- buffer = audioresample_buffer_new_and_alloc (sample_size *
- (r->filter_length / 2));
+ /* if we have no pending samples, we don't need to do anything. */
+ if (buffer_filled <= 0)
+ return;
+
+ /* send filter_length/2 number of samples so we can get to the
+ * last queued samples */
+ buffer = audioresample_buffer_new_and_alloc (filter_bytes / 2);
memset (buffer->data, 0, buffer->length);
+ RESAMPLE_DEBUG ("pushthrough", buffer->length);
+
audioresample_buffer_queue_push (r->queue, buffer);
+}
+void
+resample_input_eos (ResampleState * r)
+{
+ RESAMPLE_DEBUG ("EOS");
+ resample_input_pushthrough (r);
r->eos = 1;
}
@@ -142,22 +173,61 @@ resample_get_output_size_for_input (ResampleState * r, int size)
{
int outsize;
double outd;
+ int avail;
+ int filter_bytes;
+ int buffer_filled;
+
+ if (r->sample_size == 0)
+ return 0;
+
+ filter_bytes = r->filter_length * r->sample_size;
+ buffer_filled = filter_bytes / 2 - r->buffer_filled / 2;
- g_return_val_if_fail (r->sample_size != 0, 0);
+ avail =
+ audioresample_buffer_queue_get_depth (r->queue) + size - buffer_filled;
+
+ RESAMPLE_DEBUG ("avail %d, o_rate %f, i_rate %f, filter_bytes %d, filled %d",
+ avail, r->o_rate, r->i_rate, filter_bytes, buffer_filled);
+ if (avail <= 0)
+ return 0;
+
+ outd = (double) avail *r->o_rate / r->i_rate;
- RESAMPLE_DEBUG ("size %d, o_rate %f, i_rate %f", size, r->o_rate, r->i_rate);
- outd = (double) size / r->i_rate * r->o_rate;
outsize = (int) floor (outd);
/* round off for sample size */
- return outsize - (outsize % r->sample_size);
+ outsize -= outsize % r->sample_size;
+
+ return outsize;
+}
+
+int
+resample_get_input_size_for_output (ResampleState * r, int size)
+{
+ int outsize;
+ double outd;
+ int avail;
+
+ if (r->sample_size == 0)
+ return 0;
+
+ avail = size;
+
+ RESAMPLE_DEBUG ("size %d, o_rate %f, i_rate %f", avail, r->o_rate, r->i_rate);
+ outd = (double) avail *r->i_rate / r->o_rate;
+
+ outsize = (int) ceil (outd);
+
+ /* round off for sample size */
+ outsize -= outsize % r->sample_size;
+
+ return outsize;
}
int
resample_get_output_size (ResampleState * r)
{
- return resample_get_output_size_for_input (r,
- audioresample_buffer_queue_get_depth (r->queue));
+ return resample_get_output_size_for_input (r, 0);
}
int
@@ -166,6 +236,9 @@ resample_get_output_data (ResampleState * r, void *data, int size)
r->o_buf = data;
r->o_size = size;
+ if (size == 0)
+ return 0;
+
switch (r->method) {
case 0:
resample_scale_ref (r);
diff --git a/gst/audioresample/resample.h b/gst/audioresample/resample.h
index ea4aa305..f27e2be4 100644
--- a/gst/audioresample/resample.h
+++ b/gst/audioresample/resample.h
@@ -67,6 +67,7 @@ struct _ResampleState {
void *buffer;
int buffer_len;
+ int buffer_filled;
double i_start;
double o_start;
@@ -98,8 +99,12 @@ void resample_free (ResampleState *state);
void resample_add_input_data (ResampleState * r, void *data, int size,
ResampleCallback free_func, void *closure);
void resample_input_eos (ResampleState *r);
+void resample_input_flush (ResampleState *r);
+void resample_input_pushthrough (ResampleState *r);
int resample_get_output_size_for_input (ResampleState * r, int size);
+int resample_get_input_size_for_output (ResampleState * r, int size);
+
int resample_get_output_size (ResampleState *r);
int resample_get_output_data (ResampleState *r, void *data, int size);
diff --git a/gst/audioresample/resample_ref.c b/gst/audioresample/resample_ref.c
index 4cb3d250..bb8d2411 100644
--- a/gst/audioresample/resample_ref.c
+++ b/gst/audioresample/resample_ref.c
@@ -63,6 +63,7 @@ resample_scale_ref (ResampleState * r)
r->buffer_len = r->sample_size * r->filter_length;
r->buffer = malloc (r->buffer_len);
memset (r->buffer, 0, r->buffer_len);
+ r->buffer_filled = 0;
r->i_inc = r->o_rate / r->i_rate;
r->o_inc = r->i_rate / r->o_rate;
@@ -127,6 +128,8 @@ resample_scale_ref (ResampleState * r)
memcpy (r->buffer + r->buffer_len - r->sample_size, buffer->data,
r->sample_size);
+ r->buffer_filled = MIN (r->buffer_filled + r->sample_size, r->buffer_len);
+
audioresample_buffer_unref (buffer);
}