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 #define DEFAULT_LEFT 0
88 #define DEFAULT_RIGHT 0
90 #define DEFAULT_BOTTOM 0
91 #define DEFAULT_FILL_TYPE VIDEO_BOX_FILL_BLACK
92 #define DEFAULT_ALPHA 1.0
93 #define DEFAULT_BORDER_ALPHA 1.0
108 static GstStaticPadTemplate gst_video_box_src_template =
109 GST_STATIC_PAD_TEMPLATE ("src",
112 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("{ I420, AYUV }"))
115 static GstStaticPadTemplate gst_video_box_sink_template =
116 GST_STATIC_PAD_TEMPLATE ("sink",
119 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("I420"))
123 static void gst_video_box_base_init (gpointer g_class);
124 static void gst_video_box_class_init (GstVideoBoxClass * klass);
125 static void gst_video_box_init (GstVideoBox * video_box);
127 static void gst_video_box_set_property (GObject * object, guint prop_id,
128 const GValue * value, GParamSpec * pspec);
129 static void gst_video_box_get_property (GObject * object, guint prop_id,
130 GValue * value, GParamSpec * pspec);
132 static gboolean gst_video_box_sink_setcaps (GstPad * pad, GstCaps * caps);
133 static GstFlowReturn gst_video_box_chain (GstPad * pad, GstBuffer * buffer);
135 static GstElementStateReturn gst_video_box_change_state (GstElement * element);
138 static GstElementClass *parent_class = NULL;
140 #define GST_TYPE_VIDEO_BOX_FILL (gst_video_box_fill_get_type())
142 gst_video_box_fill_get_type (void)
144 static GType video_box_fill_type = 0;
145 static GEnumValue video_box_fill[] = {
146 {VIDEO_BOX_FILL_BLACK, "0", "Black"},
147 {VIDEO_BOX_FILL_GREEN, "1", "Colorkey green"},
148 {VIDEO_BOX_FILL_BLUE, "2", "Colorkey blue"},
152 if (!video_box_fill_type) {
153 video_box_fill_type =
154 g_enum_register_static ("GstVideoBoxFill", video_box_fill);
156 return video_box_fill_type;
159 /* static guint gst_video_box_signals[LAST_SIGNAL] = { 0 }; */
162 gst_video_box_get_type (void)
164 static GType video_box_type = 0;
166 if (!video_box_type) {
167 static const GTypeInfo video_box_info = {
168 sizeof (GstVideoBoxClass),
169 gst_video_box_base_init,
171 (GClassInitFunc) gst_video_box_class_init,
174 sizeof (GstVideoBox),
176 (GInstanceInitFunc) gst_video_box_init,
180 g_type_register_static (GST_TYPE_ELEMENT, "GstVideoBox",
183 return video_box_type;
187 gst_video_box_base_init (gpointer g_class)
189 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
191 gst_element_class_set_details (element_class, &gst_video_box_details);
193 gst_element_class_add_pad_template (element_class,
194 gst_static_pad_template_get (&gst_video_box_sink_template));
195 gst_element_class_add_pad_template (element_class,
196 gst_static_pad_template_get (&gst_video_box_src_template));
199 gst_video_box_class_init (GstVideoBoxClass * klass)
201 GObjectClass *gobject_class;
202 GstElementClass *gstelement_class;
204 gobject_class = (GObjectClass *) klass;
205 gstelement_class = (GstElementClass *) klass;
207 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
209 gobject_class->set_property = gst_video_box_set_property;
210 gobject_class->get_property = gst_video_box_get_property;
212 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FILL_TYPE,
213 g_param_spec_enum ("fill", "Fill", "How to fill the borders",
214 GST_TYPE_VIDEO_BOX_FILL, DEFAULT_FILL_TYPE,
215 (GParamFlags) G_PARAM_READWRITE));
216 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LEFT,
217 g_param_spec_int ("left", "Left",
218 "Pixels to box at left (<0 = add a border)", G_MININT, G_MAXINT,
219 DEFAULT_LEFT, G_PARAM_READWRITE));
220 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_RIGHT,
221 g_param_spec_int ("right", "Right",
222 "Pixels to box at right (<0 = add a border)", G_MININT, G_MAXINT,
223 DEFAULT_RIGHT, G_PARAM_READWRITE));
224 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_TOP,
225 g_param_spec_int ("top", "Top",
226 "Pixels to box at top (<0 = add a border)", G_MININT, G_MAXINT,
227 DEFAULT_TOP, G_PARAM_READWRITE));
228 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BOTTOM,
229 g_param_spec_int ("bottom", "Bottom",
230 "Pixels to box at bottom (<0 = add a border)", G_MININT, G_MAXINT,
231 DEFAULT_BOTTOM, G_PARAM_READWRITE));
232 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_ALPHA,
233 g_param_spec_double ("alpha", "Alpha", "Alpha value picture", 0.0, 1.0,
234 DEFAULT_ALPHA, G_PARAM_READWRITE));
235 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BORDER_ALPHA,
236 g_param_spec_double ("border_alpha", "Border Alpha",
237 "Alpha value of the border", 0.0, 1.0, DEFAULT_BORDER_ALPHA,
240 gstelement_class->change_state = gst_video_box_change_state;
244 gst_video_box_init (GstVideoBox * video_box)
246 /* create the sink and src pads */
248 gst_pad_new_from_template (gst_static_pad_template_get
249 (&gst_video_box_sink_template), "sink");
250 gst_element_add_pad (GST_ELEMENT (video_box), video_box->sinkpad);
251 gst_pad_set_chain_function (video_box->sinkpad, gst_video_box_chain);
252 gst_pad_set_setcaps_function (video_box->sinkpad, gst_video_box_sink_setcaps);
255 gst_pad_new_from_template (gst_static_pad_template_get
256 (&gst_video_box_src_template), "src");
257 gst_element_add_pad (GST_ELEMENT (video_box), video_box->srcpad);
259 video_box->box_right = DEFAULT_RIGHT;
260 video_box->box_left = DEFAULT_LEFT;
261 video_box->box_top = DEFAULT_TOP;
262 video_box->box_bottom = DEFAULT_BOTTOM;
263 video_box->fill_type = DEFAULT_FILL_TYPE;
264 video_box->alpha = DEFAULT_ALPHA;
265 video_box->border_alpha = DEFAULT_BORDER_ALPHA;
268 /* do we need this function? */
270 gst_video_box_set_property (GObject * object, guint prop_id,
271 const GValue * value, GParamSpec * pspec)
273 GstVideoBox *video_box;
275 /* it's not null if we got it, but it might not be ours */
276 g_return_if_fail (GST_IS_VIDEO_BOX (object));
278 video_box = GST_VIDEO_BOX (object);
282 video_box->box_left = g_value_get_int (value);
283 if (video_box->box_left < 0) {
284 video_box->border_left = -video_box->box_left;
285 video_box->crop_left = 0;
287 video_box->border_left = 0;
288 video_box->crop_left = video_box->box_left;
292 video_box->box_right = g_value_get_int (value);
293 if (video_box->box_right < 0) {
294 video_box->border_right = -video_box->box_right;
295 video_box->crop_right = 0;
297 video_box->border_right = 0;
298 video_box->crop_right = video_box->box_right;
302 video_box->box_top = g_value_get_int (value);
303 if (video_box->box_top < 0) {
304 video_box->border_top = -video_box->box_top;
305 video_box->crop_top = 0;
307 video_box->border_top = 0;
308 video_box->crop_top = video_box->box_top;
312 video_box->box_bottom = g_value_get_int (value);
313 if (video_box->box_bottom < 0) {
314 video_box->border_bottom = -video_box->box_bottom;
315 video_box->crop_bottom = 0;
317 video_box->border_bottom = 0;
318 video_box->crop_bottom = video_box->box_bottom;
322 video_box->fill_type = g_value_get_enum (value);
325 video_box->alpha = g_value_get_double (value);
327 case ARG_BORDER_ALPHA:
328 video_box->border_alpha = g_value_get_double (value);
331 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
336 gst_video_box_get_property (GObject * object, guint prop_id, GValue * value,
339 GstVideoBox *video_box;
341 /* it's not null if we got it, but it might not be ours */
342 g_return_if_fail (GST_IS_VIDEO_BOX (object));
344 video_box = GST_VIDEO_BOX (object);
348 g_value_set_int (value, video_box->box_left);
351 g_value_set_int (value, video_box->box_right);
354 g_value_set_int (value, video_box->box_top);
357 g_value_set_int (value, video_box->box_bottom);
360 g_value_set_enum (value, video_box->fill_type);
363 g_value_set_double (value, video_box->alpha);
365 case ARG_BORDER_ALPHA:
366 g_value_set_double (value, video_box->border_alpha);
369 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
375 gst_video_box_sink_setcaps (GstPad * pad, GstCaps * caps)
377 GstVideoBox *video_box;
378 GstStructure *structure;
381 video_box = GST_VIDEO_BOX (GST_PAD_PARENT (pad));
382 structure = gst_caps_get_structure (caps, 0);
384 ret = gst_structure_get_int (structure, "width", &video_box->in_width);
385 ret &= gst_structure_get_int (structure, "height", &video_box->in_height);
390 #define ROUND_UP_2(x) (((x)+1)&~1)
391 #define ROUND_UP_4(x) (((x)+3)&~3)
392 #define ROUND_UP_8(x) (((x)+7)&~7)
394 /* see gst-plugins/gst/games/gstvideoimage.c, paint_setup_I420() */
395 #define GST_VIDEO_I420_Y_ROWSTRIDE(width) (ROUND_UP_4(width))
396 #define GST_VIDEO_I420_U_ROWSTRIDE(width) (ROUND_UP_8(width)/2)
397 #define GST_VIDEO_I420_V_ROWSTRIDE(width) ((ROUND_UP_8(GST_VIDEO_I420_Y_ROWSTRIDE(width)))/2)
399 #define GST_VIDEO_I420_Y_OFFSET(w,h) (0)
400 #define GST_VIDEO_I420_U_OFFSET(w,h) (GST_VIDEO_I420_Y_OFFSET(w,h)+(GST_VIDEO_I420_Y_ROWSTRIDE(w)*ROUND_UP_2(h)))
401 #define GST_VIDEO_I420_V_OFFSET(w,h) (GST_VIDEO_I420_U_OFFSET(w,h)+(GST_VIDEO_I420_U_ROWSTRIDE(w)*ROUND_UP_2(h)/2))
403 #define GST_VIDEO_I420_SIZE(w,h) (GST_VIDEO_I420_V_OFFSET(w,h)+(GST_VIDEO_I420_V_ROWSTRIDE(w)*ROUND_UP_2(h)/2))
405 static int yuv_colors_Y[] = { 16, 150, 29 };
406 static int yuv_colors_U[] = { 128, 46, 255 };
407 static int yuv_colors_V[] = { 128, 21, 107 };
410 gst_video_box_copy_plane_i420 (GstVideoBox * video_box, guint8 * src,
411 guint8 * dest, gint br, gint bl, gint bt, gint bb, gint src_crop_width,
412 gint src_crop_height, gint src_stride, gint dest_width, gint dest_stride,
418 for (j = 0; j < bt; j++) {
419 memset (dest, fill_color, dest_width);
423 /* copy and add left and right border */
424 for (j = 0; j < src_crop_height; j++) {
425 memset (dest, fill_color, bl);
426 memcpy (dest + bl, src, src_crop_width);
427 memset (dest + bl + src_crop_width, fill_color, br);
433 for (j = 0; j < bb; j++) {
434 memset (dest, fill_color, dest_width);
440 gst_video_box_i420 (GstVideoBox * video_box, guint8 * src, guint8 * dest)
442 guint8 *srcY, *srcU, *srcV;
443 guint8 *destY, *destU, *destV;
444 gint crop_width, crop_height;
445 gint out_width, out_height;
446 gint src_width, src_height;
447 gint src_stride, dest_stride;
450 br = video_box->border_right;
451 bl = video_box->border_left;
452 bt = video_box->border_top;
453 bb = video_box->border_bottom;
455 out_width = video_box->out_width;
456 out_height = video_box->out_height;
458 src_width = video_box->in_width;
459 src_height = video_box->in_height;
461 crop_width = src_width - (video_box->crop_left + video_box->crop_right);
462 crop_height = src_height - (video_box->crop_top + video_box->crop_bottom);
465 src_stride = GST_VIDEO_I420_Y_ROWSTRIDE (src_width);
466 dest_stride = GST_VIDEO_I420_Y_ROWSTRIDE (out_width);
468 destY = dest + GST_VIDEO_I420_Y_OFFSET (out_width, out_height);
470 srcY = src + GST_VIDEO_I420_Y_OFFSET (src_width, src_height);
471 srcY += src_stride * video_box->crop_top + video_box->crop_left;
473 gst_video_box_copy_plane_i420 (video_box, srcY, destY, br, bl, bt, bb,
474 crop_width, crop_height, src_stride, out_width, dest_stride,
475 yuv_colors_Y[video_box->fill_type]);
478 src_stride = GST_VIDEO_I420_U_ROWSTRIDE (src_width);
479 dest_stride = GST_VIDEO_I420_U_ROWSTRIDE (out_width);
481 destU = dest + GST_VIDEO_I420_U_OFFSET (out_width, out_height);
483 srcU = src + GST_VIDEO_I420_U_OFFSET (src_width, src_height);
484 srcU += src_stride * (video_box->crop_top / 2) + (video_box->crop_left / 2);
486 gst_video_box_copy_plane_i420 (video_box, srcU, destU, br / 2, bl / 2, bt / 2,
487 bb / 2, crop_width / 2, crop_height / 2, src_stride, out_width / 2,
488 dest_stride, yuv_colors_U[video_box->fill_type]);
491 src_stride = GST_VIDEO_I420_V_ROWSTRIDE (src_width);
492 dest_stride = GST_VIDEO_I420_V_ROWSTRIDE (out_width);
494 destV = dest + GST_VIDEO_I420_V_OFFSET (out_width, out_height);
496 srcV = src + GST_VIDEO_I420_V_OFFSET (src_width, src_height);
497 srcV += src_stride * (video_box->crop_top / 2) + (video_box->crop_left / 2);
499 gst_video_box_copy_plane_i420 (video_box, srcV, destV, br / 2, bl / 2, bt / 2,
500 bb / 2, crop_width / 2, crop_height / 2, src_stride, out_width / 2,
501 dest_stride, yuv_colors_V[video_box->fill_type]);
504 /* Note the source image is always I420, we
505 * are converting to AYUV on the fly here */
508 gst_video_box_ayuv (GstVideoBox * video_box, guint8 * src, guint8 * dest)
510 guint8 *srcY, *srcU, *srcV;
511 gint crop_width, crop_width2, crop_height;
512 gint out_width, out_height;
513 gint src_stride, src_stride2;
515 gint colorY, colorU, colorV;
517 guint8 b_alpha = (guint8) (video_box->border_alpha * 255);
518 guint8 i_alpha = (guint8) (video_box->alpha * 255);
519 guint32 *destp = (guint32 *) dest;
522 br = video_box->border_right;
523 bl = video_box->border_left;
524 bt = video_box->border_top;
525 bb = video_box->border_bottom;
527 out_width = video_box->out_width;
528 out_height = video_box->out_height;
530 src_stride = GST_VIDEO_I420_Y_ROWSTRIDE (video_box->in_width);
531 src_stride2 = src_stride / 2;
534 video_box->in_width - (video_box->crop_left + video_box->crop_right);
535 crop_width2 = crop_width / 2;
537 video_box->in_height - (video_box->crop_top + video_box->crop_bottom);
540 src + GST_VIDEO_I420_Y_OFFSET (video_box->in_width, video_box->in_height);
541 srcY += src_stride * video_box->crop_top + video_box->crop_left;
543 src + GST_VIDEO_I420_U_OFFSET (video_box->in_width, video_box->in_height);
544 srcU += src_stride2 * (video_box->crop_top / 2) + (video_box->crop_left / 2);
546 src + GST_VIDEO_I420_V_OFFSET (video_box->in_width, video_box->in_height);
547 srcV += src_stride2 * (video_box->crop_top / 2) + (video_box->crop_left / 2);
549 colorY = yuv_colors_Y[video_box->fill_type];
550 colorU = yuv_colors_U[video_box->fill_type];
551 colorV = yuv_colors_V[video_box->fill_type];
554 GUINT32_FROM_BE ((b_alpha << 24) | (colorY << 16) | (colorU << 8) |
558 for (i = 0; i < bt; i++) {
559 for (j = 0; j < out_width; j++) {
563 for (i = 0; i < crop_height; i++) {
565 for (j = 0; j < bl; j++) {
568 dest = (guint8 *) destp;
570 for (j = 0; j < crop_width2; j++) {
584 srcU += src_stride2 - crop_width2;
585 srcV += src_stride2 - crop_width2;
587 srcY += src_stride - crop_width;
589 destp = (guint32 *) dest;
591 for (j = 0; j < br; j++) {
596 for (i = 0; i < bb; i++) {
597 for (j = 0; j < out_width; j++) {
604 gst_video_box_chain (GstPad * pad, GstBuffer * buffer)
606 GstVideoBox *video_box;
608 gint new_width, new_height;
611 video_box = GST_VIDEO_BOX (gst_pad_get_parent (pad));
614 video_box->in_width - (video_box->box_left + video_box->box_right);
616 video_box->in_height - (video_box->box_top + video_box->box_bottom);
618 if (new_width != video_box->out_width ||
619 new_height != video_box->out_height ||
620 !GST_PAD_CAPS (video_box->srcpad)) {
623 newcaps = gst_caps_copy (gst_pad_get_negotiated_caps (video_box->sinkpad));
625 video_box->use_alpha = TRUE;
628 gst_caps_set_simple (newcaps,
629 "format", GST_TYPE_FOURCC, GST_STR_FOURCC ("AYUV"),
630 "width", G_TYPE_INT, new_width, "height", G_TYPE_INT, new_height, NULL);
632 if (GST_PAD_LINK_FAILED (gst_pad_try_set_caps (video_box->srcpad, newcaps))) {
633 video_box->use_alpha = FALSE;
635 gst_caps_copy (gst_pad_get_negotiated_caps (video_box->sinkpad));
636 gst_caps_set_simple (newcaps, "format", GST_TYPE_FOURCC,
637 GST_STR_FOURCC ("I420"), "width", G_TYPE_INT, new_width, "height",
638 G_TYPE_INT, new_height, NULL);
640 if (GST_PAD_LINK_FAILED (gst_pad_try_set_caps (video_box->srcpad,
642 GST_ELEMENT_ERROR (video_box, CORE, NEGOTIATION, (NULL), (NULL));
647 video_box->out_width = new_width;
648 video_box->out_height = new_height;
651 if (video_box->use_alpha) {
652 ret = gst_pad_alloc_buffer (video_box->srcpad,
653 GST_BUFFER_OFFSET_NONE, new_width * new_height * 4,
654 GST_RPAD_CAPS (video_box->srcpad), &outbuf);
655 if (ret != GST_FLOW_OK)
658 gst_video_box_ayuv (video_box,
659 GST_BUFFER_DATA (buffer), GST_BUFFER_DATA (outbuf));
661 ret = gst_pad_alloc_buffer (video_box->srcpad,
662 GST_BUFFER_OFFSET_NONE, GST_VIDEO_I420_SIZE (new_width, new_height),
663 GST_RPAD_CAPS (video_box->srcpad), &outbuf);
664 if (ret != GST_FLOW_OK)
667 gst_video_box_i420 (video_box,
668 GST_BUFFER_DATA (buffer), GST_BUFFER_DATA (outbuf));
670 GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buffer);
671 GST_BUFFER_DURATION (outbuf) = GST_BUFFER_DURATION (buffer);
673 ret = gst_pad_push (video_box->srcpad, outbuf);
676 gst_buffer_unref (buffer);
681 static GstElementStateReturn
682 gst_video_box_change_state (GstElement * element)
684 GstVideoBox *video_box;
686 video_box = GST_VIDEO_BOX (element);
688 switch (GST_STATE_TRANSITION (element)) {
689 case GST_STATE_NULL_TO_READY:
691 case GST_STATE_READY_TO_PAUSED:
693 case GST_STATE_PAUSED_TO_PLAYING:
695 case GST_STATE_PLAYING_TO_PAUSED:
697 case GST_STATE_PAUSED_TO_READY:
699 case GST_STATE_READY_TO_NULL:
703 parent_class->change_state (element);
705 return GST_STATE_SUCCESS;
709 plugin_init (GstPlugin * plugin)
711 return gst_element_register (plugin, "videobox", GST_RANK_NONE,
715 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
718 "resizes a video by adding borders or cropping",
719 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE, GST_ORIGIN)