videomixer: Add support for ARGB
authorSebastian Dröge <sebastian.droege@collabora.co.uk>
Wed, 8 Jul 2009 16:17:48 +0000 (18:17 +0200)
committerSebastian Dröge <sebastian.droege@collabora.co.uk>
Wed, 8 Jul 2009 16:17:48 +0000 (18:17 +0200)
And clean up the caps parsing.

gst/videomixer/Makefile.am
gst/videomixer/blend_bgra.c
gst/videomixer/videomixer.c

index 392c9b7..f3a5a10 100644 (file)
@@ -1,8 +1,8 @@
 plugin_LTLIBRARIES = libgstvideomixer.la
 
 libgstvideomixer_la_SOURCES = videomixer.c blend_ayuv.c blend_bgra.c blend_i420.c
-libgstvideomixer_la_CFLAGS = $(GST_CFLAGS) $(GST_BASE_CFLAGS) $(GST_CONTROLLER_CFLAGS)  $(GST_PLUGINS_BASE_CFLAGS)
-libgstvideomixer_la_LIBADD = $(GST_LIBS) $(GST_BASE_LIBS) $(GST_CONTROLLER_LIBS)
+libgstvideomixer_la_CFLAGS = $(GST_CFLAGS) $(GST_BASE_CFLAGS) $(GST_CONTROLLER_CFLAGS) $(GST_PLUGINS_BASE_CFLAGS)
+libgstvideomixer_la_LIBADD = $(GST_LIBS) $(GST_BASE_LIBS) $(GST_CONTROLLER_LIBS) $(GST_PLUGINS_BASE_CFLAGS) -lgstvideo-@GST_MAJORMINOR@
 libgstvideomixer_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS)
 libgstvideomixer_la_LIBTOOLFLAGS = --tag=disable-static
 
index 1c9630e..3cf0846 100644 (file)
 
 #define BLEND_MODE BLEND_NORMAL
 
