2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
24 #include <gst/video/video.h>
28 #define GST_TYPE_VIDEO_BOX \
29 (gst_video_box_get_type())
30 #define GST_VIDEO_BOX(obj) \
31 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_VIDEO_BOX,GstVideoBox))
32 #define GST_VIDEO_BOX_CLASS(klass) \
33 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_VIDEO_BOX,GstVideoBoxClass))
34 #define GST_IS_VIDEO_BOX(obj) \
35 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_VIDEO_BOX))
36 #define GST_IS_VIDEO_BOX_CLASS(obj) \
37 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_VIDEO_BOX))
39 typedef struct _GstVideoBox GstVideoBox;
40 typedef struct _GstVideoBoxClass GstVideoBoxClass;
59 gint in_width, in_height;
60 gint out_width, out_height;
62 gint box_left, box_right, box_top, box_bottom;
64 gint border_left, border_right, border_top, border_bottom;
65 gint crop_left, crop_right, crop_top, crop_bottom;
71 GstVideoBoxFill fill_type;
74 struct _GstVideoBoxClass
76 GstElementClass parent_class;
79 /* elementfactory information */
80 static GstElementDetails gst_video_box_details =
81 GST_ELEMENT_DETAILS ("video box filter",
82 "Filter/Effect/Video",
83 "Resizes a video by adding borders or cropping",
84 "Wim Taymans <wim@fluendo.com>");
87 /* VideoBox signals and args */
94 #define DEFAULT_LEFT 0
95 #define DEFAULT_RIGHT 0
97 #define DEFAULT_BOTTOM 0
98 #define DEFAULT_FILL_TYPE VIDEO_BOX_FILL_BLACK
99 #define DEFAULT_ALPHA 1.0
100 #define DEFAULT_BORDER_ALPHA 1.0
115 static GstStaticPadTemplate gst_video_box_src_template =
116 GST_STATIC_PAD_TEMPLATE ("src",
119 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("{ I420, AYUV }"))
122 static GstStaticPadTemplate gst_video_box_sink_template =
123 GST_STATIC_PAD_TEMPLATE ("sink",
126 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("I420"))
130 static void gst_video_box_base_init (gpointer g_class);
131 static void gst_video_box_class_init (GstVideoBoxClass * klass);
132 static void gst_video_box_init (GstVideoBox * video_box);
134 static void gst_video_box_set_property (GObject * object, guint prop_id,
135 const GValue * value, GParamSpec * pspec);
136 static void gst_video_box_get_property (GObject * object, guint prop_id,
137 GValue * value, GParamSpec * pspec);
139 static GstPadLinkReturn
140 gst_video_box_sink_link (GstPad * pad, const GstCaps * caps);
141 static void gst_video_box_chain (GstPad * pad, GstData * _data);
143 static GstElementStateReturn gst_video_box_change_state (GstElement * element);
146 static GstElementClass *parent_class = NULL;
148 #define GST_TYPE_VIDEO_BOX_FILL (gst_video_box_fill_get_type())
150 gst_video_box_fill_get_type (void)
152 static GType video_box_fill_type = 0;
153 static GEnumValue video_box_fill[] = {
154 {VIDEO_BOX_FILL_BLACK, "0", "Black"},
155 {VIDEO_BOX_FILL_GREEN, "1", "Colorkey green"},
156 {VIDEO_BOX_FILL_BLUE, "2", "Colorkey blue"},
160 if (!video_box_fill_type) {
161 video_box_fill_type =
162 g_enum_register_static ("GstVideoBoxFill", video_box_fill);
164 return video_box_fill_type;
167 /* static guint gst_video_box_signals[LAST_SIGNAL] = { 0 }; */
170 gst_video_box_get_type (void)
172 static GType video_box_type = 0;
174 if (!video_box_type) {
175 static const GTypeInfo video_box_info = {
176 sizeof (GstVideoBoxClass),
177 gst_video_box_base_init,
179 (GClassInitFunc) gst_video_box_class_init,
182 sizeof (GstVideoBox),
184 (GInstanceInitFunc) gst_video_box_init,
188 g_type_register_static (GST_TYPE_ELEMENT, "GstVideoBox",
191 return video_box_type;
195 gst_video_box_base_init (gpointer g_class)
197 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
199 gst_element_class_set_details (element_class, &gst_video_box_details);
201 gst_element_class_add_pad_template (element_class,
202 gst_static_pad_template_get (&gst_video_box_sink_template));
203 gst_element_class_add_pad_template (element_class,
204 gst_static_pad_template_get (&gst_video_box_src_template));
207 gst_video_box_class_init (GstVideoBoxClass * klass)
209 GObjectClass *gobject_class;
210 GstElementClass *gstelement_class;
212 gobject_class = (GObjectClass *) klass;
213 gstelement_class = (GstElementClass *) klass;
215 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
217 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FILL_TYPE,
218 g_param_spec_enum ("fill", "Fill", "How to fill the borders",
219 GST_TYPE_VIDEO_BOX_FILL, DEFAULT_FILL_TYPE,
220 (GParamFlags) G_PARAM_READWRITE));
221 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LEFT,
222 g_param_spec_int ("left", "Left", "Pixels to box at left",
223 G_MININT, G_MAXINT, DEFAULT_LEFT, G_PARAM_READWRITE));
224 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_RIGHT,
225 g_param_spec_int ("right", "Right", "Pixels to box at right",
226 G_MININT, G_MAXINT, DEFAULT_RIGHT, G_PARAM_READWRITE));
227 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_TOP,
228 g_param_spec_int ("top", "Top", "Pixels to box at top",
229 G_MININT, G_MAXINT, DEFAULT_TOP, G_PARAM_READWRITE));
230 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BOTTOM,
231 g_param_spec_int ("bottom", "Bottom", "Pixels to box at bottom",
232 G_MININT, G_MAXINT, DEFAULT_BOTTOM, G_PARAM_READWRITE));
233 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_ALPHA,
234 g_param_spec_double ("alpha", "Alpha", "Alpha value picture",
235 0.0, 1.0, DEFAULT_ALPHA, G_PARAM_READWRITE));
236 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BORDER_ALPHA,
237 g_param_spec_double ("border_alpha", "Border Alpha",
238 "Alpha value of the border", 0.0, 1.0, DEFAULT_BORDER_ALPHA,
241 gobject_class->set_property = gst_video_box_set_property;
242 gobject_class->get_property = gst_video_box_get_property;
244 gstelement_class->change_state = gst_video_box_change_state;
248 gst_video_box_init (GstVideoBox * video_box)
250 /* create the sink and src pads */
252 gst_pad_new_from_template (gst_static_pad_template_get
253 (&gst_video_box_sink_template), "sink");
254 gst_element_add_pad (GST_ELEMENT (video_box), video_box->sinkpad);
255 gst_pad_set_chain_function (video_box->sinkpad, gst_video_box_chain);
256 gst_pad_set_link_function (video_box->sinkpad, gst_video_box_sink_link);
259 gst_pad_new_from_template (gst_static_pad_template_get
260 (&gst_video_box_src_template), "src");
261 gst_element_add_pad (GST_ELEMENT (video_box), video_box->srcpad);
263 video_box->box_right = DEFAULT_RIGHT;
264 video_box->box_left = DEFAULT_LEFT;
265 video_box->box_top = DEFAULT_TOP;
266 video_box->box_bottom = DEFAULT_BOTTOM;
267 video_box->fill_type = DEFAULT_FILL_TYPE;
268 video_box->alpha = DEFAULT_ALPHA;
269 video_box->border_alpha = DEFAULT_BORDER_ALPHA;
271 GST_FLAG_SET (video_box, GST_ELEMENT_EVENT_AWARE);
274 /* do we need this function? */
276 gst_video_box_set_property (GObject * object, guint prop_id,
277 const GValue * value, GParamSpec * pspec)
279 GstVideoBox *video_box;
281 /* it's not null if we got it, but it might not be ours */
282 g_return_if_fail (GST_IS_VIDEO_BOX (object));
284 video_box = GST_VIDEO_BOX (object);
288 video_box->box_left = g_value_get_int (value);
289 if (video_box->box_left < 0) {
290 video_box->border_left = -video_box->box_left;
291 video_box->crop_left = 0;
293 video_box->border_left = 0;
294 video_box->crop_left = video_box->box_left;
298 video_box->box_right = g_value_get_int (value);
299 if (video_box->box_right < 0) {
300 video_box->border_right = -video_box->box_right;
301 video_box->crop_right = 0;
303 video_box->border_right = 0;
304 video_box->crop_right = video_box->box_right;
308 video_box->box_top = g_value_get_int (value);
309 if (video_box->box_top < 0) {
310 video_box->border_top = -video_box->box_top;
311 video_box->crop_top = 0;
313 video_box->border_top = 0;
314 video_box->crop_top = video_box->box_top;
318 video_box->box_bottom = g_value_get_int (value);
319 if (video_box->box_bottom < 0) {
320 video_box->border_bottom = -video_box->box_bottom;
321 video_box->crop_bottom = 0;
323 video_box->border_bottom = 0;
324 video_box->crop_bottom = video_box->box_bottom;
328 video_box->fill_type = g_value_get_enum (value);
331 video_box->alpha = g_value_get_double (value);
333 case ARG_BORDER_ALPHA:
334 video_box->border_alpha = g_value_get_double (value);
337 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
342 gst_video_box_get_property (GObject * object, guint prop_id, GValue * value,
345 GstVideoBox *video_box;
347 /* it's not null if we got it, but it might not be ours */
348 g_return_if_fail (GST_IS_VIDEO_BOX (object));
350 video_box = GST_VIDEO_BOX (object);
354 g_value_set_int (value, video_box->box_left);
357 g_value_set_int (value, video_box->box_right);
360 g_value_set_int (value, video_box->box_top);
363 g_value_set_int (value, video_box->box_bottom);
366 g_value_set_enum (value, video_box->fill_type);
369 g_value_set_double (value, video_box->alpha);
371 case ARG_BORDER_ALPHA:
372 g_value_set_double (value, video_box->border_alpha);
375 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
380 static GstPadLinkReturn
381 gst_video_box_sink_link (GstPad * pad, const GstCaps * caps)
383 GstVideoBox *video_box;
384 GstStructure *structure;
387 video_box = GST_VIDEO_BOX (gst_pad_get_parent (pad));
388 structure = gst_caps_get_structure (caps, 0);
390 ret = gst_structure_get_int (structure, "width", &video_box->in_width);
391 ret &= gst_structure_get_int (structure, "height", &video_box->in_height);
393 return GST_PAD_LINK_OK;
396 #define GST_VIDEO_I420_Y_OFFSET(width,height) (0)
397 #define GST_VIDEO_I420_U_OFFSET(width,height) ((width)*(height))
398 #define GST_VIDEO_I420_V_OFFSET(width,height) ((width)*(height) + ((width/2)*(height/2)))
400 #define GST_VIDEO_I420_Y_ROWSTRIDE(width) (width)
401 #define GST_VIDEO_I420_U_ROWSTRIDE(width) ((width)/2)
402 #define GST_VIDEO_I420_V_ROWSTRIDE(width) ((width)/2)
404 static int yuv_colors_Y[] = { 16, 150, 29 };
405 static int yuv_colors_U[] = { 128, 46, 255 };
406 static int yuv_colors_V[] = { 128, 21, 107 };
409 gst_video_box_i420 (GstVideoBox * video_box, guint8 * src, guint8 * dest)
411 guint8 *srcY, *srcU, *srcV;
412 guint8 *destY, *destU, *destV;
413 gint crop_width, crop_height;
414 gint out_width, out_height;
420 br = video_box->border_right;
421 bl = video_box->border_left;
422 bt = video_box->border_top;
423 bb = video_box->border_bottom;
425 out_width = video_box->out_width;
426 out_height = video_box->out_height;
428 destY = dest + GST_VIDEO_I420_Y_OFFSET (out_width, out_height);
431 src + GST_VIDEO_I420_Y_OFFSET (video_box->in_width, video_box->in_height);
432 src_stride = GST_VIDEO_I420_Y_ROWSTRIDE (video_box->in_width);
435 video_box->in_width - (video_box->crop_left + video_box->crop_right);
437 video_box->in_height - (video_box->crop_top + video_box->crop_bottom);
439 srcY += src_stride * video_box->crop_top + video_box->crop_left;
441 color1 = yuv_colors_Y[video_box->fill_type];
443 /* copy Y plane first */
444 for (j = 0; j < bt; j++) {
445 memset (destY, color1, out_width);
448 for (j = 0; j < crop_height; j++) {
449 memset (destY, color1, bl);
451 memcpy (destY, srcY, crop_width);
453 memset (destY, color1, br);
457 for (j = 0; j < bb; j++) {
458 memset (destY, color1, out_width);
462 src_stride = GST_VIDEO_I420_U_ROWSTRIDE (video_box->in_width);
464 destU = dest + GST_VIDEO_I420_U_OFFSET (out_width, out_height);
465 destV = dest + GST_VIDEO_I420_V_OFFSET (out_width, out_height);
477 src + GST_VIDEO_I420_U_OFFSET (video_box->in_width, video_box->in_height);
479 src + GST_VIDEO_I420_V_OFFSET (video_box->in_width, video_box->in_height);
480 srcU += src_stride * (video_box->crop_top / 2) + (video_box->crop_left / 2);
481 srcV += src_stride * (video_box->crop_top / 2) + (video_box->crop_left / 2);
483 color1 = yuv_colors_U[video_box->fill_type];
484 color2 = yuv_colors_V[video_box->fill_type];
486 for (j = 0; j < bt; j++) {
487 memset (destU, color1, out_width);
488 memset (destV, color2, out_width);
492 for (j = 0; j < crop_height; j++) {
493 memset (destU, color1, bl);
496 memcpy (destU, srcU, crop_width);
498 memset (destU, color1, br);
502 memset (destV, color2, bl);
505 memcpy (destV, srcV, crop_width);
507 memset (destV, color2, br);
511 for (j = 0; j < bb; j++) {
512 memset (destU, color1, out_width);
513 memset (destV, color2, out_width);
520 gst_video_box_ayuv (GstVideoBox * video_box, guint8 * src, guint8 * dest)
522 guint8 *srcY, *srcU, *srcV;
523 gint crop_width, crop_width2, crop_height;
524 gint out_width, out_height;
525 gint src_stride, src_stride2;
527 gint colorY, colorU, colorV;
529 guint8 b_alpha = (guint8) (video_box->border_alpha * 255);
530 guint8 i_alpha = (guint8) (video_box->alpha * 255);
531 guint32 *destp = (guint32 *) dest;
534 br = video_box->border_right;
535 bl = video_box->border_left;
536 bt = video_box->border_top;
537 bb = video_box->border_bottom;
539 out_width = video_box->out_width;
540 out_height = video_box->out_height;
542 src_stride = GST_VIDEO_I420_Y_ROWSTRIDE (video_box->in_width);
543 src_stride2 = src_stride / 2;
546 video_box->in_width - (video_box->crop_left + video_box->crop_right);
547 crop_width2 = crop_width / 2;
549 video_box->in_height - (video_box->crop_top + video_box->crop_bottom);
552 src + GST_VIDEO_I420_Y_OFFSET (video_box->in_width, video_box->in_height);
553 srcY += src_stride * video_box->crop_top + video_box->crop_left;
555 src + GST_VIDEO_I420_U_OFFSET (video_box->in_width, video_box->in_height);
556 srcU += src_stride2 * (video_box->crop_top / 2) + (video_box->crop_left / 2);
558 src + GST_VIDEO_I420_V_OFFSET (video_box->in_width, video_box->in_height);
559 srcV += src_stride2 * (video_box->crop_top / 2) + (video_box->crop_left / 2);
561 colorY = yuv_colors_Y[video_box->fill_type];
562 colorU = yuv_colors_U[video_box->fill_type];
563 colorV = yuv_colors_V[video_box->fill_type];
566 GUINT32_FROM_BE ((b_alpha << 24) | (colorY << 16) | (colorU << 8) |
570 for (i = 0; i < bt; i++) {
571 for (j = 0; j < out_width; j++) {
575 for (i = 0; i < crop_height; i++) {
577 for (j = 0; j < bl; j++) {
580 dest = (guint8 *) destp;
582 for (j = 0; j < crop_width2; j++) {
596 srcU += src_stride2 - crop_width2;
597 srcV += src_stride2 - crop_width2;
599 srcY += src_stride - crop_width;
601 destp = (guint32 *) dest;
603 for (j = 0; j < br; j++) {
608 for (i = 0; i < bb; i++) {
609 for (j = 0; j < out_width; j++) {
616 gst_video_box_chain (GstPad * pad, GstData * _data)
619 GstVideoBox *video_box;
621 gint new_width, new_height;
623 video_box = GST_VIDEO_BOX (gst_pad_get_parent (pad));
625 if (GST_IS_EVENT (_data)) {
626 GstEvent *event = GST_EVENT (_data);
628 switch (GST_EVENT_TYPE (event)) {
630 gst_pad_event_default (pad, event);
636 buffer = GST_BUFFER (_data);
639 video_box->in_width - (video_box->box_left + video_box->box_right);
641 video_box->in_height - (video_box->box_top + video_box->box_bottom);
643 if (new_width != video_box->out_width ||
644 new_height != video_box->out_height ||
645 !GST_PAD_CAPS (video_box->srcpad)) {
648 newcaps = gst_caps_copy (gst_pad_get_negotiated_caps (video_box->sinkpad));
650 video_box->use_alpha = TRUE;
653 gst_caps_set_simple (newcaps,
654 "format", GST_TYPE_FOURCC, GST_STR_FOURCC ("AYUV"),
655 "width", G_TYPE_INT, new_width, "height", G_TYPE_INT, new_height, NULL);
657 if (GST_PAD_LINK_FAILED (gst_pad_try_set_caps (video_box->srcpad, newcaps))) {
658 video_box->use_alpha = FALSE;
660 gst_caps_copy (gst_pad_get_negotiated_caps (video_box->sinkpad));
661 gst_caps_set_simple (newcaps, "format", GST_TYPE_FOURCC,
662 GST_STR_FOURCC ("I420"), "width", G_TYPE_INT, new_width, "height",
663 G_TYPE_INT, new_height, NULL);
665 if (GST_PAD_LINK_FAILED (gst_pad_try_set_caps (video_box->srcpad,
667 GST_ELEMENT_ERROR (video_box, CORE, NEGOTIATION, (NULL), (NULL));
672 video_box->out_width = new_width;
673 video_box->out_height = new_height;
676 if (video_box->use_alpha) {
677 outbuf = gst_buffer_new_and_alloc (new_width * new_height * 4);
679 gst_video_box_ayuv (video_box,
680 GST_BUFFER_DATA (buffer), GST_BUFFER_DATA (outbuf));
682 outbuf = gst_buffer_new_and_alloc ((new_width * new_height * 3) / 2);
684 gst_video_box_i420 (video_box,
685 GST_BUFFER_DATA (buffer), GST_BUFFER_DATA (outbuf));
687 GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buffer);
688 GST_BUFFER_DURATION (outbuf) = GST_BUFFER_DURATION (buffer);
691 gst_buffer_unref (buffer);
693 gst_pad_push (video_box->srcpad, GST_DATA (outbuf));
696 static GstElementStateReturn
697 gst_video_box_change_state (GstElement * element)
699 GstVideoBox *video_box;
701 video_box = GST_VIDEO_BOX (element);
703 switch (GST_STATE_TRANSITION (element)) {
704 case GST_STATE_NULL_TO_READY:
706 case GST_STATE_READY_TO_PAUSED:
708 case GST_STATE_PAUSED_TO_PLAYING:
710 case GST_STATE_PLAYING_TO_PAUSED:
712 case GST_STATE_PAUSED_TO_READY:
714 case GST_STATE_READY_TO_NULL:
718 parent_class->change_state (element);
720 return GST_STATE_SUCCESS;
724 plugin_init (GstPlugin * plugin)
726 return gst_element_register (plugin, "videobox", GST_RANK_NONE,
730 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
733 "resizes a video by adding borders or cropping",
734 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE, GST_ORIGIN)