gstreamer/gst/videoconvert/gstvideoconvert.c

491 lines
14 KiB
C
Raw Normal View History

/* GStreamer
* Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
* This file:
* Copyright (C) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
* Copyright (C) 2010 David Schleef <ds@schleef.org>
*
* 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.
*/
/**
2011-06-15 15:49:21 +00:00
* SECTION:element-videoconvert
*
2011-06-16 10:48:33 +00:00
* Convert video frames between a great variety of video formats.
*
* <refsect2>
* <title>Example launch line</title>
* |[
2011-06-16 10:48:33 +00:00
* gst-launch -v videotestsrc ! video/x-raw,format=\(fourcc\)YUY2 ! videoconvert ! ximagesink
* ]|
* </refsect2>
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
2011-06-15 15:49:21 +00:00
#include "gstvideoconvert.h"
#include <gst/video/video.h>
#include <string.h>
2011-06-15 15:49:21 +00:00
GST_DEBUG_CATEGORY (videoconvert_debug);
#define GST_CAT_DEFAULT videoconvert_debug
GST_DEBUG_CATEGORY (videoconvert_performance);
enum
{
PROP_0,
PROP_DITHER
};
2011-06-16 10:48:33 +00:00
#define CSP_VIDEO_CAPS GST_VIDEO_CAPS_MAKE (GST_VIDEO_FORMATS_ALL)
2011-06-15 15:49:21 +00:00
static GstStaticPadTemplate gst_video_convert_src_template =
GST_STATIC_PAD_TEMPLATE ("src",
GST_PAD_SRC,
GST_PAD_ALWAYS,
GST_STATIC_CAPS (CSP_VIDEO_CAPS)
);
2011-06-15 15:49:21 +00:00
static GstStaticPadTemplate gst_video_convert_sink_template =
GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_STATIC_CAPS (CSP_VIDEO_CAPS)
);
2011-06-15 15:49:21 +00:00
GType gst_video_convert_get_type (void);
2011-06-15 15:49:21 +00:00
static void gst_video_convert_set_property (GObject * object,
guint property_id, const GValue * value, GParamSpec * pspec);
2011-06-15 15:49:21 +00:00
static void gst_video_convert_get_property (GObject * object,
guint property_id, GValue * value, GParamSpec * pspec);
2011-06-15 15:49:21 +00:00
static gboolean gst_video_convert_set_caps (GstBaseTransform * btrans,
GstCaps * incaps, GstCaps * outcaps);
2011-06-15 15:49:21 +00:00
static gboolean gst_video_convert_get_unit_size (GstBaseTransform * btrans,
2011-06-15 14:28:44 +00:00
GstCaps * caps, gsize * size);
2011-06-15 15:49:21 +00:00
static GstFlowReturn gst_video_convert_transform (GstBaseTransform * btrans,
GstBuffer * inbuf, GstBuffer * outbuf);
static GType
dither_method_get_type (void)
{
static GType gtype = 0;
if (gtype == 0) {
static const GEnumValue values[] = {
{DITHER_NONE, "No dithering (default)", "none"},
{DITHER_VERTERR, "Vertical error propogation", "verterr"},
{DITHER_HALFTONE, "Half-tone", "halftone"},
{0, NULL, NULL}
};
gtype = g_enum_register_static ("GstColorspaceDitherMethod", values);
}
return gtype;
}
/* copies the given caps */
static GstCaps *
2011-06-15 15:49:21 +00:00
gst_video_convert_caps_remove_format_info (GstCaps * caps)
{
2011-06-16 10:48:33 +00:00
GstStructure *st;
2011-06-15 14:52:52 +00:00
gint i, n;
GstCaps *res;
2011-06-15 14:52:52 +00:00
res = gst_caps_new_empty ();
2011-06-15 14:52:52 +00:00
n = gst_caps_get_size (caps);
for (i = 0; i < n; i++) {
2011-06-16 10:48:33 +00:00
st = gst_caps_get_structure (caps, i);
2011-06-15 14:52:52 +00:00
/* If this is already expressed by the existing caps
* skip this structure */
2011-06-16 10:48:33 +00:00
if (i > 0 && gst_caps_is_subset_structure (res, st))
2011-06-15 14:52:52 +00:00
continue;
2011-06-16 10:48:33 +00:00
st = gst_structure_copy (st);
gst_structure_remove_fields (st, "format", "palette_data",
"color-matrix", "chroma-site", NULL);
2011-06-16 10:48:33 +00:00
gst_caps_append_structure (res, st);
}
2011-06-15 14:52:52 +00:00
return res;
}
/* The caps can be transformed into any other caps with format info removed.
* However, we should prefer passthrough, so if passthrough is possible,
* put it first in the list. */
static GstCaps *
2011-06-15 15:49:21 +00:00
gst_video_convert_transform_caps (GstBaseTransform * btrans,
2011-06-15 14:28:44 +00:00
GstPadDirection direction, GstCaps * caps, GstCaps * filter)
{
GstCaps *template;
GstCaps *tmp, *tmp2;
GstCaps *result;
2011-06-15 15:49:21 +00:00
template = gst_static_pad_template_get_caps (&gst_video_convert_src_template);
result = gst_caps_copy (caps);
/* Get all possible caps that we can transform to */
2011-06-15 15:49:21 +00:00
tmp = gst_video_convert_caps_remove_format_info (caps);
2011-06-15 14:52:52 +00:00
if (filter) {
tmp2 = gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
gst_caps_unref (tmp);
tmp = tmp2;
}
2011-06-15 14:52:52 +00:00
result = tmp;
GST_DEBUG_OBJECT (btrans, "transformed %" GST_PTR_FORMAT " into %"
GST_PTR_FORMAT, caps, result);
return result;
}
static gboolean
2011-06-15 15:49:21 +00:00
gst_video_convert_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
GstCaps * outcaps)
{
2011-06-15 15:49:21 +00:00
GstVideoConvert *space;
GstVideoInfo in_info;
GstVideoInfo out_info;
gboolean ret;
ColorSpaceColorSpec in_spec, out_spec;
2011-06-15 15:49:21 +00:00
space = GST_VIDEO_CONVERT_CAST (btrans);
if (space->convert) {
2011-06-15 15:49:21 +00:00
videoconvert_convert_free (space->convert);
}
/* input caps */
ret = gst_video_info_from_caps (&in_info, incaps);
if (!ret)
goto invalid_caps;
if (gst_video_format_is_rgb (in_info.format)) {
in_spec = COLOR_SPEC_RGB;
} else if (gst_video_format_is_yuv (in_info.format)) {
if (in_info.color_matrix && g_str_equal (in_info.color_matrix, "hdtv"))
in_spec = COLOR_SPEC_YUV_BT709;
else
in_spec = COLOR_SPEC_YUV_BT470_6;
} else {
in_spec = COLOR_SPEC_GRAY;
}
/* output caps */
ret = gst_video_info_from_caps (&out_info, outcaps);
if (!ret)
goto invalid_caps;
if (gst_video_format_is_rgb (out_info.format)) {
out_spec = COLOR_SPEC_RGB;
} else if (gst_video_format_is_yuv (out_info.format)) {
if (out_info.color_matrix && g_str_equal (out_info.color_matrix, "hdtv"))
out_spec = COLOR_SPEC_YUV_BT709;
else
out_spec = COLOR_SPEC_YUV_BT470_6;
} else {
out_spec = COLOR_SPEC_GRAY;
}
/* these must match */
if (in_info.width != out_info.width || in_info.height != out_info.height ||
in_info.fps_n != out_info.fps_n || in_info.fps_d != out_info.fps_d)
goto format_mismatch;
/* if present, these must match too */
if (in_info.par_n != out_info.par_n || in_info.par_d != out_info.par_d)
goto format_mismatch;
/* if present, these must match too */
if ((in_info.flags & GST_VIDEO_FLAG_INTERLACED) !=
(out_info.flags & GST_VIDEO_FLAG_INTERLACED))
goto format_mismatch;
space->from_info = in_info;
space->from_spec = in_spec;
space->to_info = out_info;
space->to_spec = out_spec;
space->width = in_info.width;
space->height = in_info.height;
space->interlaced = (in_info.flags & GST_VIDEO_FLAG_INTERLACED) != 0;
space->convert =
videoconvert_convert_new (out_info.format, out_spec, in_info.format,
in_spec, in_info.width, in_info.height);
if (space->convert) {
videoconvert_convert_set_interlaced (space->convert, space->interlaced);
}
/* palette, only for from data */
if (space->from_info.format == GST_VIDEO_FORMAT_RGB8_PALETTED &&
space->to_info.format == GST_VIDEO_FORMAT_RGB8_PALETTED) {
goto format_mismatch;
} else if (space->from_info.format == GST_VIDEO_FORMAT_RGB8_PALETTED) {
GstBuffer *palette;
2011-06-15 14:28:44 +00:00
guint32 *data;
palette = gst_video_parse_caps_palette (incaps);
2011-06-15 14:28:44 +00:00
if (!palette || gst_buffer_get_size (palette) < 256 * 4) {
if (palette)
gst_buffer_unref (palette);
goto invalid_palette;
}
2011-06-15 14:28:44 +00:00
data = gst_buffer_map (palette, NULL, NULL, GST_MAP_READ);
2011-06-15 15:49:21 +00:00
videoconvert_convert_set_palette (space->convert, data);
2011-06-15 14:28:44 +00:00
gst_buffer_unmap (palette, data, -1);
gst_buffer_unref (palette);
} else if (space->to_info.format == GST_VIDEO_FORMAT_RGB8_PALETTED) {
const guint32 *palette;
GstBuffer *p_buf;
2011-06-15 15:49:21 +00:00
palette = videoconvert_convert_get_palette (space->convert);
2011-06-15 14:28:44 +00:00
p_buf = gst_buffer_new_and_alloc (256 * 4);
2011-06-15 14:28:44 +00:00
gst_buffer_fill (p_buf, 0, palette, 256 * 4);
gst_caps_set_simple (outcaps, "palette_data", GST_TYPE_BUFFER, p_buf, NULL);
gst_buffer_unref (p_buf);
}
GST_DEBUG ("reconfigured %d %d", space->from_info.format,
space->to_info.format);
return TRUE;
/* ERRORS */
invalid_caps:
{
GST_ERROR_OBJECT (space, "invalid caps");
space->from_info.format = GST_VIDEO_FORMAT_UNKNOWN;
space->to_info.format = GST_VIDEO_FORMAT_UNKNOWN;
return FALSE;
}
format_mismatch:
{
GST_ERROR_OBJECT (space, "input and output formats do not match");
space->from_info.format = GST_VIDEO_FORMAT_UNKNOWN;
space->to_info.format = GST_VIDEO_FORMAT_UNKNOWN;
return FALSE;
}
invalid_palette:
{
GST_ERROR_OBJECT (space, "invalid palette");
space->from_info.format = GST_VIDEO_FORMAT_UNKNOWN;
space->to_info.format = GST_VIDEO_FORMAT_UNKNOWN;
return FALSE;
}
}
2011-06-15 15:49:21 +00:00
#define gst_video_convert_parent_class parent_class
G_DEFINE_TYPE (GstVideoConvert, gst_video_convert, GST_TYPE_VIDEO_FILTER);
static void
2011-06-15 15:49:21 +00:00
gst_video_convert_finalize (GObject * obj)
{
2011-06-15 15:49:21 +00:00
GstVideoConvert *space = GST_VIDEO_CONVERT (obj);
if (space->convert) {
2011-06-15 15:49:21 +00:00
videoconvert_convert_free (space->convert);
}
G_OBJECT_CLASS (parent_class)->finalize (obj);
}
static void
2011-06-15 15:49:21 +00:00
gst_video_convert_class_init (GstVideoConvertClass * klass)
{
GObjectClass *gobject_class = (GObjectClass *) klass;
2011-06-15 14:28:44 +00:00
GstElementClass *gstelement_class = (GstElementClass *) klass;
GstBaseTransformClass *gstbasetransform_class =
(GstBaseTransformClass *) klass;
2011-06-15 15:49:21 +00:00
gobject_class->set_property = gst_video_convert_set_property;
gobject_class->get_property = gst_video_convert_get_property;
gobject_class->finalize = gst_video_convert_finalize;
2011-06-15 14:28:44 +00:00
gst_element_class_add_pad_template (gstelement_class,
2011-06-15 15:49:21 +00:00
gst_static_pad_template_get (&gst_video_convert_src_template));
2011-06-15 14:28:44 +00:00
gst_element_class_add_pad_template (gstelement_class,
2011-06-15 15:49:21 +00:00
gst_static_pad_template_get (&gst_video_convert_sink_template));
2011-06-15 14:28:44 +00:00
gst_element_class_set_details_simple (gstelement_class,
" Colorspace converter", "Filter/Converter/Video",
"Converts video from one colorspace to another",
"GStreamer maintainers <gstreamer-devel@lists.sourceforge.net>");
gstbasetransform_class->transform_caps =
2011-06-15 15:49:21 +00:00
GST_DEBUG_FUNCPTR (gst_video_convert_transform_caps);
gstbasetransform_class->set_caps =
GST_DEBUG_FUNCPTR (gst_video_convert_set_caps);
gstbasetransform_class->get_unit_size =
2011-06-15 15:49:21 +00:00
GST_DEBUG_FUNCPTR (gst_video_convert_get_unit_size);
gstbasetransform_class->transform =
GST_DEBUG_FUNCPTR (gst_video_convert_transform);
gstbasetransform_class->passthrough_on_same_caps = TRUE;
g_object_class_install_property (gobject_class, PROP_DITHER,
g_param_spec_enum ("dither", "Dither", "Apply dithering while converting",
dither_method_get_type (), DITHER_NONE,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
}
static void
2011-06-15 15:49:21 +00:00
gst_video_convert_init (GstVideoConvert * space)
{
space->from_info.format = GST_VIDEO_FORMAT_UNKNOWN;
space->to_info.format = GST_VIDEO_FORMAT_UNKNOWN;
}
void
2011-06-15 15:49:21 +00:00
gst_video_convert_set_property (GObject * object, guint property_id,
const GValue * value, GParamSpec * pspec)
{
2011-06-15 15:49:21 +00:00
GstVideoConvert *csp;
2011-06-15 15:49:21 +00:00
csp = GST_VIDEO_CONVERT (object);
switch (property_id) {
case PROP_DITHER:
csp->dither = g_value_get_enum (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
void
2011-06-15 15:49:21 +00:00
gst_video_convert_get_property (GObject * object, guint property_id,
GValue * value, GParamSpec * pspec)
{
2011-06-15 15:49:21 +00:00
GstVideoConvert *csp;
2011-06-15 15:49:21 +00:00
csp = GST_VIDEO_CONVERT (object);
switch (property_id) {
case PROP_DITHER:
g_value_set_enum (value, csp->dither);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static gboolean
2011-06-15 15:49:21 +00:00
gst_video_convert_get_unit_size (GstBaseTransform * btrans, GstCaps * caps,
gsize * size)
{
gboolean ret = TRUE;
GstVideoInfo info;
g_assert (size);
ret = gst_video_info_from_caps (&info, caps);
if (ret) {
*size = info.size;
}
return ret;
}
static GstFlowReturn
2011-06-15 15:49:21 +00:00
gst_video_convert_transform (GstBaseTransform * btrans, GstBuffer * inbuf,
GstBuffer * outbuf)
{
2011-06-15 15:49:21 +00:00
GstVideoConvert *space;
2011-06-15 14:28:44 +00:00
guint8 *indata, *outdata;
gsize insize, outsize;
gint i;
2011-06-15 15:49:21 +00:00
space = GST_VIDEO_CONVERT_CAST (btrans);
GST_DEBUG ("from %d -> to %d", space->from_info.format,
space->to_info.format);
if (G_UNLIKELY (space->from_info.format == GST_VIDEO_FORMAT_UNKNOWN ||
space->to_info.format == GST_VIDEO_FORMAT_UNKNOWN))
goto unknown_format;
2011-06-15 15:49:21 +00:00
videoconvert_convert_set_dither (space->convert, space->dither);
2011-06-15 14:28:44 +00:00
indata = gst_buffer_map (inbuf, &insize, NULL, GST_MAP_READ);
outdata = gst_buffer_map (outbuf, &outsize, NULL, GST_MAP_WRITE);
for (i = 0; i < space->to_info.n_planes; i++) {
space->convert->dest_stride[i] = space->to_info.plane[i].stride;
space->convert->dest_offset[i] = space->to_info.plane[i].offset;
space->convert->src_stride[i] = space->from_info.plane[i].stride;
space->convert->src_offset[i] = space->from_info.plane[i].offset;
}
2011-06-15 15:49:21 +00:00
videoconvert_convert_convert (space->convert, outdata, indata);
2011-06-15 14:28:44 +00:00
gst_buffer_unmap (outbuf, outdata, outsize);
gst_buffer_unmap (inbuf, indata, insize);
/* baseclass copies timestamps */
GST_DEBUG ("from %d -> to %d done", space->from_info.format,
space->to_info.format);
return GST_FLOW_OK;
/* ERRORS */
unknown_format:
{
GST_ELEMENT_ERROR (space, CORE, NOT_IMPLEMENTED, (NULL),
("attempting to convert colorspaces between unknown formats"));
return GST_FLOW_NOT_NEGOTIATED;
}
#if 0
not_supported:
{
GST_ELEMENT_ERROR (space, CORE, NOT_IMPLEMENTED, (NULL),
("cannot convert between formats"));
return GST_FLOW_NOT_SUPPORTED;
}
#endif
}
static gboolean
plugin_init (GstPlugin * plugin)
{
2011-06-15 15:49:21 +00:00
GST_DEBUG_CATEGORY_INIT (videoconvert_debug, "videoconvert", 0,
"Colorspace Converter");
2011-06-15 15:49:21 +00:00
GST_DEBUG_CATEGORY_GET (videoconvert_performance, "GST_PERFORMANCE");
2011-06-15 15:49:21 +00:00
return gst_element_register (plugin, "videoconvert",
GST_RANK_NONE, GST_TYPE_VIDEO_CONVERT);
}
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
GST_VERSION_MINOR,
2011-06-15 15:49:21 +00:00
"videoconvert", "Colorspace conversion", plugin_init, VERSION, "LGPL", "",
"")