Commit c962e657 authored by Wim Taymans's avatar Wim Taymans

gst/audioresample/: Fix audioresample, seek torture, new segments, reverse...

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.
parent 130b68ae
...@@ -237,3 +237,17 @@ audioresample_buffer_queue_peek (AudioresampleBufferQueue * queue, int length) ...@@ -237,3 +237,17 @@ audioresample_buffer_queue_peek (AudioresampleBufferQueue * queue, int length)
return newbuffer; 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;
}
...@@ -28,21 +28,24 @@ struct _AudioresampleBufferQueue ...@@ -28,21 +28,24 @@ struct _AudioresampleBufferQueue
int offset; int offset;
}; };
AudioresampleBuffer *audioresample_buffer_new (void); AudioresampleBuffer * audioresample_buffer_new (void);
AudioresampleBuffer *audioresample_buffer_new_and_alloc (int size); AudioresampleBuffer * audioresample_buffer_new_and_alloc (int size);
AudioresampleBuffer *audioresample_buffer_new_with_data (void *data, int size); AudioresampleBuffer * audioresample_buffer_new_with_data (void *data, int size);
AudioresampleBuffer *audioresample_buffer_new_subbuffer (AudioresampleBuffer * buffer, int offset, AudioresampleBuffer * audioresample_buffer_new_subbuffer (AudioresampleBuffer * buffer,
int length); int offset,
void audioresample_buffer_ref (AudioresampleBuffer * buffer); int length);
void audioresample_buffer_unref (AudioresampleBuffer * buffer); 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); AudioresampleBufferQueue *
int audioresample_buffer_queue_get_depth (AudioresampleBufferQueue * queue); audioresample_buffer_queue_new (void);
int audioresample_buffer_queue_get_offset (AudioresampleBufferQueue * queue); void audioresample_buffer_queue_free (AudioresampleBufferQueue * queue);
void audioresample_buffer_queue_push (AudioresampleBufferQueue * queue, int audioresample_buffer_queue_get_depth (AudioresampleBufferQueue * queue);
AudioresampleBuffer * buffer); int audioresample_buffer_queue_get_offset (AudioresampleBufferQueue * queue);
AudioresampleBuffer *audioresample_buffer_queue_pull (AudioresampleBufferQueue * queue, int len); void audioresample_buffer_queue_push (AudioresampleBufferQueue * queue,
AudioresampleBuffer *audioresample_buffer_queue_peek (AudioresampleBufferQueue * queue, int len); 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 #endif
...@@ -48,6 +48,8 @@ enum ...@@ -48,6 +48,8 @@ enum
LAST_SIGNAL LAST_SIGNAL
}; };
#define DEFAULT_FILTERLEN 16
enum enum
{ {
ARG_0, ARG_0,
...@@ -97,8 +99,12 @@ GST_STATIC_CAPS ( \ ...@@ -97,8 +99,12 @@ GST_STATIC_CAPS ( \
GstCaps * outcaps, guint * outsize); GstCaps * outcaps, guint * outsize);
gboolean audioresample_set_caps (GstBaseTransform * base, GstCaps * incaps, gboolean audioresample_set_caps (GstBaseTransform * base, GstCaps * incaps,
GstCaps * outcaps); GstCaps * outcaps);
static GstFlowReturn audioresample_pushthrough (GstAudioresample *
audioresample);
static GstFlowReturn audioresample_transform (GstBaseTransform * base, static GstFlowReturn audioresample_transform (GstBaseTransform * base,
GstBuffer * inbuf, GstBuffer * outbuf); GstBuffer * inbuf, GstBuffer * outbuf);
static gboolean audioresample_event (GstBaseTransform * base,
GstEvent * event);
/*static guint gst_audioresample_signals[LAST_SIGNAL] = { 0 }; */ /*static guint gst_audioresample_signals[LAST_SIGNAL] = { 0 }; */
...@@ -133,7 +139,8 @@ static void gst_audioresample_class_init (GstAudioresampleClass * klass) ...@@ -133,7 +139,8 @@ static void gst_audioresample_class_init (GstAudioresampleClass * klass)
g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FILTERLEN, g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FILTERLEN,
g_param_spec_int ("filter_length", "filter_length", "filter_length", 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_BASE_TRANSFORM_CLASS (klass)->transform_size =
GST_DEBUG_FUNCPTR (audioresample_transform_size); GST_DEBUG_FUNCPTR (audioresample_transform_size);
...@@ -145,19 +152,32 @@ static void gst_audioresample_class_init (GstAudioresampleClass * klass) ...@@ -145,19 +152,32 @@ static void gst_audioresample_class_init (GstAudioresampleClass * klass)
GST_DEBUG_FUNCPTR (audioresample_set_caps); GST_DEBUG_FUNCPTR (audioresample_set_caps);
GST_BASE_TRANSFORM_CLASS (klass)->transform = GST_BASE_TRANSFORM_CLASS (klass)->transform =
GST_DEBUG_FUNCPTR (audioresample_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; 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) GstAudioresampleClass * klass)
{ {
ResampleState *r; 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 (); r = resample_new ();
audioresample->resample = r; 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); resample_set_format (r, RESAMPLE_FORMAT_S16);
} }
...@@ -197,16 +217,14 @@ gboolean ...@@ -197,16 +217,14 @@ gboolean
GstCaps *audioresample_transform_caps (GstBaseTransform * base, GstCaps *audioresample_transform_caps (GstBaseTransform * base,
GstPadDirection direction, GstCaps * caps) GstPadDirection direction, GstCaps * caps)
{ {
GstCaps *temp, *res; GstCaps *res;
const GstCaps *templcaps;
GstStructure *structure; GstStructure *structure;
temp = gst_caps_copy (caps); /* transform caps gives one single caps so we can just replace
structure = gst_caps_get_structure (temp, 0); * the rate property with our range. */
gst_structure_remove_field (structure, "rate"); res = gst_caps_copy (caps);
templcaps = gst_pad_get_pad_template_caps (base->srcpad); structure = gst_caps_get_structure (res, 0);
res = gst_caps_intersect (templcaps, temp); gst_structure_set (structure, "rate", GST_TYPE_INT_RANGE, 1, G_MAXINT, NULL);
gst_caps_unref (temp);
return res; return res;
} }
...@@ -286,6 +304,7 @@ gboolean ...@@ -286,6 +304,7 @@ gboolean
GST_DEBUG_OBJECT (audioresample, GST_DEBUG_OBJECT (audioresample,
"caps are not the set caps, creating state"); "caps are not the set caps, creating state");
state = resample_new (); state = resample_new ();
resample_set_filter_length (state, audioresample->filter_length);
resample_set_state_from_caps (state, sinkcaps, srccaps, NULL, NULL, NULL); resample_set_state_from_caps (state, sinkcaps, srccaps, NULL, NULL, NULL);
} }
...@@ -293,12 +312,9 @@ gboolean ...@@ -293,12 +312,9 @@ gboolean
/* asked to convert size of an incoming buffer */ /* asked to convert size of an incoming buffer */
*othersize = resample_get_output_size_for_input (state, size); *othersize = resample_get_output_size_for_input (state, size);
} else { } else {
/* take a best guess, this is called cheating */ /* asked to convert size of an outgoing buffer */
*othersize = floor (size * state->i_rate / state->o_rate); *othersize = resample_get_input_size_for_output (state, size);
*othersize -= *othersize % state->sample_size;
} }
*othersize += state->sample_size;
g_assert (*othersize % state->sample_size == 0); g_assert (*othersize % state->sample_size == 0);
/* we make room for one extra sample, given that the resampling filter /* we make room for one extra sample, given that the resampling filter
...@@ -346,35 +362,50 @@ gboolean ...@@ -346,35 +362,50 @@ gboolean
return TRUE; 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 static GstFlowReturn
audioresample_transform (GstBaseTransform * base, GstBuffer * inbuf, audioresample_do_output (GstAudioresample * audioresample,
GstBuffer * outbuf) GstBuffer * outbuf)
{ {
/* FIXME: this-> */
GstAudioresample *audioresample = GST_AUDIORESAMPLE (base);
ResampleState *r;
guchar *data;
gulong size;
int outsize; int outsize;
int outsamples; int outsamples;
ResampleState *r;
/* FIXME: move to _inplace */
#if 0
if (audioresample->passthru) {
gst_pad_push (audioresample->srcpad, GST_DATA (buf));
return;
}
#endif
r = audioresample->resample; 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); outsize = resample_get_output_size (r);
GST_DEBUG_OBJECT (audioresample, "audioresample can give me %d bytes", GST_DEBUG_OBJECT (audioresample, "audioresample can give me %d bytes",
outsize); outsize);
...@@ -399,18 +430,27 @@ static GstFlowReturn ...@@ -399,18 +430,27 @@ static GstFlowReturn
outsize, outsamples); outsize, outsamples);
GST_BUFFER_OFFSET (outbuf) = audioresample->offset; GST_BUFFER_OFFSET (outbuf) = audioresample->offset;
GST_BUFFER_TIMESTAMP (outbuf) = base->segment.start + GST_BUFFER_TIMESTAMP (outbuf) = audioresample->next_ts;
audioresample->offset * GST_SECOND / audioresample->o_rate;
if (audioresample->ts_offset != -1) {
audioresample->offset += outsamples; audioresample->offset += outsamples;
GST_BUFFER_OFFSET_END (outbuf) = audioresample->offset; audioresample->ts_offset += outsamples;
audioresample->next_ts =
/* we calculate DURATION as the difference between "next" timestamp gst_util_uint64_scale_int (audioresample->ts_offset, GST_SECOND,
* and current timestamp so we ensure a contiguous stream, instead of audioresample->o_rate);
* having rounding errors. */ GST_BUFFER_OFFSET_END (outbuf) = audioresample->offset;
GST_BUFFER_DURATION (outbuf) = base->segment.start +
audioresample->offset * GST_SECOND / audioresample->o_rate - /* we calculate DURATION as the difference between "next" timestamp
GST_BUFFER_TIMESTAMP (outbuf); * 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 */ /* check for possible mem corruption */
if (outsize > GST_BUFFER_SIZE (outbuf)) { if (outsize > GST_BUFFER_SIZE (outbuf)) {
...@@ -429,10 +469,87 @@ static GstFlowReturn ...@@ -429,10 +469,87 @@ static GstFlowReturn
"audioresample's written outsize %d too far from outbuffer's size %d", "audioresample's written outsize %d too far from outbuffer's size %d",
outsize, GST_BUFFER_SIZE (outbuf)); outsize, GST_BUFFER_SIZE (outbuf));
} }
GST_BUFFER_SIZE (outbuf) = outsize;
return GST_FLOW_OK; 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 static void
gst_audioresample_set_property (GObject * object, guint prop_id, gst_audioresample_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec) const GValue * value, GParamSpec * pspec)
......
...@@ -53,6 +53,8 @@ struct _GstAudioresample { ...@@ -53,6 +53,8 @@ struct _GstAudioresample {
gboolean passthru; gboolean passthru;
guint64 offset; guint64 offset;
guint64 ts_offset;
GstClockTime next_ts;
int channels; int channels;
int i_rate; int i_rate;
......
...@@ -121,19 +121,50 @@ resample_add_input_data (ResampleState * r, void *data, int size, ...@@ -121,19 +121,50 @@ resample_add_input_data (ResampleState * r, void *data, int size,
} }
void 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; 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 * /* if we have no pending samples, we don't need to do anything. */
(r->filter_length / 2)); 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); memset (buffer->data, 0, buffer->length);
RESAMPLE_DEBUG ("pushthrough", buffer->length);
audioresample_buffer_queue_push (r->queue, buffer); audioresample_buffer_queue_push (r->queue, buffer);
}
void
resample_input_eos (ResampleState * r)
{
RESAMPLE_DEBUG ("EOS");
resample_input_pushthrough (r);
r->eos = 1; r->eos = 1;
} }
...@@ -142,22 +173,61 @@ resample_get_output_size_for_input (ResampleState * r, int size) ...@@ -142,22 +173,61 @@ resample_get_output_size_for_input (ResampleState * r, int size)
{ {
int outsize; int outsize;
double outd; 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); outsize = (int) floor (outd);
/* round off for sample size */ /* 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 int
resample_get_output_size (ResampleState * r) resample_get_output_size (ResampleState * r)
{ {
return resample_get_output_size_for_input (r, return resample_get_output_size_for_input (r, 0);
audioresample_buffer_queue_get_depth (r->queue));
} }
int int
...@@ -166,6 +236,9 @@ resample_get_output_data (ResampleState * r, void *data, int size) ...@@ -166,6 +236,9 @@ resample_get_output_data (ResampleState * r, void *data, int size)
r->o_buf = data; r->o_buf = data;
r->o_size = size; r->o_size = size;
if (size == 0)
return 0;
switch (r->method) { switch (r->method) {
case 0: case 0:
resample_scale_ref (r); resample_scale_ref (r);
......
...@@ -67,6 +67,7 @@ struct _ResampleState { ...@@ -67,6 +67,7 @@ struct _ResampleState {