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.
26
#include <gst/video/video.h>
28
#include "gstvdpvideobuffer.h"
29
#include "gstvdputils.h"
30
#include "gstvdpvideoyuv.h"
32
GST_DEBUG_CATEGORY_STATIC (gst_vdp_video_yuv_debug);
33
#define GST_CAT_DEFAULT gst_vdp_video_yuv_debug
35
/* Filter signals and args */
48
static GstStaticPadTemplate sink_template =
49
GST_STATIC_PAD_TEMPLATE (GST_BASE_TRANSFORM_SINK_NAME,
52
GST_STATIC_CAPS (GST_VDP_VIDEO_CAPS));
54
static GstStaticPadTemplate src_template =
55
GST_STATIC_PAD_TEMPLATE (GST_BASE_TRANSFORM_SRC_NAME,
58
GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("I420") ";"
59
GST_VIDEO_CAPS_YUV ("YV12") ";" GST_VIDEO_CAPS_YUV ("NV12") ";"
60
GST_VIDEO_CAPS_YUV ("UYVY") ";" GST_VIDEO_CAPS_YUV ("YUY2")));
62
#define DEBUG_INIT(bla) \
63
GST_DEBUG_CATEGORY_INIT (gst_vdp_video_yuv_debug, "vdpauvideoyuv", 0, "VDPAU VdpSurface to YUV");
65
GST_BOILERPLATE_FULL (GstVdpVideoYUV, gst_vdp_video_yuv, GstBaseTransform,
66
GST_TYPE_BASE_TRANSFORM, DEBUG_INIT);
69
gst_vdp_video_yuv_transform (GstBaseTransform * trans, GstBuffer * inbuf,
72
GstVdpVideoYUV *video_yuv = GST_VDP_VIDEO_YUV (trans);
74
VdpVideoSurface surface;
76
device = GST_VDP_VIDEO_BUFFER (inbuf)->device;
77
surface = GST_VDP_VIDEO_BUFFER (inbuf)->surface;
79
switch (video_yuv->format) {
80
case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
86
data[0] = GST_BUFFER_DATA (outbuf) +
87
gst_video_format_get_component_offset (GST_VIDEO_FORMAT_YV12,
88
0, video_yuv->width, video_yuv->height);
89
data[1] = GST_BUFFER_DATA (outbuf) +
90
gst_video_format_get_component_offset (GST_VIDEO_FORMAT_YV12,
91
2, video_yuv->width, video_yuv->height);
92
data[2] = GST_BUFFER_DATA (outbuf) +
93
gst_video_format_get_component_offset (GST_VIDEO_FORMAT_YV12,
94
1, video_yuv->width, video_yuv->height);
96
stride[0] = gst_video_format_get_row_stride (GST_VIDEO_FORMAT_YV12,
98
stride[1] = gst_video_format_get_row_stride (GST_VIDEO_FORMAT_YV12,
100
stride[2] = gst_video_format_get_row_stride (GST_VIDEO_FORMAT_YV12,
101
1, video_yuv->width);
103
GST_LOG_OBJECT (video_yuv, "Entering vdp_video_surface_get_bits_ycbcr");
105
device->vdp_video_surface_get_bits_ycbcr (surface,
106
VDP_YCBCR_FORMAT_YV12, (void *) data, stride);
107
GST_LOG_OBJECT (video_yuv,
108
"Got status %d from vdp_video_surface_get_bits_ycbcr", status);
109
if (G_UNLIKELY (status != VDP_STATUS_OK)) {
110
GST_ELEMENT_ERROR (video_yuv, RESOURCE, READ,
111
("Couldn't get data from vdpau"),
112
("Error returned from vdpau was: %s",
113
device->vdp_get_error_string (status)));
114
return GST_FLOW_ERROR;
118
case GST_MAKE_FOURCC ('I', '4', '2', '0'):
124
data[0] = GST_BUFFER_DATA (outbuf) +
125
gst_video_format_get_component_offset (GST_VIDEO_FORMAT_I420,
126
0, video_yuv->width, video_yuv->height);
127
data[1] = GST_BUFFER_DATA (outbuf) +
128
gst_video_format_get_component_offset (GST_VIDEO_FORMAT_I420,
129
2, video_yuv->width, video_yuv->height);
130
data[2] = GST_BUFFER_DATA (outbuf) +
131
gst_video_format_get_component_offset (GST_VIDEO_FORMAT_I420,
132
1, video_yuv->width, video_yuv->height);
134
stride[0] = gst_video_format_get_row_stride (GST_VIDEO_FORMAT_I420,
135
0, video_yuv->width);
136
stride[1] = gst_video_format_get_row_stride (GST_VIDEO_FORMAT_I420,
137
2, video_yuv->width);
138
stride[2] = gst_video_format_get_row_stride (GST_VIDEO_FORMAT_I420,
139
1, video_yuv->width);
141
GST_LOG_OBJECT (video_yuv, "Entering vdp_video_surface_get_bits_ycbcr");
143
device->vdp_video_surface_get_bits_ycbcr (surface,
144
VDP_YCBCR_FORMAT_YV12, (void *) data, stride);
145
GST_LOG_OBJECT (video_yuv,
146
"Got status %d from vdp_video_surface_get_bits_ycbcr", status);
147
if (G_UNLIKELY (status != VDP_STATUS_OK)) {
148
GST_ELEMENT_ERROR (video_yuv, RESOURCE, READ,
149
("Couldn't get data from vdpau"),
150
("Error returned from vdpau was: %s",
151
device->vdp_get_error_string (status)));
152
return GST_FLOW_ERROR;
156
case GST_MAKE_FOURCC ('N', 'V', '1', '2'):
162
data[0] = GST_BUFFER_DATA (outbuf);
163
data[1] = GST_BUFFER_DATA (outbuf) + video_yuv->width * video_yuv->height;
165
stride[0] = video_yuv->width;
166
stride[1] = video_yuv->width;
168
GST_LOG_OBJECT (video_yuv, "Entering vdp_video_surface_get_bits_ycbcr");
170
device->vdp_video_surface_get_bits_ycbcr (surface,
171
VDP_YCBCR_FORMAT_NV12, (void *) data, stride);
172
GST_LOG_OBJECT (video_yuv,
173
"Got status %d from vdp_video_surface_get_bits_ycbcr", status);
174
if (G_UNLIKELY (status != VDP_STATUS_OK)) {
175
GST_ELEMENT_ERROR (video_yuv, RESOURCE, READ,
176
("Couldn't get data from vdpau"),
177
("Error returned from vdpau was: %s",
178
device->vdp_get_error_string (status)));
179
return GST_FLOW_ERROR;
183
case GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'):
189
data[0] = GST_BUFFER_DATA (outbuf);
191
stride[0] = gst_video_format_get_row_stride (GST_VIDEO_FORMAT_UYVY,
192
0, video_yuv->width);
194
GST_LOG_OBJECT (video_yuv, "Entering vdp_video_surface_get_bits_ycbcr");
196
device->vdp_video_surface_get_bits_ycbcr (surface,
197
VDP_YCBCR_FORMAT_UYVY, (void *) data, stride);
198
GST_LOG_OBJECT (video_yuv,
199
"Got status %d from vdp_video_surface_get_bits_ycbcr", status);
200
if (G_UNLIKELY (status != VDP_STATUS_OK)) {
201
GST_ELEMENT_ERROR (video_yuv, RESOURCE, READ,
202
("Couldn't get data from vdpau"),
203
("Error returned from vdpau was: %s",
204
device->vdp_get_error_string (status)));
205
return GST_FLOW_ERROR;
209
case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
215
data[0] = GST_BUFFER_DATA (outbuf);
217
stride[0] = gst_video_format_get_row_stride (GST_VIDEO_FORMAT_YUY2,
218
0, video_yuv->width);
220
GST_LOG_OBJECT (video_yuv, "Entering vdp_video_surface_get_bits_ycbcr");
222
device->vdp_video_surface_get_bits_ycbcr (surface,
223
VDP_YCBCR_FORMAT_YUYV, (void *) data, stride);
224
GST_LOG_OBJECT (video_yuv,
225
"Got status %d from vdp_video_surface_get_bits_ycbcr", status);
226
if (G_UNLIKELY (status != VDP_STATUS_OK)) {
227
GST_ELEMENT_ERROR (video_yuv, RESOURCE, READ,
228
("Couldn't get data from vdpau"),
229
("Error returned from vdpau was: %s",
230
device->vdp_get_error_string (status)));
231
return GST_FLOW_ERROR;
239
gst_buffer_copy_metadata (outbuf, inbuf,
240
GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS);
242
GST_LOG_OBJECT (video_yuv, "Pushing buffer with ts %" GST_TIME_FORMAT,
243
GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)));
249
gst_vdp_video_transform_size (GstBaseTransform * trans,
250
GstPadDirection direction, GstCaps * caps, guint size,
251
GstCaps * othercaps, guint * othersize)
253
GstVdpVideoYUV *video_yuv = GST_VDP_VIDEO_YUV (trans);
255
if (direction == GST_PAD_SINK) {
256
switch (video_yuv->format) {
257
case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
260
gst_video_format_get_size (GST_VIDEO_FORMAT_YV12, video_yuv->width,
264
case GST_MAKE_FOURCC ('I', '4', '2', '0'):
267
gst_video_format_get_size (GST_VIDEO_FORMAT_YV12, video_yuv->width,
271
case GST_MAKE_FOURCC ('N', 'V', '1', '2'):
274
video_yuv->width * video_yuv->height +
275
video_yuv->width * video_yuv->height / 2;
278
case GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'):
281
gst_video_format_get_size (GST_VIDEO_FORMAT_UYVY, video_yuv->width,
285
case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
288
gst_video_format_get_size (GST_VIDEO_FORMAT_YUY2, video_yuv->width,
302
gst_vdp_video_yuv_set_caps (GstBaseTransform * trans, GstCaps * incaps,
305
GstVdpVideoYUV *video_yuv = GST_VDP_VIDEO_YUV (trans);
306
GstStructure *structure;
308
structure = gst_caps_get_structure (incaps, 0);
310
if (!gst_structure_get_int (structure, "width", &video_yuv->width))
312
if (!gst_structure_get_int (structure, "height", &video_yuv->height))
315
structure = gst_caps_get_structure (outcaps, 0);
316
if (!gst_structure_get_fourcc (structure, "format", &video_yuv->format))
323
gst_vdp_video_yuv_transform_caps (GstBaseTransform * trans,
324
GstPadDirection direction, GstCaps * caps)
326
GstVdpVideoYUV *video_yuv = GST_VDP_VIDEO_YUV (trans);
327
GstCaps *result = NULL;
329
if (direction == GST_PAD_SINK)
330
result = gst_vdp_video_to_yuv_caps (caps, video_yuv->device);
332
else if (direction == GST_PAD_SRC)
333
result = gst_vdp_yuv_to_video_caps (caps, video_yuv->device);
335
GST_LOG ("transformed %" GST_PTR_FORMAT " to %" GST_PTR_FORMAT, caps, result);
341
gst_vdp_video_yuv_start (GstBaseTransform * trans)
343
GstVdpVideoYUV *video_yuv = GST_VDP_VIDEO_YUV (trans);
345
video_yuv->device = gst_vdp_get_device (video_yuv->display);
346
if (!video_yuv->device)
353
gst_vdp_video_yuv_stop (GstBaseTransform * trans)
355
GstVdpVideoYUV *video_yuv = GST_VDP_VIDEO_YUV (trans);
357
g_object_unref (video_yuv->device);
363
gst_vdp_video_yuv_buffer_alloc (GstPad * pad, guint64 offset,
364
guint size, GstCaps * caps, GstBuffer ** buf)
366
GstVdpVideoYUV *video_yuv = GST_VDP_VIDEO_YUV (gst_pad_get_parent (pad));
367
GstFlowReturn ret = GST_FLOW_ERROR;
368
GstStructure *structure;
372
structure = gst_caps_get_structure (caps, 0);
376
if (!gst_structure_get_int (structure, "width", &width))
378
if (!gst_structure_get_int (structure, "height", &height))
381
if (!gst_structure_get_int (structure, "chroma-type", &chroma_type))
384
*buf = GST_BUFFER (gst_vdp_video_buffer_new (video_yuv->device,
385
chroma_type, width, height));
390
GST_BUFFER_SIZE (*buf) = size;
391
GST_BUFFER_OFFSET (*buf) = offset;
393
gst_buffer_set_caps (*buf, caps);
398
gst_object_unref (video_yuv);
402
/* GObject vmethod implementations */
405
gst_vdp_video_yuv_base_init (gpointer klass)
407
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
409
gst_element_class_set_details_simple (element_class,
411
"Filter/Converter/Decoder/Video",
412
"VDPAU video surface to YUV",
413
"Carl-Anton Ingmarsson <ca.ingmarsson@gmail.com>");
415
gst_element_class_add_pad_template (element_class,
416
gst_static_pad_template_get (&sink_template));
417
gst_element_class_add_pad_template (element_class,
418
gst_static_pad_template_get (&src_template));
422
gst_vdp_video_yuv_finalize (GObject * object)
424
GstVdpVideoYUV *video_yuv = (GstVdpVideoYUV *) object;
426
g_free (video_yuv->display);
430
gst_vdp_video_yuv_set_property (GObject * object, guint prop_id,
431
const GValue * value, GParamSpec * pspec)
433
GstVdpVideoYUV *video_yuv = GST_VDP_VIDEO_YUV (object);
437
g_free (video_yuv->display);
438
video_yuv->display = g_value_dup_string (value);
441
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
447
gst_vdp_video_yuv_get_property (GObject * object, guint prop_id,
448
GValue * value, GParamSpec * pspec)
450
GstVdpVideoYUV *video_yuv = GST_VDP_VIDEO_YUV (object);
454
g_value_set_string (value, video_yuv->display);
457
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
463
gst_vdp_video_yuv_class_init (GstVdpVideoYUVClass * klass)
465
GObjectClass *gobject_class;
466
GstBaseTransformClass *trans_class;
468
gobject_class = (GObjectClass *) klass;
469
trans_class = (GstBaseTransformClass *) klass;
471
gobject_class->finalize = gst_vdp_video_yuv_finalize;
472
gobject_class->set_property = gst_vdp_video_yuv_set_property;
473
gobject_class->get_property = gst_vdp_video_yuv_get_property;
475
g_object_class_install_property (gobject_class, PROP_DISPLAY,
476
g_param_spec_string ("display", "Display", "X Display name",
477
NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
479
trans_class->start = gst_vdp_video_yuv_start;
480
trans_class->stop = gst_vdp_video_yuv_stop;
481
trans_class->transform_caps = gst_vdp_video_yuv_transform_caps;
482
trans_class->transform_size = gst_vdp_video_transform_size;
483
trans_class->transform = gst_vdp_video_yuv_transform;
484
trans_class->set_caps = gst_vdp_video_yuv_set_caps;
488
gst_vdp_video_yuv_init (GstVdpVideoYUV * video_yuv, GstVdpVideoYUVClass * klass)
490
video_yuv->display = NULL;
492
gst_pad_set_bufferalloc_function (GST_BASE_TRANSFORM_SINK_PAD (video_yuv),
493
gst_vdp_video_yuv_buffer_alloc);