From 854a7e0af7f9e106ff389ba0b2d4361021639fea Mon Sep 17 00:00:00 2001 From: Thomas Vander Stichele Date: Sun, 23 Dec 2001 14:25:32 +0000 Subject: adding hermes Original commit message from CVS: adding hermes --- ext/hermes/Makefile.am | 17 + ext/hermes/gstcolorspace.c | 501 +++++++++++++++++++++++++++ ext/hermes/gstcolorspace.h | 95 ++++++ ext/hermes/yuv2rgb.c | 823 +++++++++++++++++++++++++++++++++++++++++++++ ext/hermes/yuv2rgb.h | 68 ++++ ext/hermes/yuv2rgb_mmx16.s | 188 +++++++++++ ext/hermes/yuv2yuv.c | 55 +++ 7 files changed, 1747 insertions(+) create mode 100644 ext/hermes/Makefile.am create mode 100644 ext/hermes/gstcolorspace.c create mode 100644 ext/hermes/gstcolorspace.h create mode 100644 ext/hermes/yuv2rgb.c create mode 100644 ext/hermes/yuv2rgb.h create mode 100644 ext/hermes/yuv2rgb_mmx16.s create mode 100644 ext/hermes/yuv2yuv.c (limited to 'ext') diff --git a/ext/hermes/Makefile.am b/ext/hermes/Makefile.am new file mode 100644 index 00000000..b7447e5e --- /dev/null +++ b/ext/hermes/Makefile.am @@ -0,0 +1,17 @@ +plugindir = $(libdir)/gst + +plugin_LTLIBRARIES = libgstcolorspace.la + +if HAVE_CPU_I386 +ARCHSRCS = yuv2rgb_mmx16.s +else +ARCHSRCS = +endif + +libcolorspace_la_SOURCES = gstcolorspace.c yuv2yuv.c yuv2rgb.c $(ARCHSRCS) +libcolorspace_la_CFLAGS = $(GST_CFLAGS) +if HAVE_LIBHERMES +libcolorspace_la_LIBADD = $(HERMES_LIBS) +endif + +noinst_HEADERS = gstcolorspace.h yuv2rgb.h diff --git a/ext/hermes/gstcolorspace.c b/ext/hermes/gstcolorspace.c new file mode 100644 index 00000000..eebe19b6 --- /dev/null +++ b/ext/hermes/gstcolorspace.c @@ -0,0 +1,501 @@ +/* Gnome-Streamer + * Copyright (C) <1999> Erik Walthinsen + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include + +#include "gstcolorspace.h" +#include "yuv2rgb.h" + + +static GstElementDetails colorspace_details = { + "Colorspace converter", + "Filter/Effect", + "Converts video from one colorspace to another", + VERSION, + "Wim Taymans ", + "(C) 2001", +}; + + +/* Stereo signals and args */ +enum { + /* FILL ME */ + LAST_SIGNAL +}; + +enum { + ARG_0, + ARG_SOURCE, + ARG_DEST, +}; + +GST_PADTEMPLATE_FACTORY (colorspace_src_template_factory, + "src", + GST_PAD_SRC, + GST_PAD_ALWAYS, + GST_CAPS_NEW ( + "colorspace_src", + "video/raw", + NULL + ) +) + +GST_PADTEMPLATE_FACTORY (colorspace_sink_template_factory, + "sink", + GST_PAD_SINK, + GST_PAD_ALWAYS, + GST_CAPS_NEW ( + "colorspace_sink", + "video/raw", + NULL + ) +) + +static void gst_colorspace_class_init (GstColorspaceClass *klass); +static void gst_colorspace_init (GstColorspace *space); + +static void gst_colorspace_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec); +static void gst_colorspace_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec); + +static void gst_colorspace_chain (GstPad *pad, GstBuffer *buf); + +// FIXME +extern void gst_colorspace_yuy2_to_i420(unsigned char *src, unsigned char *dest, guint width, guint height); + +static GstElementClass *parent_class = NULL; +//static guint gst_colorspace_signals[LAST_SIGNAL] = { 0 }; + +static GstBufferPool* +colorspace_get_bufferpool (GstPad *pad) +{ + GstColorspace *space; + + space = GST_COLORSPACE (gst_pad_get_parent (pad)); + + if (space->type == GST_COLORSPACE_NONE) + return gst_pad_get_bufferpool (space->srcpad); + else + return NULL; +} + +static gboolean +colorspace_setup_converter (GstColorspace *space) +{ + gulong from_space, to_space; + GstCaps *from_caps, *to_caps; + + from_caps = space->sinkcaps; + to_caps = space->srccaps; + + g_return_val_if_fail (to_caps != NULL, FALSE); + g_return_val_if_fail (from_caps != NULL, FALSE); + + from_space = gst_caps_get_fourcc_int (from_caps, "format"); + to_space = gst_caps_get_fourcc_int (to_caps, "format"); + + g_warning ("set up converter for %08lx to %08lx\n", from_space, to_space); + + switch (from_space) { + case GST_MAKE_FOURCC ('R','G','B',' '): + switch (to_space) { +#ifdef HAVE_LIBHERMES + case GST_MAKE_FOURCC ('R','G','B',' '): + { + space->source.r = gst_caps_get_int (from_caps, "red_mask"); + space->source.g = gst_caps_get_int (from_caps, "green_mask"); + space->source.b = gst_caps_get_int (from_caps, "blue_mask"); + space->source.a = 0; + space->srcbpp = space->source.bits = gst_caps_get_int (from_caps, "bpp"); + space->source.indexed = 0; + space->source.has_colorkey = 0; + + GST_INFO (0,"source red mask %08x\n", space->source.r); + GST_INFO (0, "source green mask %08x\n", space->source.g); + GST_INFO (0, "source blue mask %08x\n", space->source.b); + GST_INFO (0, "source bpp %08x\n", space->srcbpp); + + space->dest.r = gst_caps_get_int (to_caps, "red_mask"); + space->dest.g = gst_caps_get_int (to_caps, "green_mask"); + space->dest.b = gst_caps_get_int (to_caps, "blue_mask"); + space->dest.a = 0; + space->destbpp = space->dest.bits = gst_caps_get_int (to_caps, "bpp"); + space->dest.indexed = 0; + space->dest.has_colorkey = 0; + + GST_INFO (0, "dest red mask %08x\n", space->dest.r); + GST_INFO (0, "dest green mask %08x\n", space->dest.g); + GST_INFO (0, "dest blue mask %08x\n", space->dest.b); + GST_INFO (0, "dest bpp %08x\n", space->destbpp); + + if (!Hermes_ConverterRequest (space->h_handle, &space->source, &space->dest)) { + g_warning ("could not get converter\n"); + return FALSE; + } + GST_INFO (0, "converter set up\n"); + space->type = GST_COLORSPACE_HERMES; + break; + } +#endif + case GST_MAKE_FOURCC ('Y','U','Y','2'): + case GST_MAKE_FOURCC ('I','4','2','0'): + g_error ("colorspace: RGB to YUV implement me"); + break; + } + break; + case GST_MAKE_FOURCC ('Y','U','Y','2'): + case GST_MAKE_FOURCC ('I','4','2','0'): + switch (to_space) { + case GST_MAKE_FOURCC ('R','G','B',' '): + g_warning ("colorspace: YUV to RGB"); + + space->destbpp = gst_caps_get_int (to_caps, "bpp"); + space->converter = gst_colorspace_yuv2rgb_get_converter (from_caps, to_caps); + space->type = GST_COLORSPACE_YUV_RGB; + break; + case GST_MAKE_FOURCC ('I','4','2','0'): + space->type = GST_COLORSPACE_YUY2_I420; + space->destbpp = 12; + break; + } + break; + } + return TRUE; +} + +static GstPadNegotiateReturn +colorspace_negotiate_src (GstPad *pad, GstCaps **caps, gpointer *data) +{ + GstColorspace* space = GST_COLORSPACE (gst_object_get_parent (GST_OBJECT (pad))); + GstCaps *original; + gint src_width, src_height; + + GST_DEBUG (GST_CAT_NEGOTIATION, "colorspace: src negotiate\n"); + + g_return_val_if_fail (space->sinkcaps != NULL, GST_PAD_NEGOTIATE_FAIL); + + src_width = gst_caps_get_int (space->sinkcaps, "width"); + src_height = gst_caps_get_int (space->sinkcaps, "height"); + + space->width = src_width; + space->height = src_height; + + if (*caps==NULL) { + *caps = gst_caps_new ("colorspace_caps", + "video/raw", + gst_props_new ( + "format", GST_PROPS_FOURCC (GST_STR_FOURCC ("I420")), + "width", GST_PROPS_INT (src_width), + "height", GST_PROPS_INT (src_height), + NULL)); + space->srccaps = gst_caps_ref (*caps); + return GST_PAD_NEGOTIATE_TRY; + //return gst_pad_negotiate_proxy (pad, space->sinkpad, caps); + } + + + original = gst_caps_copy (*caps); + //g_print ("%d %d\n", src_width, src_height); + + // peers couldn't agree, we need to help + switch (gst_caps_get_fourcc_int (original, "format")) { + case GST_MAKE_FOURCC ('R','G','B',' '): + gst_caps_ref (*caps); + if (gst_caps_get_int (*caps, "width") == src_width && + gst_caps_get_int (*caps, "height") == src_height) + { + space->srccaps = *caps; + if (colorspace_setup_converter (space)) { + return GST_PAD_NEGOTIATE_AGREE; + } + } + else { + gst_caps_set (*caps, "width", GST_PROPS_INT (src_width)); + gst_caps_set (*caps, "height", GST_PROPS_INT (src_height)); + + space->srccaps = *caps; + // FIXME + GST_PAD_CAPS (space->srcpad) = gst_caps_ref (*caps); + + return GST_PAD_NEGOTIATE_TRY; + } + break; + case GST_MAKE_FOURCC ('Y','U','Y','2'): + case GST_MAKE_FOURCC ('I','4','2','0'): + //space->srccaps = original; + //fprintf (stderr, "found something suitable\n"); + return GST_PAD_NEGOTIATE_AGREE; + default: + *caps = NULL; + return GST_PAD_NEGOTIATE_TRY; + break; + } + return GST_PAD_NEGOTIATE_FAIL; +} + +static GstPadNegotiateReturn +colorspace_negotiate_sink (GstPad *pad, GstCaps **caps, gpointer *data) +{ + GstColorspace* space = GST_COLORSPACE (gst_object_get_parent (GST_OBJECT (pad))); + GstCaps *original; + + GST_DEBUG (GST_CAT_NEGOTIATION, "colorspace: sink negotiate\n"); + + if (*caps==NULL) + return gst_pad_negotiate_proxy (pad, space->srcpad, caps); + //return GST_PAD_NEGOTIATE_FAIL; + + + space->type = GST_COLORSPACE_NONE; + + original = gst_caps_copy (*caps); + + // see if a common format exists between both peers... + switch (gst_pad_negotiate_proxy (pad, space->srcpad, caps)) { + case GST_PAD_NEGOTIATE_AGREE: + //g_print ("colorspace: common format found\n"); + return GST_PAD_NEGOTIATE_AGREE; + default: + break; + } + g_warning ("colorspace: no common format found\n"); + g_warning ("colorspace: src: %08lx\n", gst_caps_get_fourcc_int (original, "format")); + + // peers couldn't agree, we need to help + space->sinkcaps = original; + + /* + space->width = gst_caps_get_int (original, "width"); + space->height = gst_caps_get_int (original, "height"); + + space->srccaps = gst_caps_new ( + "testcaps", + "video/raw", + gst_props_new ( + "format", GST_PROPS_FOURCC (GST_STR_FOURCC ("I420")), + "width", GST_PROPS_INT (gst_caps_get_int (original, "width")), + "height", GST_PROPS_INT (gst_caps_get_int (original, "height")), + NULL + )); + + GST_PAD_CAPS (space->srcpad) = space->srccaps; + */ + + if (gst_pad_renegotiate (space->srcpad)) { + g_warning ("found something suitable\n"); + if (colorspace_setup_converter (space)) { + return GST_PAD_NEGOTIATE_AGREE; + } + } + + return GST_PAD_NEGOTIATE_FAIL; +} + +GType +gst_colorspace_get_type (void) +{ + static GType colorspace_type = 0; + + if (!colorspace_type) { + static const GTypeInfo colorspace_info = { + sizeof(GstColorspaceClass), NULL, + NULL, + (GClassInitFunc)gst_colorspace_class_init, + NULL, + NULL, + sizeof(GstColorspace), + 0, + (GInstanceInitFunc)gst_colorspace_init, + }; + colorspace_type = g_type_register_static(GST_TYPE_ELEMENT, "GstColorspace", &colorspace_info, 0); + } + return colorspace_type; +} + +static void +gst_colorspace_class_init (GstColorspaceClass *klass) +{ + GObjectClass *gobject_class; + GstElementClass *gstelement_class; + + gobject_class = (GObjectClass*)klass; + gstelement_class = (GstElementClass*)klass; + + parent_class = g_type_class_ref(GST_TYPE_ELEMENT); + + gobject_class->set_property = gst_colorspace_set_property; + gobject_class->get_property = gst_colorspace_get_property; +} + +static void +gst_colorspace_init (GstColorspace *space) +{ + space->sinkpad = gst_pad_new_from_template ( + GST_PADTEMPLATE_GET (colorspace_sink_template_factory), "sink"); + gst_pad_set_negotiate_function (space->sinkpad, colorspace_negotiate_sink); + gst_pad_set_bufferpool_function (space->sinkpad, colorspace_get_bufferpool); + gst_pad_set_chain_function(space->sinkpad,gst_colorspace_chain); + gst_element_add_pad(GST_ELEMENT(space),space->sinkpad); + + space->srcpad = gst_pad_new_from_template ( + GST_PADTEMPLATE_GET (colorspace_src_template_factory), "src"); + gst_pad_set_negotiate_function (space->srcpad, colorspace_negotiate_src); + gst_element_add_pad(GST_ELEMENT(space),space->srcpad); + +#ifdef HAVE_LIBHERMES + space->h_handle = Hermes_ConverterInstance (0); +#endif + space->pool = NULL; + space->converter = NULL; +} + +static void +gst_colorspace_chain (GstPad *pad,GstBuffer *buf) +{ + GstColorspace *space; + gint size; + GstBuffer *outbuf = NULL; + + g_return_if_fail (pad != NULL); + g_return_if_fail (GST_IS_PAD (pad)); + g_return_if_fail (buf != NULL); + + space = GST_COLORSPACE (gst_pad_get_parent (pad)); + + g_return_if_fail (space != NULL); + g_return_if_fail (GST_IS_COLORSPACE (space)); + + if (space->type == GST_COLORSPACE_NONE) { + outbuf = buf; + } + else { + gint dest_bytes, src_bytes; + + if (!space->pool) { + space->pool = gst_pad_get_bufferpool (space->srcpad); + } + + size = space->width * space->height; + dest_bytes = ((space->destbpp+7)/8); + src_bytes = ((space->srcbpp+7)/8); + + if (space->pool) { + outbuf = gst_buffer_new_from_pool (space->pool, 0, 0); + } + + if (!outbuf) { + outbuf = gst_buffer_new (); + + GST_BUFFER_SIZE (outbuf) = (size * space->destbpp)/8; + GST_BUFFER_DATA (outbuf) = g_malloc (GST_BUFFER_SIZE (outbuf)); + } + + if (space->type == GST_COLORSPACE_YUV_RGB) { + gst_colorspace_convert (space->converter, GST_BUFFER_DATA (buf), GST_BUFFER_DATA (outbuf)); + } +#ifdef HAVE_LIBHERMES + else if (space->type == GST_COLORSPACE_HERMES) { + Hermes_ConverterCopy (space->h_handle, + GST_BUFFER_DATA (buf), 0, 0, space->width, space->height, space->width * src_bytes, + GST_BUFFER_DATA (outbuf), 0, 0, space->width, space->height, space->width * dest_bytes); + } +#endif + else if (space->type == GST_COLORSPACE_YUY2_I420) { + gst_colorspace_yuy2_to_i420 (GST_BUFFER_DATA (buf), + GST_BUFFER_DATA (outbuf), + space->width, + space->height); + } + + GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf); + + gst_buffer_unref (buf); + } + gst_pad_push (space->srcpad, outbuf); +} + +static void +gst_colorspace_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) +{ + GstColorspace *space; + + /* it's not null if we got it, but it might not be ours */ + g_return_if_fail(GST_IS_COLORSPACE(object)); + space = GST_COLORSPACE(object); + + switch (prop_id) { + default: + break; + } +} + +static void +gst_colorspace_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) +{ + GstColorspace *space; + + /* it's not null if we got it, but it might not be ours */ + g_return_if_fail(GST_IS_COLORSPACE(object)); + space = GST_COLORSPACE(object); + + switch (prop_id) { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static gboolean +plugin_init (GModule *module, GstPlugin *plugin) +{ + GstElementFactory *factory; +#ifdef HAVE_LIBHERMES + gint hermes_res; + + hermes_res = Hermes_Init(); + g_return_val_if_fail (hermes_res != 0, FALSE); +#endif + + factory = gst_elementfactory_new ("colorspace", GST_TYPE_COLORSPACE, + &colorspace_details); + g_return_val_if_fail (factory != NULL, FALSE); + + gst_elementfactory_add_padtemplate (factory, + GST_PADTEMPLATE_GET (colorspace_src_template_factory)); + gst_elementfactory_add_padtemplate (factory, + GST_PADTEMPLATE_GET (colorspace_sink_template_factory)); + + gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory)); + + return TRUE; +} + +GstPluginDesc plugin_desc = { + GST_VERSION_MAJOR, + GST_VERSION_MINOR, + "colorspace", + plugin_init +}; + + + + + + + + diff --git a/ext/hermes/gstcolorspace.h b/ext/hermes/gstcolorspace.h new file mode 100644 index 00000000..8d780b41 --- /dev/null +++ b/ext/hermes/gstcolorspace.h @@ -0,0 +1,95 @@ +/* Gnome-Streamer + * Copyright (C) <1999> Erik Walthinsen + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + + +#ifndef __GST_COLORSPACE_H__ +#define __GST_COLORSPACE_H__ + + +#include +#include +#include "yuv2rgb.h" + +#ifdef HAVE_LIBHERMES +# include +#endif + +// #include + + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + + +#define GST_TYPE_COLORSPACE \ + (gst_colorspace_get_type()) +#define GST_COLORSPACE(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_COLORSPACE,GstColorspace)) +#define GST_COLORSPACE_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_ULAW,GstColorspace)) +#define GST_IS_COLORSPACE(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_COLORSPACE)) +#define GST_IS_COLORSPACE_CLASS(obj) \ + (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_COLORSPACE)) + +typedef struct _GstColorspace GstColorspace; +typedef struct _GstColorspaceClass GstColorspaceClass; + +typedef enum { + GST_COLORSPACE_NONE, + GST_COLORSPACE_HERMES, + GST_COLORSPACE_YUV_RGB, + GST_COLORSPACE_YUY2_I420, +} GstColorSpaceConverterType; + +struct _GstColorspace { + GstElement element; + + GstPad *sinkpad,*srcpad; + +#ifdef HAVE_LIBHERMES + HermesHandle h_handle; + HermesFormat source, dest; +#endif + + GstColorSpaceConverter *converter; + + GstColorSpaceConverterType type; + gint width, height; + gint srcbpp, destbpp; + + GstCaps *srccaps; + GstCaps *sinkcaps; + + GstBufferPool *pool; +}; + +struct _GstColorspaceClass { + GstElementClass parent_class; +}; + +GType gst_colorspace_get_type(void); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + + +#endif /* __GST_COLORSPACE_H */ diff --git a/ext/hermes/yuv2rgb.c b/ext/hermes/yuv2rgb.c new file mode 100644 index 00000000..0b5a9b4c --- /dev/null +++ b/ext/hermes/yuv2rgb.c @@ -0,0 +1,823 @@ +/* Gnome-Streamer + * Copyright (C) <1999> Erik Walthinsen + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include "config.h" + +#include +#include + +#include "yuv2rgb.h" + +//#undef HAVE_LIBMMX + +#ifdef HAVE_LIBMMX +#include "mmx.h" +#endif + +#define CB_BASE 1 +#define CR_BASE (CB_BASE*CB_RANGE) +#define LUM_BASE (CR_BASE*CR_RANGE) + +#define Min(x,y) (((x) < (y)) ? (x) : (y)) +#define Max(x,y) (((x) > (y)) ? (x) : (y)) + +#define GAMMA_CORRECTION(x) ((int)(pow((x) / 255.0, 1.0 / gammaCorrect) * 255.0)) +#define CHROMA_CORRECTION256(x) ((x) >= 128 \ + ? 128 + Min(127, (int)(((x) - 128.0) * chromaCorrect)) \ + : 128 - Min(128, (int)((128.0 - (x)) * chromaCorrect))) +#define CHROMA_CORRECTION128(x) ((x) >= 0 \ + ? Min(127, (int)(((x) * chromaCorrect))) \ + : Max(-128, (int)(((x) * chromaCorrect)))) +#define CHROMA_CORRECTION256D(x) ((x) >= 128 \ + ? 128.0 + Min(127.0, (((x) - 128.0) * chromaCorrect)) \ + : 128.0 - Min(128.0, (((128.0 - (x)) * chromaCorrect)))) +#define CHROMA_CORRECTION128D(x) ((x) >= 0 \ + ? Min(127.0, ((x) * chromaCorrect)) \ + : Max(-128.0, ((x) * chromaCorrect))) + + +static void gst_colorspace_yuv420P_to_rgb16 (GstColorSpaceConverter *space, unsigned char *src, unsigned char *dest); +static void gst_colorspace_yuv420P_to_rgb24 (GstColorSpaceConverter *space, unsigned char *src, unsigned char *dest); +static void gst_colorspace_yuv420P_to_rgb32 (GstColorSpaceConverter *space, unsigned char *src, unsigned char *dest); + +#ifdef HAVE_LIBMMX +static void gst_colorspace_yuv420P_to_bgr16_mmx (GstColorSpaceConverter *space, unsigned char *src, unsigned char *dest); +static void gst_colorspace_yuv420P_to_bgr32_mmx (GstColorSpaceConverter *space, unsigned char *src, unsigned char *dest); +#endif + +static void gst_colorspace_yuv_to_rgb16(GstColorSpaceYUVTables *tables, + unsigned char *lum, + unsigned char *cr, + unsigned char *cb, + unsigned char *out, + int cols, int rows); +static void gst_colorspace_yuv_to_rgb24(GstColorSpaceYUVTables *tables, + unsigned char *lum, + unsigned char *cr, + unsigned char *cb, + unsigned char *out, + int cols, int rows); +static void gst_colorspace_yuv_to_rgb32(GstColorSpaceYUVTables *tables, + unsigned char *lum, + unsigned char *cr, + unsigned char *cb, + unsigned char *out, + int cols, int rows); +#ifdef HAVE_LIBMMX +static void gst_colorspace_yuv_to_bgr32_mmx(GstColorSpaceYUVTables *tables, + unsigned char *lum, + unsigned char *cr, + unsigned char *cb, + unsigned char *out, + int cols, int rows); +extern void gst_colorspace_yuv_to_bgr16_mmx(GstColorSpaceYUVTables *tables, + unsigned char *lum, + unsigned char *cr, + unsigned char *cb, + unsigned char *out, + int cols, int rows); +#endif + +static GstColorSpaceYUVTables * gst_colorspace_init_yuv(long depth, + long red_mask, long green_mask, long blue_mask); + +GstColorSpaceConverter* +gst_colorspace_yuv2rgb_get_converter (GstCaps *from, GstCaps *to) +{ + gulong from_space, to_space; + GstColorSpaceConverter *new; + gint to_bpp; + + GST_DEBUG (0,"gst_colorspace_yuv2rgb_get_converter\n"); + + new = g_malloc (sizeof (GstColorSpaceConverter)); + + new->width = gst_caps_get_int (from, "width"); + new->height = gst_caps_get_int (from, "height"); + new->color_tables = NULL; + + from_space = gst_caps_get_fourcc_int (from, "format"); + to_space = gst_caps_get_fourcc_int (to, "format"); + to_bpp = gst_caps_get_int (to, "bpp"); + + // FIXME we leak new here. + g_return_val_if_fail (to_space == GST_STR_FOURCC ("RGB "), NULL); + + switch(from_space) { + case GST_MAKE_FOURCC ('I','4','2','0'): + { + gulong red_mask; + gulong green_mask; + gulong blue_mask; + + red_mask = gst_caps_get_int (to, "red_mask"); + green_mask = gst_caps_get_int (to, "green_mask"); + blue_mask = gst_caps_get_int (to, "blue_mask"); + + GST_INFO (GST_CAT_PLUGIN_INFO, "red_mask %08lx", red_mask); + GST_INFO (GST_CAT_PLUGIN_INFO, "green_mask %08lx", green_mask); + GST_INFO (GST_CAT_PLUGIN_INFO, "blue_mask %08lx", blue_mask); + + new->insize = new->width * new->height + new->width * new->height/2; + new->color_tables = gst_colorspace_init_yuv (to_bpp, red_mask, green_mask, blue_mask); + new->outsize = new->width * new->height * (to_bpp/8); + + switch(to_bpp) { + case 32: +#ifdef HAVE_LIBMMX + if (red_mask == 0xff0000 && green_mask == 0x00ff00 && red_mask == 0x0000ff && + (gst_cpu_get_flags () & GST_CPU_FLAG_MMX) ) { + new->convert = gst_colorspace_yuv420P_to_bgr32_mmx; + } + else +#endif + new->convert = gst_colorspace_yuv420P_to_rgb32; + break; + case 24: + new->convert = gst_colorspace_yuv420P_to_rgb24; + break; + case 15: + case 16: +#ifdef HAVE_LIBMMX + if (red_mask == 0xf800 && green_mask == 0x07e0 && blue_mask == 0x001f && + (gst_cpu_get_flags () & GST_CPU_FLAG_MMX) ) { + new->convert = gst_colorspace_yuv420P_to_bgr16_mmx; + } + else +#endif + new->convert = gst_colorspace_yuv420P_to_rgb16; + break; + default: + g_print("gst_colorspace_yuv2rgb not implemented\n"); + g_free (new); + new = NULL; + } + break; + } + default: + g_print("gst_colorspace_yuv2rgb not implemented\n"); + g_free (new); + new = NULL; + } + return new; +} + +static void gst_colorspace_yuv420P_to_rgb32(GstColorSpaceConverter *space, unsigned char *src, unsigned char *dest) +{ + int size; + GST_DEBUG (0,"gst_colorspace_yuv420P_to_rgb32\n"); + + size = space->width * space->height; + + gst_colorspace_yuv_to_rgb32(space->color_tables, + src, // Y component + src+size, // cr component + src+size+(size>>2), // cb component + dest, + space->height, + space->width); + +} + +static void gst_colorspace_yuv420P_to_rgb24(GstColorSpaceConverter *space, unsigned char *src, unsigned char *dest) { + int size; + GST_DEBUG (0,"gst_colorspace_yuv420P_to_rgb24\n"); + + size = space->width * space->height; + + gst_colorspace_yuv_to_rgb24(space->color_tables, + src, // Y component + src+size, // cr component + src+size+(size>>2), // cb component + dest, + space->height, + space->width); + +} + +static void gst_colorspace_yuv420P_to_rgb16(GstColorSpaceConverter *space, unsigned char *src, unsigned char *dest) { + int size; + GST_DEBUG (0,"gst_colorspace_yuv420P_to_rgb16\n"); + + size = space->width * space->height; + + gst_colorspace_yuv_to_rgb16(space->color_tables, + src, // Y component + src+size, // cr component + src+size+(size>>2), // cb component + dest, + space->height, + space->width); + +} + +#ifdef HAVE_LIBMMX +static void gst_colorspace_yuv420P_to_bgr32_mmx(GstColorSpaceConverter *space, unsigned char *src, unsigned char *dest) { + int size; + GST_DEBUG (0,"gst_colorspace_yuv420P_to_rgb32_mmx\n"); + + size = space->width * space->height; + + gst_colorspace_yuv_to_bgr32_mmx(NULL, + src, // Y component + src+size, // cr component + src+size+(size>>2), // cb component + dest, + space->height, + space->width); + +} +static void gst_colorspace_yuv420P_to_bgr16_mmx(GstColorSpaceConverter *space, unsigned char *src, unsigned char *dest) { + int size; + GST_DEBUG (0,"gst_colorspace_yuv420P_to_bgr16_mmx \n"); + + size = space->width * space->height; + + gst_colorspace_yuv_to_bgr16_mmx(NULL, + src, // Y component + src+size, // cr component + src+size+(size>>2), // cb component + dest, + space->height, + space->width); + GST_DEBUG (0,"gst_colorspace_yuv420P_to_bgr16_mmx done\n"); + +} +#endif + +/* + * How many 1 bits are there in the longword. + * Low performance, do not call often. + */ + +static int +number_of_bits_set(a) +unsigned long a; +{ + if(!a) return 0; + if(a & 1) return 1 + number_of_bits_set(a >> 1); + return(number_of_bits_set(a >> 1)); +} + +/* + * Shift the 0s in the least significant end out of the longword. + * Low performance, do not call often. + */ +static unsigned long +shifted_down(a) +unsigned long a; +{ + if(!a) return 0; + if(a & 1) return a; + return a >> 1; +} + +/* + * How many 0 bits are there at most significant end of longword. + * Low performance, do not call often. + */ +static int +free_bits_at_top(a) +unsigned long a; +{ + /* assume char is 8 bits */ + if(!a) return sizeof(unsigned long) * 8; + /* assume twos complement */ + if(((long)a) < 0l) return 0; + return 1 + free_bits_at_top ( a << 1); +} + +/* + * How many 0 bits are there at least significant end of longword. + * Low performance, do not call often. + */ +static int +free_bits_at_bottom(a) +unsigned long a; +{ + /* assume char is 8 bits */ + if(!a) return sizeof(unsigned long) * 8; + if(((long)a) & 1l) return 0; + return 1 + free_bits_at_bottom ( a >> 1); +} + +/* + *-------------------------------------------------------------- + * + * InitColor16Dither -- + * + * To get rid of the multiply and other conversions in color + * dither, we use a lookup table. + * + * Results: + * None. + * + * Side effects: + * The lookup tables are initialized. + * + *-------------------------------------------------------------- + */ + +static GstColorSpaceYUVTables * +gst_colorspace_init_yuv(long depth, long red_mask, long green_mask, long blue_mask) +{ + int CR, CB, i; + int *L_tab, *Cr_r_tab, *Cr_g_tab, *Cb_g_tab, *Cb_b_tab; + long *r_2_pix_alloc; + long *g_2_pix_alloc; + long *b_2_pix_alloc; + GstColorSpaceYUVTables *tables = g_malloc(sizeof(GstColorSpaceYUVTables)); + + L_tab = tables->L_tab = (int *)malloc(256*sizeof(int)); + Cr_r_tab = tables->Cr_r_tab = (int *)malloc(256*sizeof(int)); + Cr_g_tab = tables->Cr_g_tab = (int *)malloc(256*sizeof(int)); + Cb_g_tab = tables->Cb_g_tab = (int *)malloc(256*sizeof(int)); + Cb_b_tab = tables->Cb_b_tab = (int *)malloc(256*sizeof(int)); + + r_2_pix_alloc = (long *)malloc(768*sizeof(long)); + g_2_pix_alloc = (long *)malloc(768*sizeof(long)); + b_2_pix_alloc = (long *)malloc(768*sizeof(long)); + + if (L_tab == NULL || + Cr_r_tab == NULL || + Cr_g_tab == NULL || + Cb_g_tab == NULL || + Cb_b_tab == NULL || + r_2_pix_alloc == NULL || + g_2_pix_alloc == NULL || + b_2_pix_alloc == NULL) { + fprintf(stderr, "Could not get enough memory in InitColorDither\n"); + exit(1); + } + + for (i=0; i<256; i++) { + L_tab[i] = i; + /* + if (gammaCorrectFlag) { + L_tab[i] = GAMMA_CORRECTION(i); + } + */ + + CB = CR = i; + /* + if (chromaCorrectFlag) { + CB -= 128; + CB = CHROMA_CORRECTION128(CB); + CR -= 128; + CR = CHROMA_CORRECTION128(CR); + } + else + */ + { + CB -= 128; CR -= 128; + } + Cr_r_tab[i] = (0.419/0.299) * CR; + Cr_g_tab[i] = -(0.299/0.419) * CR; + Cb_g_tab[i] = -(0.114/0.331) * CB; + Cb_b_tab[i] = (0.587/0.331) * CB; + + } + + /* + * Set up entries 0-255 in rgb-to-pixel value tables. + */ + for (i = 0; i < 256; i++) { + r_2_pix_alloc[i + 256] = i >> (8 - number_of_bits_set(red_mask)); + r_2_pix_alloc[i + 256] <<= free_bits_at_bottom(red_mask); + g_2_pix_alloc[i + 256] = i >> (8 - number_of_bits_set(green_mask)); + g_2_pix_alloc[i + 256] <<= free_bits_at_bottom(green_mask); + b_2_pix_alloc[i + 256] = i >> (8 - number_of_bits_set(blue_mask)); + b_2_pix_alloc[i + 256] <<= free_bits_at_bottom(blue_mask); + /* + * If we have 16-bit output depth, then we double the value + * in the top word. This means that we can write out both + * pixels in the pixel doubling mode with one op. It is + * harmless in the normal case as storing a 32-bit value + * through a short pointer will lose the top bits anyway. + * A similar optimisation for Alpha for 64 bit has been + * prepared for, but is not yet implemented. + */ + if(!(depth == 32) && !(depth == 24)) { + + r_2_pix_alloc[i + 256] |= (r_2_pix_alloc[i + 256]) << 16; + g_2_pix_alloc[i + 256] |= (g_2_pix_alloc[i + 256]) << 16; + b_2_pix_alloc[i + 256] |= (b_2_pix_alloc[i + 256]) << 16; + + } +#ifdef SIXTYFOUR_BIT + if(depth == 32) { + + r_2_pix_alloc[i + 256] |= (r_2_pix_alloc[i + 256]) << 32; + g_2_pix_alloc[i + 256] |= (g_2_pix_alloc[i + 256]) << 32; + b_2_pix_alloc[i + 256] |= (b_2_pix_alloc[i + 256]) << 32; + + } +#endif + } + + /* + * Spread out the values we have to the rest of the array so that + * we do not need to check for overflow. + */ + for (i = 0; i < 256; i++) { + r_2_pix_alloc[i] = r_2_pix_alloc[256]; + r_2_pix_alloc[i+ 512] = r_2_pix_alloc[511]; + g_2_pix_alloc[i] = g_2_pix_alloc[256]; + g_2_pix_alloc[i+ 512] = g_2_pix_alloc[511]; + b_2_pix_alloc[i] = b_2_pix_alloc[256]; + b_2_pix_alloc[i+ 512] = b_2_pix_alloc[511]; + } + + tables->r_2_pix = r_2_pix_alloc + 256; + tables->g_2_pix = g_2_pix_alloc + 256; + tables->b_2_pix = b_2_pix_alloc + 256; + + return tables; + +} + +/* + *-------------------------------------------------------------- + * + * Color16DitherImage -- + * + * Converts image into 16 bit color. + * + * Results: + * None. + * + * Side effects: + * None. + * + *-------------------------------------------------------------- + */ + +static void +gst_colorspace_yuv_to_rgb16(tables, lum, cb, cr, out, rows, cols) + GstColorSpaceYUVTables *tables; + unsigned char *lum; + unsigned char *cr; + unsigned char *cb; + unsigned char *out; + int cols, rows; + +{ + int L, CR, CB; + unsigned short *row1, *row2; + unsigned char *lum2; + int x, y; + int cr_r; + int crb_g; + int cb_b; + int cols_2 = cols>>1; + + row1 = (unsigned short *)out; + row2 = row1 + cols; + lum2 = lum + cols; + + for (y=rows>>1; y; y--) { + for (x=cols_2; x; x--) { + + CR = *cr++; + CB = *cb++; + cr_r = tables->Cr_r_tab[CR]; + crb_g = tables->Cr_g_tab[CR] + tables->Cb_g_tab[CB]; + cb_b = tables->Cb_b_tab[CB]; + + L = tables->L_tab[(int) *lum++]; + + *row1++ = (tables->r_2_pix[L+cr_r] | tables->g_2_pix[L+crb_g] | tables->b_2_pix[L+cb_b]); + + L = tables->L_tab[(int) *lum++]; + + *row1++ = (tables->r_2_pix[L+cr_r] | tables->g_2_pix[L+crb_g] | tables->b_2_pix[L+cb_b]); + + /* + * Now, do second row. + */ + L = tables->L_tab[(int) *lum2++]; + + *row2++ = (tables->r_2_pix[L+cr_r] | tables->g_2_pix[L+crb_g] | tables->b_2_pix[L+cb_b]); + + L = tables->L_tab[(int) *lum2++]; + + *row2++ = (tables->r_2_pix[L+cr_r] | tables->g_2_pix[L+crb_g] | tables->b_2_pix[L+cb_b]); + } + /* + * These values are at the start of the next line, (due + * to the ++'s above),but they need to be at the start + * of the line after that. + */ + lum = lum2; + row1 = row2; + lum2 += cols; + row2 += cols; + } +} + +static void +gst_colorspace_yuv_to_rgb24(tables, lum, cb, cr, out, rows, cols) + GstColorSpaceYUVTables *tables; + unsigned char *lum; + unsigned char *cr; + unsigned char *cb; + unsigned char *out; + int cols, rows; + +{ + int L, CR, CB; + unsigned char *row1, *row2; + unsigned char *lum2; + int x, y; + int cr_r; + int crb_g; + int cb_b; + int cols_2 = cols>>1; + int cols_3 = cols*3; + unsigned char pixels[4]; + + row1 = out; + row2 = row1 + cols_3; + lum2 = lum + cols; + for (y=rows>>1; y; y--) { + for (x=cols_2; x; x--) { + + CR = *cr++; + CB = *cb++; + cr_r = tables->Cr_r_tab[CR]; + crb_g = tables->Cr_g_tab[CR] + tables->Cb_g_tab[CB]; + cb_b = tables->Cb_b_tab[CB]; + + L = tables->L_tab[(int) *lum++]; + + ((int *)pixels)[0] = (tables->r_2_pix[L+cr_r] | tables->g_2_pix[L+crb_g] | tables->b_2_pix[L+cb_b]); + *row1++ = pixels[0]; *row1++ = pixels[1]; *row1++ = pixels[2]; + + L = tables->L_tab[(int) *lum++]; + + ((int *)pixels)[0] = (tables->r_2_pix[L+cr_r] | tables->g_2_pix[L+crb_g] | tables->b_2_pix[L+cb_b]); + *row1++ = pixels[0]; *row1++ = pixels[1]; *row1++ = pixels[2]; + + /* + * Now, do second row. + */ + + L = tables->L_tab [(int) *lum2++]; + + ((int *)pixels)[0] = (tables->r_2_pix[L+cr_r] | tables->g_2_pix[L+crb_g] | tables->b_2_pix[L+cb_b]); + *row2++ = pixels[0]; *row2++ = pixels[1]; *row2++ = pixels[2]; + + L = tables->L_tab [(int) *lum2++]; + + ((int *)pixels)[0] = (tables->r_2_pix[L+cr_r] | tables->g_2_pix[L+crb_g] | tables->b_2_pix[L+cb_b]); + *row2++ = pixels[0]; *row2++ = pixels[1]; *row2++ = pixels[2]; + } + lum = lum2; + row1 = row2; + lum2 += cols; + row2 += cols_3; + } +} + +/* + *-------------------------------------------------------------- + * + * Color32DitherImage -- + * + * Converts image into 32 bit color (or 24-bit non-packed). + * + * Results: + * None. + * + * Side effects: + * None. + * + *-------------------------------------------------------------- + */ + +/* + * This is a copysoft version of the function above with ints instead + * of shorts to cause a 4-byte pixel size + */ + +static void +gst_colorspace_yuv_to_rgb32(tables, lum, cb, cr, out, rows, cols) + GstColorSpaceYUVTables *tables; + unsigned char *lum; + unsigned char *cr; + unsigned char *cb; + unsigned char *out; + int cols, rows; + +{ + int L, CR, CB; + unsigned int *row1, *row2; + unsigned char *lum2; + int x, y; + int cr_r; + int crb_g; + int cb_b; + int cols_2 = cols>>1; + + row1 = (guint32 *)out; + row2 = row1 + cols; + lum2 = lum + cols; + for (y=rows>>1; y; y--) { + for (x=cols_2; x; x--) { + + CR = *cr++; + CB = *cb++; + cr_r = tables->Cr_r_tab[CR]; + crb_g = tables->Cr_g_tab[CR] + tables->Cb_g_tab[CB]; + cb_b = tables->Cb_b_tab[CB]; + + L = tables->L_tab[(int) *lum++]; + + *row1++ = (tables->r_2_pix[L+cr_r] | tables->g_2_pix[L+crb_g] | tables->b_2_pix[L+cb_b]); + + L = tables->L_tab[(int) *lum++]; + + *row1++ = (tables->r_2_pix[L+cr_r] | tables->g_2_pix[L+crb_g] | tables->b_2_pix[L+cb_b]); + + /* + * Now, do second row. + */ + + L = tables->L_tab [(int) *lum2++]; + + *row2++ = (tables->r_2_pix[L+cr_r] | tables->g_2_pix[L+crb_g] | tables->b_2_pix[L+cb_b]); + + L = tables->L_tab [(int) *lum2++]; + + *row2++ = (tables->r_2_pix[L+cr_r] | tables->g_2_pix[L+crb_g] | tables->b_2_pix[L+cb_b]); + } + lum = lum2; + row1 = row2; + lum2 += cols; + row2 += cols; + } +} + +#ifdef HAVE_LIBMMX +static mmx_t MMX_80w = (mmx_t)(long long)0x0080008000800080LL; //dd 00080 0080h, 000800080h + +static mmx_t MMX_00FFw = (mmx_t)(long long)0x00ff00ff00ff00ffLL; //dd 000FF 00FFh, 000FF00FFh +static mmx_t MMX_FF00w = (mmx_t)(long long)0xff00ff00ff00ff00LL; //dd 000FF 00FFh, 000FF00FFh + +static mmx_t MMX32_Vredcoeff = (mmx_t)(long long)0x0059005900590059LL; +static mmx_t MMX32_Ubluecoeff = (mmx_t)(long long)0x0072007200720072LL; +static mmx_t MMX32_Ugrncoeff = (mmx_t)(long long)0xffeaffeaffeaffeaLL; +static mmx_t MMX32_Vgrncoeff = (mmx_t)(long long)0xffd2ffd2ffd2ffd2LL; + +static void +gst_colorspace_yuv_to_bgr32_mmx(tables, lum, cr, cb, out, rows, cols) + GstColorSpaceYUVTables *tables; + unsigned char *lum; + unsigned char *cr; + unsigned char *cb; + unsigned char *out; + int cols, rows; + +{ + guint32 *row1 = (guint32 *)out; // 32 bit target + int cols4 = cols>>2; + + int y, x; + + for (y=rows>>1; y; y--) { + for (x=cols4; x; x--) { + + // create Cr (result in mm1) + movd_m2r(*(mmx_t *)cb, mm1); // 0 0 0 0 v3 v2 v1 v0 + pxor_r2r(mm7, mm7); // 00 00 00 00 00 00 00 00 + movd_m2r(*(mmx_t *)lum, mm2); // 0 0 0 0 l3 l2 l1 l0 + punpcklbw_r2r(mm7, mm1); // 0 v3 0 v2 00 v1 00 v0 + punpckldq_r2r(mm1, mm1); // 00 v1 00 v0 00 v1 00 v0 + psubw_m2r(MMX_80w, mm1); // mm1-128:r1 r1 r0 r0 r1 r1 r0 r0 + + // create Cr_g (result in mm0) + movq_r2r(mm1, mm0); // r1 r1 r0 r0 r1 r1 r0 r0 + pmullw_m2r(MMX32_Vgrncoeff, mm0); // red*-46dec=0.7136*64 + pmullw_m2r(MMX32_Vredcoeff, mm1); // red*89dec=1.4013*64 + psraw_i2r(6, mm0); // red=red/64 + psraw_i2r(6, mm1); // red=red/64 + + // create L1 L2 (result in mm2,mm4) + // L2=lum+cols + movq_m2r(*(mmx_t *)(lum+cols),mm3); // 0 0 0 0 L3 L2 L1 L0 + punpckldq_r2r(mm3, mm2); // L3 L2 L1 L0 l3 l2 l1 l0 + movq_r2r(mm2, mm4); // L3 L2 L1 L0 l3 l2 l1 l0 + pand_m2r(MMX_FF00w, mm2); // L3 0 L1 0 l3 0 l1 0 + pand_m2r(MMX_00FFw, mm4); // 0 L2 0 L0 0 l2 0 l0 + psrlw_i2r(8, mm2); // 0 L3 0 L1 0 l3 0 l1 + + // create R (result in mm6) + movq_r2r(mm2, mm5); // 0 L3 0 L1 0 l3 0 l1 + movq_r2r(mm4, mm6); // 0 L2 0 L0 0 l2 0 l0 + paddsw_r2r(mm1, mm5); // lum1+red:x R3 x R1 x r3 x r1 + paddsw_r2r(mm1, mm6); // lum1+red:x R2 x R0 x r2 x r0 + packuswb_r2r(mm5, mm5); // R3 R1 r3 r1 R3 R1 r3 r1 + packuswb_r2r(mm6, mm6); // R2 R0 r2 r0 R2 R0 r2 r0 + pxor_r2r(mm7, mm7); // 00 00 00 00 00 00 00 00 + punpcklbw_r2r(mm5, mm6); // R3 R2 R1 R0 r3 r2 r1 r0 + + // create Cb (result in mm1) + movd_m2r(*(mmx_t *)cr, mm1); // 0 0 0 0 u3 u2 u1 u0 + punpcklbw_r2r(mm7, mm1); // 0 u3 0 u2 00 u1 00 u0 + punpckldq_r2r(mm1, mm1); // 00 u1 00 u0 00 u1 00 u0 + psubw_m2r(MMX_80w, mm1); // mm1-128:u1 u1 u0 u0 u1 u1 u0 u0 + // create Cb_g (result in mm5) + movq_r2r(mm1, mm5); // u1 u1 u0 u0 u1 u1 u0 u0 + pmullw_m2r(MMX32_Ugrncoeff, mm5); // blue*-109dec=1.7129*64 + pmullw_m2r(MMX32_Ubluecoeff, mm1); // blue*114dec=1.78125*64 + psraw_i2r(6, mm5); // blue=red/64 + psraw_i2r(6, mm1); // blue=blue/64 + + // create G (result in mm7) + movq_r2r(mm2, mm3); // 0 L3 0 L1 0 l3 0 l1 + movq_r2r(mm4, mm7); // 0 L2 0 L0 0 l2 0 l1 + paddsw_r2r(mm5, mm3); // lum1+Cb_g:x G3t x G1t x g3t x g1t + paddsw_r2r(mm5, mm7); // lum1+Cb_g:x G2t x G0t x g2t x g0t + paddsw_r2r(mm0, mm3); // lum1+Cr_g:x G3 x G1 x g3 x g1 + paddsw_r2r(mm0, mm7); // lum1+blue:x G2 x G0 x g2 x g0 + packuswb_r2r(mm3, mm3); // G3 G1 g3 g1 G3 G1 g3 g1 + packuswb_r2r(mm7, mm7); // G2 G0 g2 g0 G2 G0 g2 g0 + punpcklbw_r2r(mm3, mm7); // G3 G2 G1 G0 g3 g2 g1 g0 + + // create B (result in mm5) + movq_r2r(mm2, mm3); // 0 L3 0 L1 0 l3 0 l1 + movq_r2r(mm4, mm5); // 0 L2 0 L0 0 l2 0 l1 + paddsw_r2r(mm1, mm3); // lum1+blue:x B3 x B1 x b3 x b1 + paddsw_r2r(mm1, mm5); // lum1+blue:x B2 x B0 x b2 x b0 + packuswb_r2r(mm3, mm3); // B3 B1 b3 b1 B3 B1 b3 b1 + packuswb_r2r(mm5, mm5); // B2 B0 b2 b0 B2 B0 b2 b0 + punpcklbw_r2r(mm3, mm5); // B3 B2 B1 B0 b3 b2 b1 b0 + + // fill destination row1 (needed are mm6=Rr,mm7=Gg,mm5=Bb) + + pxor_r2r(mm2, mm2); // 0 0 0 0 0 0 0 0 + pxor_r2r(mm4, mm4); // 0 0 0 0 0 0 0 0 + movq_r2r(mm6, mm1); // R3 R2 R1 R0 r3 r2 r1 r0 + movq_r2r(mm5, mm3); // B3 B2 B1 B0 b3 b2 b1 b0 + // process lower lum + punpcklbw_r2r(mm4, mm1); // 0 r3 0 r2 0 r1 0 r0 + punpcklbw_r2r(mm4, mm3); // 0 b3 0 b2 0 b1 0 b0 + movq_r2r(mm1, mm2); // 0 r3 0 r2 0 r1 0 r0 + movq_r2r(mm3, mm0); // 0 b3 0 b2 0 b1 0 b0 + punpcklwd_r2r(mm1, mm3); // 0 r1 0 b1 0 r0 0 b0 + punpckhwd_r2r(mm2, mm0); // 0 r3 0 b3 0 r2 0 b2 + + pxor_r2r(mm2, mm2); // 0 0 0 0 0 0 0 0 + movq_r2r(mm7, mm1); // G3 G2 G1 G0 g3 g2 g1 g0 + punpcklbw_r2r(mm1, mm2); // g3 0 g2 0 g1 0 g0 0 + punpcklwd_r2r(mm4, mm2); // 0 0 g1 0 0 0 g0 0 + por_r2r(mm3, mm2); // 0 r1 g1 b1 0 r0 g0 b0 + movq_r2m(mm2, *(mmx_t *)row1); // wrote out ! row1 + + pxor_r2r(mm2, mm2); // 0 0 0 0 0 0 0 0 + punpcklbw_r2r(mm1, mm4); // g3 0 g2 0 g1 0 g0 0 + punpckhwd_r2r(mm2, mm4); // 0 0 g3 0 0 0 g2 0 + por_r2r(mm0, mm4); // 0 r3 g3 b3 0 r2 g2 b2 + movq_r2m(mm4, *(mmx_t *)(row1+2)); // wrote out ! row1 + + // fill destination row2 (needed are mm6=Rr,mm7=Gg,mm5=Bb) + // this can be done "destructive" + pxor_r2r(mm2, mm2); // 0 0 0 0 0 0 0 0 + punpckhbw_r2r(mm2, mm6); // 0 R3 0 R2 0 R1 0 R0 + punpckhbw_r2r(mm1, mm5); // G3 B3 G2 B2 G1 B1 G0 B0 + movq_r2r(mm5, mm1); // G3 B3 G2 B2 G1 B1 G0 B0 + punpcklwd_r2r(mm6, mm1); // 0 R1 G1 B1 0 R0 G0 B0 + movq_r2m(mm1, *(mmx_t *)(row1+cols)); // wrote out ! row2 + punpckhwd_r2r(mm6, mm5); // 0 R3 G3 B3 0 R2 G2 B2 + movq_r2m(mm5, *(mmx_t *)(row1+cols+2)); // wrote out ! row2 + + lum+=4; + cr+=2; + cb+=2; + row1 +=4; + } + lum += cols; + row1 += cols; + } + + emms(); + +} +#endif + diff --git a/ext/hermes/yuv2rgb.h b/ext/hermes/yuv2rgb.h new file mode 100644 index 00000000..c77fd702 --- /dev/null +++ b/ext/hermes/yuv2rgb.h @@ -0,0 +1,68 @@ +/* + * Copyright (c) 1995 The Regents of the University of California. + * All rights reserved. + * + * Permission to use, copy, modify, and distribute this software and its + * documentation for any purpose, without fee, and without written agreement is + * hereby granted, provided that the above copyright notice and the following + * two paragraphs appear in all copies of this software. + * + * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT + * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF + * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY + * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + */ + +#ifndef __YUV2RGB_H__ +#define __YUV2RGB_H__ + +#include + +typedef struct _GstColorSpaceYUVTables GstColorSpaceYUVTables; + +struct _GstColorSpaceYUVTables { + int gammaCorrectFlag; + double gammaCorrect; + int chromaCorrectFlag; + double chromaCorrect; + + int *L_tab, *Cr_r_tab, *Cr_g_tab, *Cb_g_tab, *Cb_b_tab; + + /* + * We define tables that convert a color value between -256 and 512 + * into the R, G and B parts of the pixel. The normal range is 0-255. + **/ + + long *r_2_pix; + long *g_2_pix; + long *b_2_pix; +}; + + +typedef struct _GstColorSpaceConverter GstColorSpaceConverter; +typedef void (*GstColorSpaceConvertFunction) (GstColorSpaceConverter *space, guchar *src, guchar *dest); + +struct _GstColorSpaceConverter { + guint width; + guint height; + guint insize; + guint outsize; + /* private */ + GstColorSpaceYUVTables *color_tables; + GstColorSpaceConvertFunction convert; +}; + + +GstColorSpaceConverter* gst_colorspace_yuv2rgb_get_converter (GstCaps *from, GstCaps *to); +#define gst_colorspace_convert(converter, src, dest) \ + (converter)->convert((converter), (src), (dest)) +void gst_colorspace_destroy (GstColorSpaceConverter *space); + +#endif + diff --git a/ext/hermes/yuv2rgb_mmx16.s b/ext/hermes/yuv2rgb_mmx16.s new file mode 100644 index 00000000..8f7b1f77 --- /dev/null +++ b/ext/hermes/yuv2rgb_mmx16.s @@ -0,0 +1,188 @@ +.globl mmx_80w +.data + .align 4 + .type mmx_80w,@object + .size mmx_80w,8 +mmx_80w: + .long 8388736 + .long 8388736 +.globl mmx_10w + .align 4 + .type mmx_10w,@object + .size mmx_10w,8 +mmx_10w: + .long 269488144 + .long 269488144 +.globl mmx_00ffw + .align 4 + .type mmx_00ffw,@object + .size mmx_00ffw,8 +mmx_00ffw: + .long 16711935 + .long 16711935 +.globl mmx_Y_coeff + .align 4 + .type mmx_Y_coeff,@object + .size mmx_Y_coeff,8 +mmx_Y_coeff: + .long 624895295 + .long 624895295 +.globl mmx_U_green + .align 4 + .type mmx_U_green,@object + .size mmx_U_green,8 +mmx_U_green: + .long -209849475 + .long -209849475 +.globl mmx_U_blue + .align 4 + .type mmx_U_blue,@object + .size mmx_U_blue,8 +mmx_U_blue: + .long 1083392147 + .long 1083392147 +.globl mmx_V_red + .align 4 + .type mmx_V_red,@object + .size mmx_V_red,8 +mmx_V_red: + .long 856830738 + .long 856830738 +.globl mmx_V_green + .align 4 + .type mmx_V_green,@object + .size mmx_V_green,8 +mmx_V_green: + .long -436410884 + .long -436410884 +.globl mmx_redmask + .align 4 + .type mmx_redmask,@object + .size mmx_redmask,8 +mmx_redmask: + .long -117901064 + .long -117901064 +.globl mmx_grnmask + .align 4 + .type mmx_grnmask,@object + .size mmx_grnmask,8 +mmx_grnmask: + .long -50529028 + .long -50529028 +.text + .align 4 +.globl gst_colorspace_yuv_to_bgr16_mmx + .type gst_colorspace_yuv_to_bgr16_mmx,@function +gst_colorspace_yuv_to_bgr16_mmx: + subl $8,%esp + pushl %ebp + pushl %edi + pushl %esi + movl 28(%esp),%edi + movl 32(%esp),%ecx + movl 36(%esp),%edx + movl $1,%ebp + movl 48(%esp),%esi + sarl $1,%esi + movl %esi,16(%esp) + + pxor %mm4, %mm4 # zero mm4 + + movl %esi,12(%esp) + sarl $2,12(%esp) + + movl 40(%esp),%esi + + .p2align 4,,7 +.L68: + + movd (%ecx), %mm0 # Load 4 Cb 00 00 00 00 00 u3 u2 u1 u0 + movd (%edx), %mm1 # Load 4 Cr 00 00 00 00 00 v2 v1 v0 + movq (%edi), %mm6 # Load 8 Y Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0 + + movl 12(%esp),%eax + + .p2align 4,,7 +.L74: + punpcklbw %mm4, %mm0 # scatter 4 Cb 00 u3 00 u2 00 u1 00 u0 + punpcklbw %mm4, %mm1 # scatter 4 Cr 00 v3 00 v2 00 v1 00 v0 + psubsw mmx_80w, %mm0 # Cb -= 128 + psubsw mmx_80w, %mm1 # Cr -= 128 + psllw $3, %mm0 # Promote precision + psllw $3, %mm1 # Promote precision + movq %mm0, %mm2 # Copy 4 Cb 00 u3 00 u2 00 u1 00 u0 + movq %mm1, %mm3 # Copy 4 Cr 00 v3 00 v2 00 v1 00 v0 + pmulhw mmx_U_green, %mm2 # Mul Cb with green coeff -> Cb green + pmulhw mmx_V_green, %mm3 # Mul Cr with green coeff -> Cr green + pmulhw mmx_U_blue, %mm0 # Mul Cb -> Cblue 00 b3 00 b2 00 b1 00 b0 + pmulhw mmx_V_red, %mm1 # Mul Cr -> Cred 00 r3 00 r2 00 r1 00 r0 + paddsw %mm3, %mm2 # Cb green + Cr green -> Cgreen + psubusb mmx_10w, %mm6 # Y -= 16 + movq %mm6, %mm7 # Copy 8 Y Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0 + pand mmx_00ffw, %mm6 # get Y even 00 Y6 00 Y4 00 Y2 00 Y0 + psrlw $8, %mm7 # get Y odd 00 Y7 00 Y5 00 Y3 00 Y1 + psllw $3, %mm6 # Promote precision + psllw $3, %mm7 # Promote precision + pmulhw mmx_Y_coeff, %mm6 # Mul 4 Y even 00 y6 00 y4 00 y2 00 y0 + pmulhw mmx_Y_coeff, %mm7 # Mul 4 Y odd 00 y7 00 y5 00 y3 00 y1 + movq %mm0, %mm3 # Copy Cblue + movq %mm1, %mm4 # Copy Cred + movq %mm2, %mm5 # Copy Cgreen + paddsw %mm6, %mm0 # Y even + Cblue 00 B6 00 B4 00 B2 00 B0 + paddsw %mm7, %mm3 # Y odd + Cblue 00 B7 00 B5 00 B3 00 B1 + paddsw %mm6, %mm1 # Y even + Cred 00 R6 00 R4 00 R2 00 R0 + paddsw %mm7, %mm4 # Y odd + Cred 00 R7 00 R5 00 R3 00 R1 + paddsw %mm6, %mm2 # Y even + Cgreen 00 G6 00 G4 00 G2 00 G0 + paddsw %mm7, %mm5 # Y odd + Cgreen 00 G7 00 G5 00 G3 00 G1 + packuswb %mm0, %mm0 # B6 B4 B2 B0 | B6 B4 B2 B0 + packuswb %mm1, %mm1 # R6 R4 R2 R0 | R6 R4 R2 R0 + packuswb %mm2, %mm2 # G6 G4 G2 G0 | G6 G4 G2 G0 + packuswb %mm3, %mm3 # B7 B5 B3 B1 | B7 B5 B3 B1 + packuswb %mm4, %mm4 # R7 R5 R3 R1 | R7 R5 R3 R1 + packuswb %mm5, %mm5 # G7 G5 G3 G1 | G7 G5 G3 G1 + punpcklbw %mm3, %mm0 # B7 B6 B5 B4 B3 B2 B1 B0 + punpcklbw %mm4, %mm1 # R7 R6 R5 R4 R3 R2 R1 R0 + punpcklbw %mm5, %mm2 # G7 G6 G5 G4 G3 G2 G1 G0 + pand mmx_redmask, %mm0 # b7b6b5b4 b3_0_0_0 b7b6b5b4 b3_0_0_0 + pand mmx_grnmask, %mm2 # g7g6g5g4 g3g2_0_0 g7g6g5g4 g3g2_0_0 + pand mmx_redmask, %mm1 # r7r6r5r4 r3_0_0_0 r7r6r5r4 r3_0_0_0 + psrlw $3,%mm0 #0_0_0_b7 b6b5b4b3 0_0_0_b7 b6b5b4b3 + pxor %mm4, %mm4 # zero mm4 + movq %mm0, %mm5 # Copy B7-B0 + movq %mm2, %mm7 # Copy G7-G0 + punpcklbw %mm4, %mm2 # 0_0_0_0 0_0_0_0 g7g6g5g4 g3g2_0_0 + punpcklbw %mm1, %mm0 # r7r6r5r4 r3_0_0_0 0_0_0_b7 b6b5b4b3 + psllw $3,%mm2 # 0_0_0_0 0_g7g6g5 g4g3g2_0 0_0_0_0 + por %mm2, %mm0 # r7r6r5r4 r3g7g6g5 g4g3g2b7 b6b5b4b3 + movq 8(%edi), %mm6 # Load 8 Y Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0 + movq %mm0, (%esi) # store pixel 0-3 + punpckhbw %mm4, %mm7 # 0_0_0_0 0_0_0_0 g7g6g5g4 g3g2_0_0 + punpckhbw %mm1, %mm5 # r7r6r5r4 r3_0_0_0 0_0_0_b7 b6b5b4b3 + psllw $3,%mm7 # 0_0_0_0 0_g7g6g5 g4g3g2_0 0_0_0_0 + movd 4(%ecx), %mm0 # Load 4 Cb 00 00 00 00 u3 u2 u1 u0 + por %mm7, %mm5 # r7r6r5r4 r3g7g6g5 g4g3g2b7 b6b5b4b3 + movd 4(%edx), %mm1 # Load 4 Cr 00 00 00 00 v3 v2 v1 v0 + movq %mm5, 8(%esi) # store pixel 4-7 + + addl $8,%edi + addl $4,%ecx + addl $4,%edx + addl $16,%esi + decl %eax + jnz .L74 +.L72: + xorl $1,%ebp + jne .L76 + subl 16(%esp),%ecx + subl 16(%esp),%edx +.L76: + subl $1,44(%esp) + jnz .L68 + + emms + + popl %esi + popl %edi + popl %ebp + addl $8,%esp + ret diff --git a/ext/hermes/yuv2yuv.c b/ext/hermes/yuv2yuv.c new file mode 100644 index 00000000..bec60c8a --- /dev/null +++ b/ext/hermes/yuv2yuv.c @@ -0,0 +1,55 @@ +/* Gnome-Streamer + * Copyright (C) <1999> Erik Walthinsen + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include "config.h" + +#include +#include + +#include + +//#undef HAVE_LIBMMX + +#ifdef HAVE_LIBMMX +#include "mmx.h" +#endif + +void gst_colorspace_yuy2_to_i420(unsigned char *src, unsigned char *dest, guint width, guint height) +{ + int size, i, j; + guchar *desty, *destr, *destb; + + size = width * height; + + desty = dest; + destr = desty + size; + destb = destr + (size>>2); + + for (i=0; i>1); j++) { + *desty++ = *src; + *desty++ = *(src+2); + if ((i&1) == 0) { + *destr++ = *(src+1); + *destb++ = *(src+3); + } + src += 4; + } + } +} -- cgit v1.2.1