-void
-gst_videomixer_blend_bgra_bgra (guint8 * src, gint xpos, gint ypos,
-    gint src_width, gint src_height, gdouble src_alpha,
-    guint8 * dest, gint dest_width, gint dest_height)
-{
-  gint alpha, b_alpha;
-  gint i, j;
-  gint src_stride, dest_stride;
-  gint src_add, dest_add;
-  gint B, G, R;
-
-  src_stride = src_width * 4;
-  dest_stride = dest_width * 4;
-
-  b_alpha = (gint) (src_alpha * 255);
-
-  /* adjust src pointers for negative sizes */
-  if (xpos < 0) {
-    src += -xpos * 4;
-    src_width -= -xpos;
-    xpos = 0;
-  }
-  if (ypos < 0) {
-    src += -ypos * src_stride;
-    src_height -= -ypos;
-    ypos = 0;
-  }
-  /* adjust width/height if the src is bigger than dest */
-  if (xpos + src_width > dest_width) {
-    src_width = dest_width - xpos;
-  }
-  if (ypos + src_height > dest_height) {
-    src_height = dest_height - ypos;
-  }
-
-  src_add = src_stride - (4 * src_width);
-  dest_add = dest_stride - (4 * src_width);
-
-  dest = dest + 4 * xpos + (ypos * dest_stride);
-
-  /* we convert a square of 2x2 samples to generate 4 Luma and 2 chroma samples */
-  for (i = 0; i < src_height; i++) {
-    for (j = 0; j < src_width; j++) {
-      alpha = (src[3] * b_alpha) >> 8;
-      BLEND_MODE (dest[0], dest[1], dest[2], src[0], src[1], src[2],
-          B, G, R, alpha);
-      dest[0] = B;
-      dest[1] = G;
-      dest[2] = R;
-      dest[3] = 0xff;
-
-      src += 4;
-      dest += 4;
-    }
-    src += src_add;
-    dest += dest_add;
-  }
-}
-
-#undef BLEND_MODE
-
-/* fill a buffer with a checkerboard pattern */
-void
-gst_videomixer_fill_bgra_checker (guint8 * dest, gint width, gint height)
-{
-  gint i, j;
-  static int tab[] = { 80, 160, 80, 160 };
-
-  for (i = 0; i < height; i++) {
-    for (j = 0; j < width; j++) {
-      *dest++ = tab[((i & 0x8) >> 3) + ((j & 0x8) >> 3)];       /* blue */
-      *dest++ = tab[((i & 0x8) >> 3) + ((j & 0x8) >> 3)];       /* green */
-      *dest++ = tab[((i & 0x8) >> 3) + ((j & 0x8) >> 3)];       /* red */
-      *dest++ = 0xFF;           /* alpha */
-    }
-  }
+#define CREATE_FUNCTIONS(name, a, r, g, b) \
+void \
+gst_videomixer_blend_##name##_##name (guint8 * src, gint xpos, gint ypos, \
+    gint src_width, gint src_height, gdouble src_alpha, \
+    guint8 * dest, gint dest_width, gint dest_height) \
+{ \
+  gint alpha, b_alpha; \
+  gint i, j; \
+  gint src_stride, dest_stride; \
+  gint src_add, dest_add; \
+  gint B, G, R; \
+  \
+  src_stride = src_width * 4; \
+  dest_stride = dest_width * 4; \
+  \
+  b_alpha = (gint) (src_alpha * 255); \
+  \
+  /* adjust src pointers for negative sizes */ \
+  if (xpos < 0) { \
+    src += -xpos * 4; \
+    src_width -= -xpos; \
+    xpos = 0; \
+  } \
+  if (ypos < 0) { \
+    src += -ypos * src_stride; \
+    src_height -= -ypos; \
+    ypos = 0; \
+  } \
+  /* adjust width/height if the src is bigger than dest */ \
+  if (xpos + src_width > dest_width) { \
+    src_width = dest_width - xpos; \
+  } \
+  if (ypos + src_height > dest_height) { \
+    src_height = dest_height - ypos; \
+  } \
+  \
+  src_add = src_stride - (4 * src_width); \
+  dest_add = dest_stride - (4 * src_width); \
+  \
+  dest = dest + 4 * xpos + (ypos * dest_stride); \
+  \
+  /* we convert a square of 2x2 samples to generate 4 Luma and 2 chroma samples */ \
+  for (i = 0; i < src_height; i++) { \
+    for (j = 0; j < src_width; j++) { \
+      alpha = (src[a] * b_alpha) >> 8; \
+      BLEND_MODE (dest[b], dest[g], dest[r], src[b], src[g], src[r], \
+          B, G, R, alpha); \
+      dest[b] = B; \
+      dest[g] = G; \
+      dest[r] = R; \
+      dest[a] = 0xff; \
+      \
+      src += 4; \
+      dest += 4; \
+    } \
+    src += src_add; \
+    dest += dest_add; \
+  } \
+} \
+\
+/* fill a buffer with a checkerboard pattern */ \
+void \
+gst_videomixer_fill_##name##_checker (guint8 * dest, gint width, gint height) \
+{ \
+  gint i, j; \
+  static const int tab[] = { 80, 160, 80, 160 }; \
+  \
+  for (i = 0; i < height; i++) { \
+    for (j = 0; j < width; j++) { \
+      dest[b] = tab[((i & 0x8) >> 3) + ((j & 0x8) >> 3)];       /* blue */ \
+      dest[g] = tab[((i & 0x8) >> 3) + ((j & 0x8) >> 3)];       /* green */ \
+      dest[r] = tab[((i & 0x8) >> 3) + ((j & 0x8) >> 3)];       /* red */ \
+      dest[a] = 0xFF;           /* alpha */ \
+      dest += 4; \
+    } \
+  } \
+} \
+\
+void \
+gst_videomixer_fill_##name##_color (guint8 * dest, gint width, gint height, \
+    gint colY, gint colU, gint colV) \
+{ \
+  gint red, green, blue; \
+  gint i, j; \
+  \
+  red = CLAMP (1.164 * (colY - 16) + 1.596 * (colV - 128), 0, 255); \
+  green = \
+      CLAMP (1.164 * (colY - 16) - 0.813 * (colV - 128) - 0.391 * (colU - 128), \
+      0, 255); \
+  blue = CLAMP (1.164 * (colY - 16) + 2.018 * (colU - 128), 0, 255); \
+  \
+  for (i = 0; i < height; i++) { \
+    for (j = 0; j < width; j++) { \
+      dest[b] = blue; \
+      dest[g] = green; \
+      dest[r] = red; \
+      dest[a] = 0xff; \
+      dest += 4; \
+    } \
+  } \
 }
 
