Tizen 2.0 Release
[framework/multimedia/gst-plugins-bad0.10.git] / sys / vdpau / gstvdp / gstvdpoutputsrcpad.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 "gstvdputils.h"
22 #include "gstvdpvideobuffer.h"
23 #include "gstvdpoutputbufferpool.h"
24
25 #include "gstvdpoutputsrcpad.h"
26
27 GST_DEBUG_CATEGORY_STATIC (gst_vdp_output_src_pad_debug);
28 #define GST_CAT_DEFAULT gst_vdp_output_src_pad_debug
29
30 enum
31 {
32   PROP_0,
33   PROP_DEVICE
34 };
35
36 typedef enum _GstVdpOutputSrcPadFormat GstVdpOutputSrcPadFormat;
37
38 enum _GstVdpOutputSrcPadFormat
39 {
40   GST_VDP_OUTPUT_SRC_PAD_FORMAT_RGB,
41   GST_VDP_OUTPUT_SRC_PAD_FORMAT_VDPAU
42 };
43
44 struct _GstVdpOutputSrcPad
45 {
46   GstPad pad;
47
48   GstCaps *caps;
49
50   GstCaps *output_caps;
51   GstVdpOutputSrcPadFormat output_format;
52   VdpRGBAFormat rgba_format;
53   gint width, height;
54
55   GstVdpBufferPool *bpool;
56   gboolean lock_caps;
57
58   /* properties */
59   GstVdpDevice *device;
60 };
61
62 struct _GstVdpOutputSrcPadClass
63 {
64   GstPadClass pad_class;
65 };
66
67 #define DEBUG_INIT(bla) \
68 GST_DEBUG_CATEGORY_INIT (gst_vdp_output_src_pad_debug, "vdpoutputsrcpad", 0, "GstVdpOutputSrcPad");
69
70 G_DEFINE_TYPE_WITH_CODE (GstVdpOutputSrcPad, gst_vdp_output_src_pad,
71     GST_TYPE_PAD, DEBUG_INIT ());
72
73 GstFlowReturn
74 gst_vdp_output_src_pad_push (GstVdpOutputSrcPad * vdp_pad,
75     GstVdpOutputBuffer * output_buf, GError ** error)
76 {
77   GstPad *pad;
78   GstBuffer *outbuf;
79
80   g_return_val_if_fail (GST_IS_VDP_OUTPUT_SRC_PAD (vdp_pad), GST_FLOW_ERROR);
81   g_return_val_if_fail (GST_IS_VDP_OUTPUT_BUFFER (output_buf), GST_FLOW_ERROR);
82
83   pad = (GstPad *) vdp_pad;
84
85   if (G_UNLIKELY (!GST_PAD_CAPS (pad)))
86     return GST_FLOW_NOT_NEGOTIATED;
87
88   switch (vdp_pad->output_format) {
89     case GST_VDP_OUTPUT_SRC_PAD_FORMAT_RGB:
90     {
91       GstFlowReturn ret;
92       guint size;
93
94       gst_vdp_output_buffer_calculate_size (output_buf, &size);
95
96       vdp_pad->lock_caps = TRUE;
97       ret = gst_pad_alloc_buffer (pad, 0, size, GST_PAD_CAPS (vdp_pad),
98           &outbuf);
99       vdp_pad->lock_caps = FALSE;
100
101       if (ret != GST_FLOW_OK) {
102         gst_buffer_unref (GST_BUFFER_CAST (output_buf));
103         return ret;
104       }
105
106       if (!gst_vdp_output_buffer_download (output_buf, outbuf, error)) {
107         gst_buffer_unref (GST_BUFFER_CAST (output_buf));
108         gst_buffer_unref (outbuf);
109         return GST_FLOW_ERROR;
110       }
111
112       gst_buffer_copy_metadata (outbuf, (const GstBuffer *) output_buf,
113           GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS);
114       gst_buffer_unref (GST_BUFFER_CAST (output_buf));
115       break;
116     }
117     case GST_VDP_OUTPUT_SRC_PAD_FORMAT_VDPAU:
118     {
119       outbuf = GST_BUFFER_CAST (output_buf);
120       break;
121     }
122
123     default:
124       g_assert_not_reached ();
125       break;
126   }
127
128   gst_buffer_set_caps (outbuf, GST_PAD_CAPS (vdp_pad));
129
130   return gst_pad_push (pad, outbuf);
131 }
132
133 static GstFlowReturn
134 gst_vdp_output_src_pad_create_buffer (GstVdpOutputSrcPad * vdp_pad,
135     GstVdpOutputBuffer ** output_buf, GError ** error)
136 {
137   GstFlowReturn ret;
138   GstBuffer *neg_buf;
139
140   /* negotiate */
141   ret = gst_pad_alloc_buffer_and_set_caps (GST_PAD_CAST (vdp_pad),
142       GST_BUFFER_OFFSET_NONE, 0, GST_PAD_CAPS (vdp_pad), &neg_buf);
143   if (ret == GST_FLOW_OK)
144     gst_buffer_unref (neg_buf);
145
146   *output_buf =
147       (GstVdpOutputBuffer *) gst_vdp_buffer_pool_get_buffer (vdp_pad->bpool,
148       error);
149   if (!*output_buf)
150     return GST_FLOW_ERROR;
151
152   return GST_FLOW_OK;
153 }
154
155 static GstFlowReturn
156 gst_vdp_output_src_pad_alloc_with_caps (GstVdpOutputSrcPad * vdp_pad,
157     GstCaps * caps, GstVdpOutputBuffer ** output_buf, GError ** error)
158 {
159   GstFlowReturn ret;
160
161   ret = gst_pad_alloc_buffer_and_set_caps ((GstPad *) vdp_pad, 0, 0, caps,
162       (GstBuffer **) output_buf);
163   if (ret != GST_FLOW_OK)
164     return ret;
165
166   if (!GST_IS_VDP_OUTPUT_BUFFER (*output_buf))
167     goto invalid_buf;
168
169   return GST_FLOW_OK;
170
171 invalid_buf:
172   gst_buffer_unref (GST_BUFFER (*output_buf));
173   g_set_error (error, GST_STREAM_ERROR, GST_STREAM_ERROR_FAILED,
174       "Sink element returned buffer of wrong type");
175   return GST_FLOW_ERROR;
176 }
177
178 GstFlowReturn
179 gst_vdp_output_src_pad_alloc_buffer (GstVdpOutputSrcPad * vdp_pad,
180     GstVdpOutputBuffer ** output_buf, GError ** error)
181 {
182   GstCaps *caps;
183   GstFlowReturn ret;
184
185   g_return_val_if_fail (GST_IS_VDP_OUTPUT_SRC_PAD (vdp_pad), GST_FLOW_ERROR);
186
187   caps = GST_PAD_CAPS (vdp_pad);
188   if (!caps)
189     return GST_FLOW_NOT_NEGOTIATED;
190
191   switch (vdp_pad->output_format) {
192     case GST_VDP_OUTPUT_SRC_PAD_FORMAT_RGB:
193     {
194       ret = gst_vdp_output_src_pad_create_buffer (vdp_pad, output_buf, error);
195       if (ret != GST_FLOW_OK)
196         return ret;
197
198       break;
199     }
200
201     case GST_VDP_OUTPUT_SRC_PAD_FORMAT_VDPAU:
202     {
203       ret = gst_vdp_output_src_pad_alloc_with_caps (vdp_pad, caps, output_buf,
204           error);
205       if (ret != GST_FLOW_OK)
206         return ret;
207
208       break;
209     }
210
211     default:
212       g_assert_not_reached ();
213       break;
214   }
215
216   return GST_FLOW_OK;
217
218 }
219
220 static gboolean
221 gst_vdp_output_src_pad_acceptcaps (GstPad * pad, GstCaps * caps)
222 {
223   GstVdpOutputSrcPad *vdp_pad = GST_VDP_OUTPUT_SRC_PAD (pad);
224
225   if (!vdp_pad->lock_caps)
226     return TRUE;
227
228   return gst_caps_is_equal_fixed (caps, GST_PAD_CAPS (pad));
229 }
230
231 static gboolean
232 gst_vdp_output_src_pad_setcaps (GstPad * pad, GstCaps * caps)
233 {
234   GstVdpOutputSrcPad *vdp_pad = GST_VDP_OUTPUT_SRC_PAD (pad);
235   const GstStructure *structure;
236
237   structure = gst_caps_get_structure (caps, 0);
238
239   if (!gst_structure_get_int (structure, "width", &vdp_pad->width))
240     return FALSE;
241   if (!gst_structure_get_int (structure, "height", &vdp_pad->height))
242     return FALSE;
243
244   if (gst_structure_has_name (structure, "video/x-raw-rgb")) {
245     if (!gst_vdp_caps_to_rgba_format (caps, &vdp_pad->rgba_format))
246       return FALSE;
247
248     /* create buffer pool if we dont't have one */
249     if (!vdp_pad->bpool)
250       vdp_pad->bpool = gst_vdp_output_buffer_pool_new (vdp_pad->device);
251
252     if (vdp_pad->output_caps)
253       gst_caps_unref (vdp_pad->output_caps);
254
255     vdp_pad->output_caps = gst_caps_new_simple ("video/x-vdpau-output",
256         "rgba-format", G_TYPE_INT, vdp_pad->rgba_format,
257         "width", G_TYPE_INT, vdp_pad->width, "height", G_TYPE_INT,
258         vdp_pad->height, NULL);
259     gst_vdp_buffer_pool_set_caps (vdp_pad->bpool, vdp_pad->output_caps);
260
261     vdp_pad->output_format = GST_VDP_OUTPUT_SRC_PAD_FORMAT_RGB;
262   } else if (gst_structure_has_name (structure, "video/x-vdpau-output")) {
263     if (!gst_structure_get_int (structure, "rgba-format",
264             (gint *) & vdp_pad->rgba_format))
265       return FALSE;
266
267     /* don't need the buffer pool */
268     if (vdp_pad->bpool) {
269       gst_object_unref (vdp_pad->bpool);
270       vdp_pad->bpool = NULL;
271     }
272
273     vdp_pad->output_format = GST_VDP_OUTPUT_SRC_PAD_FORMAT_VDPAU;
274   } else
275     return FALSE;
276
277   return TRUE;
278 }
279
280 static GstCaps *
281 gst_vdp_output_src_pad_getcaps (GstPad * pad)
282 {
283   GstVdpOutputSrcPad *vdp_pad = (GstVdpOutputSrcPad *) pad;
284
285   const GstCaps *templ_caps;
286
287   if (vdp_pad->caps)
288     return gst_caps_ref (vdp_pad->caps);
289
290   else if ((templ_caps = gst_pad_get_pad_template_caps (pad)))
291     return gst_caps_copy (templ_caps);
292
293   return NULL;
294 }
295
296 static gboolean
297 gst_vdp_output_src_pad_activate_push (GstPad * pad, gboolean active)
298 {
299   GstVdpOutputSrcPad *vdp_pad = GST_VDP_OUTPUT_SRC_PAD (pad);
300
301   if (!active) {
302     if (vdp_pad->caps)
303       gst_caps_unref (vdp_pad->caps);
304     vdp_pad->caps = NULL;
305
306     if (vdp_pad->output_caps)
307       gst_caps_unref (vdp_pad->output_caps);
308     vdp_pad->output_caps = NULL;
309
310     if (vdp_pad->bpool)
311       g_object_unref (vdp_pad->bpool);
312     vdp_pad->bpool = NULL;
313
314     if (vdp_pad->device)
315       g_object_unref (vdp_pad->device);
316     vdp_pad->device = NULL;
317   }
318
319   return TRUE;
320 }
321
322 GstVdpOutputSrcPad *
323 gst_vdp_output_src_pad_new (GstPadTemplate * templ, const gchar * name)
324 {
325   return g_object_new (GST_TYPE_VDP_OUTPUT_SRC_PAD, "name", name,
326       "template", templ, "direction", GST_PAD_SRC, NULL);
327 }
328
329 static void
330 gst_vdp_output_src_pad_update_caps (GstVdpOutputSrcPad * vdp_pad)
331 {
332   GstCaps *caps;
333   const GstCaps *templ_caps;
334
335   if (vdp_pad->caps)
336     gst_caps_unref (vdp_pad->caps);
337
338   caps = gst_vdp_output_buffer_get_allowed_caps (vdp_pad->device);
339
340   if ((templ_caps = gst_pad_get_pad_template_caps (GST_PAD (vdp_pad)))) {
341     vdp_pad->caps = gst_caps_intersect (caps, templ_caps);
342     gst_caps_unref (caps);
343   } else
344     vdp_pad->caps = caps;
345 }
346
347 static void
348 gst_vdp_output_src_pad_get_property (GObject * object, guint prop_id,
349     GValue * value, GParamSpec * pspec)
350 {
351   GstVdpOutputSrcPad *vdp_pad = (GstVdpOutputSrcPad *) object;
352
353   switch (prop_id) {
354     case PROP_DEVICE:
355       g_value_set_object (value, vdp_pad->device);
356       break;
357
358     default:
359       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
360       break;
361   }
362 }
363
364 static void
365 gst_vdp_output_src_pad_set_property (GObject * object, guint prop_id,
366     const GValue * value, GParamSpec * pspec)
367 {
368   GstVdpOutputSrcPad *vdp_pad = (GstVdpOutputSrcPad *) object;
369
370   switch (prop_id) {
371     case PROP_DEVICE:
372       if (vdp_pad->device)
373         g_object_unref (vdp_pad->device);
374       vdp_pad->device = g_value_dup_object (value);
375       gst_vdp_output_src_pad_update_caps (vdp_pad);
376       break;
377
378     default:
379       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
380       break;
381   }
382 }
383
384 static void
385 gst_vdp_output_src_pad_init (GstVdpOutputSrcPad * vdp_pad)
386 {
387   GstPad *pad = GST_PAD (vdp_pad);
388
389   vdp_pad->caps = NULL;
390   vdp_pad->output_caps = NULL;
391   vdp_pad->bpool = NULL;
392   vdp_pad->device = NULL;
393
394   vdp_pad->lock_caps = FALSE;
395
396   gst_pad_set_getcaps_function (pad,
397       GST_DEBUG_FUNCPTR (gst_vdp_output_src_pad_getcaps));
398   gst_pad_set_setcaps_function (pad,
399       GST_DEBUG_FUNCPTR (gst_vdp_output_src_pad_setcaps));
400   gst_pad_set_acceptcaps_function (pad,
401       GST_DEBUG_FUNCPTR (gst_vdp_output_src_pad_acceptcaps));
402
403   gst_pad_set_activatepush_function (pad,
404       GST_DEBUG_FUNCPTR (gst_vdp_output_src_pad_activate_push));
405 }
406
407 static void
408 gst_vdp_output_src_pad_class_init (GstVdpOutputSrcPadClass * klass)
409 {
410   GObjectClass *object_class = G_OBJECT_CLASS (klass);
411
412   object_class->get_property = gst_vdp_output_src_pad_get_property;
413   object_class->set_property = gst_vdp_output_src_pad_set_property;
414
415   /**
416    * GstVdpVideoSrcPad:device:
417    *
418    * The #GstVdpDevice this pool is bound to.
419    */
420   g_object_class_install_property
421       (object_class,
422       PROP_DEVICE,
423       g_param_spec_object ("device",
424           "Device",
425           "The GstVdpDevice the pad should use",
426           GST_TYPE_VDP_DEVICE, G_PARAM_READWRITE));
427 }