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