1/**************************************************************************
2 *
3 * Copyright 2009 Younes Manton.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28#include <assert.h>
29#include <stdlib.h>
30
31#include <X11/Xlib.h>
32#include <X11/extensions/Xvlib.h>
33#include <X11/extensions/XvMClib.h>
34
35#include "vl/vl_compositor.h"
36
37#include "xvmc_private.h"
38
39#define XV_BRIGHTNESS "XV_BRIGHTNESS"
40#define XV_CONTRAST   "XV_CONTRAST"
41#define XV_SATURATION "XV_SATURATION"
42#define XV_HUE        "XV_HUE"
43#define XV_COLORSPACE "XV_COLORSPACE"
44
45static const XvAttribute attributes[] = {
46   { XvGettable | XvSettable, -1000, 1000, XV_BRIGHTNESS },
47   { XvGettable | XvSettable, -1000, 1000, XV_CONTRAST },
48   { XvGettable | XvSettable, -1000, 1000, XV_SATURATION },
49   { XvGettable | XvSettable, -1000, 1000, XV_HUE },
50   { XvGettable | XvSettable, 0, 1, XV_COLORSPACE }
51};
52
53PUBLIC
54XvAttribute* XvMCQueryAttributes(Display *dpy, XvMCContext *context, int *number)
55{
56   XvAttribute *result;
57
58   assert(dpy && number);
59
60   if (!context || !context->privData)
61      return NULL;
62
63   result = malloc(sizeof(attributes));
64   if (!result)
65      return NULL;
66
67   memcpy(result, attributes, sizeof(attributes));
68   *number = sizeof(attributes) / sizeof(XvAttribute);
69
70   XVMC_MSG(XVMC_TRACE, "[XvMC] Returning %d attributes for context %p.\n", *number, context);
71
72   return result;
73}
74
75PUBLIC
76Status XvMCSetAttribute(Display *dpy, XvMCContext *context, Atom attribute, int value)
77{
78   XvMCContextPrivate *context_priv;
79   const char *attr;
80   vl_csc_matrix csc;
81
82   assert(dpy);
83
84   if (!context || !context->privData)
85      return XvMCBadContext;
86
87   context_priv = context->privData;
88
89   attr = XGetAtomName(dpy, attribute);
90   if (!attr)
91      return XvMCBadContext;
92
93   if (strcmp(attr, XV_BRIGHTNESS))
94      context_priv->procamp.brightness = value / 1000.0f;
95   else if (strcmp(attr, XV_CONTRAST))
96      context_priv->procamp.contrast = value / 1000.0f + 1.0f;
97   else if (strcmp(attr, XV_SATURATION))
98      context_priv->procamp.saturation = value / 1000.0f + 1.0f;
99   else if (strcmp(attr, XV_HUE))
100      context_priv->procamp.hue = value / 1000.0f;
101   else if (strcmp(attr, XV_COLORSPACE))
102      context_priv->color_standard = value ?
103         VL_CSC_COLOR_STANDARD_BT_601 :
104         VL_CSC_COLOR_STANDARD_BT_709;
105   else
106      return BadName;
107
108   vl_csc_get_matrix
109   (
110      context_priv->color_standard,
111      &context_priv->procamp, true, &csc
112   );
113   vl_compositor_set_csc_matrix(&context_priv->cstate, (const vl_csc_matrix *)&csc);
114
115   XVMC_MSG(XVMC_TRACE, "[XvMC] Set attribute %s to value %d.\n", attr, value);
116
117   return Success;
118}
119
120PUBLIC
121Status XvMCGetAttribute(Display *dpy, XvMCContext *context, Atom attribute, int *value)
122{
123   XvMCContextPrivate *context_priv;
124   const char *attr;
125
126   assert(dpy);
127
128   if (!context || !context->privData)
129      return XvMCBadContext;
130
131   context_priv = context->privData;
132
133   attr = XGetAtomName(dpy, attribute);
134   if (!attr)
135      return XvMCBadContext;
136
137   if (strcmp(attr, XV_BRIGHTNESS))
138      *value = context_priv->procamp.brightness * 1000;
139   else if (strcmp(attr, XV_CONTRAST))
140      *value = context_priv->procamp.contrast * 1000 - 1000;
141   else if (strcmp(attr, XV_SATURATION))
142      *value = context_priv->procamp.saturation * 1000 + 1000;
143   else if (strcmp(attr, XV_HUE))
144      *value = context_priv->procamp.hue * 1000;
145   else if (strcmp(attr, XV_COLORSPACE))
146      *value = context_priv->color_standard == VL_CSC_COLOR_STANDARD_BT_709;
147   else
148      return BadName;
149
150   XVMC_MSG(XVMC_TRACE, "[XvMC] Got value %d for attribute %s.\n", *value, attr);
151
152   return Success;
153}
154