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