-void
-gst_videomixer_fill_bgra_color (guint8 * dest, gint width, gint height,
-    gint colY, gint colU, gint colV)
-{
-  gint red, green, blue;
-  gint i, j;
-
-  red = CLAMP (1.164 * (colY - 16) + 1.596 * (colV - 128), 0, 255);
-  green =
-      CLAMP (1.164 * (colY - 16) - 0.813 * (colV - 128) - 0.391 * (colU - 128),
-      0, 255);
-  blue = CLAMP (1.164 * (colY - 16) + 2.018 * (colU - 128), 0, 255);
-
-  for (i = 0; i < height; i++) {
-    for (j = 0; j < width; j++) {
-      *dest++ = blue;
-      *dest++ = green;
-      *dest++ = red;
-      *dest++ = 0xff;
-    }
-  }
-}
+CREATE_FUNCTIONS (argb, 0, 1, 2, 3);
+CREATE_FUNCTIONS (bgra, 3, 2, 1, 0);
 
 size_t
 gst_videomixer_calculate_frame_size_bgra (gint width, gint height)
 {
   return GST_ROUND_UP_4 (width) * height * 4;
 }
+
+#undef BLEND_MODE
index 47689a6..f199a73 100644 (file)
 /**
  * SECTION:element-videomixer
  *
- * Videomixer can accept AYUV and BGRA video streams. For each of the requested
+ * Videomixer can accept AYUV, ARGB and BGRA video streams. For each of the requested
  * sink pads it will compare the incoming geometry and framerate to define the
  * output parameters. Indeed output video frames will have the geometry of the
  * biggest incoming video stream and the framerate of the fastest incoming one.
  *
- * All sink pads must be either AYUV or BGRA, but a mixture of them is not 
+ * All sink pads must be either AYUV, ARGB or BGRA, but a mixture of them is not 
  * supported. The src pad will have the same colorspace as the sinks. 
  * No colorspace conversion is done. 
  * 
@@ -105,13 +105,19 @@ void gst_videomixer_fill_ayuv_checker (guint8 * dest, gint width, gint height);
 void gst_videomixer_fill_ayuv_color (guint8 * dest, gint width, gint height,
     gint colY, gint colU, gint colV);
 size_t gst_videomixer_calculate_frame_size_ayuv (gint width, gint height);
-/*BGRA function definitions see file: blend_ayuv*/
+/*BGRA/ARGB function definitions see file: blend_bgra*/
 void gst_videomixer_blend_bgra_bgra (guint8 * src, gint xpos, gint ypos,
     gint src_width, gint src_height, gdouble src_alpha,
     guint8 * dest, gint dest_width, gint dest_height);
 void gst_videomixer_fill_bgra_checker (guint8 * dest, gint width, gint height);
 void gst_videomixer_fill_bgra_color (guint8 * dest, gint width, gint height,
     gint colY, gint colU, gint colV);
+void gst_videomixer_blend_argb_argb (guint8 * src, gint xpos, gint ypos,
+    gint src_width, gint src_height, gdouble src_alpha,
+    guint8 * dest, gint dest_width, gint dest_height);
+void gst_videomixer_fill_argb_checker (guint8 * dest, gint width, gint height);
+void gst_videomixer_fill_argb_color (guint8 * dest, gint width, gint height,
+    gint colY, gint colU, gint colV);
 size_t gst_videomixer_calculate_frame_size_bgra (gint width, gint height);
 /*I420 function definitions see file: blend_i420.c*/
 void gst_videomixer_blend_i420_i420 (guint8 * src, gint xpos, gint ypos,
@@ -462,14 +468,14 @@ static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
     GST_PAD_SRC,
     GST_PAD_ALWAYS,
     GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("AYUV") ";" GST_VIDEO_CAPS_BGRA ";"
-        GST_VIDEO_CAPS_YUV ("I420"))
+        GST_VIDEO_CAPS_ARGB ";" GST_VIDEO_CAPS_YUV ("I420"))
     );
 
 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%d",
     GST_PAD_SINK,
     GST_PAD_REQUEST,
     GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("AYUV") ";" GST_VIDEO_CAPS_BGRA ";"
-        GST_VIDEO_CAPS_YUV ("I420"))
+        GST_VIDEO_CAPS_ARGB ";" GST_VIDEO_CAPS_YUV ("I420"))
     );
 
 static void gst_videomixer_finalize (GObject * object);
