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