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 "gstvdpvideobuffer.h"
22 #include "gstvdpvideobufferpool.h"
23 #include "gstvdputils.h"
25 #include "gstvdpvideosrcpad.h"
27 GST_DEBUG_CATEGORY_STATIC (gst_vdp_video_src_pad_debug);
28 #define GST_CAT_DEFAULT gst_vdp_video_src_pad_debug
36 struct _GstVdpVideoSrcPad
40 GstVdpBufferPool *bpool;
51 struct _GstVdpVideoSrcPadClass
53 GstPadClass pad_class;
56 #define DEBUG_INIT(bla) \
57 GST_DEBUG_CATEGORY_INIT (gst_vdp_video_src_pad_debug, "vdpvideosrcpad", 0, "GstVdpVideoSrcPad");
59 G_DEFINE_TYPE_WITH_CODE (GstVdpVideoSrcPad, gst_vdp_video_src_pad, GST_TYPE_PAD,
63 gst_vdp_video_src_pad_new (GstPadTemplate * templ, const gchar * name)
65 g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
66 g_return_val_if_fail ((templ->direction == GST_PAD_SRC), NULL);
68 return g_object_new (GST_TYPE_VDP_VIDEO_SRC_PAD,
69 "name", name, "direction", templ->direction, "template", templ, NULL);
73 gst_vdp_video_src_pad_push (GstVdpVideoSrcPad * vdp_pad,
74 GstVdpVideoBuffer * video_buf)
79 g_return_val_if_fail (GST_IS_VDP_VIDEO_SRC_PAD (vdp_pad), GST_FLOW_ERROR);
80 g_return_val_if_fail (GST_IS_VDP_VIDEO_BUFFER (video_buf), GST_FLOW_ERROR);
82 pad = (GstPad *) vdp_pad;
84 if (G_UNLIKELY (!GST_PAD_CAPS (pad)))
85 return GST_FLOW_NOT_NEGOTIATED;
87 if (vdp_pad->yuv_output) {
92 if (!gst_vdp_video_buffer_calculate_size (vdp_pad->fourcc, vdp_pad->width,
93 vdp_pad->height, &size)) {
94 GST_ERROR_OBJECT (vdp_pad, "Couldn't calculate buffer size for caps");
95 gst_buffer_unref (GST_BUFFER_CAST (video_buf));
96 return GST_FLOW_ERROR;
99 caps = GST_PAD_CAPS (pad);
100 ret = gst_pad_alloc_buffer (pad,
101 GST_BUFFER_OFFSET_NONE, size, caps, &out_buf);
102 if (ret != GST_FLOW_OK) {
103 gst_buffer_unref (GST_BUFFER_CAST (video_buf));
107 if (!gst_caps_is_equal_fixed (caps, GST_BUFFER_CAPS (out_buf))) {
108 GST_ERROR_OBJECT (vdp_pad,
109 "Sink element allocated buffer with different caps");
110 gst_buffer_unref (GST_BUFFER_CAST (video_buf));
111 gst_buffer_unref (out_buf);
112 return GST_FLOW_ERROR;
115 if (!gst_vdp_video_buffer_download (video_buf, out_buf, vdp_pad->fourcc,
116 vdp_pad->width, vdp_pad->height)) {
117 GST_ERROR_OBJECT (vdp_pad,
118 "Couldn't convert from GstVdpVideoBuffer to the requested format");
119 gst_buffer_unref (GST_BUFFER_CAST (video_buf));
120 gst_buffer_unref (out_buf);
121 return GST_FLOW_ERROR;
124 gst_buffer_copy_metadata (out_buf, (const GstBuffer *) video_buf,
125 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS);
126 gst_buffer_unref (GST_BUFFER_CAST (video_buf));
128 out_buf = GST_BUFFER_CAST (video_buf);
130 /* FIXME: can't use gst_buffer_set_caps since we may have additional
131 * references to the bufffer. We can't either use
132 * gst_buffer_make_metadata_writable since that creates a regular buffer and
133 * not a GstVdpVideoBuffer */
134 gst_caps_replace (&(GST_BUFFER_CAPS (out_buf)), GST_PAD_CAPS (vdp_pad));
136 return gst_pad_push (pad, out_buf);
140 gst_vdp_video_src_pad_alloc_buffer (GstVdpVideoSrcPad * vdp_pad,
141 GstVdpVideoBuffer ** video_buf, GError ** error)
145 g_return_val_if_fail (GST_IS_VDP_VIDEO_SRC_PAD (vdp_pad), GST_FLOW_ERROR);
147 caps = GST_PAD_CAPS (vdp_pad);
149 return GST_FLOW_NOT_NEGOTIATED;
152 (GstVdpVideoBuffer *) gst_vdp_buffer_pool_get_buffer (vdp_pad->bpool,
155 return GST_FLOW_ERROR;
161 gst_vdp_video_src_pad_setcaps (GstPad * pad, GstCaps * caps)
163 GstVdpVideoSrcPad *vdp_pad = GST_VDP_VIDEO_SRC_PAD (pad);
164 const GstStructure *structure;
168 structure = gst_caps_get_structure (caps, 0);
169 if (gst_structure_has_name (structure, "video/x-raw-yuv")) {
170 if (!gst_structure_get_int (structure, "width", &vdp_pad->width))
172 if (!gst_structure_get_int (structure, "height", &vdp_pad->height))
174 if (!gst_structure_get_fourcc (structure, "format", &vdp_pad->fourcc))
177 video_caps = gst_vdp_yuv_to_video_caps (caps);
178 vdp_pad->yuv_output = TRUE;
179 } else if (gst_structure_has_name (structure, "video/x-vdpau-video")) {
180 if (!gst_structure_get_int (structure, "width", &vdp_pad->width))
182 if (!gst_structure_get_int (structure, "height", &vdp_pad->height))
185 video_caps = gst_caps_ref (caps);
186 vdp_pad->yuv_output = FALSE;
190 gst_vdp_buffer_pool_set_caps (vdp_pad->bpool, video_caps);
191 gst_caps_unref (video_caps);
197 gst_vdp_video_src_pad_getcaps (GstPad * pad)
199 GstVdpVideoSrcPad *vdp_pad = (GstVdpVideoSrcPad *) pad;
201 const GstCaps *templ_caps;
204 return gst_caps_ref (vdp_pad->caps);
206 else if ((templ_caps = gst_pad_get_pad_template_caps (pad)))
207 return gst_caps_copy (templ_caps);
213 gst_vdp_video_src_pad_activate_push (GstPad * pad, gboolean active)
215 GstVdpVideoSrcPad *vdp_pad = (GstVdpVideoSrcPad *) pad;
219 gst_caps_unref (vdp_pad->caps);
220 vdp_pad->caps = NULL;
223 gst_object_unref (vdp_pad->device);
224 vdp_pad->device = NULL;
231 gst_vdp_video_src_pad_set_device (GstVdpVideoSrcPad * vdp_pad,
232 GstVdpDevice * device)
235 const GstCaps *templ_caps;
238 g_object_unref (vdp_pad->bpool);
240 g_object_unref (vdp_pad->device);
242 vdp_pad->device = device;
243 vdp_pad->bpool = gst_vdp_video_buffer_pool_new (device);
247 gst_caps_unref (vdp_pad->caps);
249 caps = gst_vdp_video_buffer_get_allowed_caps (device);
251 if ((templ_caps = gst_pad_get_pad_template_caps (GST_PAD (vdp_pad)))) {
252 vdp_pad->caps = gst_caps_intersect (caps, templ_caps);
253 gst_caps_unref (caps);
255 vdp_pad->caps = caps;
259 gst_vdp_video_src_pad_get_property (GObject * object, guint prop_id,
260 GValue * value, GParamSpec * pspec)
262 GstVdpVideoSrcPad *vdp_pad = (GstVdpVideoSrcPad *) object;
266 g_value_set_object (value, vdp_pad->device);
270 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
276 gst_vdp_video_src_pad_set_property (GObject * object, guint prop_id,
277 const GValue * value, GParamSpec * pspec)
279 GstVdpVideoSrcPad *vdp_pad = (GstVdpVideoSrcPad *) object;
283 gst_vdp_video_src_pad_set_device (vdp_pad, g_value_dup_object (value));
287 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
293 gst_vdp_video_src_pad_init (GstVdpVideoSrcPad * vdp_pad)
295 GstPad *pad = GST_PAD (vdp_pad);
297 vdp_pad->device = NULL;
298 vdp_pad->caps = NULL;
300 gst_pad_set_getcaps_function (pad,
301 GST_DEBUG_FUNCPTR (gst_vdp_video_src_pad_getcaps));
302 gst_pad_set_setcaps_function (pad,
303 GST_DEBUG_FUNCPTR (gst_vdp_video_src_pad_setcaps));
304 gst_pad_set_activatepush_function (pad,
305 GST_DEBUG_FUNCPTR (gst_vdp_video_src_pad_activate_push));
309 gst_vdp_video_src_pad_class_init (GstVdpVideoSrcPadClass * klass)
311 GObjectClass *object_class = G_OBJECT_CLASS (klass);
313 object_class->get_property = gst_vdp_video_src_pad_get_property;
314 object_class->set_property = gst_vdp_video_src_pad_set_property;
317 * GstVdpVideoSrcPad:device:
319 * The #GstVdpDevice this pool is bound to.
321 g_object_class_install_property
324 g_param_spec_object ("device",
326 "The GstVdpDevice the pad should use",
327 GST_TYPE_VDP_DEVICE, G_PARAM_READWRITE));