A plugin to add an alpha channel to I420 video. Can optionally do chroma keying.
[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/video/video.h>
25
26 #include <string.h>
27
28 #define GST_TYPE_VIDEO_BOX \
29   (gst_video_box_get_type())
30 #define GST_VIDEO_BOX(obj) \
31   (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_VIDEO_BOX,GstVideoBox))
32 #define GST_VIDEO_BOX_CLASS(klass) \
33   (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_VIDEO_BOX,GstVideoBoxClass))
34 #define GST_IS_VIDEO_BOX(obj) \
35   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_VIDEO_BOX))
36 #define GST_IS_VIDEO_BOX_CLASS(obj) \
37   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_VIDEO_BOX))
38
39 typedef struct _GstVideoBox GstVideoBox;
40 typedef struct _GstVideoBoxClass GstVideoBoxClass;
41
42 typedef enum
43 {
44   VIDEO_BOX_FILL_BLACK,
45   VIDEO_BOX_FILL_GREEN,
46   VIDEO_BOX_FILL_BLUE,
47 }
48 GstVideoBoxFill;
49
50 struct _GstVideoBox
51 {
52   GstElement element;
53
54   /* pads */
55   GstPad *sinkpad;
56   GstPad *srcpad;
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   GstElementClass 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 /* VideoBox signals and args */
88 enum
89 {
90   /* FILL ME */
91   LAST_SIGNAL
92 };
93
94 #define DEFAULT_LEFT      0
95 #define DEFAULT_RIGHT     0
96 #define DEFAULT_TOP       0
97 #define DEFAULT_BOTTOM    0
98 #define DEFAULT_FILL_TYPE VIDEO_BOX_FILL_BLACK
99 #define DEFAULT_ALPHA     1.0
100 #define DEFAULT_BORDER_ALPHA 1.0
101
102 enum
103 {
104   ARG_0,
105   ARG_LEFT,
106   ARG_RIGHT,
107   ARG_TOP,
108   ARG_BOTTOM,
109   ARG_FILL_TYPE,
110   ARG_ALPHA,
111   ARG_BORDER_ALPHA,
112   /* FILL ME */
113 };
114
115 static GstStaticPadTemplate gst_video_box_src_template =
116 GST_STATIC_PAD_TEMPLATE ("src",
117     GST_PAD_SRC,
118     GST_PAD_ALWAYS,
119     GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("{ I420, AYUV }"))
120     );
121
122 static GstStaticPadTemplate gst_video_box_sink_template =
123 GST_STATIC_PAD_TEMPLATE ("sink",
124     GST_PAD_SINK,
125     GST_PAD_ALWAYS,
126     GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("I420"))
127     );
128
129
130 static void gst_video_box_base_init (gpointer g_class);
131 static void gst_video_box_class_init (GstVideoBoxClass * klass);
132 static void gst_video_box_init (GstVideoBox * video_box);
133
134 static void gst_video_box_set_property (GObject * object, guint prop_id,
135     const GValue * value, GParamSpec * pspec);
136 static void gst_video_box_get_property (GObject * object, guint prop_id,
137     GValue * value, GParamSpec * pspec);
138
139 static GstPadLinkReturn
140 gst_video_box_sink_link (GstPad * pad, const GstCaps * caps);
141 static void gst_video_box_chain (GstPad * pad, GstData * _data);
142
143 static GstElementStateReturn gst_video_box_change_state (GstElement * element);
144
145
146 static GstElementClass *parent_class = NULL;
147
148 #define GST_TYPE_VIDEO_BOX_FILL (gst_video_box_fill_get_type())
149 static GType
150 gst_video_box_fill_get_type (void)
151 {
152   static GType video_box_fill_type = 0;
153   static GEnumValue video_box_fill[] = {
154     {VIDEO_BOX_FILL_BLACK, "0", "Black"},
155     {VIDEO_BOX_FILL_GREEN, "1", "Colorkey green"},
156     {VIDEO_BOX_FILL_BLUE, "2", "Colorkey blue"},
157     {0, NULL, NULL},
158   };
159
160   if (!video_box_fill_type) {
161     video_box_fill_type =
162         g_enum_register_static ("GstVideoBoxFill", video_box_fill);
163   }
164   return video_box_fill_type;
165 }
166
167 /* static guint gst_video_box_signals[LAST_SIGNAL] = { 0 }; */
168
169 GType
170 gst_video_box_get_type (void)
171 {
172   static GType video_box_type = 0;
173
174   if (!video_box_type) {
175     static const GTypeInfo video_box_info = {
176       sizeof (GstVideoBoxClass),
177       gst_video_box_base_init,
178       NULL,
179       (GClassInitFunc) gst_video_box_class_init,
180       NULL,
181       NULL,
182       sizeof (GstVideoBox),
183       0,
184       (GInstanceInitFunc) gst_video_box_init,
185     };
186
187     video_box_type =
188         g_type_register_static (GST_TYPE_ELEMENT, "GstVideoBox",
189         &video_box_info, 0);
190   }
191   return video_box_type;
192 }
193
194 static void
195 gst_video_box_base_init (gpointer g_class)
196 {
197   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
198
199   gst_element_class_set_details (element_class, &gst_video_box_details);
200
201   gst_element_class_add_pad_template (element_class,
202       gst_static_pad_template_get (&gst_video_box_sink_template));
203   gst_element_class_add_pad_template (element_class,
204       gst_static_pad_template_get (&gst_video_box_src_template));
205 }
206 static void
207 gst_video_box_class_init (GstVideoBoxClass * klass)
208 {
209   GObjectClass *gobject_class;
210   GstElementClass *gstelement_class;
211
212   gobject_class = (GObjectClass *) klass;
213   gstelement_class = (GstElementClass *) klass;
214
215   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
216
217   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FILL_TYPE,
218       g_param_spec_enum ("fill", "Fill", "How to fill the borders",
219           GST_TYPE_VIDEO_BOX_FILL, DEFAULT_FILL_TYPE,
220           (GParamFlags) G_PARAM_READWRITE));
221   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LEFT,
222       g_param_spec_int ("left", "Left", "Pixels to box at left",
223           G_MININT, G_MAXINT, DEFAULT_LEFT, G_PARAM_READWRITE));
224   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_RIGHT,
225       g_param_spec_int ("right", "Right", "Pixels to box at right",
226           G_MININT, G_MAXINT, DEFAULT_RIGHT, G_PARAM_READWRITE));
227   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_TOP,
228       g_param_spec_int ("top", "Top", "Pixels to box at top",
229           G_MININT, G_MAXINT, DEFAULT_TOP, G_PARAM_READWRITE));
230   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BOTTOM,
231       g_param_spec_int ("bottom", "Bottom", "Pixels to box at bottom",
232           G_MININT, G_MAXINT, DEFAULT_BOTTOM, G_PARAM_READWRITE));
233   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_ALPHA,
234       g_param_spec_double ("alpha", "Alpha", "Alpha value picture",
235           0.0, 1.0, DEFAULT_ALPHA, G_PARAM_READWRITE));
236   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BORDER_ALPHA,
237       g_param_spec_double ("border_alpha", "Border Alpha",
238           "Alpha value of the border", 0.0, 1.0, DEFAULT_BORDER_ALPHA,
239           G_PARAM_READWRITE));
240
241   gobject_class->set_property = gst_video_box_set_property;
242   gobject_class->get_property = gst_video_box_get_property;
243
244   gstelement_class->change_state = gst_video_box_change_state;
245 }
246
247 static void
248 gst_video_box_init (GstVideoBox * video_box)
249 {
250   /* create the sink and src pads */
251   video_box->sinkpad =
252       gst_pad_new_from_template (gst_static_pad_template_get
253       (&gst_video_box_sink_template), "sink");
254   gst_element_add_pad (GST_ELEMENT (video_box), video_box->sinkpad);
255   gst_pad_set_chain_function (video_box->sinkpad, gst_video_box_chain);
256   gst_pad_set_link_function (video_box->sinkpad, gst_video_box_sink_link);
257
258   video_box->srcpad =
259       gst_pad_new_from_template (gst_static_pad_template_get
260       (&gst_video_box_src_template), "src");
261   gst_element_add_pad (GST_ELEMENT (video_box), video_box->srcpad);
262
263   video_box->box_right = DEFAULT_RIGHT;
264   video_box->box_left = DEFAULT_LEFT;
265   video_box->box_top = DEFAULT_TOP;
266   video_box->box_bottom = DEFAULT_BOTTOM;
267   video_box->fill_type = DEFAULT_FILL_TYPE;
268   video_box->alpha = DEFAULT_ALPHA;
269   video_box->border_alpha = DEFAULT_BORDER_ALPHA;
270
271   GST_FLAG_SET (video_box, GST_ELEMENT_EVENT_AWARE);
272 }
273
274 /* do we need this function? */
275 static void
276 gst_video_box_set_property (GObject * object, guint prop_id,
277     const GValue * value, GParamSpec * pspec)
278 {
279   GstVideoBox *video_box;
280
281   /* it's not null if we got it, but it might not be ours */
282   g_return_if_fail (GST_IS_VIDEO_BOX (object));
283
284   video_box = GST_VIDEO_BOX (object);
285
286   switch (prop_id) {
287     case ARG_LEFT:
288       video_box->box_left = g_value_get_int (value);
289       if (video_box->box_left < 0) {
290         video_box->border_left = -video_box->box_left;
291         video_box->crop_left = 0;
292       } else {
293         video_box->border_left = 0;
294         video_box->crop_left = video_box->box_left;
295       }
296       break;
297     case ARG_RIGHT:
298       video_box->box_right = g_value_get_int (value);
299       if (video_box->box_right < 0) {
300         video_box->border_right = -video_box->box_right;
301         video_box->crop_right = 0;
302       } else {
303         video_box->border_right = 0;
304         video_box->crop_right = video_box->box_right;
305       }
306       break;
307     case ARG_TOP:
308       video_box->box_top = g_value_get_int (value);
309       if (video_box->box_top < 0) {
310         video_box->border_top = -video_box->box_top;
311         video_box->crop_top = 0;
312       } else {
313         video_box->border_top = 0;
314         video_box->crop_top = video_box->box_top;
315       }
316       break;
317     case ARG_BOTTOM:
318       video_box->box_bottom = g_value_get_int (value);
319       if (video_box->box_bottom < 0) {
320         video_box->border_bottom = -video_box->box_bottom;
321         video_box->crop_bottom = 0;
322       } else {
323         video_box->border_bottom = 0;
324         video_box->crop_bottom = video_box->box_bottom;
325       }
326       break;
327     case ARG_FILL_TYPE:
328       video_box->fill_type = g_value_get_enum (value);
329       break;
330     case ARG_ALPHA:
331       video_box->alpha = g_value_get_double (value);
332       break;
333     case ARG_BORDER_ALPHA:
334       video_box->border_alpha = g_value_get_double (value);
335       break;
336     default:
337       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
338       break;
339   }
340 }
341 static void
342 gst_video_box_get_property (GObject * object, guint prop_id, GValue * value,
343     GParamSpec * pspec)
344 {
345   GstVideoBox *video_box;
346
347   /* it's not null if we got it, but it might not be ours */
348   g_return_if_fail (GST_IS_VIDEO_BOX (object));
349
350   video_box = GST_VIDEO_BOX (object);
351
352   switch (prop_id) {
353     case ARG_LEFT:
354       g_value_set_int (value, video_box->box_left);
355       break;
356     case ARG_RIGHT:
357       g_value_set_int (value, video_box->box_right);
358       break;
359     case ARG_TOP:
360       g_value_set_int (value, video_box->box_top);
361       break;
362     case ARG_BOTTOM:
363       g_value_set_int (value, video_box->box_bottom);
364       break;
365     case ARG_FILL_TYPE:
366       g_value_set_enum (value, video_box->fill_type);
367       break;
368     case ARG_ALPHA:
369       g_value_set_double (value, video_box->alpha);
370       break;
371     case ARG_BORDER_ALPHA:
372       g_value_set_double (value, video_box->border_alpha);
373       break;
374     default:
375       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
376       break;
377   }
378 }
379
380 static GstPadLinkReturn
381 gst_video_box_sink_link (GstPad * pad, const GstCaps * caps)
382 {
383   GstVideoBox *video_box;
384   GstStructure *structure;
385   gboolean ret;
386
387   video_box = GST_VIDEO_BOX (gst_pad_get_parent (pad));
388   structure = gst_caps_get_structure (caps, 0);
389
390   ret = gst_structure_get_int (structure, "width", &video_box->in_width);
391   ret &= gst_structure_get_int (structure, "height", &video_box->in_height);
392
393   return GST_PAD_LINK_OK;
394 }
395
396 #define GST_VIDEO_I420_Y_OFFSET(width,height) (0)
397 #define GST_VIDEO_I420_U_OFFSET(width,height) ((width)*(height))
398 #define GST_VIDEO_I420_V_OFFSET(width,height) ((width)*(height) + ((width/2)*(height/2)))
399
400 #define GST_VIDEO_I420_Y_ROWSTRIDE(width) (width)
401 #define GST_VIDEO_I420_U_ROWSTRIDE(width) ((width)/2)
402 #define GST_VIDEO_I420_V_ROWSTRIDE(width) ((width)/2)
403
404 static int yuv_colors_Y[] = { 16, 150, 29 };
405 static int yuv_colors_U[] = { 128, 46, 255 };
406 static int yuv_colors_V[] = { 128, 21, 107 };
407
408 static void
409 gst_video_box_i420 (GstVideoBox * video_box, guint8 * src, guint8 * dest)
410 {
411   guint8 *srcY, *srcU, *srcV;
412   guint8 *destY, *destU, *destV;
413   gint crop_width, crop_height;
414   gint out_width, out_height;
415   gint src_stride;
416   gint br, bl, bt, bb;
417   gint j;
418   gint color1, color2;
419
420   br = video_box->border_right;
421   bl = video_box->border_left;
422   bt = video_box->border_top;
423   bb = video_box->border_bottom;
424
425   out_width = video_box->out_width;
426   out_height = video_box->out_height;
427
428   destY = dest + GST_VIDEO_I420_Y_OFFSET (out_width, out_height);
429
430   srcY =
431       src + GST_VIDEO_I420_Y_OFFSET (video_box->in_width, video_box->in_height);
432   src_stride = GST_VIDEO_I420_Y_ROWSTRIDE (video_box->in_width);
433
434   crop_width =
435       video_box->in_width - (video_box->crop_left + video_box->crop_right);
436   crop_height =
437       video_box->in_height - (video_box->crop_top + video_box->crop_bottom);
438
439   srcY += src_stride * video_box->crop_top + video_box->crop_left;
440
441   color1 = yuv_colors_Y[video_box->fill_type];
442
443   /* copy Y plane first */
444   for (j = 0; j < bt; j++) {
445     memset (destY, color1, out_width);
446     destY += out_width;
447   }
448   for (j = 0; j < crop_height; j++) {
449     memset (destY, color1, bl);
450     destY += bl;
451     memcpy (destY, srcY, crop_width);
452     destY += crop_width;
453     memset (destY, color1, br);
454     destY += br;
455     srcY += src_stride;
456   }
457   for (j = 0; j < bb; j++) {
458     memset (destY, color1, out_width);
459     destY += out_width;
460   }
461
462   src_stride = GST_VIDEO_I420_U_ROWSTRIDE (video_box->in_width);
463
464   destU = dest + GST_VIDEO_I420_U_OFFSET (out_width, out_height);
465   destV = dest + GST_VIDEO_I420_V_OFFSET (out_width, out_height);
466
467   crop_width /= 2;
468   crop_height /= 2;
469   out_width /= 2;
470   out_height /= 2;
471   bb /= 2;
472   bt /= 2;
473   br /= 2;
474   bl /= 2;
475
476   srcU =
477       src + GST_VIDEO_I420_U_OFFSET (video_box->in_width, video_box->in_height);
478   srcV =
479       src + GST_VIDEO_I420_V_OFFSET (video_box->in_width, video_box->in_height);
480   srcU += src_stride * (video_box->crop_top / 2) + (video_box->crop_left / 2);
481   srcV += src_stride * (video_box->crop_top / 2) + (video_box->crop_left / 2);
482
483   color1 = yuv_colors_U[video_box->fill_type];
484   color2 = yuv_colors_V[video_box->fill_type];
485
486   for (j = 0; j < bt; j++) {
487     memset (destU, color1, out_width);
488     memset (destV, color2, out_width);
489     destU += out_width;
490     destV += out_width;
491   }
492   for (j = 0; j < crop_height; j++) {
493     memset (destU, color1, bl);
494     destU += bl;
495     /* copy U plane */
496     memcpy (destU, srcU, crop_width);
497     destU += crop_width;
498     memset (destU, color1, br);
499     destU += br;
500     srcU += src_stride;
501
502     memset (destV, color2, bl);
503     destV += bl;
504     /* copy V plane */
505     memcpy (destV, srcV, crop_width);
506     destV += crop_width;
507     memset (destV, color2, br);
508     destV += br;
509     srcV += src_stride;
510   }
511   for (j = 0; j < bb; j++) {
512     memset (destU, color1, out_width);
513     memset (destV, color2, out_width);
514     destU += out_width;
515     destV += out_width;
516   }
517 }
518
519 static void
520 gst_video_box_ayuv (GstVideoBox * video_box, guint8 * src, guint8 * dest)
521 {
522   guint8 *srcY, *srcU, *srcV;
523   gint crop_width, crop_width2, crop_height;
524   gint out_width, out_height;
525   gint src_stride, src_stride2;
526   gint br, bl, bt, bb;
527   gint colorY, colorU, colorV;
528   gint i, j;
529   guint8 b_alpha = (guint8) (video_box->border_alpha * 255);
530   guint8 i_alpha = (guint8) (video_box->alpha * 255);
531   guint32 *destp = (guint32 *) dest;
532   guint32 ayuv;
533
534   br = video_box->border_right;
535   bl = video_box->border_left;
536   bt = video_box->border_top;
537   bb = video_box->border_bottom;
538
539   out_width = video_box->out_width;
540   out_height = video_box->out_height;
541
542   src_stride = GST_VIDEO_I420_Y_ROWSTRIDE (video_box->in_width);
543   src_stride2 = src_stride / 2;
544
545   crop_width =
546       video_box->in_width - (video_box->crop_left + video_box->crop_right);
547   crop_width2 = crop_width / 2;
548   crop_height =
549       video_box->in_height - (video_box->crop_top + video_box->crop_bottom);
550
551   srcY =
552       src + GST_VIDEO_I420_Y_OFFSET (video_box->in_width, video_box->in_height);
553   srcY += src_stride * video_box->crop_top + video_box->crop_left;
554   srcU =
555       src + GST_VIDEO_I420_U_OFFSET (video_box->in_width, video_box->in_height);
556   srcU += src_stride2 * (video_box->crop_top / 2) + (video_box->crop_left / 2);
557   srcV =
558       src + GST_VIDEO_I420_V_OFFSET (video_box->in_width, video_box->in_height);
559   srcV += src_stride2 * (video_box->crop_top / 2) + (video_box->crop_left / 2);
560
561   colorY = yuv_colors_Y[video_box->fill_type];
562   colorU = yuv_colors_U[video_box->fill_type];
563   colorV = yuv_colors_V[video_box->fill_type];
564
565   ayuv =
566       GUINT32_FROM_BE ((b_alpha << 24) | (colorY << 16) | (colorU << 8) |
567       colorV);
568
569   /* top border */
570   for (i = 0; i < bt; i++) {
571     for (j = 0; j < out_width; j++) {
572       *destp++ = ayuv;
573     }
574   }
575   for (i = 0; i < crop_height; i++) {
576     /* left border */
577     for (j = 0; j < bl; j++) {
578       *destp++ = ayuv;
579     }
580     dest = (guint8 *) destp;
581     /* center */
582     for (j = 0; j < crop_width2; j++) {
583       *dest++ = i_alpha;
584       *dest++ = *srcY++;
585       *dest++ = *srcU;
586       *dest++ = *srcV;
587       *dest++ = i_alpha;
588       *dest++ = *srcY++;
589       *dest++ = *srcU++;
590       *dest++ = *srcV++;
591     }
592     if (i % 2 == 0) {
593       srcU -= crop_width2;
594       srcV -= crop_width2;
595     } else {
596       srcU += src_stride2 - crop_width2;
597       srcV += src_stride2 - crop_width2;
598     }
599     srcY += src_stride - crop_width;
600
601     destp = (guint32 *) dest;
602     /* right border */
603     for (j = 0; j < br; j++) {
604       *destp++ = ayuv;
605     }
606   }
607   /* bottom border */
608   for (i = 0; i < bb; i++) {
609     for (j = 0; j < out_width; j++) {
610       *destp++ = ayuv;
611     }
612   }
613 }
614
615 static void
616 gst_video_box_chain (GstPad * pad, GstData * _data)
617 {
618   GstBuffer *buffer;
619   GstVideoBox *video_box;
620   GstBuffer *outbuf;
621   gint new_width, new_height;
622
623   video_box = GST_VIDEO_BOX (gst_pad_get_parent (pad));
624
625   if (GST_IS_EVENT (_data)) {
626     GstEvent *event = GST_EVENT (_data);
627
628     switch (GST_EVENT_TYPE (event)) {
629       default:
630         gst_pad_event_default (pad, event);
631         break;
632     }
633     return;
634   }
635
636   buffer = GST_BUFFER (_data);
637
638   new_width =
639       video_box->in_width - (video_box->box_left + video_box->box_right);
640   new_height =
641       video_box->in_height - (video_box->box_top + video_box->box_bottom);
642
643   if (new_width != video_box->out_width ||
644       new_height != video_box->out_height ||
645       !GST_PAD_CAPS (video_box->srcpad)) {
646     GstCaps *newcaps;
647
648     newcaps = gst_caps_copy (gst_pad_get_negotiated_caps (video_box->sinkpad));
649
650     video_box->use_alpha = TRUE;
651
652     /* try AYUV first */
653     gst_caps_set_simple (newcaps,
654         "format", GST_TYPE_FOURCC, GST_STR_FOURCC ("AYUV"),
655         "width", G_TYPE_INT, new_width, "height", G_TYPE_INT, new_height, NULL);
656
657     if (GST_PAD_LINK_FAILED (gst_pad_try_set_caps (video_box->srcpad, newcaps))) {
658       video_box->use_alpha = FALSE;
659       newcaps =
660           gst_caps_copy (gst_pad_get_negotiated_caps (video_box->sinkpad));
661       gst_caps_set_simple (newcaps, "format", GST_TYPE_FOURCC,
662           GST_STR_FOURCC ("I420"), "width", G_TYPE_INT, new_width, "height",
663           G_TYPE_INT, new_height, NULL);
664
665       if (GST_PAD_LINK_FAILED (gst_pad_try_set_caps (video_box->srcpad,
666                   newcaps))) {
667         GST_ELEMENT_ERROR (video_box, CORE, NEGOTIATION, (NULL), (NULL));
668         return;
669       }
670     }
671
672     video_box->out_width = new_width;
673     video_box->out_height = new_height;
674   }
675
676   if (video_box->use_alpha) {
677     outbuf = gst_buffer_new_and_alloc (new_width * new_height * 4);
678
679     gst_video_box_ayuv (video_box,
680         GST_BUFFER_DATA (buffer), GST_BUFFER_DATA (outbuf));
681   } else {
682     outbuf = gst_buffer_new_and_alloc ((new_width * new_height * 3) / 2);
683
684     gst_video_box_i420 (video_box,
685         GST_BUFFER_DATA (buffer), GST_BUFFER_DATA (outbuf));
686   }
687   GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buffer);
688   GST_BUFFER_DURATION (outbuf) = GST_BUFFER_DURATION (buffer);
689
690
691   gst_buffer_unref (buffer);
692
693   gst_pad_push (video_box->srcpad, GST_DATA (outbuf));
694 }
695
696 static GstElementStateReturn
697 gst_video_box_change_state (GstElement * element)
698 {
699   GstVideoBox *video_box;
700
701   video_box = GST_VIDEO_BOX (element);
702
703   switch (GST_STATE_TRANSITION (element)) {
704     case GST_STATE_NULL_TO_READY:
705       break;
706     case GST_STATE_READY_TO_PAUSED:
707       break;
708     case GST_STATE_PAUSED_TO_PLAYING:
709       break;
710     case GST_STATE_PLAYING_TO_PAUSED:
711       break;
712     case GST_STATE_PAUSED_TO_READY:
713       break;
714     case GST_STATE_READY_TO_NULL:
715       break;
716   }
717
718   parent_class->change_state (element);
719
720   return GST_STATE_SUCCESS;
721 }
722
723 static gboolean
724 plugin_init (GstPlugin * plugin)
725 {
726   return gst_element_register (plugin, "videobox", GST_RANK_NONE,
727       GST_TYPE_VIDEO_BOX);
728 }
729
730 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
731     GST_VERSION_MINOR,
732     "videobox",
733     "resizes a video by adding borders or cropping",
734     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE, GST_ORIGIN)