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",
223 "Pixels to box at left (<0 = add a border)", G_MININT, G_MAXINT,
224 DEFAULT_LEFT, G_PARAM_READWRITE));
225 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_RIGHT,
226 g_param_spec_int ("right", "Right",
227 "Pixels to box at right (<0 = add a border)", G_MININT, G_MAXINT,
228 DEFAULT_RIGHT, G_PARAM_READWRITE));
229 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_TOP,
230 g_param_spec_int ("top", "Top",
231 "Pixels to box at top (<0 = add a border)", G_MININT, G_MAXINT,
232 DEFAULT_TOP, G_PARAM_READWRITE));
233 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BOTTOM,
234 g_param_spec_int ("bottom", "Bottom",
235 "Pixels to box at bottom (<0 = add a border)", G_MININT, G_MAXINT,
236 DEFAULT_BOTTOM, G_PARAM_READWRITE));
237 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_ALPHA,
238 g_param_spec_double ("alpha", "Alpha", "Alpha value picture", 0.0, 1.0,
239 DEFAULT_ALPHA, G_PARAM_READWRITE));
240 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BORDER_ALPHA,
241 g_param_spec_double ("border_alpha", "Border Alpha",
242 "Alpha value of the border", 0.0, 1.0, DEFAULT_BORDER_ALPHA,
245 gobject_class->set_property = gst_video_box_set_property;
246 gobject_class->get_property = gst_video_box_get_property;
248 gstelement_class->change_state = gst_video_box_change_state;
252 gst_video_box_init (GstVideoBox * video_box)
254 /* create the sink and src pads */
256 gst_pad_new_from_template (gst_static_pad_template_get
257 (&gst_video_box_sink_template), "sink");
258 gst_element_add_pad (GST_ELEMENT (video_box), video_box->sinkpad);
259 gst_pad_set_chain_function (video_box->sinkpad, gst_video_box_chain);
260 gst_pad_set_link_function (video_box->sinkpad, gst_video_box_sink_link);
263 gst_pad_new_from_template (gst_static_pad_template_get
264 (&gst_video_box_src_template), "src");
265 gst_element_add_pad (GST_ELEMENT (video_box), video_box->srcpad);
267 video_box->box_right = DEFAULT_RIGHT;
268 video_box->box_left = DEFAULT_LEFT;
269 video_box->box_top = DEFAULT_TOP;
270 video_box->box_bottom = DEFAULT_BOTTOM;
271 video_box->fill_type = DEFAULT_FILL_TYPE;
272 video_box->alpha = DEFAULT_ALPHA;
273 video_box->border_alpha = DEFAULT_BORDER_ALPHA;
275 GST_FLAG_SET (video_box, GST_ELEMENT_EVENT_AWARE);
278 /* do we need this function? */
280 gst_video_box_set_property (GObject * object, guint prop_id,
281 const GValue * value, GParamSpec * pspec)
283 GstVideoBox *video_box;
285 /* it's not null if we got it, but it might not be ours */
286 g_return_if_fail (GST_IS_VIDEO_BOX (object));
288 video_box = GST_VIDEO_BOX (object);
292 video_box->box_left = g_value_get_int (value);
293 if (video_box->box_left < 0) {
294 video_box->border_left = -video_box->box_left;
295 video_box->crop_left = 0;
297 video_box->border_left = 0;
298 video_box->crop_left = video_box->box_left;
302 video_box->box_right = g_value_get_int (value);
303 if (video_box->box_right < 0) {
304 video_box->border_right = -video_box->box_right;
305 video_box->crop_right = 0;
307 video_box->border_right = 0;
308 video_box->crop_right = video_box->box_right;
312 video_box->box_top = g_value_get_int (value);
313 if (video_box->box_top < 0) {
314 video_box->border_top = -video_box->box_top;
315 video_box->crop_top = 0;
317 video_box->border_top = 0;
318 video_box->crop_top = video_box->box_top;
322 video_box->box_bottom = g_value_get_int (value);
323 if (video_box->box_bottom < 0) {
324 video_box->border_bottom = -video_box->box_bottom;
325 video_box->crop_bottom = 0;
327 video_box->border_bottom = 0;
328 video_box->crop_bottom = video_box->box_bottom;
332 video_box->fill_type = g_value_get_enum (value);
335 video_box->alpha = g_value_get_double (value);
337 case ARG_BORDER_ALPHA:
338 video_box->border_alpha = g_value_get_double (value);
341 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
346 gst_video_box_get_property (GObject * object, guint prop_id, GValue * value,
349 GstVideoBox *video_box;
351 /* it's not null if we got it, but it might not be ours */
352 g_return_if_fail (GST_IS_VIDEO_BOX (object));
354 video_box = GST_VIDEO_BOX (object);
358 g_value_set_int (value, video_box->box_left);
361 g_value_set_int (value, video_box->box_right);
364 g_value_set_int (value, video_box->box_top);
367 g_value_set_int (value, video_box->box_bottom);
370 g_value_set_enum (value, video_box->fill_type);
373 g_value_set_double (value, video_box->alpha);
375 case ARG_BORDER_ALPHA:
376 g_value_set_double (value, video_box->border_alpha);
379 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
384 static GstPadLinkReturn
385 gst_video_box_sink_link (GstPad * pad, const GstCaps * caps)
387 GstVideoBox *video_box;
388 GstStructure *structure;
391 video_box = GST_VIDEO_BOX (gst_pad_get_parent (pad));
392 structure = gst_caps_get_structure (caps, 0);
394 ret = gst_structure_get_int (structure, "width", &video_box->in_width);
395 ret &= gst_structure_get_int (structure, "height", &video_box->in_height);
397 return GST_PAD_LINK_OK;
400 #define GST_VIDEO_I420_Y_OFFSET(width,height) (0)
401 #define GST_VIDEO_I420_U_OFFSET(width,height) ((width)*(height))
402 #define GST_VIDEO_I420_V_OFFSET(width,height) ((width)*(height) + ((width/2)*(height/2)))
404 #define GST_VIDEO_I420_Y_ROWSTRIDE(width) (width)
405 #define GST_VIDEO_I420_U_ROWSTRIDE(width) ((width)/2)
406 #define GST_VIDEO_I420_V_ROWSTRIDE(width) ((width)/2)
408 static int yuv_colors_Y[] = { 16, 150, 29 };
409 static int yuv_colors_U[] = { 128, 46, 255 };
410 static int yuv_colors_V[] = { 128, 21, 107 };
413 gst_video_box_i420 (GstVideoBox * video_box, guint8 * src, guint8 * dest)
415 guint8 *srcY, *srcU, *srcV;
416 guint8 *destY, *destU, *destV;
417 gint crop_width, crop_height;
418 gint out_width, out_height;
424 br = video_box->border_right;
425 bl = video_box->border_left;
426 bt = video_box->border_top;
427 bb = video_box->border_bottom;
429 out_width = video_box->out_width;
430 out_height = video_box->out_height;
432 destY = dest + GST_VIDEO_I420_Y_OFFSET (out_width, out_height);
435 src + GST_VIDEO_I420_Y_OFFSET (video_box->in_width, video_box->in_height);
436 src_stride = GST_VIDEO_I420_Y_ROWSTRIDE (video_box->in_width);
439 video_box->in_width - (video_box->crop_left + video_box->crop_right);
441 video_box->in_height - (video_box->crop_top + video_box->crop_bottom);
443 srcY += src_stride * video_box->crop_top + video_box->crop_left;
445 color1 = yuv_colors_Y[video_box->fill_type];
447 /* copy Y plane first */
448 for (j = 0; j < bt; j++) {
449 memset (destY, color1, out_width);
452 for (j = 0; j < crop_height; j++) {
453 memset (destY, color1, bl);
455 memcpy (destY, srcY, crop_width);
457 memset (destY, color1, br);
461 for (j = 0; j < bb; j++) {
462 memset (destY, color1, out_width);
466 src_stride = GST_VIDEO_I420_U_ROWSTRIDE (video_box->in_width);
468 destU = dest + GST_VIDEO_I420_U_OFFSET (out_width, out_height);
469 destV = dest + GST_VIDEO_I420_V_OFFSET (out_width, out_height);
481 src + GST_VIDEO_I420_U_OFFSET (video_box->in_width, video_box->in_height);
483 src + GST_VIDEO_I420_V_OFFSET (video_box->in_width, video_box->in_height);
484 srcU += src_stride * (video_box->crop_top / 2) + (video_box->crop_left / 2);
485 srcV += src_stride * (video_box->crop_top / 2) + (video_box->crop_left / 2);
487 color1 = yuv_colors_U[video_box->fill_type];
488 color2 = yuv_colors_V[video_box->fill_type];
490 for (j = 0; j < bt; j++) {
491 memset (destU, color1, out_width);
492 memset (destV, color2, out_width);
496 for (j = 0; j < crop_height; j++) {
497 memset (destU, color1, bl);
500 memcpy (destU, srcU, crop_width);
502 memset (destU, color1, br);
506 memset (destV, color2, bl);
509 memcpy (destV, srcV, crop_width);
511 memset (destV, color2, br);
515 for (j = 0; j < bb; j++) {
516 memset (destU, color1, out_width);
517 memset (destV, color2, out_width);
524 gst_video_box_ayuv (GstVideoBox * video_box, guint8 * src, guint8 * dest)
526 guint8 *srcY, *srcU, *srcV;
527 gint crop_width, crop_width2, crop_height;
528 gint out_width, out_height;
529 gint src_stride, src_stride2;
531 gint colorY, colorU, colorV;
533 guint8 b_alpha = (guint8) (video_box->border_alpha * 255);
534 guint8 i_alpha = (guint8) (video_box->alpha * 255);
535 guint32 *destp = (guint32 *) dest;
538 br = video_box->border_right;
539 bl = video_box->border_left;
540 bt = video_box->border_top;
541 bb = video_box->border_bottom;
543 out_width = video_box->out_width;
544 out_height = video_box->out_height;
546 src_stride = GST_VIDEO_I420_Y_ROWSTRIDE (video_box->in_width);
547 src_stride2 = src_stride / 2;
550 video_box->in_width - (video_box->crop_left + video_box->crop_right);
551 crop_width2 = crop_width / 2;
553 video_box->in_height - (video_box->crop_top + video_box->crop_bottom);
556 src + GST_VIDEO_I420_Y_OFFSET (video_box->in_width, video_box->in_height);
557 srcY += src_stride * video_box->crop_top + video_box->crop_left;
559 src + GST_VIDEO_I420_U_OFFSET (video_box->in_width, video_box->in_height);
560 srcU += src_stride2 * (video_box->crop_top / 2) + (video_box->crop_left / 2);
562 src + GST_VIDEO_I420_V_OFFSET (video_box->in_width, video_box->in_height);
563 srcV += src_stride2 * (video_box->crop_top / 2) + (video_box->crop_left / 2);
565 colorY = yuv_colors_Y[video_box->fill_type];
566 colorU = yuv_colors_U[video_box->fill_type];
567 colorV = yuv_colors_V[video_box->fill_type];
570 GUINT32_FROM_BE ((b_alpha << 24) | (colorY << 16) | (colorU << 8) |
574 for (i = 0; i < bt; i++) {
575 for (j = 0; j < out_width; j++) {
579 for (i = 0; i < crop_height; i++) {
581 for (j = 0; j < bl; j++) {
584 dest = (guint8 *) destp;
586 for (j = 0; j < crop_width2; j++) {
600 srcU += src_stride2 - crop_width2;
601 srcV += src_stride2 - crop_width2;
603 srcY += src_stride - crop_width;
605 destp = (guint32 *) dest;
607 for (j = 0; j < br; j++) {
612 for (i = 0; i < bb; i++) {
613 for (j = 0; j < out_width; j++) {
620 gst_video_box_chain (GstPad * pad, GstData * _data)
623 GstVideoBox *video_box;
625 gint new_width, new_height;
627 video_box = GST_VIDEO_BOX (gst_pad_get_parent (pad));
629 if (GST_IS_EVENT (_data)) {
630 GstEvent *event = GST_EVENT (_data);
632 switch (GST_EVENT_TYPE (event)) {
634 gst_pad_event_default (pad, event);
640 buffer = GST_BUFFER (_data);
643 video_box->in_width - (video_box->box_left + video_box->box_right);
645 video_box->in_height - (video_box->box_top + video_box->box_bottom);
647 if (new_width != video_box->out_width ||
648 new_height != video_box->out_height ||
649 !GST_PAD_CAPS (video_box->srcpad)) {
652 newcaps = gst_caps_copy (gst_pad_get_negotiated_caps (video_box->sinkpad));
654 video_box->use_alpha = TRUE;
657 gst_caps_set_simple (newcaps,
658 "format", GST_TYPE_FOURCC, GST_STR_FOURCC ("AYUV"),
659 "width", G_TYPE_INT, new_width, "height", G_TYPE_INT, new_height, NULL);
661 if (GST_PAD_LINK_FAILED (gst_pad_try_set_caps (video_box->srcpad, newcaps))) {
662 video_box->use_alpha = FALSE;
664 gst_caps_copy (gst_pad_get_negotiated_caps (video_box->sinkpad));
665 gst_caps_set_simple (newcaps, "format", GST_TYPE_FOURCC,
666 GST_STR_FOURCC ("I420"), "width", G_TYPE_INT, new_width, "height",
667 G_TYPE_INT, new_height, NULL);
669 if (GST_PAD_LINK_FAILED (gst_pad_try_set_caps (video_box->srcpad,
671 GST_ELEMENT_ERROR (video_box, CORE, NEGOTIATION, (NULL), (NULL));
676 video_box->out_width = new_width;
677 video_box->out_height = new_height;
680 if (video_box->use_alpha) {
681 outbuf = gst_pad_alloc_buffer (video_box->srcpad,
682 GST_BUFFER_OFFSET_NONE, new_width * new_height * 4);
684 gst_video_box_ayuv (video_box,
685 GST_BUFFER_DATA (buffer), GST_BUFFER_DATA (outbuf));
687 outbuf = gst_pad_alloc_buffer (video_box->srcpad,
688 GST_BUFFER_OFFSET_NONE, (new_width * new_height * 3) / 2);
690 gst_video_box_i420 (video_box,
691 GST_BUFFER_DATA (buffer), GST_BUFFER_DATA (outbuf));
693 GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buffer);
694 GST_BUFFER_DURATION (outbuf) = GST_BUFFER_DURATION (buffer);
697 gst_buffer_unref (buffer);
699 gst_pad_push (video_box->srcpad, GST_DATA (outbuf));
702 static GstElementStateReturn
703 gst_video_box_change_state (GstElement * element)
705 GstVideoBox *video_box;
707 video_box = GST_VIDEO_BOX (element);
709 switch (GST_STATE_TRANSITION (element)) {
710 case GST_STATE_NULL_TO_READY:
712 case GST_STATE_READY_TO_PAUSED:
714 case GST_STATE_PAUSED_TO_PLAYING:
716 case GST_STATE_PLAYING_TO_PAUSED:
718 case GST_STATE_PAUSED_TO_READY:
720 case GST_STATE_READY_TO_NULL:
724 parent_class->change_state (element);
726 return GST_STATE_SUCCESS;
730 plugin_init (GstPlugin * plugin)
732 return gst_element_register (plugin, "videobox", GST_RANK_NONE,
736 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
739 "resizes a video by adding borders or cropping",
740 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE, GST_ORIGIN)