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