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"
23
#include "gstvdpvideosrcpad.h"
25
GST_DEBUG_CATEGORY_STATIC (gst_vdp_video_src_pad_debug);
26
#define GST_CAT_DEFAULT gst_vdp_video_src_pad_debug
35
#define DEBUG_INIT(bla) \
36
GST_DEBUG_CATEGORY_INIT (gst_vdp_video_src_pad_debug, "vdpvideosrcpad", 0, "GstVdpVideoSrcPad");
38
G_DEFINE_TYPE_WITH_CODE (GstVdpVideoSrcPad, gst_vdp_video_src_pad, GST_TYPE_PAD,
42
gst_vdp_video_src_pad_push (GstVdpVideoSrcPad * vdp_pad,
43
GstVdpVideoBuffer * video_buf)
48
g_return_val_if_fail (GST_IS_VDP_VIDEO_SRC_PAD (vdp_pad), GST_FLOW_ERROR);
49
g_return_val_if_fail (GST_IS_VDP_VIDEO_BUFFER (video_buf), GST_FLOW_ERROR);
51
pad = (GstPad *) vdp_pad;
53
if (G_UNLIKELY (!GST_PAD_CAPS (pad)))
54
return GST_FLOW_NOT_NEGOTIATED;
56
if (vdp_pad->yuv_output) {
61
if (!gst_vdp_video_buffer_calculate_size (vdp_pad->fourcc, vdp_pad->width,
62
vdp_pad->height, &size)) {
63
GST_ERROR_OBJECT (vdp_pad, "Couldn't calculate buffer size for caps");
64
gst_buffer_unref (GST_BUFFER_CAST (video_buf));
65
return GST_FLOW_ERROR;
68
caps = GST_PAD_CAPS (pad);
69
ret = gst_pad_alloc_buffer (pad,
70
GST_BUFFER_OFFSET_NONE, size, caps, &out_buf);
71
if (ret != GST_FLOW_OK) {
72
gst_buffer_unref (GST_BUFFER_CAST (video_buf));
76
if (!gst_caps_is_equal_fixed (caps, GST_BUFFER_CAPS (out_buf))) {
77
GST_ERROR_OBJECT (vdp_pad,
78
"Sink element allocated buffer with different caps");
79
gst_buffer_unref (GST_BUFFER_CAST (video_buf));
80
gst_buffer_unref (out_buf);
81
return GST_FLOW_ERROR;
84
if (!gst_vdp_video_buffer_download (video_buf, out_buf, vdp_pad->fourcc,
85
vdp_pad->width, vdp_pad->height)) {
86
GST_ERROR_OBJECT (vdp_pad,
87
"Couldn't convert from GstVdpVideoBuffer to the requested format");
88
gst_buffer_unref (GST_BUFFER_CAST (video_buf));
89
gst_buffer_unref (out_buf);
90
return GST_FLOW_ERROR;
93
gst_buffer_copy_metadata (out_buf, (const GstBuffer *) video_buf,
94
GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS);
95
gst_buffer_unref (GST_BUFFER_CAST (video_buf));
97
out_buf = GST_BUFFER (video_buf);
99
gst_buffer_set_caps (out_buf, GST_PAD_CAPS (vdp_pad));
101
return gst_pad_push (pad, out_buf);
105
gst_vdp_video_src_pad_update_caps (GstVdpVideoSrcPad * vdp_pad)
107
GstCaps *yuv_caps, *video_caps;
109
video_caps = gst_vdp_video_buffer_get_allowed_video_caps (vdp_pad->device);
110
yuv_caps = gst_vdp_video_buffer_get_allowed_yuv_caps (vdp_pad->device);
111
gst_caps_append (video_caps, yuv_caps);
115
gst_caps_unref (vdp_pad->caps);
117
if (vdp_pad->templ_caps) {
118
vdp_pad->caps = gst_caps_intersect (video_caps, vdp_pad->templ_caps);
119
gst_caps_unref (video_caps);
121
vdp_pad->caps = video_caps;
125
gst_vdp_video_src_pad_alloc_buffer (GstVdpVideoSrcPad * vdp_pad,
126
GstVdpVideoBuffer ** video_buf)
131
g_return_val_if_fail (GST_IS_VDP_VIDEO_SRC_PAD (vdp_pad), GST_FLOW_ERROR);
133
caps = GST_PAD_CAPS (vdp_pad);
135
return GST_FLOW_NOT_NEGOTIATED;
137
if (vdp_pad->yuv_output) {
138
GstVdpDevice *device;
140
if (G_UNLIKELY (!vdp_pad->device)) {
141
vdp_pad->device = gst_vdp_get_device (vdp_pad->display);
142
if (G_UNLIKELY (!vdp_pad->device))
145
gst_vdp_video_src_pad_update_caps (vdp_pad);
147
device = vdp_pad->device;
149
*video_buf = gst_vdp_video_buffer_new (device, VDP_CHROMA_TYPE_420,
150
vdp_pad->width, vdp_pad->height);
152
goto video_buffer_error;
154
ret = gst_pad_alloc_buffer ((GstPad *) vdp_pad, 0, 0, caps,
155
(GstBuffer **) video_buf);
156
if (ret != GST_FLOW_OK)
159
if (!gst_caps_is_equal_fixed (caps, GST_BUFFER_CAPS (*video_buf)))
162
if (G_UNLIKELY (!vdp_pad->device)) {
164
g_object_ref (GST_VDP_VIDEO_BUFFER (*video_buf)->device);
166
gst_vdp_video_src_pad_update_caps (vdp_pad);
173
GST_ERROR_OBJECT (vdp_pad, "Couldn't create GstVdpDevice");
174
return GST_FLOW_ERROR;
177
GST_ERROR_OBJECT (vdp_pad, "Couldn't create GstVdpVideoBuffer");
178
return GST_FLOW_ERROR;
181
GST_ERROR_OBJECT (vdp_pad, "Sink element returned buffer with wrong caps");
182
gst_buffer_unref (GST_BUFFER_CAST (*video_buf));
183
return GST_FLOW_ERROR;
187
gst_vdp_video_src_pad_set_caps (GstVdpVideoSrcPad * vdp_pad, GstCaps * caps)
189
const GstStructure *structure;
191
g_return_val_if_fail (GST_IS_VDP_VIDEO_SRC_PAD (vdp_pad), FALSE);
193
if (G_UNLIKELY (!caps))
194
return gst_pad_set_caps (GST_PAD (vdp_pad), caps);
196
if (G_UNLIKELY (!GST_IS_CAPS (caps) || !gst_caps_is_fixed (caps)))
199
structure = gst_caps_get_structure (caps, 0);
200
if (gst_structure_has_name (structure, "video/x-raw-yuv")) {
201
if (!gst_structure_get_int (structure, "width", &vdp_pad->width))
203
if (!gst_structure_get_int (structure, "height", &vdp_pad->height))
205
if (!gst_structure_get_fourcc (structure, "format", &vdp_pad->fourcc))
208
vdp_pad->yuv_output = TRUE;
209
} else if (gst_structure_has_name (structure, "video/x-vdpau-video")) {
210
if (!gst_structure_get_int (structure, "width", &vdp_pad->width))
212
if (!gst_structure_get_int (structure, "height", &vdp_pad->height))
215
vdp_pad->yuv_output = FALSE;
219
return gst_pad_set_caps (GST_PAD (vdp_pad), caps);
223
gst_vdp_video_src_pad_get_device (GstVdpVideoSrcPad * vdp_pad)
225
g_return_val_if_fail (GST_IS_VDP_VIDEO_SRC_PAD (vdp_pad), FALSE);
227
return vdp_pad->device;
231
gst_vdp_video_src_pad_getcaps (GstPad * pad)
233
GstVdpVideoSrcPad *vdp_pad = (GstVdpVideoSrcPad *) pad;
236
return gst_caps_ref (vdp_pad->caps);
237
else if (vdp_pad->templ_caps)
238
return gst_caps_ref (vdp_pad->templ_caps);
244
gst_vdp_video_src_pad_activate_push (GstPad * pad, gboolean active)
246
GstVdpVideoSrcPad *vdp_pad = GST_VDP_VIDEO_SRC_PAD (pad);
250
g_object_unref (vdp_pad->device);
251
vdp_pad->device = NULL;
254
gst_caps_unref (vdp_pad->caps);
255
vdp_pad->caps = NULL;
262
gst_vdp_video_src_pad_new (GstCaps * templ_caps)
264
return g_object_new (GST_TYPE_VDP_VIDEO_SRC_PAD, "direction", GST_PAD_SRC,
265
"template-caps", templ_caps, NULL);
269
gst_vdp_video_src_pad_get_property (GObject * object, guint prop_id,
270
GValue * value, GParamSpec * pspec)
272
GstVdpVideoSrcPad *vdp_pad = (GstVdpVideoSrcPad *) object;
276
g_value_set_string (value, vdp_pad->display);
278
case PROP_TEMPL_CAPS:
279
gst_value_set_caps (value, vdp_pad->templ_caps);
282
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
288
gst_vdp_video_src_pad_set_property (GObject * object, guint prop_id,
289
const GValue * value, GParamSpec * pspec)
291
GstVdpVideoSrcPad *vdp_pad = (GstVdpVideoSrcPad *) object;
295
vdp_pad->display = g_value_dup_string (value);
297
case PROP_TEMPL_CAPS:
298
if (vdp_pad->templ_caps)
299
gst_caps_unref (vdp_pad->templ_caps);
300
vdp_pad->templ_caps = gst_caps_copy (gst_value_get_caps (value));
303
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
309
gst_vdp_video_src_pad_finalize (GObject * object)
311
GstVdpVideoSrcPad *vdp_pad = (GstVdpVideoSrcPad *) object;
313
g_free (vdp_pad->display);
314
if (vdp_pad->templ_caps)
315
gst_caps_unref (vdp_pad->templ_caps);
317
G_OBJECT_CLASS (gst_vdp_video_src_pad_parent_class)->finalize (object);
321
gst_vdp_video_src_pad_init (GstVdpVideoSrcPad * vdp_pad)
323
GstPad *pad = GST_PAD (vdp_pad);
325
vdp_pad->device = NULL;
326
vdp_pad->caps = NULL;
328
vdp_pad->display = NULL;
329
vdp_pad->templ_caps = NULL;
331
gst_pad_set_getcaps_function (pad,
332
GST_DEBUG_FUNCPTR (gst_vdp_video_src_pad_getcaps));
333
gst_pad_set_activatepush_function (pad,
334
GST_DEBUG_FUNCPTR (gst_vdp_video_src_pad_activate_push));
338
gst_vdp_video_src_pad_class_init (GstVdpVideoSrcPadClass * klass)
340
GObjectClass *object_class = G_OBJECT_CLASS (klass);
342
object_class->get_property = gst_vdp_video_src_pad_get_property;
343
object_class->set_property = gst_vdp_video_src_pad_set_property;
344
object_class->finalize = gst_vdp_video_src_pad_finalize;
346
g_object_class_install_property (object_class, PROP_DISPLAY,
347
g_param_spec_string ("display", "Display", "X Display name",
348
NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
350
g_object_class_install_property (object_class, PROP_TEMPL_CAPS,
351
g_param_spec_boxed ("template-caps", "Template caps",
352
"Template caps", GST_TYPE_CAPS,
353
G_PARAM_READWRITE | G_PARAM_CONSTRUCT));