Skip to content

Commit

Permalink
Redo csc calculations
Browse files Browse the repository at this point in the history
There is no possibility to forward the csc matrix to linux-sunxi kernel
display driver directly with VDPAU. We have to set the values for
brightness, saturation, contrast and hue via ioctl. But there are no
attributes for these values in VDOAU to get them directly. So we need
to do some reverse calculation to get these values out of the (changed)
csc matrix.
The old calculation was wrong and is now corrected to get the right
values for brightness, saturation, contrast and hue. Due to the reverse
calculation algorithm it's not possible to get all values for every
scenario. This usecase is documented in the source code.

Note:
The linux-sunxi 3.4 kernel is buggy when doing this calculations. So
in order to get this work correctly, you have to patch it or use an
already patched kernel.
  • Loading branch information
rellla committed Jun 9, 2016
1 parent 0dc6174 commit 55afc1b
Show file tree
Hide file tree
Showing 4 changed files with 263 additions and 54 deletions.
72 changes: 72 additions & 0 deletions csc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright (c) 2015-2016 Andreas Baierl <ichgeh@imkreisrum.de>
*
* This library 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 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/

#ifndef __CSC_H__
#define __CSC_H__

#include "vdpau_private.h"

typedef float csc_m[3][4];

#ifdef CSC_FULL_RANGE
/*
* This matrizes are from vl_csc.c from the mesa project
* The calculation routines are there, too.
*/

/* Full range: RGB is in 0~255 */
static const csc_m cs_bt601 = {
{ 1.164f, 0.0f, 1.596f, 0.0f, },
{ 1.164f, -0.391f, -0.813f, 0.0f, },
{ 1.164f, 2.018f, 0.0f, 0.0f, }
};
static const csc_m cs_bt709 = {
{ 1.164f, 0.0f, 1.793f, 0.0f, },
{ 1.164f, -0.213f, -0.534f, 0.0f, },
{ 1.164f, 2.115f, 0.0f, 0.0f, }
};
static const csc_m cs_smpte_240m = {
{ 1.164f, 0.0f, 1.794f, 0.0f, },
{ 1.164f, -0.258f, -0.543f, 0.0f, },
{ 1.164f, 2.079f, 0.0f, 0.0f, }
};
const float ybias = -16.0f / 255.0f;
#else
/* Normal range: RGB is in 16~235 */
static const csc_m cs_bt601 = {
{ 1.0f, 0.0f, 1.371f, 0.0f, },
{ 1.0f, -0.336f, -0.698f, 0.0f, },
{ 1.0f, 1.732f, 0.0f, 0.0f, }
};
static const csc_m cs_bt709 = {
{ 1.0f, 0.0f, 1.540f, 0.0f, },
{ 1.0f, -0.183f, -0.459f, 0.0f, },
{ 1.0f, 1.816f, 0.0f, 0.0f, }
};
static const csc_m cs_smpte_240m = {
{ 1.0f, 0.0f, 1.582f, 0.0f, },
{ 1.0f, -0.228f, -0.478f, 0.0f, },
{ 1.0f, 1.833f, 0.0f, 0.0f, }
};
static const float ybias = 0.0f;
#endif
const float cbbias = -128.0f / 255.0f;
const float crbias = -128.0f / 255.0f;

#endif
36 changes: 25 additions & 11 deletions sunxi_disp.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

#include <fcntl.h>
#include <math.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
Expand Down Expand Up @@ -253,25 +254,38 @@ static int sunxi_disp_set_video_layer(struct sunxi_disp *sunxi_disp, int x, int
ioctl(disp->fd, DISP_CMD_LAYER_OPEN, args);
}


// Note: might be more reliable (but slower and problematic when there
// are driver issues and the GET functions return wrong values) to query the
// old values instead of relying on our internal csc_change.
// Since the driver calculates a matrix out of these values after each
// set doing this unconditionally is costly.
if (surface->csc_change)
{
uint32_t b, c, s, h;

ioctl(disp->fd, DISP_CMD_LAYER_ENHANCE_OFF, args);
args[2] = 0xff * surface->brightness + 0x20;

/* scale VDPAU: -1.0 ~ 1.0 to SUNXI: 0 ~ 100 */
b = args[2] = ((surface->brightness + 1.0) * 50.0) + 0.5;
ioctl(disp->fd, DISP_CMD_LAYER_SET_BRIGHT, args);
args[2] = 0x20 * surface->contrast;

/* scale VDPAU: 0.0 ~ 10.0 to SUNXI: 0 ~ 100 */
if (surface->contrast <= 1.0)
c = args[2] = (surface->contrast * 50.0) + 0.5;
else
c = args[2] = (50.0 + (surface->contrast - 1.0) * 50.0 / 9.0) + 0.5;
ioctl(disp->fd, DISP_CMD_LAYER_SET_CONTRAST, args);
args[2] = 0x20 * surface->saturation;

/* scale VDPAU: 0.0 ~ 10.0 to SUNXI: 0 ~ 100 */
if (surface->saturation <= 1.0)
s = args[2] = (surface->saturation * 50.0) + 0.5;
else
s = args[2] = (50.0 + (surface->saturation - 1.0) * 50.0 / 9.0) + 0.5;
ioctl(disp->fd, DISP_CMD_LAYER_SET_SATURATION, args);
// hue scale is randomly chosen, no idea how it maps exactly
args[2] = (32 / 3.14) * surface->hue + 0x20;

/* scale VDPAU: -PI ~ PI to SUNXI: 0 ~ 100 */
h = args[2] = (((surface->hue / M_PI) + 1.0) * 50.0) + 0.5;
ioctl(disp->fd, DISP_CMD_LAYER_SET_HUE, args);

ioctl(disp->fd, DISP_CMD_LAYER_ENHANCE_ON, args);

VDPAU_DBG("Presentation queue csc change");
VDPAU_DBG("display driver -> bright: %d, contrast: %d, saturation: %d, hue: %d", b, c, s, h);
surface->csc_change = 0;
}

Expand Down
3 changes: 3 additions & 0 deletions vdpau_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#define MAX_HANDLES 64
#define VBV_SIZE (1 * 1024 * 1024)
#define MAX_SURFACE_BUFFER (3)
#define CSC_FULL_RANGE 1

#include <pthread.h>
#include <stdlib.h>
Expand Down Expand Up @@ -109,6 +110,8 @@ typedef struct
float saturation;
float hue;
int start_stream, deinterlace;
int custom_csc;
VdpCSCMatrix csc_matrix;
} mixer_ctx_t;

#define RGBA_FLAG_DIRTY (1 << 0)
Expand Down
Loading

0 comments on commit 55afc1b

Please sign in to comment.