Imported Upstream version 0.10.23
[profile/ivi/gst-plugins-bad.git] / sys / vdpau / gstvdp / gstvdpvideosrcpad.c
1 /*
2  * GStreamer
3  * Copyright (C) 2009 Carl-Anton Ingmarsson <ca.ingmarsson@gmail.com>
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 #include "gstvdpvideobuffer.h"
22 #include "gstvdpvideobufferpool.h"
23 #include "gstvdputils.h"
24
25 #include "gstvdpvideosrcpad.h"
26
27 GST_DEBUG_CATEGORY_STATIC (gst_vdp_video_src_pad_debug);
28 #define GST_CAT_DEFAULT gst_vdp_video_src_pad_debug
29
30 enum
31 {
32   PROP_0,
33   PROP_DEVICE
34 };
35
36 struct _GstVdpVideoSrcPad
37 {
38   GstPad pad;
39
40   GstVdpBufferPool *bpool;
41   GstCaps *caps;
42
43   gboolean yuv_output;
44   gint width, height;
45   guint32 fourcc;
46
47   /* properties */
48   GstVdpDevice *device;
49 };
50
51 struct _GstVdpVideoSrcPadClass
52 {
53   GstPadClass pad_class;
54 };
55
56 #define DEBUG_INIT(bla) \
57 GST_DEBUG_CATEGORY_INIT (gst_vdp_video_src_pad_debug, "vdpvideosrcpad", 0, "GstVdpVideoSrcPad");
58
59 G_DEFINE_TYPE_WITH_CODE (GstVdpVideoSrcPad, gst_vdp_video_src_pad, GST_TYPE_PAD,
60     DEBUG_INIT ());
61
62 GstVdpVideoSrcPad *
63 gst_vdp_video_src_pad_new (GstPadTemplate * templ, const gchar * name)
64 {
65   g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
66   g_return_val_if_fail ((templ->direction == GST_PAD_SRC), NULL);
67
68   return g_object_new (GST_TYPE_VDP_VIDEO_SRC_PAD,
69       "name", name, "direction", templ->direction, "template", templ, NULL);
70 }
71
72 GstFlowReturn
73 gst_vdp_video_src_pad_push (GstVdpVideoSrcPad * vdp_pad,
74     GstVdpVideoBuffer * video_buf)
75 {
76   GstPad *pad;
77   GstBuffer *out_buf;
78
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);
81
82   pad = (GstPad *) vdp_pad;
83
84   if (G_UNLIKELY (!GST_PAD_CAPS (pad)))
85     return GST_FLOW_NOT_NEGOTIATED;
86
87   if (vdp_pad->yuv_output) {
88     guint size;
89     GstFlowReturn ret;
90     GstCaps *caps;
91
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;
97     }
98
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));
104       return ret;
105     }
106
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;
113     }
114
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;
122     }
123
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));
127   } else
128     out_buf = GST_BUFFER_CAST (video_buf);
129
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));
135
136   return gst_pad_push (pad, out_buf);
137 }
138
139 GstFlowReturn
140 gst_vdp_video_src_pad_alloc_buffer (GstVdpVideoSrcPad * vdp_pad,
141     GstVdpVideoBuffer ** video_buf, GError ** error)
142 {
143   GstCaps *caps;
144
145   g_return_val_if_fail (GST_IS_VDP_VIDEO_SRC_PAD (vdp_pad), GST_FLOW_ERROR);
146
147   caps = GST_PAD_CAPS (vdp_pad);
148   if (!caps)
149     return GST_FLOW_NOT_NEGOTIATED;
150
151   *video_buf =
152       (GstVdpVideoBuffer *) gst_vdp_buffer_pool_get_buffer (vdp_pad->bpool,
153       error);
154   if (!*video_buf)
155     return GST_FLOW_ERROR;
156
157   return GST_FLOW_OK;
158 }
159
160 static gboolean
161 gst_vdp_video_src_pad_setcaps (GstPad * pad, GstCaps * caps)
162 {
163   GstVdpVideoSrcPad *vdp_pad = GST_VDP_VIDEO_SRC_PAD (pad);
164   const GstStructure *structure;
165
166   GstCaps *video_caps;
167
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))
171       return FALSE;
172     if (!gst_structure_get_int (structure, "height", &vdp_pad->height))
173       return FALSE;
174     if (!gst_structure_get_fourcc (structure, "format", &vdp_pad->fourcc))
175       return FALSE;
176
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))
181       return FALSE;
182     if (!gst_structure_get_int (structure, "height", &vdp_pad->height))
183       return FALSE;
184
185     video_caps = gst_caps_ref (caps);
186     vdp_pad->yuv_output = FALSE;
187   } else
188     return FALSE;
189
190   gst_vdp_buffer_pool_set_caps (vdp_pad->bpool, video_caps);
191   gst_caps_unref (video_caps);
192
193   return TRUE;
194 }
195
196 static GstCaps *
197 gst_vdp_video_src_pad_getcaps (GstPad * pad)
198 {
199   GstVdpVideoSrcPad *vdp_pad = (GstVdpVideoSrcPad *) pad;
200
201   const GstCaps *templ_caps;
202
203   if (vdp_pad->caps)
204     return gst_caps_ref (vdp_pad->caps);
205
206   else if ((templ_caps = gst_pad_get_pad_template_caps (pad)))
207     return gst_caps_copy (templ_caps);
208
209   return NULL;
210 }
211
212 static gboolean
213 gst_vdp_video_src_pad_activate_push (GstPad * pad, gboolean active)
214 {
215   GstVdpVideoSrcPad *vdp_pad = (GstVdpVideoSrcPad *) pad;
216
217   if (!active) {
218     if (vdp_pad->caps)
219       gst_caps_unref (vdp_pad->caps);
220     vdp_pad->caps = NULL;
221
222     if (vdp_pad->device)
223       gst_object_unref (vdp_pad->device);
224     vdp_pad->device = NULL;
225   }
226
227   return TRUE;
228 }
229
230 static void
231 gst_vdp_video_src_pad_set_device (GstVdpVideoSrcPad * vdp_pad,
232     GstVdpDevice * device)
233 {
234   GstCaps *caps;
235   const GstCaps *templ_caps;
236
237   if (vdp_pad->bpool)
238     g_object_unref (vdp_pad->bpool);
239   if (vdp_pad->device)
240     g_object_unref (vdp_pad->device);
241
242   vdp_pad->device = device;
243   vdp_pad->bpool = gst_vdp_video_buffer_pool_new (device);
244
245   /* update caps */
246   if (vdp_pad->caps)
247     gst_caps_unref (vdp_pad->caps);
248
249   caps = gst_vdp_video_buffer_get_allowed_caps (device);
250
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);
254   } else
255     vdp_pad->caps = caps;
256 }
257
258 static void
259 gst_vdp_video_src_pad_get_property (GObject * object, guint prop_id,
260     GValue * value, GParamSpec * pspec)
261 {
262   GstVdpVideoSrcPad *vdp_pad = (GstVdpVideoSrcPad *) object;
263
264   switch (prop_id) {
265     case PROP_DEVICE:
266       g_value_set_object (value, vdp_pad->device);
267       break;
268
269     default:
270       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
271       break;
272   }
273 }
274
275 static void
276 gst_vdp_video_src_pad_set_property (GObject * object, guint prop_id,
277     const GValue * value, GParamSpec * pspec)
278 {
279   GstVdpVideoSrcPad *vdp_pad = (GstVdpVideoSrcPad *) object;
280
281   switch (prop_id) {
282     case PROP_DEVICE:
283       gst_vdp_video_src_pad_set_device (vdp_pad, g_value_dup_object (value));
284       break;
285
286     default:
287       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
288       break;
289   }
290 }
291
292 static void
293 gst_vdp_video_src_pad_init (GstVdpVideoSrcPad * vdp_pad)
294 {
295   GstPad *pad = GST_PAD (vdp_pad);
296
297   vdp_pad->device = NULL;
298   vdp_pad->caps = NULL;
299
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));
306 }
307
308 static void
309 gst_vdp_video_src_pad_class_init (GstVdpVideoSrcPadClass * klass)
310 {
311   GObjectClass *object_class = G_OBJECT_CLASS (klass);
312
313   object_class->get_property = gst_vdp_video_src_pad_get_property;
314   object_class->set_property = gst_vdp_video_src_pad_set_property;
315
316   /**
317    * GstVdpVideoSrcPad:device:
318    *
319    * The #GstVdpDevice this pool is bound to.
320    */
321   g_object_class_install_property
322       (object_class,
323       PROP_DEVICE,
324       g_param_spec_object ("device",
325           "Device",
326           "The GstVdpDevice the pad should use",
327           GST_TYPE_VDP_DEVICE, G_PARAM_READWRITE));
328 }