gst/videobox/gstvideobox.c: Use pad_alloc where possible.
[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",
223           "Pixels to box at left (<0  = add a border)", G_MININT, G_MAXINT,
224           DEFAULT_LEFT, G_PARAM_READWRITE));
225   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_RIGHT,
226       g_param_spec_int ("right", "Right",
227           "Pixels to box at right (<0 = add a border)", G_MININT, G_MAXINT,
228           DEFAULT_RIGHT, G_PARAM_READWRITE));
229   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_TOP,
230       g_param_spec_int ("top", "Top",
231           "Pixels to box at top (<0 = add a border)", G_MININT, G_MAXINT,
232           DEFAULT_TOP, G_PARAM_READWRITE));
233   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BOTTOM,
234       g_param_spec_int ("bottom", "Bottom",
235           "Pixels to box at bottom (<0 = add a border)", G_MININT, G_MAXINT,
236           DEFAULT_BOTTOM, G_PARAM_READWRITE));
237   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_ALPHA,
238       g_param_spec_double ("alpha", "Alpha", "Alpha value picture", 0.0, 1.0,
239           DEFAULT_ALPHA, G_PARAM_READWRITE));
240   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BORDER_ALPHA,
241       g_param_spec_double ("border_alpha", "Border Alpha",
242           "Alpha value of the border", 0.0, 1.0, DEFAULT_BORDER_ALPHA,
243           G_PARAM_READWRITE));
244
245   gobject_class->set_property = gst_video_box_set_property;
246   gobject_class->get_property = gst_video_box_get_property;
247
248   gstelement_class->change_state = gst_video_box_change_state;
249 }
250
251 static void
252 gst_video_box_init (GstVideoBox * video_box)
253 {
254   /* create the sink and src pads */
255   video_box->sinkpad =
256       gst_pad_new_from_template (gst_static_pad_template_get
257       (&gst_video_box_sink_template), "sink");
258   gst_element_add_pad (GST_ELEMENT (video_box), video_box->sinkpad);
259   gst_pad_set_chain_function (video_box->sinkpad, gst_video_box_chain);
260   gst_pad_set_link_function (video_box->sinkpad, gst_video_box_sink_link);
261
262   video_box->srcpad =
263       gst_pad_new_from_template (gst_static_pad_template_get
264       (&gst_video_box_src_template), "src");
265   gst_element_add_pad (GST_ELEMENT (video_box), video_box->srcpad);
266
267   video_box->box_right = DEFAULT_RIGHT;
268   video_box->box_left = DEFAULT_LEFT;
269   video_box->box_top = DEFAULT_TOP;
270   video_box->box_bottom = DEFAULT_BOTTOM;
271   video_box->fill_type = DEFAULT_FILL_TYPE;
272   video_box->alpha = DEFAULT_ALPHA;
273   video_box->border_alpha = DEFAULT_BORDER_ALPHA;
274
275   GST_FLAG_SET (video_box, GST_ELEMENT_EVENT_AWARE);
276 }
277
278 /* do we need this function? */
279 static void
280 gst_video_box_set_property (GObject * object, guint prop_id,
281     const GValue * value, GParamSpec * pspec)
282 {
283   GstVideoBox *video_box;
284
285   /* it's not null if we got it, but it might not be ours */
286   g_return_if_fail (GST_IS_VIDEO_BOX (object));
287
288   video_box = GST_VIDEO_BOX (object);
289
290   switch (prop_id) {
291     case ARG_LEFT:
292       video_box->box_left = g_value_get_int (value);
293       if (video_box->box_left < 0) {
294         video_box->border_left = -video_box->box_left;
295         video_box->crop_left = 0;
296       } else {
297         video_box->border_left = 0;
298         video_box->crop_left = video_box->box_left;
299       }
300       break;
301     case ARG_RIGHT:
302       video_box->box_right = g_value_get_int (value);
303       if (video_box->box_right < 0) {
304         video_box->border_right = -video_box->box_right;
305         video_box->crop_right = 0;
306       } else {
307         video_box->border_right = 0;
308         video_box->crop_right = video_box->box_right;
309       }
310       break;
311     case ARG_TOP:
312       video_box->box_top = g_value_get_int (value);
313       if (video_box->box_top < 0) {
314         video_box->border_top = -video_box->box_top;
315         video_box->crop_top = 0;
316       } else {
317         video_box->border_top = 0;
318         video_box->crop_top = video_box->box_top;
319       }
320       break;
321     case ARG_BOTTOM:
322       video_box->box_bottom = g_value_get_int (value);
323       if (video_box->box_bottom < 0) {
324         video_box->border_bottom = -video_box->box_bottom;
325         video_box->crop_bottom = 0;
326       } else {
327         video_box->border_bottom = 0;
328         video_box->crop_bottom = video_box->box_bottom;
329       }
330       break;
331     case ARG_FILL_TYPE:
332       video_box->fill_type = g_value_get_enum (value);
333       break;
334     case ARG_ALPHA:
335       video_box->alpha = g_value_get_double (value);
336       break;
337     case ARG_BORDER_ALPHA:
338       video_box->border_alpha = g_value_get_double (value);
339       break;
340     default:
341       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
342       break;
343   }
344 }
345 static void
346 gst_video_box_get_property (GObject * object, guint prop_id, GValue * value,
347     GParamSpec * pspec)
348 {
349   GstVideoBox *video_box;
350
351   /* it's not null if we got it, but it might not be ours */
352   g_return_if_fail (GST_IS_VIDEO_BOX (object));
353
354   video_box = GST_VIDEO_BOX (object);
355
356   switch (prop_id) {
357     case ARG_LEFT:
358       g_value_set_int (value, video_box->box_left);
359       break;
360     case ARG_RIGHT:
361       g_value_set_int (value, video_box->box_right);
362       break;
363     case ARG_TOP:
364       g_value_set_int (value, video_box->box_top);
365       break;
366     case ARG_BOTTOM:
367       g_value_set_int (value, video_box->box_bottom);
368       break;
369     case ARG_FILL_TYPE:
370       g_value_set_enum (value, video_box->fill_type);
371       break;
372     case ARG_ALPHA:
373       g_value_set_double (value, video_box->alpha);
374       break;
375     case ARG_BORDER_ALPHA:
376       g_value_set_double (value, video_box->border_alpha);
377       break;
378     default:
379       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
380       break;
381   }
382 }
383
384 static GstPadLinkReturn
385 gst_video_box_sink_link (GstPad * pad, const GstCaps * caps)
386 {
387   GstVideoBox *video_box;
388   GstStructure *structure;
389   gboolean ret;
390
391   video_box = GST_VIDEO_BOX (gst_pad_get_parent (pad));
392   structure = gst_caps_get_structure (caps, 0);
393
394   ret = gst_structure_get_int (structure, "width", &video_box->in_width);
395   ret &= gst_structure_get_int (structure, "height", &video_box->in_height);
396
397   return GST_PAD_LINK_OK;
398 }
399
400 #define GST_VIDEO_I420_Y_OFFSET(width,height) (0)
401 #define GST_VIDEO_I420_U_OFFSET(width,height) ((width)*(height))
402 #define GST_VIDEO_I420_V_OFFSET(width,height) ((width)*(height) + ((width/2)*(height/2)))
403
404 #define GST_VIDEO_I420_Y_ROWSTRIDE(width) (width)
405 #define GST_VIDEO_I420_U_ROWSTRIDE(width) ((width)/2)
406 #define GST_VIDEO_I420_V_ROWSTRIDE(width) ((width)/2)
407
408 static int yuv_colors_Y[] = { 16, 150, 29 };
409 static int yuv_colors_U[] = { 128, 46, 255 };
410 static int yuv_colors_V[] = { 128, 21, 107 };
411
412 static void
413 gst_video_box_i420 (GstVideoBox * video_box, guint8 * src, guint8 * dest)
414 {
415   guint8 *srcY, *srcU, *srcV;
416   guint8 *destY, *destU, *destV;
417   gint crop_width, crop_height;
418   gint out_width, out_height;
419   gint src_stride;
420   gint br, bl, bt, bb;
421   gint j;
422   gint color1, color2;
423
424   br = video_box->border_right;
425   bl = video_box->border_left;
426   bt = video_box->border_top;
427   bb = video_box->border_bottom;
428
429   out_width = video_box->out_width;
430   out_height = video_box->out_height;
431
432   destY = dest + GST_VIDEO_I420_Y_OFFSET (out_width, out_height);
433
434   srcY =
435       src + GST_VIDEO_I420_Y_OFFSET (video_box->in_width, video_box->in_height);
436   src_stride = GST_VIDEO_I420_Y_ROWSTRIDE (video_box->in_width);
437
438   crop_width =
439       video_box->in_width - (video_box->crop_left + video_box->crop_right);
440   crop_height =
441       video_box->in_height - (video_box->crop_top + video_box->crop_bottom);
442
443   srcY += src_stride * video_box->crop_top + video_box->crop_left;
444
445   color1 = yuv_colors_Y[video_box->fill_type];
446
447   /* copy Y plane first */
448   for (j = 0; j < bt; j++) {
449     memset (destY, color1, out_width);
450     destY += out_width;
451   }
452   for (j = 0; j < crop_height; j++) {
453     memset (destY, color1, bl);
454     destY += bl;
455     memcpy (destY, srcY, crop_width);
456     destY += crop_width;
457     memset (destY, color1, br);
458     destY += br;
459     srcY += src_stride;
460   }
461   for (j = 0; j < bb; j++) {
462     memset (destY, color1, out_width);
463     destY += out_width;
464   }
465
466   src_stride = GST_VIDEO_I420_U_ROWSTRIDE (video_box->in_width);
467
468   destU = dest + GST_VIDEO_I420_U_OFFSET (out_width, out_height);
469   destV = dest + GST_VIDEO_I420_V_OFFSET (out_width, out_height);
470
471   crop_width /= 2;
472   crop_height /= 2;
473   out_width /= 2;
474   out_height /= 2;
475   bb /= 2;
476   bt /= 2;
477   br /= 2;
478   bl /= 2;
479
480   srcU =
481       src + GST_VIDEO_I420_U_OFFSET (video_box->in_width, video_box->in_height);
482   srcV =
483       src + GST_VIDEO_I420_V_OFFSET (video_box->in_width, video_box->in_height);
484   srcU += src_stride * (video_box->crop_top / 2) + (video_box->crop_left / 2);
485   srcV += src_stride * (video_box->crop_top / 2) + (video_box->crop_left / 2);
486
487   color1 = yuv_colors_U[video_box->fill_type];
488   color2 = yuv_colors_V[video_box->fill_type];
489
490   for (j = 0; j < bt; j++) {
491     memset (destU, color1, out_width);
492     memset (destV, color2, out_width);
493     destU += out_width;
494     destV += out_width;
495   }
496   for (j = 0; j < crop_height; j++) {
497     memset (destU, color1, bl);
498     destU += bl;
499     /* copy U plane */
500     memcpy (destU, srcU, crop_width);
501     destU += crop_width;
502     memset (destU, color1, br);
503     destU += br;
504     srcU += src_stride;
505
506     memset (destV, color2, bl);
507     destV += bl;
508     /* copy V plane */
509     memcpy (destV, srcV, crop_width);
510     destV += crop_width;
511     memset (destV, color2, br);
512     destV += br;
513     srcV += src_stride;
514   }
515   for (j = 0; j < bb; j++) {
516     memset (destU, color1, out_width);
517     memset (destV, color2, out_width);
518     destU += out_width;
519     destV += out_width;
520   }
521 }
522
523 static void
524 gst_video_box_ayuv (GstVideoBox * video_box, guint8 * src, guint8 * dest)
525 {
526   guint8 *srcY, *srcU, *srcV;
527   gint crop_width, crop_width2, crop_height;
528   gint out_width, out_height;
529   gint src_stride, src_stride2;
530   gint br, bl, bt, bb;
531   gint colorY, colorU, colorV;
532   gint i, j;
533   guint8 b_alpha = (guint8) (video_box->border_alpha * 255);
534   guint8 i_alpha = (guint8) (video_box->alpha * 255);
535   guint32 *destp = (guint32 *) dest;
536   guint32 ayuv;
537
538   br = video_box->border_right;
539   bl = video_box->border_left;
540   bt = video_box->border_top;
541   bb = video_box->border_bottom;
542
543   out_width = video_box->out_width;
544   out_height = video_box->out_height;
545
546   src_stride = GST_VIDEO_I420_Y_ROWSTRIDE (video_box->in_width);
547   src_stride2 = src_stride / 2;
548
549   crop_width =
550       video_box->in_width - (video_box->crop_left + video_box->crop_right);
551   crop_width2 = crop_width / 2;
552   crop_height =
553       video_box->in_height - (video_box->crop_top + video_box->crop_bottom);
554
555   srcY =
556       src + GST_VIDEO_I420_Y_OFFSET (video_box->in_width, video_box->in_height);
557   srcY += src_stride * video_box->crop_top + video_box->crop_left;
558   srcU =
559       src + GST_VIDEO_I420_U_OFFSET (video_box->in_width, video_box->in_height);
560   srcU += src_stride2 * (video_box->crop_top / 2) + (video_box->crop_left / 2);
561   srcV =
562       src + GST_VIDEO_I420_V_OFFSET (video_box->in_width, video_box->in_height);
563   srcV += src_stride2 * (video_box->crop_top / 2) + (video_box->crop_left / 2);
564
565   colorY = yuv_colors_Y[video_box->fill_type];
566   colorU = yuv_colors_U[video_box->fill_type];
567   colorV = yuv_colors_V[video_box->fill_type];
568
569   ayuv =
570       GUINT32_FROM_BE ((b_alpha << 24) | (colorY << 16) | (colorU << 8) |
571       colorV);
572
573   /* top border */
574   for (i = 0; i < bt; i++) {
575     for (j = 0; j < out_width; j++) {
576       *destp++ = ayuv;
577     }
578   }
579   for (i = 0; i < crop_height; i++) {
580     /* left border */
581     for (j = 0; j < bl; j++) {
582       *destp++ = ayuv;
583     }
584     dest = (guint8 *) destp;
585     /* center */
586     for (j = 0; j < crop_width2; j++) {
587       *dest++ = i_alpha;
588       *dest++ = *srcY++;
589       *dest++ = *srcU;
590       *dest++ = *srcV;
591       *dest++ = i_alpha;
592       *dest++ = *srcY++;
593       *dest++ = *srcU++;
594       *dest++ = *srcV++;
595     }
596     if (i % 2 == 0) {
597       srcU -= crop_width2;
598       srcV -= crop_width2;
599     } else {
600       srcU += src_stride2 - crop_width2;
601       srcV += src_stride2 - crop_width2;
602     }
603     srcY += src_stride - crop_width;
604
605     destp = (guint32 *) dest;
606     /* right border */
607     for (j = 0; j < br; j++) {
608       *destp++ = ayuv;
609     }
610   }
611   /* bottom border */
612   for (i = 0; i < bb; i++) {
613     for (j = 0; j < out_width; j++) {
614       *destp++ = ayuv;
615     }
616   }
617 }
618
619 static void
620 gst_video_box_chain (GstPad * pad, GstData * _data)
621 {
622   GstBuffer *buffer;
623   GstVideoBox *video_box;
624   GstBuffer *outbuf;
625   gint new_width, new_height;
626
627   video_box = GST_VIDEO_BOX (gst_pad_get_parent (pad));
628
629   if (GST_IS_EVENT (_data)) {
630     GstEvent *event = GST_EVENT (_data);
631
632     switch (GST_EVENT_TYPE (event)) {
633       default:
634         gst_pad_event_default (pad, event);
635         break;
636     }
637     return;
638   }
639
640   buffer = GST_BUFFER (_data);
641
642   new_width =
643       video_box->in_width - (video_box->box_left + video_box->box_right);
644   new_height =
645       video_box->in_height - (video_box->box_top + video_box->box_bottom);
646
647   if (new_width != video_box->out_width ||
648       new_height != video_box->out_height ||
649       !GST_PAD_CAPS (video_box->srcpad)) {
650     GstCaps *newcaps;
651
652     newcaps = gst_caps_copy (gst_pad_get_negotiated_caps (video_box->sinkpad));
653
654     video_box->use_alpha = TRUE;
655
656     /* try AYUV first */
657     gst_caps_set_simple (newcaps,
658         "format", GST_TYPE_FOURCC, GST_STR_FOURCC ("AYUV"),
659         "width", G_TYPE_INT, new_width, "height", G_TYPE_INT, new_height, NULL);
660
661     if (GST_PAD_LINK_FAILED (gst_pad_try_set_caps (video_box->srcpad, newcaps))) {
662       video_box->use_alpha = FALSE;
663       newcaps =
664           gst_caps_copy (gst_pad_get_negotiated_caps (video_box->sinkpad));
665       gst_caps_set_simple (newcaps, "format", GST_TYPE_FOURCC,
666           GST_STR_FOURCC ("I420"), "width", G_TYPE_INT, new_width, "height",
667           G_TYPE_INT, new_height, NULL);
668
669       if (GST_PAD_LINK_FAILED (gst_pad_try_set_caps (video_box->srcpad,
670                   newcaps))) {
671         GST_ELEMENT_ERROR (video_box, CORE, NEGOTIATION, (NULL), (NULL));
672         return;
673       }
674     }
675
676     video_box->out_width = new_width;
677     video_box->out_height = new_height;
678   }
679
680   if (video_box->use_alpha) {
681     outbuf = gst_pad_alloc_buffer (video_box->srcpad,
682         GST_BUFFER_OFFSET_NONE, new_width * new_height * 4);
683
684     gst_video_box_ayuv (video_box,
685         GST_BUFFER_DATA (buffer), GST_BUFFER_DATA (outbuf));
686   } else {
687     outbuf = gst_pad_alloc_buffer (video_box->srcpad,
688         GST_BUFFER_OFFSET_NONE, (new_width * new_height * 3) / 2);
689
690     gst_video_box_i420 (video_box,
691         GST_BUFFER_DATA (buffer), GST_BUFFER_DATA (outbuf));
692   }
693   GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buffer);
694   GST_BUFFER_DURATION (outbuf) = GST_BUFFER_DURATION (buffer);
695
696
697   gst_buffer_unref (buffer);
698
699   gst_pad_push (video_box->srcpad, GST_DATA (outbuf));
700 }
701
702 static GstElementStateReturn
703 gst_video_box_change_state (GstElement * element)
704 {
705   GstVideoBox *video_box;
706
707   video_box = GST_VIDEO_BOX (element);
708
709   switch (GST_STATE_TRANSITION (element)) {
710     case GST_STATE_NULL_TO_READY:
711       break;
712     case GST_STATE_READY_TO_PAUSED:
713       break;
714     case GST_STATE_PAUSED_TO_PLAYING:
715       break;
716     case GST_STATE_PLAYING_TO_PAUSED:
717       break;
718     case GST_STATE_PAUSED_TO_READY:
719       break;
720     case GST_STATE_READY_TO_NULL:
721       break;
722   }
723
724   parent_class->change_state (element);
725
726   return GST_STATE_SUCCESS;
727 }
728
729 static gboolean
730 plugin_init (GstPlugin * plugin)
731 {
732   return gst_element_register (plugin, "videobox", GST_RANK_NONE,
733       GST_TYPE_VIDEO_BOX);
734 }
735
736 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
737     GST_VERSION_MINOR,
738     "videobox",
739     "resizes a video by adding borders or cropping",
740     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE, GST_ORIGIN)