Use GST_DEBUG_CATEGORY_STATIC where possible (#342503) plus two minor macro fixes.
[platform/upstream/gstreamer.git] / gst / videobox / gstvideobox.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 #include <gst/gst.h>
24 #include <gst/base/gstbasetransform.h>
25 #include <gst/video/video.h>
26
27 #include <liboil/liboil.h>
28 #include <string.h>
29
30 GST_DEBUG_CATEGORY_STATIC (videobox_debug);
31 #define GST_CAT_DEFAULT videobox_debug
32
33 #define GST_TYPE_VIDEO_BOX \
34   (gst_video_box_get_type())
35 #define GST_VIDEO_BOX(obj) \
36   (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_VIDEO_BOX,GstVideoBox))
37 #define GST_VIDEO_BOX_CLASS(klass) \
38   (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_VIDEO_BOX,GstVideoBoxClass))
39 #define GST_IS_VIDEO_BOX(obj) \
40   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_VIDEO_BOX))
41 #define GST_IS_VIDEO_BOX_CLASS(obj) \
42   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_VIDEO_BOX))
43
44 typedef struct _GstVideoBox GstVideoBox;
45 typedef struct _GstVideoBoxClass GstVideoBoxClass;
46
47 typedef enum
48 {
49   VIDEO_BOX_FILL_BLACK,
50   VIDEO_BOX_FILL_GREEN,
51   VIDEO_BOX_FILL_BLUE,
52 }
53 GstVideoBoxFill;
54
55 struct _GstVideoBox
56 {
57   GstBaseTransform element;
58
59   /* caps */
60   gint in_width, in_height;
61   gint out_width, out_height;
62
63   gint box_left, box_right, box_top, box_bottom;
64
65   gint border_left, border_right, border_top, border_bottom;
66   gint crop_left, crop_right, crop_top, crop_bottom;
67
68   gboolean use_alpha;
69   gdouble alpha;
70   gdouble border_alpha;
71
72   GstVideoBoxFill fill_type;
73 };
74
75 struct _GstVideoBoxClass
76 {
77   GstBaseTransformClass parent_class;
78 };
79
80 /* elementfactory information */
81 static const GstElementDetails gst_video_box_details =
82 GST_ELEMENT_DETAILS ("Video box filter",
83     "Filter/Effect/Video",
84     "Resizes a video by adding borders or cropping",
85     "Wim Taymans <wim@fluendo.com>");
86
87
88 #define DEFAULT_LEFT      0
89 #define DEFAULT_RIGHT     0
90 #define DEFAULT_TOP       0
91 #define DEFAULT_BOTTOM    0
92 #define DEFAULT_FILL_TYPE VIDEO_BOX_FILL_BLACK
93 #define DEFAULT_ALPHA     1.0
94 #define DEFAULT_BORDER_ALPHA 1.0
95
96 enum
97 {
98   PROP_0,
99   PROP_LEFT,
100   PROP_RIGHT,
101   PROP_TOP,
102   PROP_BOTTOM,
103   PROP_FILL_TYPE,
104   PROP_ALPHA,
105   PROP_BORDER_ALPHA,
106   /* FILL ME */
107 };
108
109 static GstStaticPadTemplate gst_video_box_src_template =
110 GST_STATIC_PAD_TEMPLATE ("src",
111     GST_PAD_SRC,
112     GST_PAD_ALWAYS,
113     GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("{ AYUV, I420 }"))
114     );
115
116 static GstStaticPadTemplate gst_video_box_sink_template =
117 GST_STATIC_PAD_TEMPLATE ("sink",
118     GST_PAD_SINK,
119     GST_PAD_ALWAYS,
120     GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("I420"))
121     );
122
123
124 GST_BOILERPLATE (GstVideoBox, gst_video_box, GstBaseTransform,
125     GST_TYPE_BASE_TRANSFORM);
126
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);
131
132 static GstCaps *gst_video_box_transform_caps (GstBaseTransform * trans,
133     GstPadDirection direction, GstCaps * from);
134 static gboolean gst_video_box_set_caps (GstBaseTransform * trans,
135     GstCaps * in, GstCaps * out);
136 static gboolean gst_video_box_get_unit_size (GstBaseTransform * trans,
137     GstCaps * caps, guint * size);
138 static GstFlowReturn gst_video_box_transform (GstBaseTransform * trans,
139     GstBuffer * in, GstBuffer * out);
140
141
142 #define GST_TYPE_VIDEO_BOX_FILL (gst_video_box_fill_get_type())
143 static GType
144 gst_video_box_fill_get_type (void)
145 {
146   static GType video_box_fill_type = 0;
147   static const GEnumValue video_box_fill[] = {
148     {VIDEO_BOX_FILL_BLACK, "Black", "black"},
149     {VIDEO_BOX_FILL_GREEN, "Colorkey green", "green"},
150     {VIDEO_BOX_FILL_BLUE, "Colorkey blue", "blue"},
151     {0, NULL, NULL},
152   };
153
154   if (!video_box_fill_type) {
155     video_box_fill_type =
156         g_enum_register_static ("GstVideoBoxFill", video_box_fill);
157   }
158   return video_box_fill_type;
159 }
160
161
162 static void
163 gst_video_box_base_init (gpointer g_class)
164 {
165   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
166
167   gst_element_class_set_details (element_class, &gst_video_box_details);
168
169   gst_element_class_add_pad_template (element_class,
170       gst_static_pad_template_get (&gst_video_box_sink_template));
171   gst_element_class_add_pad_template (element_class,
172       gst_static_pad_template_get (&gst_video_box_src_template));
173 }
174
175 static void
176 gst_video_box_class_init (GstVideoBoxClass * klass)
177 {
178   GObjectClass *gobject_class;
179   GstBaseTransformClass *trans_class;
180
181   gobject_class = (GObjectClass *) klass;
182   trans_class = (GstBaseTransformClass *) klass;
183
184   gobject_class->set_property = gst_video_box_set_property;
185   gobject_class->get_property = gst_video_box_get_property;
186
187   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_FILL_TYPE,
188       g_param_spec_enum ("fill", "Fill", "How to fill the borders",
189           GST_TYPE_VIDEO_BOX_FILL, DEFAULT_FILL_TYPE,
190           (GParamFlags) G_PARAM_READWRITE));
191   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_LEFT,
192       g_param_spec_int ("left", "Left",
193           "Pixels to box at left (<0  = add a border)", G_MININT, G_MAXINT,
194           DEFAULT_LEFT, G_PARAM_READWRITE));
195   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_RIGHT,
196       g_param_spec_int ("right", "Right",
197           "Pixels to box at right (<0 = add a border)", G_MININT, G_MAXINT,
198           DEFAULT_RIGHT, G_PARAM_READWRITE));
199   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_TOP,
200       g_param_spec_int ("top", "Top",
201           "Pixels to box at top (<0 = add a border)", G_MININT, G_MAXINT,
202           DEFAULT_TOP, G_PARAM_READWRITE));
203   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BOTTOM,
204       g_param_spec_int ("bottom", "Bottom",
205           "Pixels to box at bottom (<0 = add a border)", G_MININT, G_MAXINT,
206           DEFAULT_BOTTOM, G_PARAM_READWRITE));
207   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_ALPHA,
208       g_param_spec_double ("alpha", "Alpha", "Alpha value picture", 0.0, 1.0,
209           DEFAULT_ALPHA, G_PARAM_READWRITE));
210   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BORDER_ALPHA,
211       g_param_spec_double ("border_alpha", "Border Alpha",
212           "Alpha value of the border", 0.0, 1.0, DEFAULT_BORDER_ALPHA,
213           G_PARAM_READWRITE));
214
215   trans_class->transform_caps = gst_video_box_transform_caps;
216   trans_class->set_caps = gst_video_box_set_caps;
217   trans_class->get_unit_size = gst_video_box_get_unit_size;
218   trans_class->transform = gst_video_box_transform;
219
220   GST_DEBUG_CATEGORY_INIT (videobox_debug, "videobox", 0,
221       "Resizes a video by adding borders or cropping");
222
223   oil_init ();
224 }
225
226 static void
227 gst_video_box_init (GstVideoBox * video_box, GstVideoBoxClass * g_class)
228 {
229   video_box->box_right = DEFAULT_RIGHT;
230   video_box->box_left = DEFAULT_LEFT;
231   video_box->box_top = DEFAULT_TOP;
232   video_box->box_bottom = DEFAULT_BOTTOM;
233   video_box->crop_right = 0;
234   video_box->crop_left = 0;
235   video_box->crop_top = 0;
236   video_box->crop_bottom = 0;
237   video_box->fill_type = DEFAULT_FILL_TYPE;
238   video_box->alpha = DEFAULT_ALPHA;
239   video_box->border_alpha = DEFAULT_BORDER_ALPHA;
240 }
241
242 static void
243 gst_video_box_set_property (GObject * object, guint prop_id,
244     const GValue * value, GParamSpec * pspec)
245 {
246   GstVideoBox *video_box = GST_VIDEO_BOX (object);
247
248   switch (prop_id) {
249     case PROP_LEFT:
250       video_box->box_left = g_value_get_int (value);
251       if (video_box->box_left < 0) {
252         video_box->border_left = -video_box->box_left;
253         video_box->crop_left = 0;
254       } else {
255         video_box->border_left = 0;
256         video_box->crop_left = video_box->box_left;
257       }
258       break;
259     case PROP_RIGHT:
260       video_box->box_right = g_value_get_int (value);
261       if (video_box->box_right < 0) {
262         video_box->border_right = -video_box->box_right;
263         video_box->crop_right = 0;
264       } else {
265         video_box->border_right = 0;
266         video_box->crop_right = video_box->box_right;
267       }
268       break;
269     case PROP_TOP:
270       video_box->box_top = g_value_get_int (value);
271       if (video_box->box_top < 0) {
272         video_box->border_top = -video_box->box_top;
273         video_box->crop_top = 0;
274       } else {
275         video_box->border_top = 0;
276         video_box->crop_top = video_box->box_top;
277       }
278       break;
279     case PROP_BOTTOM:
280       video_box->box_bottom = g_value_get_int (value);
281       if (video_box->box_bottom < 0) {
282         video_box->border_bottom = -video_box->box_bottom;
283         video_box->crop_bottom = 0;
284       } else {
285         video_box->border_bottom = 0;
286         video_box->crop_bottom = video_box->box_bottom;
287       }
288       break;
289     case PROP_FILL_TYPE:
290       video_box->fill_type = g_value_get_enum (value);
291       break;
292     case PROP_ALPHA:
293       video_box->alpha = g_value_get_double (value);
294       break;
295     case PROP_BORDER_ALPHA:
296       video_box->border_alpha = g_value_get_double (value);
297       break;
298     default:
299       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
300       break;
301   }
302 }
303
304 static void
305 gst_video_box_get_property (GObject * object, guint prop_id, GValue * value,
306     GParamSpec * pspec)
307 {
308   GstVideoBox *video_box = GST_VIDEO_BOX (object);
309
310   switch (prop_id) {
311     case PROP_LEFT:
312       g_value_set_int (value, video_box->box_left);
313       break;
314     case PROP_RIGHT:
315       g_value_set_int (value, video_box->box_right);
316       break;
317     case PROP_TOP:
318       g_value_set_int (value, video_box->box_top);
319       break;
320     case PROP_BOTTOM:
321       g_value_set_int (value, video_box->box_bottom);
322       break;
323     case PROP_FILL_TYPE:
324       g_value_set_enum (value, video_box->fill_type);
325       break;
326     case PROP_ALPHA:
327       g_value_set_double (value, video_box->alpha);
328       break;
329     case PROP_BORDER_ALPHA:
330       g_value_set_double (value, video_box->border_alpha);
331       break;
332     default:
333       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
334       break;
335   }
336 }
337
338 static GstCaps *
339 gst_video_box_transform_caps (GstBaseTransform * trans,
340     GstPadDirection direction, GstCaps * from)
341 {
342   GstVideoBox *video_box;
343   GstCaps *to;
344   GstStructure *structure;
345   GValue list_value = { 0 }, value = {
346   0};
347   gint dir, i, tmp;
348
349   video_box = GST_VIDEO_BOX (trans);
350
351   g_value_init (&list_value, GST_TYPE_LIST);
352   g_value_init (&value, GST_TYPE_FOURCC);
353   gst_value_set_fourcc (&value, GST_MAKE_FOURCC ('I', '4', '2', '0'));
354   gst_value_list_append_value (&list_value, &value);
355   g_value_unset (&value);
356
357   to = gst_caps_copy (from);
358   dir = (direction == GST_PAD_SINK) ? -1 : 1;
359
360   for (i = 0; i < gst_caps_get_size (to); i++) {
361     structure = gst_caps_get_structure (to, i);
362     if (direction == GST_PAD_SINK) {    /* I420 to { I420, AYUV } */
363       g_value_init (&value, GST_TYPE_FOURCC);
364       gst_value_set_fourcc (&value, GST_MAKE_FOURCC ('A', 'Y', 'U', 'V'));
365       gst_value_list_append_value (&list_value, &value);
366       g_value_unset (&value);
367       gst_structure_set_value (structure, "format", &list_value);
368     } else if (direction == GST_PAD_SRC) {
369       gst_structure_set_value (structure, "format", &list_value);
370     }
371     if (gst_structure_get_int (structure, "width", &tmp))
372       gst_structure_set (structure, "width", G_TYPE_INT,
373           tmp + dir * (video_box->box_left + video_box->box_right), NULL);
374     if (gst_structure_get_int (structure, "height", &tmp))
375       gst_structure_set (structure, "height", G_TYPE_INT,
376           tmp + dir * (video_box->box_top + video_box->box_bottom), NULL);
377   }
378
379   g_value_unset (&list_value);
380
381   GST_DEBUG_OBJECT (video_box, "direction %d, transformed %" GST_PTR_FORMAT
382       " to %" GST_PTR_FORMAT, direction, from, to);
383
384   return to;
385 }
386
387 static gboolean
388 gst_video_box_set_caps (GstBaseTransform * trans, GstCaps * in, GstCaps * out)
389 {
390   GstVideoBox *video_box;
391   GstStructure *structure;
392   gboolean ret;
393   guint32 fourcc = 0;
394
395   video_box = GST_VIDEO_BOX (trans);
396
397   structure = gst_caps_get_structure (in, 0);
398   ret = gst_structure_get_int (structure, "width", &video_box->in_width);
399   ret &= gst_structure_get_int (structure, "height", &video_box->in_height);
400
401   structure = gst_caps_get_structure (out, 0);
402   ret &= gst_structure_get_int (structure, "width", &video_box->out_width);
403   ret &= gst_structure_get_int (structure, "height", &video_box->out_height);
404   ret &= gst_structure_get_fourcc (structure, "format", &fourcc);
405
406   if (fourcc == GST_STR_FOURCC ("AYUV")) {
407     video_box->use_alpha = TRUE;
408   } else {
409     if (video_box->box_left == 0 && video_box->box_right == 0 &&
410         video_box->box_top == 0 && video_box->box_bottom == 0) {
411       gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (video_box), TRUE);
412       GST_LOG ("we are using passthrough");
413     } else {
414       gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (video_box),
415           FALSE);
416       GST_LOG ("we are not using passthrough");
417     }
418   }
419
420   return ret;
421 }
422
423 /* see gst-plugins/gst/games/gstvideoimage.c, paint_setup_I420() */
424 #define GST_VIDEO_I420_Y_ROWSTRIDE(width) (GST_ROUND_UP_4(width))
425 #define GST_VIDEO_I420_U_ROWSTRIDE(width) (GST_ROUND_UP_8(width)/2)
426 #define GST_VIDEO_I420_V_ROWSTRIDE(width) ((GST_ROUND_UP_8(GST_VIDEO_I420_Y_ROWSTRIDE(width)))/2)
427
428 #define GST_VIDEO_I420_Y_OFFSET(w,h) (0)
429 #define GST_VIDEO_I420_U_OFFSET(w,h) (GST_VIDEO_I420_Y_OFFSET(w,h)+(GST_VIDEO_I420_Y_ROWSTRIDE(w)*GST_ROUND_UP_2(h)))
430 #define GST_VIDEO_I420_V_OFFSET(w,h) (GST_VIDEO_I420_U_OFFSET(w,h)+(GST_VIDEO_I420_U_ROWSTRIDE(w)*GST_ROUND_UP_2(h)/2))
431
432 #define GST_VIDEO_I420_SIZE(w,h)     (GST_VIDEO_I420_V_OFFSET(w,h)+(GST_VIDEO_I420_V_ROWSTRIDE(w)*GST_ROUND_UP_2(h)/2))
433
434 static gboolean
435 gst_video_box_get_unit_size (GstBaseTransform * trans, GstCaps * caps,
436     guint * size)
437 {
438   GstVideoBox *video_box;
439   GstStructure *structure = NULL;
440   guint32 fourcc;
441   gint width, height;
442
443   g_return_val_if_fail (size, FALSE);
444   video_box = GST_VIDEO_BOX (trans);
445
446   structure = gst_caps_get_structure (caps, 0);
447   gst_structure_get_fourcc (structure, "format", &fourcc);
448   gst_structure_get_int (structure, "width", &width);
449   gst_structure_get_int (structure, "height", &height);
450
451   switch (fourcc) {
452     case GST_MAKE_FOURCC ('A', 'Y', 'U', 'V'):
453       *size = width * height * 4;
454       break;
455     case GST_MAKE_FOURCC ('I', '4', '2', '0'):
456       *size = GST_VIDEO_I420_SIZE (width, height);
457       break;
458     default:
459       return FALSE;
460       break;
461   }
462
463   return TRUE;
464 }
465
466 static int yuv_colors_Y[] = { 16, 150, 29 };
467 static int yuv_colors_U[] = { 128, 46, 255 };
468 static int yuv_colors_V[] = { 128, 21, 107 };
469
470 static void
471 gst_video_box_copy_plane_i420 (GstVideoBox * video_box, guint8 * src,
472     guint8 * dest, gint br, gint bl, gint bt, gint bb, gint src_crop_width,
473     gint src_crop_height, gint src_stride, gint dest_width, gint dest_stride,
474     guint8 fill_color)
475 {
476   gint j;
477
478   /* top border */
479   for (j = 0; j < bt; j++) {
480     oil_splat_u8_ns (dest, &fill_color, dest_width);
481     dest += dest_stride;
482   }
483
484   /* copy and add left and right border */
485   for (j = 0; j < src_crop_height; j++) {
486     oil_splat_u8_ns (dest, &fill_color, bl);
487     oil_memcpy (dest + bl, src, src_crop_width);
488     oil_splat_u8_ns (dest + bl + src_crop_width, &fill_color, br);
489     dest += dest_stride;
490     src += src_stride;
491   }
492
493   /* bottom border */
494   for (j = 0; j < bb; j++) {
495     oil_splat_u8_ns (dest, &fill_color, dest_width);
496     dest += dest_stride;
497   }
498 }
499
500 static void
501 gst_video_box_i420 (GstVideoBox * video_box, guint8 * src, guint8 * dest)
502 {
503   guint8 *srcY, *srcU, *srcV;
504   guint8 *destY, *destU, *destV;
505   gint crop_width, crop_height;
506   gint out_width, out_height;
507   gint src_width, src_height;
508   gint src_stride, dest_stride;
509   gint br, bl, bt, bb;
510
511   br = video_box->border_right;
512   bl = video_box->border_left;
513   bt = video_box->border_top;
514   bb = video_box->border_bottom;
515
516   out_width = video_box->out_width;
517   out_height = video_box->out_height;
518
519   src_width = video_box->in_width;
520   src_height = video_box->in_height;
521
522   crop_width = src_width - (video_box->crop_left + video_box->crop_right);
523   crop_height = src_height - (video_box->crop_top + video_box->crop_bottom);
524
525   /* Y plane */
526   src_stride = GST_VIDEO_I420_Y_ROWSTRIDE (src_width);
527   dest_stride = GST_VIDEO_I420_Y_ROWSTRIDE (out_width);
528
529   destY = dest + GST_VIDEO_I420_Y_OFFSET (out_width, out_height);
530
531   srcY = src + GST_VIDEO_I420_Y_OFFSET (src_width, src_height);
532   srcY += src_stride * video_box->crop_top + video_box->crop_left;
533
534   gst_video_box_copy_plane_i420 (video_box, srcY, destY, br, bl, bt, bb,
535       crop_width, crop_height, src_stride, out_width, dest_stride,
536       yuv_colors_Y[video_box->fill_type]);
537
538   /* U plane */
539   src_stride = GST_VIDEO_I420_U_ROWSTRIDE (src_width);
540   dest_stride = GST_VIDEO_I420_U_ROWSTRIDE (out_width);
541
542   destU = dest + GST_VIDEO_I420_U_OFFSET (out_width, out_height);
543
544   srcU = src + GST_VIDEO_I420_U_OFFSET (src_width, src_height);
545   srcU += src_stride * (video_box->crop_top / 2) + (video_box->crop_left / 2);
546
547   gst_video_box_copy_plane_i420 (video_box, srcU, destU, br / 2, bl / 2, bt / 2,
548       bb / 2, crop_width / 2, crop_height / 2, src_stride, out_width / 2,
549       dest_stride, yuv_colors_U[video_box->fill_type]);
550
551   /* V plane */
552   src_stride = GST_VIDEO_I420_V_ROWSTRIDE (src_width);
553   dest_stride = GST_VIDEO_I420_V_ROWSTRIDE (out_width);
554
555   destV = dest + GST_VIDEO_I420_V_OFFSET (out_width, out_height);
556
557   srcV = src + GST_VIDEO_I420_V_OFFSET (src_width, src_height);
558   srcV += src_stride * (video_box->crop_top / 2) + (video_box->crop_left / 2);
559
560   gst_video_box_copy_plane_i420 (video_box, srcV, destV, br / 2, bl / 2, bt / 2,
561       bb / 2, crop_width / 2, crop_height / 2, src_stride, out_width / 2,
562       dest_stride, yuv_colors_V[video_box->fill_type]);
563 }
564
565 /* Note the source image is always I420, we
566  * are converting to AYUV on the fly here */
567 static void
568 gst_video_box_ayuv (GstVideoBox * video_box, guint8 * src, guint8 * dest)
569 {
570   guint8 *srcY, *srcU, *srcV;
571   gint crop_width, crop_width2, crop_height;
572   gint out_width, out_height;
573   gint src_stridey, src_strideu, src_stridev;
574   gint br, bl, bt, bb;
575   gint colorY, colorU, colorV;
576   gint i, j;
577   guint8 b_alpha = (guint8) (video_box->border_alpha * 255);
578   guint8 i_alpha = (guint8) (video_box->alpha * 255);
579   guint32 *destp = (guint32 *) dest;
580   guint32 ayuv;
581
582   br = video_box->border_right;
583   bl = video_box->border_left;
584   bt = video_box->border_top;
585   bb = video_box->border_bottom;
586
587   out_width = video_box->out_width;
588   out_height = video_box->out_height;
589
590   src_stridey = GST_VIDEO_I420_Y_ROWSTRIDE (video_box->in_width);
591   src_strideu = GST_VIDEO_I420_U_ROWSTRIDE (video_box->in_width);
592   src_stridev = GST_VIDEO_I420_V_ROWSTRIDE (video_box->in_width);
593
594   crop_width =
595       video_box->in_width - (video_box->crop_left + video_box->crop_right);
596   crop_width2 = crop_width / 2;
597   crop_height =
598       video_box->in_height - (video_box->crop_top + video_box->crop_bottom);
599
600   srcY =
601       src + GST_VIDEO_I420_Y_OFFSET (video_box->in_width, video_box->in_height);
602   srcY += src_stridey * video_box->crop_top + video_box->crop_left;
603   srcU =
604       src + GST_VIDEO_I420_U_OFFSET (video_box->in_width, video_box->in_height);
605   srcU += src_strideu * (video_box->crop_top / 2) + (video_box->crop_left / 2);
606   srcV =
607       src + GST_VIDEO_I420_V_OFFSET (video_box->in_width, video_box->in_height);
608   srcV += src_stridev * (video_box->crop_top / 2) + (video_box->crop_left / 2);
609
610   colorY = yuv_colors_Y[video_box->fill_type];
611   colorU = yuv_colors_U[video_box->fill_type];
612   colorV = yuv_colors_V[video_box->fill_type];
613
614   ayuv =
615       GUINT32_FROM_BE ((b_alpha << 24) | (colorY << 16) | (colorU << 8) |
616       colorV);
617
618   /* top border */
619   if (bt) {
620     size_t nb_pixels = bt * out_width;
621
622     oil_splat_u32_ns (destp, &ayuv, nb_pixels);
623     destp += nb_pixels;
624   }
625   for (i = 0; i < crop_height; i++) {
626     /* left border */
627     if (bl) {
628       oil_splat_u32_ns (destp, &ayuv, bl);
629       destp += bl;
630     }
631     dest = (guint8 *) destp;
632     /* center */
633     /* We can splat the alpha channel for the whole line */
634     oil_splat_u8 (dest, 4, &i_alpha, crop_width);
635     for (j = 0; j < crop_width2; j++) {
636       dest++;
637       *dest++ = *srcY++;
638       *dest++ = *srcU;
639       *dest++ = *srcV;
640       dest++;
641       *dest++ = *srcY++;
642       *dest++ = *srcU++;
643       *dest++ = *srcV++;
644     }
645     if (i % 2 == 0) {
646       srcU -= crop_width2;
647       srcV -= crop_width2;
648     } else {
649       srcU += src_strideu - crop_width2;
650       srcV += src_stridev - crop_width2;
651     }
652     srcY += src_stridey - crop_width;
653
654     destp = (guint32 *) dest;
655     /* right border */
656     if (br) {
657       oil_splat_u32_ns (destp, &ayuv, br);
658       destp += br;
659     }
660   }
661   /* bottom border */
662   if (bb) {
663     size_t nb_pixels = bb * out_width;
664
665     oil_splat_u32_ns (destp, &ayuv, nb_pixels);
666     destp += nb_pixels;
667   }
668 }
669
670 static GstFlowReturn
671 gst_video_box_transform (GstBaseTransform * trans, GstBuffer * in,
672     GstBuffer * out)
673 {
674   GstVideoBox *video_box;
675
676   video_box = GST_VIDEO_BOX (trans);
677
678   if (video_box->use_alpha) {
679     gst_video_box_ayuv (video_box, GST_BUFFER_DATA (in), GST_BUFFER_DATA (out));
680   } else {
681     gst_video_box_i420 (video_box, GST_BUFFER_DATA (in), GST_BUFFER_DATA (out));
682   }
683
684   return GST_FLOW_OK;
685 }
686
687 static gboolean
688 plugin_init (GstPlugin * plugin)
689 {
690
691   return gst_element_register (plugin, "videobox", GST_RANK_NONE,
692       GST_TYPE_VIDEO_BOX);
693 }
694
695 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
696     GST_VERSION_MINOR,
697     "videobox",
698     "resizes a video by adding borders or cropping",
699     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)