3 * Copyright (C) 2009 Carl-Anton Ingmarsson <ca.ingmarsson@gmail.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
21 #include "gstvdputils.h"
22 #include "gstvdpvideobuffer.h"
23 #include "gstvdpoutputbufferpool.h"
25 #include "gstvdpoutputsrcpad.h"
27 GST_DEBUG_CATEGORY_STATIC (gst_vdp_output_src_pad_debug);
28 #define GST_CAT_DEFAULT gst_vdp_output_src_pad_debug
36 typedef enum _GstVdpOutputSrcPadFormat GstVdpOutputSrcPadFormat;
38 enum _GstVdpOutputSrcPadFormat
40 GST_VDP_OUTPUT_SRC_PAD_FORMAT_RGB,
41 GST_VDP_OUTPUT_SRC_PAD_FORMAT_VDPAU
44 struct _GstVdpOutputSrcPad
51 GstVdpOutputSrcPadFormat output_format;
52 VdpRGBAFormat rgba_format;
55 GstVdpBufferPool *bpool;
62 struct _GstVdpOutputSrcPadClass
64 GstPadClass pad_class;
67 #define DEBUG_INIT(bla) \
68 GST_DEBUG_CATEGORY_INIT (gst_vdp_output_src_pad_debug, "vdpoutputsrcpad", 0, "GstVdpOutputSrcPad");
70 G_DEFINE_TYPE_WITH_CODE (GstVdpOutputSrcPad, gst_vdp_output_src_pad,
71 GST_TYPE_PAD, DEBUG_INIT ());
74 gst_vdp_output_src_pad_push (GstVdpOutputSrcPad * vdp_pad,
75 GstVdpOutputBuffer * output_buf, GError ** error)
80 g_return_val_if_fail (GST_IS_VDP_OUTPUT_SRC_PAD (vdp_pad), GST_FLOW_ERROR);
81 g_return_val_if_fail (GST_IS_VDP_OUTPUT_BUFFER (output_buf), GST_FLOW_ERROR);
83 pad = (GstPad *) vdp_pad;
85 if (G_UNLIKELY (!GST_PAD_CAPS (pad)))
86 return GST_FLOW_NOT_NEGOTIATED;
88 switch (vdp_pad->output_format) {
89 case GST_VDP_OUTPUT_SRC_PAD_FORMAT_RGB:
94 gst_vdp_output_buffer_calculate_size (output_buf, &size);
96 vdp_pad->lock_caps = TRUE;
97 ret = gst_pad_alloc_buffer (pad, 0, size, GST_PAD_CAPS (vdp_pad),
99 vdp_pad->lock_caps = FALSE;
101 if (ret != GST_FLOW_OK) {
102 gst_buffer_unref (GST_BUFFER_CAST (output_buf));
106 if (!gst_vdp_output_buffer_download (output_buf, outbuf, error)) {
107 gst_buffer_unref (GST_BUFFER_CAST (output_buf));
108 gst_buffer_unref (outbuf);
109 return GST_FLOW_ERROR;
112 gst_buffer_copy_metadata (outbuf, (const GstBuffer *) output_buf,
113 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS);
114 gst_buffer_unref (GST_BUFFER_CAST (output_buf));
117 case GST_VDP_OUTPUT_SRC_PAD_FORMAT_VDPAU:
119 outbuf = GST_BUFFER_CAST (output_buf);
124 g_assert_not_reached ();
128 gst_buffer_set_caps (outbuf, GST_PAD_CAPS (vdp_pad));
130 return gst_pad_push (pad, outbuf);
134 gst_vdp_output_src_pad_create_buffer (GstVdpOutputSrcPad * vdp_pad,
135 GstVdpOutputBuffer ** output_buf, GError ** error)
141 ret = gst_pad_alloc_buffer_and_set_caps (GST_PAD_CAST (vdp_pad),
142 GST_BUFFER_OFFSET_NONE, 0, GST_PAD_CAPS (vdp_pad), &neg_buf);
143 if (ret == GST_FLOW_OK)
144 gst_buffer_unref (neg_buf);
147 (GstVdpOutputBuffer *) gst_vdp_buffer_pool_get_buffer (vdp_pad->bpool,
150 return GST_FLOW_ERROR;
156 gst_vdp_output_src_pad_alloc_with_caps (GstVdpOutputSrcPad * vdp_pad,
157 GstCaps * caps, GstVdpOutputBuffer ** output_buf, GError ** error)
161 ret = gst_pad_alloc_buffer_and_set_caps ((GstPad *) vdp_pad, 0, 0, caps,
162 (GstBuffer **) output_buf);
163 if (ret != GST_FLOW_OK)
166 if (!GST_IS_VDP_OUTPUT_BUFFER (*output_buf))
172 gst_buffer_unref (GST_BUFFER (*output_buf));
173 g_set_error (error, GST_STREAM_ERROR, GST_STREAM_ERROR_FAILED,
174 "Sink element returned buffer of wrong type");
175 return GST_FLOW_ERROR;
179 gst_vdp_output_src_pad_alloc_buffer (GstVdpOutputSrcPad * vdp_pad,
180 GstVdpOutputBuffer ** output_buf, GError ** error)
185 g_return_val_if_fail (GST_IS_VDP_OUTPUT_SRC_PAD (vdp_pad), GST_FLOW_ERROR);
187 caps = GST_PAD_CAPS (vdp_pad);
189 return GST_FLOW_NOT_NEGOTIATED;
191 switch (vdp_pad->output_format) {
192 case GST_VDP_OUTPUT_SRC_PAD_FORMAT_RGB:
194 ret = gst_vdp_output_src_pad_create_buffer (vdp_pad, output_buf, error);
195 if (ret != GST_FLOW_OK)
201 case GST_VDP_OUTPUT_SRC_PAD_FORMAT_VDPAU:
203 ret = gst_vdp_output_src_pad_alloc_with_caps (vdp_pad, caps, output_buf,
205 if (ret != GST_FLOW_OK)
212 g_assert_not_reached ();
221 gst_vdp_output_src_pad_acceptcaps (GstPad * pad, GstCaps * caps)
223 GstVdpOutputSrcPad *vdp_pad = GST_VDP_OUTPUT_SRC_PAD (pad);
225 if (!vdp_pad->lock_caps)
228 return gst_caps_is_equal_fixed (caps, GST_PAD_CAPS (pad));
232 gst_vdp_output_src_pad_setcaps (GstPad * pad, GstCaps * caps)
234 GstVdpOutputSrcPad *vdp_pad = GST_VDP_OUTPUT_SRC_PAD (pad);
235 const GstStructure *structure;
237 structure = gst_caps_get_structure (caps, 0);
239 if (!gst_structure_get_int (structure, "width", &vdp_pad->width))
241 if (!gst_structure_get_int (structure, "height", &vdp_pad->height))
244 if (gst_structure_has_name (structure, "video/x-raw-rgb")) {
245 if (!gst_vdp_caps_to_rgba_format (caps, &vdp_pad->rgba_format))
248 /* create buffer pool if we dont't have one */
250 vdp_pad->bpool = gst_vdp_output_buffer_pool_new (vdp_pad->device);
252 if (vdp_pad->output_caps)
253 gst_caps_unref (vdp_pad->output_caps);
255 vdp_pad->output_caps = gst_caps_new_simple ("video/x-vdpau-output",
256 "rgba-format", G_TYPE_INT, vdp_pad->rgba_format,
257 "width", G_TYPE_INT, vdp_pad->width, "height", G_TYPE_INT,
258 vdp_pad->height, NULL);
259 gst_vdp_buffer_pool_set_caps (vdp_pad->bpool, vdp_pad->output_caps);
261 vdp_pad->output_format = GST_VDP_OUTPUT_SRC_PAD_FORMAT_RGB;
262 } else if (gst_structure_has_name (structure, "video/x-vdpau-output")) {
263 if (!gst_structure_get_int (structure, "rgba-format",
264 (gint *) & vdp_pad->rgba_format))
267 /* don't need the buffer pool */
268 if (vdp_pad->bpool) {
269 gst_object_unref (vdp_pad->bpool);
270 vdp_pad->bpool = NULL;
273 vdp_pad->output_format = GST_VDP_OUTPUT_SRC_PAD_FORMAT_VDPAU;
281 gst_vdp_output_src_pad_getcaps (GstPad * pad)
283 GstVdpOutputSrcPad *vdp_pad = (GstVdpOutputSrcPad *) pad;
285 const GstCaps *templ_caps;
288 return gst_caps_ref (vdp_pad->caps);
290 else if ((templ_caps = gst_pad_get_pad_template_caps (pad)))
291 return gst_caps_copy (templ_caps);
297 gst_vdp_output_src_pad_activate_push (GstPad * pad, gboolean active)
299 GstVdpOutputSrcPad *vdp_pad = GST_VDP_OUTPUT_SRC_PAD (pad);
303 gst_caps_unref (vdp_pad->caps);
304 vdp_pad->caps = NULL;
306 if (vdp_pad->output_caps)
307 gst_caps_unref (vdp_pad->output_caps);
308 vdp_pad->output_caps = NULL;
311 g_object_unref (vdp_pad->bpool);
312 vdp_pad->bpool = NULL;
315 g_object_unref (vdp_pad->device);
316 vdp_pad->device = NULL;
323 gst_vdp_output_src_pad_new (GstPadTemplate * templ, const gchar * name)
325 return g_object_new (GST_TYPE_VDP_OUTPUT_SRC_PAD, "name", name,
326 "template", templ, "direction", GST_PAD_SRC, NULL);
330 gst_vdp_output_src_pad_update_caps (GstVdpOutputSrcPad * vdp_pad)
333 const GstCaps *templ_caps;
336 gst_caps_unref (vdp_pad->caps);
338 caps = gst_vdp_output_buffer_get_allowed_caps (vdp_pad->device);
340 if ((templ_caps = gst_pad_get_pad_template_caps (GST_PAD (vdp_pad)))) {
341 vdp_pad->caps = gst_caps_intersect (caps, templ_caps);
342 gst_caps_unref (caps);
344 vdp_pad->caps = caps;
348 gst_vdp_output_src_pad_get_property (GObject * object, guint prop_id,
349 GValue * value, GParamSpec * pspec)
351 GstVdpOutputSrcPad *vdp_pad = (GstVdpOutputSrcPad *) object;
355 g_value_set_object (value, vdp_pad->device);
359 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
365 gst_vdp_output_src_pad_set_property (GObject * object, guint prop_id,
366 const GValue * value, GParamSpec * pspec)
368 GstVdpOutputSrcPad *vdp_pad = (GstVdpOutputSrcPad *) object;
373 g_object_unref (vdp_pad->device);
374 vdp_pad->device = g_value_dup_object (value);
375 gst_vdp_output_src_pad_update_caps (vdp_pad);
379 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
385 gst_vdp_output_src_pad_init (GstVdpOutputSrcPad * vdp_pad)
387 GstPad *pad = GST_PAD (vdp_pad);
389 vdp_pad->caps = NULL;
390 vdp_pad->output_caps = NULL;
391 vdp_pad->bpool = NULL;
392 vdp_pad->device = NULL;
394 vdp_pad->lock_caps = FALSE;
396 gst_pad_set_getcaps_function (pad,
397 GST_DEBUG_FUNCPTR (gst_vdp_output_src_pad_getcaps));
398 gst_pad_set_setcaps_function (pad,
399 GST_DEBUG_FUNCPTR (gst_vdp_output_src_pad_setcaps));
400 gst_pad_set_acceptcaps_function (pad,
401 GST_DEBUG_FUNCPTR (gst_vdp_output_src_pad_acceptcaps));
403 gst_pad_set_activatepush_function (pad,
404 GST_DEBUG_FUNCPTR (gst_vdp_output_src_pad_activate_push));
408 gst_vdp_output_src_pad_class_init (GstVdpOutputSrcPadClass * klass)
410 GObjectClass *object_class = G_OBJECT_CLASS (klass);
412 object_class->get_property = gst_vdp_output_src_pad_get_property;
413 object_class->set_property = gst_vdp_output_src_pad_set_property;
416 * GstVdpVideoSrcPad:device:
418 * The #GstVdpDevice this pool is bound to.
420 g_object_class_install_property
423 g_param_spec_object ("device",
425 "The GstVdpDevice the pad should use",
426 GST_TYPE_VDP_DEVICE, G_PARAM_READWRITE));