[vlc-devel] [PATCH 44/48] vdpau: adjust video filter
Rémi Denis-Courmont
remi at remlab.net
Tue Jul 2 19:52:10 CEST 2013
This supports brightness, contrast, saturation and hue.
---
modules/hw/vdpau/Makefile.am | 5 +
modules/hw/vdpau/adjust.c | 212 +++++++++++++++++++++++++++++++++++++++++++
modules/hw/vdpau/chroma.c | 58 ++++++++++--
modules/hw/vdpau/picture.c | 12 +++
modules/hw/vdpau/vlc_vdpau.h | 1 +
5 files changed, 281 insertions(+), 7 deletions(-)
create mode 100644 modules/hw/vdpau/adjust.c
diff --git a/modules/hw/vdpau/Makefile.am b/modules/hw/vdpau/Makefile.am
index 16ab670..0cbf07c 100644
--- a/modules/hw/vdpau/Makefile.am
+++ b/modules/hw/vdpau/Makefile.am
@@ -31,6 +31,11 @@ libvdpau_deinterlace_plugin_la_CFLAGS = $(AM_CFLAGS) # dummy
libvdpau_deinterlace_plugin_la_LIBADD = $(AM_LIBADD)
libvlc_LTLIBRARIES += libvdpau_deinterlace_plugin.la
+libvdpau_adjust_plugin_la_SOURCES = adjust.c picture.c
+libvdpau_adjust_plugin_la_CFLAGS = $(AM_CFLAGS) # dummy
+libvdpau_adjust_plugin_la_LIBADD = $(AM_LIBADD)
+libvlc_LTLIBRARIES += libvdpau_adjust_plugin.la
+
libvdpau_sharpen_plugin_la_SOURCES = sharpen.c picture.c
libvdpau_sharpen_plugin_la_CFLAGS = $(AM_CFLAGS) # dummy
libvdpau_sharpen_plugin_la_LIBADD = $(AM_LIBADD)
diff --git a/modules/hw/vdpau/adjust.c b/modules/hw/vdpau/adjust.c
new file mode 100644
index 0000000..7828579
--- /dev/null
+++ b/modules/hw/vdpau/adjust.c
@@ -0,0 +1,212 @@
+/*****************************************************************************
+ * adjust.c: VDPAU colour adjust video filter
+ *****************************************************************************
+ * Copyright (C) 2013 Rémi Denis-Courmont
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <math.h>
+
+#include <vlc_common.h>
+#include <vlc_plugin.h>
+#include <vlc_filter.h>
+#include <vlc_atomic.h>
+#include "vlc_vdpau.h"
+
+struct filter_sys_t
+{
+ atomic_uint_fast32_t brightness;
+ atomic_uint_fast32_t contrast;
+ atomic_uint_fast32_t saturation;
+ atomic_uint_fast32_t hue;
+};
+
+static inline void vlc_atomic_init_float(atomic_uint_fast32_t *var, float val)
+{
+ union { uint32_t u; float f; } u;
+
+ u.f = val;
+ atomic_init(var, u.u);
+}
+
+static inline void vlc_atomic_store_float(atomic_uint_fast32_t *var, float val)
+{
+ union { uint32_t u; float f; } u;
+
+ u.f = val;
+ atomic_store(var, u.u);
+}
+
+static inline float vlc_atomic_load_float(atomic_uint_fast32_t *var)
+{
+ union { uint32_t u; float f; } u;
+
+ u.u = atomic_load(var);
+ return u.f;
+}
+
+static float vlc_to_vdp_brightness(float brightness)
+{
+ brightness -= 1.f;
+ if (brightness > +1.f)
+ brightness = +1.f;
+ if (brightness < -1.f)
+ brightness = -1.f;
+ return brightness;
+}
+
+static int BrightnessCallback(vlc_object_t *obj, const char *varname,
+ vlc_value_t prev, vlc_value_t cur, void *data)
+{
+ vlc_atomic_store_float(data, vlc_to_vdp_brightness(cur.f_float));
+ (void) obj; (void) varname; (void) prev;
+ return VLC_SUCCESS;
+}
+
+static float vlc_to_vdp_contrast(float contrast)
+{
+ if (contrast > 10.f)
+ contrast = 10.f;
+ if (contrast < 0.f)
+ contrast = 0.f;
+ return contrast;
+}
+
+static int ContrastCallback(vlc_object_t *obj, const char *varname,
+ vlc_value_t prev, vlc_value_t cur, void *data)
+{
+ vlc_atomic_store_float(data, vlc_to_vdp_contrast(cur.f_float));
+ (void) obj; (void) varname; (void) prev;
+ return VLC_SUCCESS;
+}
+
+#define vlc_to_vdp_saturation vlc_to_vdp_contrast
+
+static int SaturationCallback(vlc_object_t *obj, const char *varname,
+ vlc_value_t prev, vlc_value_t cur, void *data)
+{
+ vlc_atomic_store_float(data, vlc_to_vdp_saturation(cur.f_float));
+ (void) obj; (void) varname; (void) prev;
+ return VLC_SUCCESS;
+}
+
+static float vlc_to_vdp_hue(int hue)
+{
+ hue %= 360;
+ if (hue > 180)
+ hue -= 360;
+ return (float)hue * (float)(M_PI / 180.);
+}
+
+static int HueCallback(vlc_object_t *obj, const char *varname,
+ vlc_value_t prev, vlc_value_t cur, void *data)
+{
+
+ vlc_atomic_store_float(data, vlc_to_vdp_hue(cur.i_int));
+ (void) obj; (void) varname; (void) prev;
+ return VLC_SUCCESS;
+}
+
+static picture_t *Adjust(filter_t *filter, picture_t *pic)
+{
+ filter_sys_t *sys = filter->p_sys;
+ vlc_vdp_video_field_t *f = pic->context;
+
+ if (unlikely(f == NULL))
+ return pic;
+
+ f->procamp.brightness = vlc_atomic_load_float(&sys->brightness);
+ f->procamp.contrast = vlc_atomic_load_float(&sys->contrast);
+ f->procamp.saturation = vlc_atomic_load_float(&sys->saturation);
+ f->procamp.hue = vlc_atomic_load_float(&sys->hue);
+
+ return pic;
+}
+
+static const char *const options[] = {
+ "brightness", "contrast", "saturation", "hue", NULL
+};
+
+static int Open(vlc_object_t *obj)
+{
+ filter_t *filter = (filter_t *)obj;
+
+ if (filter->fmt_in.video.i_chroma != VLC_CODEC_VDPAU_VIDEO_422
+ && filter->fmt_in.video.i_chroma != VLC_CODEC_VDPAU_VIDEO_420)
+ return VLC_EGENERIC;
+ if (!video_format_IsSimilar(&filter->fmt_in.video, &filter->fmt_out.video))
+ return VLC_EGENERIC;
+
+ filter_sys_t *sys = malloc(sizeof (*sys));
+ if (unlikely(sys == NULL))
+ return VLC_ENOMEM;
+
+ filter->pf_video_filter = Adjust;
+ filter->p_sys = sys;
+
+ config_ChainParse(filter, "", options, filter->p_cfg);
+
+ float f;
+ int i;
+
+ f = var_CreateGetFloatCommand(filter, "brightness");
+ var_AddCallback(filter, "brightness", BrightnessCallback,
+ &sys->brightness);
+ vlc_atomic_init_float(&sys->brightness, vlc_to_vdp_brightness(f));
+
+ f = var_CreateGetFloatCommand(filter, "contrast");
+ var_AddCallback(filter, "contrast", ContrastCallback, &sys->contrast);
+ vlc_atomic_init_float(&sys->contrast, vlc_to_vdp_contrast(f));
+
+ f = var_CreateGetFloatCommand(filter, "saturation");
+ var_AddCallback(filter, "saturation", SaturationCallback,
+ &sys->saturation);
+ vlc_atomic_init_float(&sys->saturation, vlc_to_vdp_saturation(f));
+
+ i = var_CreateGetIntegerCommand(filter, "hue");
+ var_AddCallback(filter, "hue", HueCallback, &sys->hue);
+ vlc_atomic_init_float(&sys->saturation, vlc_to_vdp_saturation(i));
+
+ return VLC_SUCCESS;
+}
+
+static void Close(vlc_object_t *obj)
+{
+ filter_t *filter = (filter_t *)obj;
+ filter_sys_t *sys = filter->p_sys;
+
+ var_DelCallback(filter, "hue", HueCallback, &sys->hue);
+ var_DelCallback(filter, "saturation", SaturationCallback,
+ &sys->saturation);
+ var_DelCallback(filter, "contrast", ContrastCallback, &sys->contrast);
+ var_DelCallback(filter, "brightness", BrightnessCallback,
+ &sys->brightness);
+ free(sys);
+}
+
+vlc_module_begin()
+ set_description(N_("VDPAU adjust video filter"))
+ set_category(CAT_VIDEO)
+ set_subcategory(SUBCAT_VIDEO_VFILTER)
+ set_capability("video filter2", 0)
+ add_shortcut("adjust")
+ set_callbacks(Open, Close)
+vlc_module_end()
diff --git a/modules/hw/vdpau/chroma.c b/modules/hw/vdpau/chroma.c
index 635ff7a..eba9fd4 100644
--- a/modules/hw/vdpau/chroma.c
+++ b/modules/hw/vdpau/chroma.c
@@ -44,10 +44,18 @@ struct filter_sys_t
VdpYCbCrFormat format;
picture_t *(*import)(filter_t *, picture_t *);
picture_t *history[MAX_PAST + 1 + MAX_FUTURE];
+ struct
+ {
+ float brightness;
+ float contrast;
+ float saturation;
+ float hue;
+ } procamp;
};
/** Initialize the colour space conversion matrix */
-static VdpStatus MixerSetupColors(filter_t *filter, VdpCSCMatrix *restrict csc)
+static VdpStatus MixerSetupColors(filter_t *filter, const VdpProcamp *procamp,
+ VdpCSCMatrix *restrict csc)
{
filter_sys_t *sys = filter->p_sys;
VdpStatus err;
@@ -55,11 +63,29 @@ static VdpStatus MixerSetupColors(filter_t *filter, VdpCSCMatrix *restrict csc)
? VDP_COLOR_STANDARD_ITUR_BT_709
: VDP_COLOR_STANDARD_ITUR_BT_601;
- err = vdp_generate_csc_matrix(sys->vdp, NULL, std, csc);
+ err = vdp_generate_csc_matrix(sys->vdp, procamp, std, csc);
if (err != VDP_STATUS_OK)
+ {
msg_Err(filter, "video %s failure: %s", "color space matrix",
vdp_get_error_string(sys->vdp, err));
- return err;
+ return err;
+ }
+
+ if (procamp != NULL)
+ {
+ sys->procamp.brightness = procamp->brightness;
+ sys->procamp.contrast = procamp->contrast;
+ sys->procamp.saturation = procamp->saturation;
+ sys->procamp.hue = procamp->hue;
+ }
+ else
+ {
+ sys->procamp.brightness = 0.f;
+ sys->procamp.contrast = 1.f;
+ sys->procamp.saturation = 1.f;
+ sys->procamp.hue = 0.f;
+ }
+ return VDP_STATUS_OK;
}
/** Create VDPAU video mixer */
@@ -138,7 +164,7 @@ static VdpVideoMixer MixerCreate(filter_t *filter)
featc = 0;
- if (MixerSetupColors(filter, &csc) == VDP_STATUS_OK)
+ if (MixerSetupColors(filter, NULL, &csc) == VDP_STATUS_OK)
{
attrv[attrc] = VDP_VIDEO_MIXER_ATTRIBUTE_CSC_MATRIX;
valv[attrc] = &csc;
@@ -415,15 +441,28 @@ static picture_t *MixerRender(filter_t *filter, picture_t *src)
vdp_get_error_string(sys->vdp, err));
/* Configure mixer depending on upstream video filters */
- const VdpVideoMixerAttribute attrs[] = {
+ VdpVideoMixerAttribute attrs[2] = {
VDP_VIDEO_MIXER_ATTRIBUTE_SHARPNESS_LEVEL,
};
- const void *const values[] = {
+ const void *values[2] = {
&f->sharpen,
};
+ unsigned count = 1;
+ VdpCSCMatrix csc;
+
+ if ((sys->procamp.brightness != f->procamp.brightness
+ || sys->procamp.contrast != f->procamp.contrast
+ || sys->procamp.saturation != f->procamp.saturation
+ || sys->procamp.hue != f->procamp.hue)
+ && (MixerSetupColors(filter, &f->procamp, &csc) == VDP_STATUS_OK))
+ {
+ attrs[count] = VDP_VIDEO_MIXER_ATTRIBUTE_CSC_MATRIX;
+ values[count] = &csc;
+ count++;
+ }
err = vdp_video_mixer_set_attribute_values(sys->vdp, sys->mixer,
- sizeof (attrs) / sizeof (attrs[0]), attrs, values);
+ count, attrs, values);
if (err != VDP_STATUS_OK)
msg_Err(filter, "video %s %s failure: %s", "mixer", "attributes",
vdp_get_error_string(sys->vdp, err));
@@ -530,6 +569,11 @@ static int OutputOpen(vlc_object_t *obj)
for (unsigned i = 0; i < MAX_PAST + MAX_FUTURE; i++)
sys->history[i] = NULL;
+ sys->procamp.brightness = 0.f;
+ sys->procamp.contrast = 1.f;
+ sys->procamp.saturation = 1.f;
+ sys->procamp.hue = 0.f;
+
filter->pf_video_filter = MixerRender;
filter->pf_video_flush = Flush;
filter->p_sys = sys;
diff --git a/modules/hw/vdpau/picture.c b/modules/hw/vdpau/picture.c
index 1c36f2e..a7c5c79 100644
--- a/modules/hw/vdpau/picture.c
+++ b/modules/hw/vdpau/picture.c
@@ -24,6 +24,7 @@
#include <stdlib.h>
#include <stdio.h>
+#include <string.h>
#include <assert.h>
#include <vlc_common.h>
#include <vlc_picture.h>
@@ -52,6 +53,15 @@ static void SurfaceDestroy(void *opaque)
free(frame);
}
+static const VdpProcamp procamp_default =
+{
+ .struct_version = VDP_PROCAMP_VERSION,
+ .brightness = 0.f,
+ .contrast = 1.f,
+ .saturation = 1.f,
+ .hue = 0.f,
+};
+
VdpStatus vlc_vdp_video_attach(vdp_t *vdp, VdpVideoSurface surface,
picture_t *pic)
{
@@ -72,6 +82,7 @@ VdpStatus vlc_vdp_video_attach(vdp_t *vdp, VdpVideoSurface surface,
field->destroy = SurfaceDestroy;
field->frame = frame;
field->structure = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_FRAME;
+ field->procamp = procamp_default;
field->sharpen = 0.f;
atomic_init(&frame->refs, 1);
@@ -102,6 +113,7 @@ picture_t *vlc_vdp_video_copy(picture_t *src)
fnew->destroy = SurfaceDestroy;
fnew->frame = frame;
fnew->structure = fold->structure;
+ fnew->procamp = fold->procamp;
fnew->sharpen = fold->sharpen;
atomic_fetch_add(&frame->refs, 1);
diff --git a/modules/hw/vdpau/vlc_vdpau.h b/modules/hw/vdpau/vlc_vdpau.h
index 6a86550..c44212e 100644
--- a/modules/hw/vdpau/vlc_vdpau.h
+++ b/modules/hw/vdpau/vlc_vdpau.h
@@ -259,6 +259,7 @@ typedef struct vlc_vdp_video_field
void (*destroy)(void *); /* must be first @ref picture_Release() */
vlc_vdp_video_frame_t *frame;
VdpVideoMixerPictureStructure structure;
+ VdpProcamp procamp;
float sharpen;
} vlc_vdp_video_field_t;
--
1.8.3.2
More information about the vlc-devel
mailing list