2005-08-24 Wim Taymans <wim@fluendo.com>
+ * gst/base/gstbasetransform.c: (gst_base_transform_init),
+ (gst_base_transform_transform_caps),
+ (gst_base_transform_transform_size),
+ (gst_base_transform_configure_caps), (gst_base_transform_setcaps),
+ (gst_base_transform_get_size), (gst_base_transform_buffer_alloc),
+ (gst_base_transform_handle_buffer):
+ * gst/base/gstbasetransform.h:
+ Many fixes and new features added by Thomas. Can now also do
+ transforms with variable sizes and a custom fixate_caps function.
+
+2005-08-24 Wim Taymans <wim@fluendo.com>
+
* gst/gstbuffer.c: (gst_buffer_new_and_alloc):
Some debugging.
* 2000 Wim Taymans <wtay@chello.be>
* 2005 Wim Taymans <wim@fluendo.com>
*
- * gstbasetransform.c:
+ * gstbasetransform.c:
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
gst_element_add_pad (GST_ELEMENT (trans), trans->srcpad);
trans->passthrough = FALSE;
+ trans->delay_configure = FALSE;
+ trans->pending_configure = FALSE;
trans->out_size = -1;
}
gst_caps_append (ret, temp);
} else {
- /* we send caps with just one structure to the transform
+ /* we send caps with just one structure to the transform
* function as this is easier for the element */
for (i = 0; i < gst_caps_get_size (caps); i++) {
GstCaps *nth;
return ret;
}
+/* by default, this keeps the number of samples in the buffer the same */
+guint
+gst_base_transform_transform_size (GstBaseTransform * trans,
+ GstPadDirection direction, GstCaps * incaps,
+ guint insize, GstCaps * outcaps)
+{
+ guint inunitsize, outunitsize, units;
+ GstBaseTransformClass *klass;
+ gint ret = -1;
+
+ klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
+
+ GST_DEBUG_OBJECT (trans, "asked to transform size %d for caps %"
+ GST_PTR_FORMAT " to size for caps %" GST_PTR_FORMAT " in direction %d",
+ insize, incaps, outcaps, direction);
+
+ /* if there is a custom transform function, use this */
+ if (klass->transform_size) {
+ ret = klass->transform_size (trans, direction, incaps, insize, outcaps);
+ } else {
+ inunitsize = gst_base_transform_get_size (trans, incaps);
+ g_return_val_if_fail (inunitsize != -1, -1);
+ g_return_val_if_fail (insize % inunitsize == 0, -1);
+
+ units = insize / inunitsize;
+ outunitsize = gst_base_transform_get_size (trans, outcaps);
+ ret = units * outunitsize;
+ }
+
+ GST_DEBUG_OBJECT (trans, "transformed size %d", ret);
+
+ return ret;
+}
+
static GstCaps *
gst_base_transform_getcaps (GstPad * pad)
{
klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
- /* no configure the element with the caps */
+ /* now configure the element with the caps */
if (klass->set_caps) {
ret = klass->set_caps (trans, in, out);
}
GstPad *otherpad, *otherpeer;
GstCaps *othercaps = NULL;
gboolean ret = TRUE;
+ gboolean peer_checked = FALSE;
+
+ /* caps must be fixed here */
+ g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE);
trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
otherpad = (pad == trans->srcpad) ? trans->sinkpad : trans->srcpad;
otherpeer = gst_pad_get_peer (otherpad);
- if (GST_PAD_IS_IN_SETCAPS (otherpad)) {
- ret = TRUE;
+ /* if we get called recursively, we bail out now to avoid an
+ * infinite loop. */
+ if (GST_PAD_IS_IN_SETCAPS (otherpad))
goto done;
- }
- /* see how we can transform the input caps */
+ /* see how we can transform the input caps. */
othercaps = gst_base_transform_transform_caps (trans, pad, caps);
/* check if transform is empty */
if (!othercaps || gst_caps_is_empty (othercaps))
goto no_transform;
+ /* if the othercaps are not fixed, we need to fixate them, first attempt
+ * is by attempting passthrough if the othercaps are a superset of caps. */
if (!gst_caps_is_fixed (othercaps)) {
GstCaps *temp;
GST_DEBUG_OBJECT (trans,
"transform returned non fixed %" GST_PTR_FORMAT, othercaps);
+ /* see if the target caps are a superset of the source caps, in this
+ * case we can try to perform passthrough */
temp = gst_caps_intersect (othercaps, caps);
GST_DEBUG_OBJECT (trans, "intersect returned %" GST_PTR_FORMAT, temp);
if (temp) {
/* try passthrough. we know it's fixed, because caps is fixed */
if (gst_pad_accept_caps (otherpeer, caps)) {
GST_DEBUG_OBJECT (trans, "peer accepted %" GST_PTR_FORMAT, caps);
+ /* peer accepted unmodified caps, we free the original non-fixed
+ * caps and work with the passthrough caps */
gst_caps_unref (othercaps);
othercaps = gst_caps_ref (caps);
- /* will fall though. calls accept_caps again, should fix that. */
+ /* mark that we checked othercaps with the peer, this
+ * makes sure we don't call accept_caps again with these same
+ * caps */
+ peer_checked = TRUE;
} else {
GST_DEBUG_OBJECT (trans,
"peer did not accept %" GST_PTR_FORMAT, caps);
}
}
+ /* second attempt at fixation is done by intersecting with
+ * the peer caps */
if (!gst_caps_is_fixed (othercaps) && otherpeer) {
/* intersect against what the peer can do */
GstCaps *peercaps;
gst_caps_unref (peercaps);
gst_caps_unref (othercaps);
othercaps = intersect;
+ peer_checked = FALSE;
GST_DEBUG_OBJECT (trans,
"filtering against peer yields %" GST_PTR_FORMAT, othercaps);
if (gst_caps_is_empty (othercaps))
goto no_transform_possible;
+ /* third attempt at fixation, call the fixate vmethod and
+ * ultimately call the pad fixate function. */
if (!gst_caps_is_fixed (othercaps)) {
GstCaps *temp;
GST_DEBUG_OBJECT (trans,
- "othercaps now, trying to fixate %" GST_PTR_FORMAT, othercaps);
+ "trying to fixate %" GST_PTR_FORMAT " on pad %s:%s",
+ othercaps, GST_DEBUG_PAD_NAME (otherpad));
- /* take first possibility and fixate if necessary */
+ /* since we have no other way to fixate left, we might as well just take
+ * the first of the caps list and fixate that */
+
+ /* FIXME: when fixating using the vmethod, it might make sense to fixate
+ * each of the caps; but Wim doesn't see a use case for that yet */
temp = gst_caps_copy_nth (othercaps, 0);
gst_caps_unref (othercaps);
othercaps = temp;
- gst_pad_fixate_caps (otherpad, othercaps);
-
+ peer_checked = FALSE;
+
+ if (klass->fixate_caps) {
+ GST_DEBUG_OBJECT (trans, "trying to fixate %" GST_PTR_FORMAT
+ " using caps %" GST_PTR_FORMAT
+ " on pad %s:%s using fixate_caps vmethod", othercaps, caps,
+ GST_DEBUG_PAD_NAME (otherpad));
+ klass->fixate_caps (trans, GST_PAD_DIRECTION (pad), caps, othercaps);
+ }
+ /* if still not fixed, no other option but to let the default pad fixate
+ * function do its job */
+ if (!gst_caps_is_fixed (othercaps)) {
+ GST_DEBUG_OBJECT (trans, "trying to fixate %" GST_PTR_FORMAT
+ " on pad %s:%s using gst_pad_fixate_caps", othercaps,
+ GST_DEBUG_PAD_NAME (otherpad));
+ gst_pad_fixate_caps (otherpad, othercaps);
+ }
GST_DEBUG_OBJECT (trans, "after fixating %" GST_PTR_FORMAT, othercaps);
}
- /* caps should be fixed now */
+ /* caps should be fixed now, if not we have to fail. */
if (!gst_caps_is_fixed (othercaps))
goto could_not_fixate;
- /* and peer should accept */
- if (otherpeer && !gst_pad_accept_caps (otherpeer, othercaps))
+ /* and peer should accept, don't check again if we already checked the
+ * othercaps against the peer. */
+ if (!peer_checked && otherpeer && !gst_pad_accept_caps (otherpeer, othercaps))
goto peer_no_accept;
GST_DEBUG_OBJECT (trans, "got final caps %" GST_PTR_FORMAT, othercaps);
- /* we know this will work, we implement the setcaps */
- gst_pad_set_caps (otherpad, othercaps);
-
trans->in_place = gst_caps_is_equal (caps, othercaps);
GST_DEBUG_OBJECT (trans, "in_place: %d", trans->in_place);
/* see if we have to configure the element now */
if (!trans->delay_configure) {
- if (pad == trans->sinkpad)
- ret = gst_base_transform_configure_caps (trans, caps, othercaps);
- else
- ret = gst_base_transform_configure_caps (trans, othercaps, caps);
+ GstCaps *incaps, *outcaps;
+
+ /* make sure in and out caps are correct */
+ if (pad == trans->sinkpad) {
+ incaps = caps;
+ outcaps = othercaps;
+ } else {
+ incaps = othercaps;
+ outcaps = caps;
+ }
+ /* call configure now */
+ if (!(ret = gst_base_transform_configure_caps (trans, incaps, outcaps)))
+ goto failed_configure;
+ } else {
+ /* set pending configure, the configure will happen later with the
+ * caps we set on the pads above. */
+ trans->pending_configure = TRUE;
}
+ /* we know this will work, we implement the setcaps */
+ gst_pad_set_caps (otherpad, othercaps);
+
done:
if (otherpeer)
gst_object_unref (otherpeer);
ret = FALSE;
goto done;
}
+failed_configure:
+ {
+ GST_DEBUG_OBJECT (trans, "FAILED to configure caps %" GST_PTR_FORMAT
+ " to accept %" GST_PTR_FORMAT, otherpad, othercaps);
+ goto done;
+ }
}
static guint
bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
if (bclass->get_size) {
res = bclass->get_size (trans, caps);
- GST_DEBUG_OBJECT (trans, "get size(%p) returned %d", caps, res);
+ GST_DEBUG_OBJECT (trans, "get size(%" GST_PTR_FORMAT ") returned %d", caps,
+ res);
}
return res;
}
+/* your upstream peer wants to send you a buffer
+ * that buffer has the given offset, size and caps
+ * you're requested to allocate a buffer
+ */
static GstFlowReturn
gst_base_transform_buffer_alloc (GstPad * pad, guint64 offset, guint size,
GstCaps * caps, GstBuffer ** buf)
{
GstBaseTransform *trans;
GstFlowReturn res;
- guint got_size;
+ guint new_size;
trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
*buf = NULL;
+ GST_DEBUG_OBJECT (trans, "allocating a buffer of size %d at offset %"
+ G_GUINT64_FORMAT, size, offset);
+
+ /* before any buffers are pushed, in_place is TRUE; allocating can trigger
+ * a renegotiation and change that to FALSE */
if (trans->in_place) {
/* request a buffer with the same caps */
+ GST_DEBUG_OBJECT (trans, "requesting buffer with same caps, size %d", size);
res = gst_pad_alloc_buffer (trans->srcpad, offset, size, caps, buf);
} else {
/* if we are configured, request a buffer with the src caps */
if (!srccaps)
goto not_configured;
- got_size = gst_base_transform_get_size (trans, srccaps);
- if (got_size == -1) {
+ GST_DEBUG_OBJECT (trans, "calling transform_size");
+ new_size = gst_base_transform_transform_size (trans,
+ GST_PAD_DIRECTION (pad), caps, size, srccaps);
+ if (new_size == -1) {
gst_caps_unref (srccaps);
goto unknown_size;
}
- res = gst_pad_alloc_buffer (trans->srcpad, offset, got_size, srccaps, buf);
+ res = gst_pad_alloc_buffer (trans->srcpad, offset, new_size, srccaps, buf);
gst_caps_unref (srccaps);
}
buffer. fall through and allocate a buffer corresponding to our sink
caps, if any */
GstCaps *sinkcaps = gst_pad_get_negotiated_caps (trans->sinkpad);
+ GstCaps *srccaps = gst_pad_get_negotiated_caps (trans->srcpad);
if (!sinkcaps)
goto not_configured;
- got_size = gst_base_transform_get_size (trans, sinkcaps);
- if (got_size == -1) {
+ new_size = gst_base_transform_transform_size (trans,
+ GST_PAD_DIRECTION (trans->srcpad), srccaps, GST_BUFFER_SIZE (*buf),
+ sinkcaps);
+ if (new_size == -1) {
+ gst_caps_unref (srccaps);
gst_caps_unref (sinkcaps);
goto unknown_size;
}
- if (*buf) {
- gst_buffer_unref (*buf);
- }
- *buf = gst_buffer_new_and_alloc (got_size);
+ gst_buffer_unref (*buf);
+
+ *buf = gst_buffer_new_and_alloc (new_size);
gst_buffer_set_caps (*buf, sinkcaps);
GST_BUFFER_OFFSET (*buf) = offset;
res = GST_FLOW_OK;
+ gst_caps_unref (srccaps);
gst_caps_unref (sinkcaps);
}
gst_object_unref (trans);
+
return res;
not_configured:
{
/* let the default allocator handle it */
+ GST_DEBUG_OBJECT (trans, "not configured");
if (*buf) {
gst_buffer_unref (*buf);
*buf = NULL;
unknown_size:
{
/* let the default allocator handle it */
+ GST_DEBUG_OBJECT (trans, "unknown size");
if (*buf) {
gst_buffer_unref (*buf);
*buf = NULL;
}
}
-
static gboolean
gst_base_transform_event (GstPad * pad, GstEvent * event)
{
{
GstBaseTransformClass *bclass;
GstFlowReturn ret = GST_FLOW_OK;
+ guint out_size;
bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
if (trans->in_place) {
- if (bclass->transform_ip) {
- /* we do not call make writable here */
+ /* check if we can do inplace and the buffer is writable */
+ if (bclass->transform_ip && gst_buffer_is_writable (inbuf)) {
gst_buffer_ref (inbuf);
/* in place transform and subclass supports method */
*outbuf = inbuf;
} else {
- /* in place transform and subclass does not support method */
+ /* in place transform and subclass does not support method or
+ * buffer is not writable. */
if (bclass->transform) {
- /* make a copy of the buffer */
+ /* make a copy of the buffer. We really need a copy since the
+ * element might not be able to really do inplace. */
*outbuf = inbuf;
inbuf = gst_buffer_copy (inbuf);
}
}
} else {
- /* figure out the output size */
- if (trans->out_size == -1) {
- /* ask subclass */
- trans->out_size = gst_base_transform_get_size (trans,
- GST_PAD_CAPS (trans->srcpad));
- if (trans->out_size == -1)
- /* we have an error */
- goto no_size;
- }
+ /* non inplace case, figure out the output size */
+ out_size = gst_base_transform_transform_size (trans,
+ GST_PAD_DIRECTION (trans->sinkpad), GST_PAD_CAPS (trans->sinkpad),
+ GST_BUFFER_SIZE (inbuf), GST_PAD_CAPS (trans->srcpad));
+ if (out_size == -1)
+ /* we have an error */
+ goto no_size;
/* we cannot reconfigure the element yet as we are still processing
* the old buffer. We will therefore delay the reconfiguration of the
/* no in place transform, get buffer, this might renegotiate. */
ret = gst_pad_alloc_buffer (trans->srcpad,
- GST_BUFFER_OFFSET (inbuf), trans->out_size,
+ GST_BUFFER_OFFSET (inbuf), out_size,
GST_PAD_CAPS (trans->srcpad), outbuf);
trans->delay_configure = FALSE;
else
ret = GST_FLOW_NOT_SUPPORTED;
- if (ret != GST_FLOW_OK)
- ret =
+ /* if we got renegotiated we can configure now */
+ if (trans->pending_configure) {
+ gboolean success;
+
+ success =
gst_base_transform_configure_caps (trans,
GST_PAD_CAPS (trans->sinkpad), GST_PAD_CAPS (trans->srcpad));
+
+ trans->pending_configure = FALSE;
+
+ if (!success)
+ goto configure_failed;
+ }
}
gst_buffer_unref (inbuf);
GST_DEBUG_OBJECT (trans, "could not get buffer from pool");
return ret;
}
+configure_failed:
+ {
+ gst_buffer_unref (inbuf);
+ GST_DEBUG_OBJECT (trans, "could not negotiate");
+ return GST_FLOW_NOT_NEGOTIATED;
+ }
}
+/* FIXME, getrange is broken, need to pull range from the other
+ * end based on the transform_size result.
+ */
static GstFlowReturn
gst_base_transform_getrange (GstPad * pad, guint64 offset,
guint length, GstBuffer ** buffer)
gboolean in_place;
guint out_size;
+
gboolean delay_configure;
+ gboolean pending_configure;
/*< private >*/
gpointer _gst_reserved[GST_PADDING];
};
+/**
+ * GstBaseTransformClass::transform_caps:
+ * @pad: the pad
+ * @caps: the caps
+ *
+ * This method should answer the question "given this pad, and given these
+ * caps, what caps would you allow on the other pad inside your element ?"
+ */
struct _GstBaseTransformClass {
GstElementClass parent_class;
/*< public >*/
/* virtual methods for subclasses */
- /* given caps on one pad, what can I do on the other pad */
+ /* given the (non-)fixed simple caps on the pad in the given direction,
+ * what can I do on the other pad ? */
+ /* FIXME: change to direction */
GstCaps* (*transform_caps) (GstBaseTransform *trans, GstPad *pad,
GstCaps *caps);
+ /* given caps on one pad, how would you fixate caps on the other pad ? */
+ void (*fixate_caps) (GstBaseTransform *trans,
+ GstPadDirection direction, GstCaps *caps,
+ GstCaps *othercaps);
+
+ /* given the size of a buffer in the given direction with the given caps,
+ * calculate the
+ * size of an outgoing buffer with the given outgoing caps; the default
+ * implementation uses get_size and keeps the number of units the same */
+ guint (*transform_size) (GstBaseTransform *trans,
+ GstPadDirection direction,
+ GstCaps *incaps, guint insize,
+ GstCaps *outcaps);
+
+ /* get the byte size of one unit for a given caps, -1 on error.
+ * Always needs to be implemented if the transform is not in-place. */
+ guint (*get_size) (GstBaseTransform *trans, GstCaps *caps);
+
/* notify the subclass of new caps */
gboolean (*set_caps) (GstBaseTransform *trans, GstCaps *incaps,
GstCaps *outcaps);
- /* get the byte size of a given caps, -1 on error */
- guint (*get_size) (GstBaseTransform *trans, GstCaps *caps);
-
/* start and stop processing, ideal for opening/closing the resource */
gboolean (*start) (GstBaseTransform *trans);
gboolean (*stop) (GstBaseTransform *trans);
gboolean (*event) (GstBaseTransform *trans, GstEvent *event);
- /* transform one incoming buffer to one outgoing buffer */
+ /* transform one incoming buffer to one outgoing buffer.
+ * Always needs to be implemented. */
GstFlowReturn (*transform) (GstBaseTransform *trans, GstBuffer *inbuf,
GstBuffer *outbuf);
* 2000 Wim Taymans <wtay@chello.be>
* 2005 Wim Taymans <wim@fluendo.com>
*
- * gstbasetransform.c:
+ * gstbasetransform.c:
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
gst_element_add_pad (GST_ELEMENT (trans), trans->srcpad);
trans->passthrough = FALSE;
+ trans->delay_configure = FALSE;
+ trans->pending_configure = FALSE;
trans->out_size = -1;
}
gst_caps_append (ret, temp);
} else {
- /* we send caps with just one structure to the transform
+ /* we send caps with just one structure to the transform
* function as this is easier for the element */
for (i = 0; i < gst_caps_get_size (caps); i++) {
GstCaps *nth;
return ret;
}
+/* by default, this keeps the number of samples in the buffer the same */
+guint
+gst_base_transform_transform_size (GstBaseTransform * trans,
+ GstPadDirection direction, GstCaps * incaps,
+ guint insize, GstCaps * outcaps)
+{
+ guint inunitsize, outunitsize, units;
+ GstBaseTransformClass *klass;
+ gint ret = -1;
+
+ klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
+
+ GST_DEBUG_OBJECT (trans, "asked to transform size %d for caps %"
+ GST_PTR_FORMAT " to size for caps %" GST_PTR_FORMAT " in direction %d",
+ insize, incaps, outcaps, direction);
+
+ /* if there is a custom transform function, use this */
+ if (klass->transform_size) {
+ ret = klass->transform_size (trans, direction, incaps, insize, outcaps);
+ } else {
+ inunitsize = gst_base_transform_get_size (trans, incaps);
+ g_return_val_if_fail (inunitsize != -1, -1);
+ g_return_val_if_fail (insize % inunitsize == 0, -1);
+
+ units = insize / inunitsize;
+ outunitsize = gst_base_transform_get_size (trans, outcaps);
+ ret = units * outunitsize;
+ }
+
+ GST_DEBUG_OBJECT (trans, "transformed size %d", ret);
+
+ return ret;
+}
+
static GstCaps *
gst_base_transform_getcaps (GstPad * pad)
{
klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
- /* no configure the element with the caps */
+ /* now configure the element with the caps */
if (klass->set_caps) {
ret = klass->set_caps (trans, in, out);
}
GstPad *otherpad, *otherpeer;
GstCaps *othercaps = NULL;
gboolean ret = TRUE;
+ gboolean peer_checked = FALSE;
+
+ /* caps must be fixed here */
+ g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE);
trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
klass = GST_BASE_TRANSFORM_GET_CLASS (trans);
otherpad = (pad == trans->srcpad) ? trans->sinkpad : trans->srcpad;
otherpeer = gst_pad_get_peer (otherpad);
- if (GST_PAD_IS_IN_SETCAPS (otherpad)) {
- ret = TRUE;
+ /* if we get called recursively, we bail out now to avoid an
+ * infinite loop. */
+ if (GST_PAD_IS_IN_SETCAPS (otherpad))
goto done;
- }
- /* see how we can transform the input caps */
+ /* see how we can transform the input caps. */
othercaps = gst_base_transform_transform_caps (trans, pad, caps);
/* check if transform is empty */
if (!othercaps || gst_caps_is_empty (othercaps))
goto no_transform;
+ /* if the othercaps are not fixed, we need to fixate them, first attempt
+ * is by attempting passthrough if the othercaps are a superset of caps. */
if (!gst_caps_is_fixed (othercaps)) {
GstCaps *temp;
GST_DEBUG_OBJECT (trans,
"transform returned non fixed %" GST_PTR_FORMAT, othercaps);
+ /* see if the target caps are a superset of the source caps, in this
+ * case we can try to perform passthrough */
temp = gst_caps_intersect (othercaps, caps);
GST_DEBUG_OBJECT (trans, "intersect returned %" GST_PTR_FORMAT, temp);
if (temp) {
/* try passthrough. we know it's fixed, because caps is fixed */
if (gst_pad_accept_caps (otherpeer, caps)) {
GST_DEBUG_OBJECT (trans, "peer accepted %" GST_PTR_FORMAT, caps);
+ /* peer accepted unmodified caps, we free the original non-fixed
+ * caps and work with the passthrough caps */
gst_caps_unref (othercaps);
othercaps = gst_caps_ref (caps);
- /* will fall though. calls accept_caps again, should fix that. */
+ /* mark that we checked othercaps with the peer, this
+ * makes sure we don't call accept_caps again with these same
+ * caps */
+ peer_checked = TRUE;
} else {
GST_DEBUG_OBJECT (trans,
"peer did not accept %" GST_PTR_FORMAT, caps);
}
}
+ /* second attempt at fixation is done by intersecting with
+ * the peer caps */
if (!gst_caps_is_fixed (othercaps) && otherpeer) {
/* intersect against what the peer can do */
GstCaps *peercaps;
gst_caps_unref (peercaps);
gst_caps_unref (othercaps);
othercaps = intersect;
+ peer_checked = FALSE;
GST_DEBUG_OBJECT (trans,
"filtering against peer yields %" GST_PTR_FORMAT, othercaps);
if (gst_caps_is_empty (othercaps))
goto no_transform_possible;
+ /* third attempt at fixation, call the fixate vmethod and
+ * ultimately call the pad fixate function. */
if (!gst_caps_is_fixed (othercaps)) {
GstCaps *temp;
GST_DEBUG_OBJECT (trans,
- "othercaps now, trying to fixate %" GST_PTR_FORMAT, othercaps);
+ "trying to fixate %" GST_PTR_FORMAT " on pad %s:%s",
+ othercaps, GST_DEBUG_PAD_NAME (otherpad));
- /* take first possibility and fixate if necessary */
+ /* since we have no other way to fixate left, we might as well just take
+ * the first of the caps list and fixate that */
+
+ /* FIXME: when fixating using the vmethod, it might make sense to fixate
+ * each of the caps; but Wim doesn't see a use case for that yet */
temp = gst_caps_copy_nth (othercaps, 0);
gst_caps_unref (othercaps);
othercaps = temp;
- gst_pad_fixate_caps (otherpad, othercaps);
-
+ peer_checked = FALSE;
+
+ if (klass->fixate_caps) {
+ GST_DEBUG_OBJECT (trans, "trying to fixate %" GST_PTR_FORMAT
+ " using caps %" GST_PTR_FORMAT
+ " on pad %s:%s using fixate_caps vmethod", othercaps, caps,
+ GST_DEBUG_PAD_NAME (otherpad));
+ klass->fixate_caps (trans, GST_PAD_DIRECTION (pad), caps, othercaps);
+ }
+ /* if still not fixed, no other option but to let the default pad fixate
+ * function do its job */
+ if (!gst_caps_is_fixed (othercaps)) {
+ GST_DEBUG_OBJECT (trans, "trying to fixate %" GST_PTR_FORMAT
+ " on pad %s:%s using gst_pad_fixate_caps", othercaps,
+ GST_DEBUG_PAD_NAME (otherpad));
+ gst_pad_fixate_caps (otherpad, othercaps);
+ }
GST_DEBUG_OBJECT (trans, "after fixating %" GST_PTR_FORMAT, othercaps);
}
- /* caps should be fixed now */
+ /* caps should be fixed now, if not we have to fail. */
if (!gst_caps_is_fixed (othercaps))
goto could_not_fixate;
- /* and peer should accept */
- if (otherpeer && !gst_pad_accept_caps (otherpeer, othercaps))
+ /* and peer should accept, don't check again if we already checked the
+ * othercaps against the peer. */
+ if (!peer_checked && otherpeer && !gst_pad_accept_caps (otherpeer, othercaps))
goto peer_no_accept;
GST_DEBUG_OBJECT (trans, "got final caps %" GST_PTR_FORMAT, othercaps);
- /* we know this will work, we implement the setcaps */
- gst_pad_set_caps (otherpad, othercaps);
-
trans->in_place = gst_caps_is_equal (caps, othercaps);
GST_DEBUG_OBJECT (trans, "in_place: %d", trans->in_place);
/* see if we have to configure the element now */
if (!trans->delay_configure) {
- if (pad == trans->sinkpad)
- ret = gst_base_transform_configure_caps (trans, caps, othercaps);
- else
- ret = gst_base_transform_configure_caps (trans, othercaps, caps);
+ GstCaps *incaps, *outcaps;
+
+ /* make sure in and out caps are correct */
+ if (pad == trans->sinkpad) {
+ incaps = caps;
+ outcaps = othercaps;
+ } else {
+ incaps = othercaps;
+ outcaps = caps;
+ }
+ /* call configure now */
+ if (!(ret = gst_base_transform_configure_caps (trans, incaps, outcaps)))
+ goto failed_configure;
+ } else {
+ /* set pending configure, the configure will happen later with the
+ * caps we set on the pads above. */
+ trans->pending_configure = TRUE;
}
+ /* we know this will work, we implement the setcaps */
+ gst_pad_set_caps (otherpad, othercaps);
+
done:
if (otherpeer)
gst_object_unref (otherpeer);
ret = FALSE;
goto done;
}
+failed_configure:
+ {
+ GST_DEBUG_OBJECT (trans, "FAILED to configure caps %" GST_PTR_FORMAT
+ " to accept %" GST_PTR_FORMAT, otherpad, othercaps);
+ goto done;
+ }
}
static guint
bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
if (bclass->get_size) {
res = bclass->get_size (trans, caps);
- GST_DEBUG_OBJECT (trans, "get size(%p) returned %d", caps, res);
+ GST_DEBUG_OBJECT (trans, "get size(%" GST_PTR_FORMAT ") returned %d", caps,
+ res);
}
return res;
}
+/* your upstream peer wants to send you a buffer
+ * that buffer has the given offset, size and caps
+ * you're requested to allocate a buffer
+ */
static GstFlowReturn
gst_base_transform_buffer_alloc (GstPad * pad, guint64 offset, guint size,
GstCaps * caps, GstBuffer ** buf)
{
GstBaseTransform *trans;
GstFlowReturn res;
- guint got_size;
+ guint new_size;
trans = GST_BASE_TRANSFORM (gst_pad_get_parent (pad));
*buf = NULL;
+ GST_DEBUG_OBJECT (trans, "allocating a buffer of size %d at offset %"
+ G_GUINT64_FORMAT, size, offset);
+
+ /* before any buffers are pushed, in_place is TRUE; allocating can trigger
+ * a renegotiation and change that to FALSE */
if (trans->in_place) {
/* request a buffer with the same caps */
+ GST_DEBUG_OBJECT (trans, "requesting buffer with same caps, size %d", size);
res = gst_pad_alloc_buffer (trans->srcpad, offset, size, caps, buf);
} else {
/* if we are configured, request a buffer with the src caps */
if (!srccaps)
goto not_configured;
- got_size = gst_base_transform_get_size (trans, srccaps);
- if (got_size == -1) {
+ GST_DEBUG_OBJECT (trans, "calling transform_size");
+ new_size = gst_base_transform_transform_size (trans,
+ GST_PAD_DIRECTION (pad), caps, size, srccaps);
+ if (new_size == -1) {
gst_caps_unref (srccaps);
goto unknown_size;
}
- res = gst_pad_alloc_buffer (trans->srcpad, offset, got_size, srccaps, buf);
+ res = gst_pad_alloc_buffer (trans->srcpad, offset, new_size, srccaps, buf);
gst_caps_unref (srccaps);
}
buffer. fall through and allocate a buffer corresponding to our sink
caps, if any */
GstCaps *sinkcaps = gst_pad_get_negotiated_caps (trans->sinkpad);
+ GstCaps *srccaps = gst_pad_get_negotiated_caps (trans->srcpad);
if (!sinkcaps)
goto not_configured;
- got_size = gst_base_transform_get_size (trans, sinkcaps);
- if (got_size == -1) {
+ new_size = gst_base_transform_transform_size (trans,
+ GST_PAD_DIRECTION (trans->srcpad), srccaps, GST_BUFFER_SIZE (*buf),
+ sinkcaps);
+ if (new_size == -1) {
+ gst_caps_unref (srccaps);
gst_caps_unref (sinkcaps);
goto unknown_size;
}
- if (*buf) {
- gst_buffer_unref (*buf);
- }
- *buf = gst_buffer_new_and_alloc (got_size);
+ gst_buffer_unref (*buf);
+
+ *buf = gst_buffer_new_and_alloc (new_size);
gst_buffer_set_caps (*buf, sinkcaps);
GST_BUFFER_OFFSET (*buf) = offset;
res = GST_FLOW_OK;
+ gst_caps_unref (srccaps);
gst_caps_unref (sinkcaps);
}
gst_object_unref (trans);
+
return res;
not_configured:
{
/* let the default allocator handle it */
+ GST_DEBUG_OBJECT (trans, "not configured");
if (*buf) {
gst_buffer_unref (*buf);
*buf = NULL;
unknown_size:
{
/* let the default allocator handle it */
+ GST_DEBUG_OBJECT (trans, "unknown size");
if (*buf) {
gst_buffer_unref (*buf);
*buf = NULL;
}
}
-
static gboolean
gst_base_transform_event (GstPad * pad, GstEvent * event)
{
{
GstBaseTransformClass *bclass;
GstFlowReturn ret = GST_FLOW_OK;
+ guint out_size;
bclass = GST_BASE_TRANSFORM_GET_CLASS (trans);
if (trans->in_place) {
- if (bclass->transform_ip) {
- /* we do not call make writable here */
+ /* check if we can do inplace and the buffer is writable */
+ if (bclass->transform_ip && gst_buffer_is_writable (inbuf)) {
gst_buffer_ref (inbuf);
/* in place transform and subclass supports method */
*outbuf = inbuf;
} else {
- /* in place transform and subclass does not support method */
+ /* in place transform and subclass does not support method or
+ * buffer is not writable. */
if (bclass->transform) {
- /* make a copy of the buffer */
+ /* make a copy of the buffer. We really need a copy since the
+ * element might not be able to really do inplace. */
*outbuf = inbuf;
inbuf = gst_buffer_copy (inbuf);
}
}
} else {
- /* figure out the output size */
- if (trans->out_size == -1) {
- /* ask subclass */
- trans->out_size = gst_base_transform_get_size (trans,
- GST_PAD_CAPS (trans->srcpad));
- if (trans->out_size == -1)
- /* we have an error */
- goto no_size;
- }
+ /* non inplace case, figure out the output size */
+ out_size = gst_base_transform_transform_size (trans,
+ GST_PAD_DIRECTION (trans->sinkpad), GST_PAD_CAPS (trans->sinkpad),
+ GST_BUFFER_SIZE (inbuf), GST_PAD_CAPS (trans->srcpad));
+ if (out_size == -1)
+ /* we have an error */
+ goto no_size;
/* we cannot reconfigure the element yet as we are still processing
* the old buffer. We will therefore delay the reconfiguration of the
/* no in place transform, get buffer, this might renegotiate. */
ret = gst_pad_alloc_buffer (trans->srcpad,
- GST_BUFFER_OFFSET (inbuf), trans->out_size,
+ GST_BUFFER_OFFSET (inbuf), out_size,
GST_PAD_CAPS (trans->srcpad), outbuf);
trans->delay_configure = FALSE;
else
ret = GST_FLOW_NOT_SUPPORTED;
- if (ret != GST_FLOW_OK)
- ret =
+ /* if we got renegotiated we can configure now */
+ if (trans->pending_configure) {
+ gboolean success;
+
+ success =
gst_base_transform_configure_caps (trans,
GST_PAD_CAPS (trans->sinkpad), GST_PAD_CAPS (trans->srcpad));
+
+ trans->pending_configure = FALSE;
+
+ if (!success)
+ goto configure_failed;
+ }
}
gst_buffer_unref (inbuf);
GST_DEBUG_OBJECT (trans, "could not get buffer from pool");
return ret;
}
+configure_failed:
+ {
+ gst_buffer_unref (inbuf);
+ GST_DEBUG_OBJECT (trans, "could not negotiate");
+ return GST_FLOW_NOT_NEGOTIATED;
+ }
}
+/* FIXME, getrange is broken, need to pull range from the other
+ * end based on the transform_size result.
+ */
static GstFlowReturn
gst_base_transform_getrange (GstPad * pad, guint64 offset,
guint length, GstBuffer ** buffer)
gboolean in_place;
guint out_size;
+
gboolean delay_configure;
+ gboolean pending_configure;
/*< private >*/
gpointer _gst_reserved[GST_PADDING];
};
+/**
+ * GstBaseTransformClass::transform_caps:
+ * @pad: the pad
+ * @caps: the caps
+ *
+ * This method should answer the question "given this pad, and given these
+ * caps, what caps would you allow on the other pad inside your element ?"
+ */
struct _GstBaseTransformClass {
GstElementClass parent_class;
/*< public >*/
/* virtual methods for subclasses */
- /* given caps on one pad, what can I do on the other pad */
+ /* given the (non-)fixed simple caps on the pad in the given direction,
+ * what can I do on the other pad ? */
+ /* FIXME: change to direction */
GstCaps* (*transform_caps) (GstBaseTransform *trans, GstPad *pad,
GstCaps *caps);
+ /* given caps on one pad, how would you fixate caps on the other pad ? */
+ void (*fixate_caps) (GstBaseTransform *trans,
+ GstPadDirection direction, GstCaps *caps,
+ GstCaps *othercaps);
+
+ /* given the size of a buffer in the given direction with the given caps,
+ * calculate the
+ * size of an outgoing buffer with the given outgoing caps; the default
+ * implementation uses get_size and keeps the number of units the same */
+ guint (*transform_size) (GstBaseTransform *trans,
+ GstPadDirection direction,
+ GstCaps *incaps, guint insize,
+ GstCaps *outcaps);
+
+ /* get the byte size of one unit for a given caps, -1 on error.
+ * Always needs to be implemented if the transform is not in-place. */
+ guint (*get_size) (GstBaseTransform *trans, GstCaps *caps);
+
/* notify the subclass of new caps */
gboolean (*set_caps) (GstBaseTransform *trans, GstCaps *incaps,
GstCaps *outcaps);
- /* get the byte size of a given caps, -1 on error */
- guint (*get_size) (GstBaseTransform *trans, GstCaps *caps);
-
/* start and stop processing, ideal for opening/closing the resource */
gboolean (*start) (GstBaseTransform *trans);
gboolean (*stop) (GstBaseTransform *trans);
gboolean (*event) (GstBaseTransform *trans, GstEvent *event);
- /* transform one incoming buffer to one outgoing buffer */
+ /* transform one incoming buffer to one outgoing buffer.
+ * Always needs to be implemented. */
GstFlowReturn (*transform) (GstBaseTransform *trans, GstBuffer *inbuf,
GstBuffer *outbuf);