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 "gstvdpoutputsrcpad.h"
25
GST_DEBUG_CATEGORY_STATIC (gst_vdp_output_src_pad_debug);
26
#define GST_CAT_DEFAULT gst_vdp_output_src_pad_debug
35
typedef enum _GstVdpOutputSrcPadFormat GstVdpOutputSrcPadFormat;
37
enum _GstVdpOutputSrcPadFormat
39
GST_VDP_OUTPUT_SRC_PAD_FORMAT_RGB,
40
GST_VDP_OUTPUT_SRC_PAD_FORMAT_VDPAU
43
struct _GstVdpOutputSrcPad
50
GstVdpOutputSrcPadFormat output_format;
51
VdpRGBAFormat rgba_format;
59
struct _GstVdpOutputSrcPadClass
61
GstPadClass pad_class;
64
#define DEBUG_INIT(bla) \
65
GST_DEBUG_CATEGORY_INIT (gst_vdp_output_src_pad_debug, "vdpoutputsrcpad", 0, "GstVdpOutputSrcPad");
67
G_DEFINE_TYPE_WITH_CODE (GstVdpOutputSrcPad, gst_vdp_output_src_pad,
68
GST_TYPE_PAD, DEBUG_INIT ());
71
gst_vdp_output_src_pad_push (GstVdpOutputSrcPad * vdp_pad,
72
GstVdpOutputBuffer * output_buf)
77
g_return_val_if_fail (GST_IS_VDP_OUTPUT_SRC_PAD (vdp_pad), GST_FLOW_ERROR);
78
g_return_val_if_fail (GST_IS_VDP_OUTPUT_BUFFER (output_buf), GST_FLOW_ERROR);
80
pad = (GstPad *) vdp_pad;
82
if (G_UNLIKELY (!GST_PAD_CAPS (pad)))
83
return GST_FLOW_NOT_NEGOTIATED;
85
switch (vdp_pad->output_format) {
86
case GST_VDP_OUTPUT_SRC_PAD_FORMAT_RGB:
90
if (!gst_vdp_output_buffer_calculate_size (output_buf, &size)) {
91
GST_ERROR_OBJECT (vdp_pad, "Couldn't calculate buffer size for caps");
92
gst_buffer_unref (GST_BUFFER_CAST (output_buf));
93
return GST_FLOW_ERROR;
96
out_buf = gst_buffer_new_and_alloc (size);
97
gst_buffer_set_caps (out_buf, GST_PAD_CAPS (vdp_pad));
99
if (!gst_vdp_output_buffer_download (output_buf, out_buf)) {
100
GST_ERROR_OBJECT (vdp_pad,
101
"Couldn't convert from GstVdpVideoBuffer to the requested format");
102
gst_buffer_unref (GST_BUFFER_CAST (output_buf));
103
gst_buffer_unref (out_buf);
106
gst_buffer_copy_metadata (out_buf, (const GstBuffer *) output_buf,
107
GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS);
108
gst_buffer_unref (GST_BUFFER_CAST (output_buf));
112
case GST_VDP_OUTPUT_SRC_PAD_FORMAT_VDPAU:
114
out_buf = GST_BUFFER_CAST (output_buf);
119
g_assert_not_reached ();
123
gst_buffer_set_caps (out_buf, GST_PAD_CAPS (vdp_pad));
125
return gst_pad_push (pad, out_buf);
129
gst_vdp_output_src_pad_update_caps (GstVdpOutputSrcPad * vdp_pad)
131
GstCaps *allowed_caps;
133
allowed_caps = gst_vdp_output_buffer_get_allowed_caps (vdp_pad->device);
136
gst_caps_unref (vdp_pad->caps);
138
if (vdp_pad->templ_caps) {
139
vdp_pad->caps = gst_caps_intersect (allowed_caps, vdp_pad->templ_caps);
140
gst_caps_unref (allowed_caps);
142
vdp_pad->caps = allowed_caps;
146
gst_vdp_output_src_pad_alloc_buffer (GstVdpOutputSrcPad * vdp_pad,
147
GstVdpOutputBuffer ** output_buf)
152
g_return_val_if_fail (GST_IS_VDP_OUTPUT_SRC_PAD (vdp_pad), GST_FLOW_ERROR);
154
caps = GST_PAD_CAPS (vdp_pad);
156
return GST_FLOW_NOT_NEGOTIATED;
158
switch (vdp_pad->output_format) {
159
case GST_VDP_OUTPUT_SRC_PAD_FORMAT_RGB:
161
GstVdpDevice *device;
164
GstStructure *structure;
167
if (G_UNLIKELY (!vdp_pad->device)) {
168
vdp_pad->device = gst_vdp_get_device (vdp_pad->display);
169
if (G_UNLIKELY (!vdp_pad->device))
172
gst_vdp_output_src_pad_update_caps (vdp_pad);
174
device = vdp_pad->device;
177
ret = gst_pad_alloc_buffer (GST_PAD_CAST (vdp_pad),
178
GST_BUFFER_OFFSET_NONE, 0, caps, &neg_buf);
179
if (ret != GST_FLOW_OK)
182
structure = gst_caps_get_structure (GST_BUFFER_CAPS (neg_buf), 0);
183
if (!gst_structure_get_int (structure, "width", &width) ||
184
!gst_structure_get_int (structure, "height", &height)) {
185
gst_buffer_unref (neg_buf);
186
GST_ERROR_OBJECT (vdp_pad,
187
"Sink element allocated buffer with invalid caps");
188
return GST_FLOW_ERROR;
191
*output_buf = gst_vdp_output_buffer_new (device, vdp_pad->rgba_format,
194
gst_buffer_unref (neg_buf);
195
goto output_buf_error;
198
gst_buffer_set_caps (GST_BUFFER_CAST (*output_buf),
199
GST_BUFFER_CAPS (neg_buf));
200
gst_buffer_unref (neg_buf);
205
case GST_VDP_OUTPUT_SRC_PAD_FORMAT_VDPAU:
207
GstStructure *structure;
209
ret = gst_pad_alloc_buffer ((GstPad *) vdp_pad, 0, 0, caps,
210
(GstBuffer **) output_buf);
211
if (ret != GST_FLOW_OK)
214
structure = gst_caps_get_structure (GST_BUFFER_CAPS (*output_buf), 0);
215
if (!gst_structure_has_name (structure, "video/x-vdpau-output"))
218
if (G_UNLIKELY (!vdp_pad->device)) {
220
g_object_ref (GST_VDP_VIDEO_BUFFER (*output_buf)->device);
222
gst_vdp_output_src_pad_update_caps (vdp_pad);
229
g_assert_not_reached ();
236
GST_ERROR_OBJECT (vdp_pad, "Couldn't create GstVdpDevice");
237
return GST_FLOW_ERROR;
240
GST_ERROR_OBJECT (vdp_pad, "Couldn't create GstVdpVideoBuffer");
241
return GST_FLOW_ERROR;
244
GST_ERROR_OBJECT (vdp_pad, "Sink element returned buffer with wrong caps");
245
gst_buffer_unref (GST_BUFFER_CAST (*output_buf));
246
return GST_FLOW_ERROR;
250
gst_vdp_output_src_pad_set_caps (GstVdpOutputSrcPad * vdp_pad, GstCaps * caps)
252
const GstStructure *structure;
254
g_return_val_if_fail (GST_IS_VDP_OUTPUT_SRC_PAD (vdp_pad), FALSE);
256
if (G_UNLIKELY (!caps))
257
return gst_pad_set_caps (GST_PAD (vdp_pad), caps);
259
if (G_UNLIKELY (!GST_IS_CAPS (caps) || !gst_caps_is_fixed (caps)))
262
structure = gst_caps_get_structure (caps, 0);
263
if (gst_structure_has_name (structure, "video/x-raw-rgb")) {
264
if (!gst_vdp_caps_to_rgba_format (caps, &vdp_pad->rgba_format))
266
if (!gst_structure_get_int (structure, "width", &vdp_pad->width))
268
if (!gst_structure_get_int (structure, "height", &vdp_pad->height))
271
vdp_pad->output_format = GST_VDP_OUTPUT_SRC_PAD_FORMAT_RGB;
272
} else if (gst_structure_has_name (structure, "video/x-vdpau-output")) {
273
if (!gst_structure_get_int (structure, "rgba-format",
274
(gint *) & vdp_pad->rgba_format))
276
if (!gst_structure_get_int (structure, "width", &vdp_pad->width))
278
if (!gst_structure_get_int (structure, "height", &vdp_pad->height))
281
vdp_pad->output_format = GST_VDP_OUTPUT_SRC_PAD_FORMAT_VDPAU;
285
return gst_pad_set_caps (GST_PAD (vdp_pad), caps);
289
gst_vdp_output_src_pad_get_device (GstVdpOutputSrcPad * vdp_pad,
290
GstVdpDevice ** device)
292
g_return_val_if_fail (GST_IS_VDP_OUTPUT_SRC_PAD (vdp_pad), FALSE);
294
if (G_UNLIKELY (!vdp_pad->device)) {
296
GstStructure *structure;
298
src_caps = gst_pad_get_allowed_caps (GST_PAD (vdp_pad));
299
gst_pad_fixate_caps (GST_PAD (vdp_pad), src_caps);
301
if (gst_caps_is_empty (src_caps)) {
302
gst_caps_unref (src_caps);
303
return GST_FLOW_NOT_NEGOTIATED;
306
structure = gst_caps_get_structure (src_caps, 0);
307
if (gst_structure_has_name (structure, "video/x-raw-rgb")) {
308
vdp_pad->device = gst_vdp_get_device (vdp_pad->display);
309
if (G_UNLIKELY (!vdp_pad->device))
317
ret = gst_pad_alloc_buffer (GST_PAD (vdp_pad), 0, 0, src_caps, &buf);
318
if (ret != GST_FLOW_OK) {
319
gst_caps_unref (src_caps);
323
if (!gst_caps_is_equal_fixed (src_caps, GST_BUFFER_CAPS (buf))) {
324
gst_caps_unref (src_caps);
325
gst_buffer_unref (buf);
329
vdp_pad->device = g_object_ref (GST_VDP_OUTPUT_BUFFER (buf)->device);
332
gst_caps_unref (src_caps);
335
*device = vdp_pad->device;
339
GST_ERROR_OBJECT (vdp_pad, "Couldn't create GstVdpDevice");
340
return GST_FLOW_ERROR;
343
GST_ERROR_OBJECT (vdp_pad, "Sink element returned buffer with wrong caps");
344
return GST_FLOW_ERROR;
348
gst_vdp_output_src_pad_getcaps (GstPad * pad)
350
GstVdpOutputSrcPad *vdp_pad = (GstVdpOutputSrcPad *) pad;
353
return gst_caps_ref (vdp_pad->caps);
355
return gst_caps_ref (vdp_pad->templ_caps);
359
gst_vdp_output_src_pad_activate_push (GstPad * pad, gboolean active)
361
GstVdpOutputSrcPad *vdp_pad = GST_VDP_OUTPUT_SRC_PAD (pad);
365
g_object_unref (vdp_pad->device);
366
vdp_pad->device = NULL;
369
gst_caps_unref (vdp_pad->caps);
370
vdp_pad->caps = NULL;
377
gst_vdp_output_src_pad_new (GstCaps * templ_caps)
379
return g_object_new (GST_TYPE_VDP_OUTPUT_SRC_PAD, "template-caps", templ_caps,
380
"direction", GST_PAD_SRC, NULL);
384
gst_vdp_output_src_pad_get_property (GObject * object, guint prop_id,
385
GValue * value, GParamSpec * pspec)
387
GstVdpOutputSrcPad *vdp_pad = (GstVdpOutputSrcPad *) object;
391
g_value_set_string (value, vdp_pad->display);
393
case PROP_TEMPL_CAPS:
394
gst_value_set_caps (value, vdp_pad->templ_caps);
397
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
403
gst_vdp_output_src_pad_set_property (GObject * object, guint prop_id,
404
const GValue * value, GParamSpec * pspec)
406
GstVdpOutputSrcPad *vdp_pad = (GstVdpOutputSrcPad *) object;
410
vdp_pad->display = g_value_dup_string (value);
412
case PROP_TEMPL_CAPS:
413
if (vdp_pad->templ_caps)
414
gst_caps_unref (vdp_pad->templ_caps);
415
vdp_pad->templ_caps = gst_caps_copy (gst_value_get_caps (value));
418
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
424
gst_vdp_output_src_pad_finalize (GObject * object)
426
GstVdpOutputSrcPad *vdp_pad = (GstVdpOutputSrcPad *) object;
428
if (vdp_pad->templ_caps)
429
gst_caps_unref (vdp_pad->templ_caps);
431
g_free (vdp_pad->display);
433
G_OBJECT_CLASS (gst_vdp_output_src_pad_parent_class)->finalize (object);
437
gst_vdp_output_src_pad_init (GstVdpOutputSrcPad * vdp_pad)
439
GstPad *pad = GST_PAD (vdp_pad);
441
vdp_pad->device = NULL;
442
vdp_pad->caps = NULL;
444
vdp_pad->display = NULL;
445
vdp_pad->templ_caps = NULL;
447
gst_pad_set_getcaps_function (pad,
448
GST_DEBUG_FUNCPTR (gst_vdp_output_src_pad_getcaps));
449
gst_pad_set_activatepush_function (pad,
450
GST_DEBUG_FUNCPTR (gst_vdp_output_src_pad_activate_push));
454
gst_vdp_output_src_pad_class_init (GstVdpOutputSrcPadClass * klass)
456
GObjectClass *object_class = G_OBJECT_CLASS (klass);
458
object_class->get_property = gst_vdp_output_src_pad_get_property;
459
object_class->set_property = gst_vdp_output_src_pad_set_property;
460
object_class->finalize = gst_vdp_output_src_pad_finalize;
462
g_object_class_install_property (object_class, PROP_DISPLAY,
463
g_param_spec_string ("display", "Display", "X Display name",
464
NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
466
g_object_class_install_property (object_class, PROP_TEMPL_CAPS,
467
g_param_spec_boxed ("template-caps", "Template caps",
468
"Template caps", GST_TYPE_CAPS,
469
G_PARAM_READWRITE | G_PARAM_CONSTRUCT));