@@ -914,56 +920,57 @@ gst_videomixer_getcaps (GstPad * pad)
 static gboolean
 gst_videomixer_setcaps (GstPad * pad, GstCaps * caps)
 {
-  GstElement *element;
-  GstVideoMixer *mixer;
-  GstStructure *str;
-  element = gst_pad_get_parent_element (pad);
-  g_assert (element);
-  mixer = GST_VIDEO_MIXER (element);
-  g_assert (mixer);
+  GstVideoMixer *mixer = GST_VIDEO_MIXER (gst_pad_get_parent_element (pad));
+  GstVideoFormat fmt;
+  gboolean ret = FALSE;
+
   GST_INFO_OBJECT (mixer, "set src caps: \n%" GST_PTR_FORMAT, caps);
 
-  str = gst_caps_get_structure (caps, 0);
-
-  if (gst_structure_has_name (str, "video/x-raw-yuv")) {
-    guint32 format;
-    int ret;
-    ret = gst_structure_get_fourcc (str, "format", &format);
-    if (!ret) {
-      mixer->blend = NULL;
-      mixer->fill_checker = NULL;
-      mixer->fill_color = NULL;
-      mixer->calculate_frame_size = NULL;
-    } else if (format == GST_STR_FOURCC ("AYUV")) {
+  mixer->blend = NULL;
+  mixer->fill_checker = NULL;
+  mixer->fill_color = NULL;
+  mixer->calculate_frame_size = NULL;
+
+  if (!gst_video_format_parse_caps (caps, &fmt, NULL, NULL))
+    goto done;
+
+  switch (fmt) {
+    case GST_VIDEO_FORMAT_AYUV:
       mixer->blend = gst_videomixer_blend_ayuv_ayuv;
       mixer->fill_checker = gst_videomixer_fill_ayuv_checker;
       mixer->fill_color = gst_videomixer_fill_ayuv_color;
       mixer->calculate_frame_size = gst_videomixer_calculate_frame_size_ayuv;
-    } else if (format == GST_STR_FOURCC ("I420")) {
+      ret = TRUE;
+      break;
+    case GST_VIDEO_FORMAT_I420:
       mixer->blend = gst_videomixer_blend_i420_i420;
       mixer->fill_checker = gst_videomixer_fill_i420_checker;
       mixer->fill_color = gst_videomixer_fill_i420_color;
       mixer->calculate_frame_size = gst_videomixer_calculate_frame_size_i420;
-    } else {
-      mixer->blend = NULL;
-      mixer->fill_checker = NULL;
-      mixer->fill_color = NULL;
-      mixer->calculate_frame_size = NULL;
-    }
-  } else if (gst_structure_has_name (str, "video/x-raw-rgb")) {
-    mixer->blend = gst_videomixer_blend_bgra_bgra;
-    mixer->fill_checker = gst_videomixer_fill_bgra_checker;
-    mixer->fill_color = gst_videomixer_fill_bgra_color;
-    mixer->calculate_frame_size = gst_videomixer_calculate_frame_size_bgra;
-  } else {
-    mixer->blend = NULL;
-    mixer->fill_checker = NULL;
-    mixer->fill_color = NULL;
-    mixer->calculate_frame_size = NULL;
+      ret = TRUE;
+      break;
+    case GST_VIDEO_FORMAT_BGRA:
+      mixer->blend = gst_videomixer_blend_bgra_bgra;
+      mixer->fill_checker = gst_videomixer_fill_bgra_checker;
+      mixer->fill_color = gst_videomixer_fill_bgra_color;
+      mixer->calculate_frame_size = gst_videomixer_calculate_frame_size_bgra;
+      ret = TRUE;
+      break;
+    case GST_VIDEO_FORMAT_ARGB:
+      mixer->blend = gst_videomixer_blend_argb_argb;
+      mixer->fill_checker = gst_videomixer_fill_argb_checker;
+      mixer->fill_color = gst_videomixer_fill_argb_color;
+      mixer->calculate_frame_size = gst_videomixer_calculate_frame_size_bgra;
+      ret = TRUE;
+      break;
+    default:
+      break;
   }
-  gst_object_unref (element);
 
-  return TRUE;
+done:
+  gst_object_unref (mixer);
+
+  return ret;
 }
 
 static GstPad *