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