2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) <2003> David Schleef <ds@schleef.org>
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 /*#define DEBUG_ENABLED */
27 #include "gstvideofilter.h"
31 /* GstVideofilter signals and args */
45 static void gst_videofilter_base_init (gpointer g_class);
46 static void gst_videofilter_class_init (gpointer g_class, gpointer class_data);
47 static void gst_videofilter_init (GTypeInstance * instance, gpointer g_class);
49 static void gst_videofilter_set_property (GObject * object, guint prop_id,
50 const GValue * value, GParamSpec * pspec);
51 static void gst_videofilter_get_property (GObject * object, guint prop_id,
52 GValue * value, GParamSpec * pspec);
54 static GstFlowReturn gst_videofilter_chain (GstPad * pad, GstBuffer * buffer);
55 GstCaps *gst_videofilter_class_get_capslist (GstVideofilterClass * klass);
56 static void gst_videofilter_setup (GstVideofilter * videofilter);
58 static GstElementClass *parent_class = NULL;
61 gst_videofilter_get_type (void)
63 static GType videofilter_type = 0;
65 if (!videofilter_type) {
66 static const GTypeInfo videofilter_info = {
67 sizeof (GstVideofilterClass),
68 gst_videofilter_base_init,
70 gst_videofilter_class_init,
73 sizeof (GstVideofilter),
78 videofilter_type = g_type_register_static (GST_TYPE_ELEMENT,
79 "GstVideofilter", &videofilter_info, G_TYPE_FLAG_ABSTRACT);
81 return videofilter_type;
85 gst_videofilter_base_init (gpointer g_class)
87 static GstElementDetails videofilter_details = {
89 "Filter/Effect/Video",
91 "David Schleef <ds@schleef.org>"
93 GstVideofilterClass *klass = (GstVideofilterClass *) g_class;
94 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
96 klass->formats = g_ptr_array_new ();
98 gst_element_class_set_details (element_class, &videofilter_details);
102 gst_videofilter_class_init (gpointer g_class, gpointer class_data)
104 GObjectClass *gobject_class;
105 GstElementClass *gstelement_class;
106 GstVideofilterClass *klass;
108 klass = (GstVideofilterClass *) g_class;
109 gobject_class = (GObjectClass *) klass;
110 gstelement_class = (GstElementClass *) klass;
112 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
114 gobject_class->set_property = gst_videofilter_set_property;
115 gobject_class->get_property = gst_videofilter_get_property;
118 static GstStructure *
119 gst_videofilter_format_get_structure (GstVideofilterFormat * format)
122 GstStructure *structure;
124 if (format->filter_func == NULL)
128 GST_MAKE_FOURCC (format->fourcc[0], format->fourcc[1], format->fourcc[2],
132 structure = gst_structure_new ("video/x-raw-rgb",
133 "depth", G_TYPE_INT, format->depth,
134 "bpp", G_TYPE_INT, format->bpp,
135 "endianness", G_TYPE_INT, format->endianness,
136 "red_mask", G_TYPE_INT, format->red_mask,
137 "green_mask", G_TYPE_INT, format->green_mask,
138 "blue_mask", G_TYPE_INT, format->blue_mask, NULL);
140 structure = gst_structure_new ("video/x-raw-yuv",
141 "format", GST_TYPE_FOURCC, fourcc, NULL);
144 gst_structure_set (structure,
145 "height", GST_TYPE_INT_RANGE, 1, G_MAXINT,
146 "width", GST_TYPE_INT_RANGE, 1, G_MAXINT,
147 "framerate", GST_TYPE_DOUBLE_RANGE, 0.0, G_MAXDOUBLE, NULL);
153 gst_videofilter_class_get_capslist (GstVideofilterClass * klass)
156 GstStructure *structure;
159 caps = gst_caps_new_empty ();
160 for (i = 0; i < klass->formats->len; i++) {
162 gst_videofilter_format_get_structure (g_ptr_array_index (klass->formats,
164 gst_caps_append_structure (caps, structure);
171 gst_videofilter_getcaps (GstPad * pad)
173 GstVideofilter *videofilter;
174 GstVideofilterClass *klass;
179 GST_DEBUG ("gst_videofilter_getcaps");
180 videofilter = GST_VIDEOFILTER (GST_PAD_PARENT (pad));
182 klass = GST_VIDEOFILTER_CLASS (G_OBJECT_GET_CLASS (videofilter));
184 /* we can handle anything that was registered */
185 caps = gst_caps_new_empty ();
186 for (i = 0; i < klass->formats->len; i++) {
190 gst_caps_new_full (gst_videofilter_format_get_structure
191 (g_ptr_array_index (klass->formats, i)), NULL);
193 gst_caps_append (caps, fromcaps);
196 peer = gst_pad_get_peer (pad);
200 peercaps = gst_pad_get_caps (peer);
204 icaps = gst_caps_intersect (peercaps, caps);
205 gst_caps_unref (peercaps);
206 gst_caps_unref (caps);
209 //gst_object_unref (peer);
216 gst_videofilter_setcaps (GstPad * pad, GstCaps * caps)
218 GstVideofilter *videofilter;
219 GstStructure *structure;
224 videofilter = GST_VIDEOFILTER (GST_PAD_PARENT (pad));
226 structure = gst_caps_get_structure (caps, 0);
228 videofilter->format =
229 gst_videofilter_find_format_by_structure (videofilter, structure);
230 g_return_val_if_fail (videofilter->format, GST_PAD_LINK_REFUSED);
232 ret = gst_structure_get_int (structure, "width", &width);
233 ret &= gst_structure_get_int (structure, "height", &height);
234 ret &= gst_structure_get_double (structure, "framerate", &framerate);
239 gst_pad_set_caps (videofilter->srcpad, caps);
241 GST_DEBUG ("width %d height %d", width, height);
244 if (pad == videofilter->srcpad) {
245 videofilter->to_width = width;
246 videofilter->to_height = height;
248 videofilter->from_width = width;
249 videofilter->from_height = height;
252 videofilter->to_width = width;
253 videofilter->to_height = height;
254 videofilter->from_width = width;
255 videofilter->from_height = height;
256 videofilter->framerate = framerate;
258 gst_videofilter_setup (videofilter);
264 gst_videofilter_init (GTypeInstance * instance, gpointer g_class)
266 GstVideofilter *videofilter = GST_VIDEOFILTER (instance);
267 GstPadTemplate *pad_template;
269 GST_DEBUG ("gst_videofilter_init");
272 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "sink");
273 g_return_if_fail (pad_template != NULL);
274 videofilter->sinkpad = gst_pad_new_from_template (pad_template, "sink");
275 gst_element_add_pad (GST_ELEMENT (videofilter), videofilter->sinkpad);
276 gst_pad_set_chain_function (videofilter->sinkpad, gst_videofilter_chain);
277 gst_pad_set_setcaps_function (videofilter->sinkpad, gst_videofilter_setcaps);
278 gst_pad_set_getcaps_function (videofilter->sinkpad, gst_videofilter_getcaps);
281 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "src");
282 g_return_if_fail (pad_template != NULL);
283 videofilter->srcpad = gst_pad_new_from_template (pad_template, "src");
284 gst_element_add_pad (GST_ELEMENT (videofilter), videofilter->srcpad);
285 gst_pad_set_getcaps_function (videofilter->srcpad, gst_videofilter_getcaps);
287 videofilter->inited = FALSE;
291 gst_videofilter_chain (GstPad * pad, GstBuffer * buf)
293 GstVideofilter *videofilter;
299 GST_DEBUG ("gst_videofilter_chain");
301 videofilter = GST_VIDEOFILTER (GST_PAD_PARENT (pad));
303 if (videofilter->passthru) {
304 return gst_pad_push (videofilter->srcpad, buf);
307 if (GST_PAD_CAPS (pad) == NULL) {
308 return GST_FLOW_NOT_NEGOTIATED;
311 data = GST_BUFFER_DATA (buf);
312 size = GST_BUFFER_SIZE (buf);
314 GST_DEBUG ("gst_videofilter_chain: got buffer of %ld bytes in '%s'", size,
315 GST_OBJECT_NAME (videofilter));
318 ("size=%ld from=%dx%d to=%dx%d fromsize=%ld (should be %d) tosize=%d",
319 size, videofilter->from_width, videofilter->from_height,
320 videofilter->to_width, videofilter->to_height, size,
321 videofilter->from_buf_size, videofilter->to_buf_size);
324 if (size > videofilter->from_buf_size) {
325 GST_INFO ("buffer size %ld larger than expected (%d)",
326 size, videofilter->from_buf_size);
327 return GST_FLOW_ERROR;
330 ret = gst_pad_alloc_buffer (videofilter->srcpad, GST_BUFFER_OFFSET_NONE,
331 videofilter->to_buf_size, GST_PAD_CAPS (videofilter->srcpad), &outbuf);
332 if (ret != GST_FLOW_OK)
335 GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf);
336 GST_BUFFER_DURATION (outbuf) = GST_BUFFER_DURATION (buf);
338 g_return_val_if_fail (videofilter->format, GST_FLOW_ERROR);
339 GST_DEBUG ("format %s", videofilter->format->fourcc);
341 videofilter->in_buf = buf;
342 videofilter->out_buf = outbuf;
344 videofilter->format->filter_func (videofilter, GST_BUFFER_DATA (outbuf),
346 gst_buffer_unref (buf);
348 GST_DEBUG ("gst_videofilter_chain: pushing buffer of %d bytes in '%s'",
349 GST_BUFFER_SIZE (outbuf), GST_OBJECT_NAME (videofilter));
351 ret = gst_pad_push (videofilter->srcpad, outbuf);
362 gst_videofilter_set_property (GObject * object, guint prop_id,
363 const GValue * value, GParamSpec * pspec)
367 g_return_if_fail (GST_IS_VIDEOFILTER (object));
368 src = GST_VIDEOFILTER (object);
370 GST_DEBUG ("gst_videofilter_set_property");
378 gst_videofilter_get_property (GObject * object, guint prop_id, GValue * value,
383 g_return_if_fail (GST_IS_VIDEOFILTER (object));
384 src = GST_VIDEOFILTER (object);
388 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
394 gst_videofilter_get_input_width (GstVideofilter * videofilter)
396 g_return_val_if_fail (GST_IS_VIDEOFILTER (videofilter), 0);
398 return videofilter->from_width;
402 gst_videofilter_get_input_height (GstVideofilter * videofilter)
404 g_return_val_if_fail (GST_IS_VIDEOFILTER (videofilter), 0);
406 return videofilter->from_height;
410 gst_videofilter_set_output_size (GstVideofilter * videofilter,
411 int width, int height)
414 GstStructure *structure;
416 g_return_if_fail (GST_IS_VIDEOFILTER (videofilter));
418 videofilter->to_width = width;
419 videofilter->to_height = height;
421 videofilter->to_buf_size = (videofilter->to_width * videofilter->to_height
422 * videofilter->format->bpp) / 8;
424 //srccaps = gst_caps_copy (gst_pad_get_negotiated_caps (videofilter->srcpad));
425 srccaps = gst_caps_copy (GST_PAD_CAPS (videofilter->srcpad));
426 structure = gst_caps_get_structure (srccaps, 0);
428 gst_structure_set (structure, "width", G_TYPE_INT, width,
429 "height", G_TYPE_INT, height, NULL);
431 gst_pad_set_caps (videofilter->srcpad, srccaps);
435 gst_videofilter_setup (GstVideofilter * videofilter)
437 GstVideofilterClass *klass;
439 klass = GST_VIDEOFILTER_CLASS (G_OBJECT_GET_CLASS (videofilter));
442 klass->setup (videofilter);
445 if (videofilter->to_width == 0) {
446 videofilter->to_width = videofilter->from_width;
448 if (videofilter->to_height == 0) {
449 videofilter->to_height = videofilter->from_height;
452 g_return_if_fail (videofilter->format != NULL);
453 g_return_if_fail (videofilter->from_width > 0);
454 g_return_if_fail (videofilter->from_height > 0);
455 g_return_if_fail (videofilter->to_width > 0);
456 g_return_if_fail (videofilter->to_height > 0);
458 videofilter->from_buf_size =
459 (videofilter->from_width * videofilter->from_height *
460 videofilter->format->bpp) / 8;
461 videofilter->to_buf_size =
462 (videofilter->to_width * videofilter->to_height *
463 videofilter->format->bpp) / 8;
465 videofilter->inited = TRUE;
468 GstVideofilterFormat *
469 gst_videofilter_find_format_by_structure (GstVideofilter * videofilter,
470 const GstStructure * structure)
473 GstVideofilterClass *klass;
474 GstVideofilterFormat *format;
477 klass = GST_VIDEOFILTER_CLASS (G_OBJECT_GET_CLASS (videofilter));
479 g_return_val_if_fail (structure != NULL, NULL);
481 if (strcmp (gst_structure_get_name (structure), "video/x-raw-yuv") == 0) {
484 ret = gst_structure_get_fourcc (structure, "format", &fourcc);
487 for (i = 0; i < klass->formats->len; i++) {
488 guint32 format_fourcc;
490 format = g_ptr_array_index (klass->formats, i);
491 format_fourcc = GST_STR_FOURCC (format->fourcc);
492 if (format->depth == 0 && format_fourcc == fourcc) {
496 } else if (strcmp (gst_structure_get_name (structure), "video/x-raw-rgb")
505 ret = gst_structure_get_int (structure, "bpp", &bpp);
506 ret &= gst_structure_get_int (structure, "depth", &depth);
507 ret &= gst_structure_get_int (structure, "endianness", &endianness);
508 ret &= gst_structure_get_int (structure, "red_mask", &red_mask);
509 ret &= gst_structure_get_int (structure, "green_mask", &green_mask);
510 ret &= gst_structure_get_int (structure, "blue_mask", &blue_mask);
513 for (i = 0; i < klass->formats->len; i++) {
514 format = g_ptr_array_index (klass->formats, i);
515 if (format->bpp == bpp && format->depth == depth &&
516 format->endianness == endianness && format->red_mask == red_mask &&
517 format->green_mask == green_mask && format->blue_mask == blue_mask) {
527 gst_videofilter_class_add_format (GstVideofilterClass * videofilterclass,
528 GstVideofilterFormat * format)
530 g_ptr_array_add (videofilterclass->formats, format);
534 gst_videofilter_class_add_pad_templates (GstVideofilterClass *
537 GstElementClass *element_class = GST_ELEMENT_CLASS (videofilter_class);
539 gst_element_class_add_pad_template (element_class,
540 gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
541 gst_videofilter_class_get_capslist (videofilter_class)));
543 gst_element_class_add_pad_template (element_class,
544 gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
545 gst_videofilter_class_get_capslist (videofilter_class)));
549 plugin_init (GstPlugin * plugin)
554 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
557 "Video filter parent class",
558 plugin_init, VERSION, "LGPL", GST_PACKAGE, GST_ORIGIN)