videoconvert: improve color transform setup

Remove hardcoded color matrices and compute the matrices using the cms helper
library that was in cogcolorspace before.
This commit is contained in:
Wim Taymans 2012-06-04 14:46:21 +02:00
parent b718dc26b5
commit 29edc0c6a5
5 changed files with 859 additions and 300 deletions

View file

@ -3,7 +3,7 @@ plugin_LTLIBRARIES = libgstvideoconvert.la
ORC_SOURCE=gstvideoconvertorc
include $(top_srcdir)/common/orc.mak
libgstvideoconvert_la_SOURCES = gstvideoconvert.c videoconvert.c
libgstvideoconvert_la_SOURCES = gstvideoconvert.c videoconvert.c gstcms.c
nodist_libgstvideoconvert_la_SOURCES = $(ORC_NODIST_SOURCES)
libgstvideoconvert_la_CFLAGS = \
$(GST_PLUGINS_BASE_CFLAGS) \
@ -17,7 +17,7 @@ libgstvideoconvert_la_LIBADD = \
libgstvideoconvert_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS)
libgstvideoconvert_la_LIBTOOLFLAGS = --tag=disable-static
noinst_HEADERS = gstvideoconvert.h videoconvert.h
noinst_HEADERS = gstvideoconvert.h videoconvert.h gstcms.h
Android.mk: Makefile.am $(BUILT_SOURCES)
androgenizer \

573
gst/videoconvert/gstcms.c Normal file
View file

@ -0,0 +1,573 @@
/* GStreamer
* Copyright (C) 2008 David Schleef <ds@entropywave.com>
*
* 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.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <gst/gst.h>
#include <gst/math-compat.h>
#include "gstcms.h"
#include <stdio.h>
#include <string.h>
#include <math.h>
/* our simple CMS */
void
color_xyY_to_XYZ (Color * c)
{
if (c->v[1] == 0) {
c->v[0] = 0;
c->v[1] = 0;
c->v[2] = 0;
} else {
double X, Y, Z;
X = c->v[0] * c->v[2] / c->v[1];
Y = c->v[2];
Z = (1.0 - c->v[0] - c->v[1]) * c->v[2] / c->v[1];
c->v[0] = X;
c->v[1] = Y;
c->v[2] = Z;
}
}
void
color_XYZ_to_xyY (Color * c)
{
double d;
d = c->v[0] + c->v[1] + c->v[2];
if (d == 0) {
c->v[0] = 0.3128;
c->v[1] = 0.3290;
c->v[2] = 0;
} else {
double x, y, Y;
x = c->v[0] / d;
y = c->v[1] / d;
Y = c->v[1];
c->v[0] = x;
c->v[1] = y;
c->v[2] = Y;
}
}
void
color_set (Color * c, double x, double y, double z)
{
c->v[0] = x;
c->v[1] = y;
c->v[2] = z;
}
void
color_matrix_set_identity (ColorMatrix * m)
{
int i, j;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
m->m[i][j] = (i == j);
}
}
}
/* Prettyprint a 4x4 matrix @m@ */
void
color_matrix_dump (ColorMatrix * m)
{
int i, j;
printf ("[\n");
for (i = 0; i < 4; i++) {
printf (" ");
for (j = 0; j < 4; j++) {
printf (" %8.5g", m->m[i][j]);
}
printf ("\n");
}
printf ("]\n");
}
/* Perform 4x4 matrix multiplication:
* - @dst@ = @a@ * @b@
* - @dst@ may be a pointer to @a@ andor @b@
*/
void
color_matrix_multiply (ColorMatrix * dst, ColorMatrix * a, ColorMatrix * b)
{
ColorMatrix tmp;
int i, j, k;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
double x = 0;
for (k = 0; k < 4; k++) {
x += a->m[i][k] * b->m[k][j];
}
tmp.m[i][j] = x;
}
}
memcpy (dst, &tmp, sizeof (ColorMatrix));
}
void
color_matrix_apply (ColorMatrix * m, Color * dest, Color * src)
{
int i;
Color tmp;
for (i = 0; i < 3; i++) {
double x = 0;
x += m->m[i][0] * src->v[0];
x += m->m[i][1] * src->v[1];
x += m->m[i][2] * src->v[2];
x += m->m[i][3];
tmp.v[i] = x;
}
memcpy (dest, &tmp, sizeof (tmp));
}
void
color_matrix_offset_components (ColorMatrix * m, double a1, double a2,
double a3)
{
ColorMatrix a;
color_matrix_set_identity (&a);
a.m[0][3] = a1;
a.m[1][3] = a2;
a.m[2][3] = a3;
color_matrix_multiply (m, &a, m);
}
void
color_matrix_scale_components (ColorMatrix * m, double a1, double a2, double a3)
{
ColorMatrix a;
color_matrix_set_identity (&a);
a.m[0][0] = a1;
a.m[1][1] = a2;
a.m[2][2] = a3;
color_matrix_multiply (m, &a, m);
}
void
color_matrix_YCbCr_to_RGB (ColorMatrix * m, double Kr, double Kb)
{
double Kg = 1.0 - Kr - Kb;
ColorMatrix k = {
{
{1., 0., 2 * (1 - Kr), 0.},
{1., -2 * Kb * (1 - Kb) / Kg, -2 * Kr * (1 - Kr) / Kg, 0.},
{1., 2 * (1 - Kb), 0., 0.},
{0., 0., 0., 1.},
}
};
color_matrix_multiply (m, &k, m);
}
void
color_matrix_RGB_to_YCbCr (ColorMatrix * m, double Kr, double Kb)
{
double Kg = 1.0 - Kr - Kb;
ColorMatrix k;
double x;
k.m[0][0] = Kr;
k.m[0][1] = Kg;
k.m[0][2] = Kb;
k.m[0][3] = 0;
x = 1 / (2 * (1 - Kb));
k.m[1][0] = -x * Kr;
k.m[1][1] = -x * Kg;
k.m[1][2] = x * (1 - Kb);
k.m[1][3] = 0;
x = 1 / (2 * (1 - Kr));
k.m[2][0] = x * (1 - Kr);
k.m[2][1] = -x * Kg;
k.m[2][2] = -x * Kb;
k.m[2][3] = 0;
k.m[3][0] = 0;
k.m[3][1] = 0;
k.m[3][2] = 0;
k.m[3][3] = 1;
color_matrix_multiply (m, &k, m);
}
void
color_matrix_build_yuv_to_rgb_601 (ColorMatrix * dst)
{
/*
* At this point, everything is in YCbCr
* All components are in the range [0,255]
*/
color_matrix_set_identity (dst);
/* offset required to get input video black to (0.,0.,0.) */
color_matrix_offset_components (dst, -16, -128, -128);
/* scale required to get input video black to (0.,0.,0.) */
color_matrix_scale_components (dst, (1 / 219.0), (1 / 224.0), (1 / 224.0));
/* colour matrix, YCbCr -> RGB */
/* Requires Y in [0,1.0], Cb&Cr in [-0.5,0.5] */
color_matrix_YCbCr_to_RGB (dst, 0.2990, 0.1140); /* SD */
/*
* We are now in RGB space
*/
#if 0
/* scale to output range. */
color_matrix_scale_components (dst, 255.0, 255.0, 255.0);
#endif
}
void
color_matrix_build_bt709_to_bt601 (ColorMatrix * dst)
{
color_matrix_set_identity (dst);
/* offset required to get input video black to (0.,0.,0.) */
color_matrix_offset_components (dst, -16, -128, -128);
/* scale required to get input video black to (0.,0.,0.) */
color_matrix_scale_components (dst, (1 / 219.0), (1 / 224.0), (1 / 224.0));
/* colour matrix, YCbCr -> RGB */
/* Requires Y in [0,1.0], Cb&Cr in [-0.5,0.5] */
color_matrix_YCbCr_to_RGB (dst, 0.2126, 0.0722); /* HD */
color_matrix_RGB_to_YCbCr (dst, 0.2990, 0.1140); /* SD */
color_matrix_scale_components (dst, 219.0, 224.0, 224.0);
color_matrix_offset_components (dst, 16, 128, 128);
}
void
color_matrix_build_rgb_to_yuv_601 (ColorMatrix * dst)
{
color_matrix_set_identity (dst);
color_matrix_RGB_to_YCbCr (dst, 0.2990, 0.1140); /* SD */
color_matrix_scale_components (dst, 219.0, 224.0, 224.0);
color_matrix_offset_components (dst, 16, 128, 128);
{
Color c;
int i;
for (i = 7; i >= 0; i--) {
color_set (&c, (i & 2) ? 0.75 : 0.0, (i & 4) ? 0.75 : 0.0,
(i & 1) ? 0.75 : 0.0);
color_matrix_apply (dst, &c, &c);
g_print (" { %g, %g, %g },\n", rint (c.v[0]), rint (c.v[1]),
rint (c.v[2]));
}
color_set (&c, -0.075, -0.075, -0.075);
color_matrix_apply (dst, &c, &c);
g_print (" { %g, %g, %g },\n", rint (c.v[0]), rint (c.v[1]),
rint (c.v[2]));
color_set (&c, 0.075, 0.075, 0.075);
color_matrix_apply (dst, &c, &c);
g_print (" { %g, %g, %g },\n", rint (c.v[0]), rint (c.v[1]),
rint (c.v[2]));
}
}
void
color_matrix_invert (ColorMatrix * m)
{
ColorMatrix tmp;
int i, j;
double det;
color_matrix_set_identity (&tmp);
for (j = 0; j < 3; j++) {
for (i = 0; i < 3; i++) {
tmp.m[j][i] =
m->m[(i + 1) % 3][(j + 1) % 3] * m->m[(i + 2) % 3][(j + 2) % 3] -
m->m[(i + 1) % 3][(j + 2) % 3] * m->m[(i + 2) % 3][(j + 1) % 3];
}
}
det =
tmp.m[0][0] * m->m[0][0] + tmp.m[0][1] * m->m[1][0] +
tmp.m[0][2] * m->m[2][0];
for (j = 0; j < 3; j++) {
for (i = 0; i < 3; i++) {
tmp.m[i][j] /= det;
}
}
memcpy (m, &tmp, sizeof (tmp));
}
void
color_matrix_copy (ColorMatrix * dest, ColorMatrix * src)
{
memcpy (dest, src, sizeof (ColorMatrix));
}
void
color_matrix_transpose (ColorMatrix * m)
{
int i, j;
ColorMatrix tmp;
color_matrix_set_identity (&tmp);
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
tmp.m[i][j] = m->m[j][i];
}
}
memcpy (m, &tmp, sizeof (ColorMatrix));
}
void
color_matrix_build_XYZ (ColorMatrix * dst,
double rx, double ry,
double gx, double gy, double bx, double by, double wx, double wy)
{
Color r, g, b, w, scale;
ColorMatrix m;
color_set (&r, rx, ry, 1.0);
color_xyY_to_XYZ (&r);
color_set (&g, gx, gy, 1.0);
color_xyY_to_XYZ (&g);
color_set (&b, bx, by, 1.0);
color_xyY_to_XYZ (&b);
color_set (&w, wx, wy, 1.0);
color_xyY_to_XYZ (&w);
color_matrix_set_identity (dst);
dst->m[0][0] = r.v[0];
dst->m[0][1] = r.v[1];
dst->m[0][2] = r.v[2];
dst->m[1][0] = g.v[0];
dst->m[1][1] = g.v[1];
dst->m[1][2] = g.v[2];
dst->m[2][0] = b.v[0];
dst->m[2][1] = b.v[1];
dst->m[2][2] = b.v[2];
color_matrix_dump (dst);
color_matrix_copy (&m, dst);
color_matrix_invert (&m);
color_matrix_dump (&m);
color_matrix_transpose (&m);
color_matrix_apply (&m, &scale, &w);
g_print ("%g %g %g\n", scale.v[0], scale.v[1], scale.v[2]);
dst->m[0][0] = r.v[0] * scale.v[0];
dst->m[0][1] = r.v[1] * scale.v[0];
dst->m[0][2] = r.v[2] * scale.v[0];
dst->m[1][0] = g.v[0] * scale.v[1];
dst->m[1][1] = g.v[1] * scale.v[1];
dst->m[1][2] = g.v[2] * scale.v[1];
dst->m[2][0] = b.v[0] * scale.v[2];
dst->m[2][1] = b.v[1] * scale.v[2];
dst->m[2][2] = b.v[2] * scale.v[2];
color_matrix_transpose (dst);
color_matrix_dump (dst);
color_set (&scale, 1, 1, 1);
color_matrix_apply (dst, &scale, &scale);
color_XYZ_to_xyY (&scale);
g_print ("white %g %g %g\n", scale.v[0], scale.v[1], scale.v[2]);
}
void
color_matrix_build_rgb_to_XYZ_601 (ColorMatrix * dst)
{
/* SMPTE C primaries, SMPTE 170M-2004 */
color_matrix_build_XYZ (dst,
0.630, 0.340, 0.310, 0.595, 0.155, 0.070, 0.3127, 0.3290);
#if 0
/* NTSC 1953 primaries, SMPTE 170M-2004 */
color_matrix_build_XYZ (dst,
0.67, 0.33, 0.21, 0.71, 0.14, 0.08, 0.3127, 0.3290);
#endif
}
void
color_matrix_build_XYZ_to_rgb_709 (ColorMatrix * dst)
{
/* Rec. ITU-R BT.709-5 */
color_matrix_build_XYZ (dst,
0.640, 0.330, 0.300, 0.600, 0.150, 0.060, 0.3127, 0.3290);
}
void
color_matrix_build_XYZ_to_rgb_dell (ColorMatrix * dst)
{
/* Dell monitor */
#if 1
color_matrix_build_XYZ (dst,
0.662, 0.329, 0.205, 0.683, 0.146, 0.077, 0.3135, 0.3290);
#endif
#if 0
color_matrix_build_XYZ (dst,
0.630, 0.340, 0.310, 0.595, 0.155, 0.070, 0.3127, 0.3290);
#endif
color_matrix_invert (dst);
}
void
color_transfer_function_apply (Color * dest, Color * src)
{
int i;
for (i = 0; i < 3; i++) {
if (src->v[i] < 0.0812) {
dest->v[i] = src->v[i] / 4.500;
} else {
dest->v[i] = pow (src->v[i] + 0.099, 1 / 0.4500);
}
}
}
void
color_transfer_function_unapply (Color * dest, Color * src)
{
int i;
for (i = 0; i < 3; i++) {
if (src->v[i] < 0.0812 / 4.500) {
dest->v[i] = src->v[i] * 4.500;
} else {
dest->v[i] = pow (src->v[i], 0.4500) - 0.099;
}
}
}
void
color_gamut_clamp (Color * dest, Color * src)
{
dest->v[0] = CLAMP (src->v[0], 0.0, 1.0);
dest->v[1] = CLAMP (src->v[1], 0.0, 1.0);
dest->v[2] = CLAMP (src->v[2], 0.0, 1.0);
}
#if 0
static guint8 *
get_color_transform_table (void)
{
static guint8 *color_transform_table = NULL;
#if 1
if (!color_transform_table) {
ColorMatrix bt601_to_rgb;
ColorMatrix bt601_to_yuv;
ColorMatrix bt601_rgb_to_XYZ;
ColorMatrix dell_XYZ_to_rgb;
guint8 *table_y;
guint8 *table_u;
guint8 *table_v;
int y, u, v;
color_matrix_build_yuv_to_rgb_601 (&bt601_to_rgb);
color_matrix_build_rgb_to_yuv_601 (&bt601_to_yuv);
color_matrix_build_rgb_to_XYZ_601 (&bt601_rgb_to_XYZ);
color_matrix_build_XYZ_to_rgb_dell (&dell_XYZ_to_rgb);
color_transform_table = g_malloc (0x1000000 * 3);
table_y = COG_OFFSET (color_transform_table, 0 * 0x1000000);
table_u = COG_OFFSET (color_transform_table, 1 * 0x1000000);
table_v = COG_OFFSET (color_transform_table, 2 * 0x1000000);
for (y = 0; y < 256; y++) {
for (u = 0; u < 256; u++) {
for (v = 0; v < 256; v++) {
Color c;
c.v[0] = y;
c.v[1] = u;
c.v[2] = v;
color_matrix_apply (&bt601_to_rgb, &c, &c);
color_gamut_clamp (&c, &c);
color_transfer_function_apply (&c, &c);
color_matrix_apply (&bt601_rgb_to_XYZ, &c, &c);
color_matrix_apply (&dell_XYZ_to_rgb, &c, &c);
color_transfer_function_unapply (&c, &c);
color_gamut_clamp (&c, &c);
color_matrix_apply (&bt601_to_yuv, &c, &c);
table_y[(y << 16) | (u << 8) | (v)] = rint (c.v[0]);
table_u[(y << 16) | (u << 8) | (v)] = rint (c.v[1]);
table_v[(y << 16) | (u << 8) | (v)] = rint (c.v[2]);
}
}
}
}
#endif
#if 0
if (!color_transform_table) {
ColorMatrix bt709_to_bt601;
guint8 *table_y;
guint8 *table_u;
guint8 *table_v;
int y, u, v;
color_matrix_build_bt709_to_bt601 (&bt709_to_bt601);
color_transform_table = g_malloc (0x1000000 * 3);
table_y = COG_OFFSET (color_transform_table, 0 * 0x1000000);
table_u = COG_OFFSET (color_transform_table, 1 * 0x1000000);
table_v = COG_OFFSET (color_transform_table, 2 * 0x1000000);
for (y = 0; y < 256; y++) {
for (u = 0; u < 256; u++) {
for (v = 0; v < 256; v++) {
Color c;
c.v[0] = y;
c.v[1] = u;
c.v[2] = v;
color_matrix_apply (&bt709_to_bt601, &c, &c);
table_y[(y << 16) | (u << 8) | (v)] = rint (c.v[0]);
table_u[(y << 16) | (u << 8) | (v)] = rint (c.v[1]);
table_v[(y << 16) | (u << 8) | (v)] = rint (c.v[2]);
}
}
}
}
#endif
return color_transform_table;
}
#endif

71
gst/videoconvert/gstcms.h Normal file
View file

@ -0,0 +1,71 @@
/* GStreamer
* Copyright (C) 2008 David Schleef <ds@entropywave.com>
*
* 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.
*/
#ifndef _GST_CMS_H_
#define _GST_CMS_H_
#include <gst/gst.h>
G_BEGIN_DECLS
typedef struct _Color Color;
typedef struct _ColorMatrix ColorMatrix;
struct _Color
{
double v[3];
};
struct _ColorMatrix
{
double m[4][4];
};
void color_xyY_to_XYZ (Color * c);
void color_XYZ_to_xyY (Color * c);
void color_set (Color * c, double x, double y, double z);
void color_matrix_set_identity (ColorMatrix * m);
void color_matrix_dump (ColorMatrix * m);
void color_matrix_multiply (ColorMatrix * dst, ColorMatrix * a, ColorMatrix * b);
void color_matrix_apply (ColorMatrix * m, Color * dest, Color * src);
void color_matrix_offset_components (ColorMatrix * m, double a1, double a2,
double a3);
void color_matrix_scale_components (ColorMatrix * m, double a1, double a2, double a3);
void color_matrix_YCbCr_to_RGB (ColorMatrix * m, double Kr, double Kb);
void color_matrix_RGB_to_YCbCr (ColorMatrix * m, double Kr, double Kb);
void color_matrix_build_yuv_to_rgb_601 (ColorMatrix * dst);
void color_matrix_build_bt709_to_bt601 (ColorMatrix * dst);
void color_matrix_build_rgb_to_yuv_601 (ColorMatrix * dst);
void color_matrix_invert (ColorMatrix * m);
void color_matrix_copy (ColorMatrix * dest, ColorMatrix * src);
void color_matrix_transpose (ColorMatrix * m);
void color_matrix_build_XYZ (ColorMatrix * dst,
double rx, double ry,
double gx, double gy, double bx, double by, double wx, double wy);
void color_matrix_build_rgb_to_XYZ_601 (ColorMatrix * dst);
void color_matrix_build_XYZ_to_rgb_709 (ColorMatrix * dst);
void color_matrix_build_XYZ_to_rgb_dell (ColorMatrix * dst);
void color_transfer_function_apply (Color * dest, Color * src);
void color_transfer_function_unapply (Color * dest, Color * src);
void color_gamut_clamp (Color * dest, Color * src);
G_END_DECLS
#endif

View file

@ -23,15 +23,20 @@
#endif
#include "videoconvert.h"
#include <glib.h>
#include <string.h>
#include <math.h>
#include "gstvideoconvertorc.h"
static void videoconvert_convert_generic (VideoConvert * convert,
GstVideoFrame * dest, const GstVideoFrame * src);
static void videoconvert_convert_lookup_fastpath (VideoConvert * convert);
static void videoconvert_convert_lookup_matrix (VideoConvert * convert);
static void videoconvert_convert_matrix (VideoConvert * convert);
static void videoconvert_convert_matrix16 (VideoConvert * convert);
static gboolean videoconvert_convert_lookup_fastpath (VideoConvert * convert);
static void videoconvert_convert_compute_matrix (VideoConvert * convert);
static void videoconvert_dither_none (VideoConvert * convert, int j);
static void videoconvert_dither_verterr (VideoConvert * convert, int j);
static void videoconvert_dither_halftone (VideoConvert * convert, int j);
@ -47,11 +52,12 @@ videoconvert_convert_new (GstVideoInfo * in_info, GstVideoInfo * out_info)
convert->in_info = *in_info;
convert->out_info = *out_info;
convert->convert = videoconvert_convert_generic;
convert->dither16 = videoconvert_dither_none;
videoconvert_convert_lookup_fastpath (convert);
videoconvert_convert_lookup_matrix (convert);
if (!videoconvert_convert_lookup_fastpath (convert)) {
convert->convert = videoconvert_convert_generic;
videoconvert_convert_compute_matrix (convert);
}
convert->width = GST_VIDEO_INFO_WIDTH (in_info);
convert->height = GST_VIDEO_INFO_HEIGHT (in_info);
@ -121,8 +127,8 @@ videoconvert_convert_convert (VideoConvert * convert,
convert->convert (convert, dest, src);
}
static void
matrix_rgb_to_yuv_bt470_6 (VideoConvert * convert)
void
videoconvert_convert_matrix (VideoConvert * convert)
{
int i;
int r, g, b;
@ -134,9 +140,12 @@ matrix_rgb_to_yuv_bt470_6 (VideoConvert * convert)
g = tmpline[i * 4 + 2];
b = tmpline[i * 4 + 3];
y = (66 * r + 129 * g + 25 * b + 4096) >> 8;
u = (-38 * r - 74 * g + 112 * b + 32768) >> 8;
v = (112 * r - 94 * g - 18 * b + 32768) >> 8;
y = (convert->cmatrix[0][0] * r + convert->cmatrix[0][1] * g +
convert->cmatrix[0][2] * b + convert->cmatrix[0][3]) >> 8;
u = (convert->cmatrix[1][0] * r + convert->cmatrix[1][1] * g +
convert->cmatrix[1][2] * b + convert->cmatrix[1][3]) >> 8;
v = (convert->cmatrix[2][0] * r + convert->cmatrix[2][1] * g +
convert->cmatrix[2][2] * b + convert->cmatrix[2][3]) >> 8;
tmpline[i * 4 + 1] = CLAMP (y, 0, 255);
tmpline[i * 4 + 2] = CLAMP (u, 0, 255);
@ -144,118 +153,29 @@ matrix_rgb_to_yuv_bt470_6 (VideoConvert * convert)
}
}
static void
matrix_rgb_to_yuv_bt709 (VideoConvert * convert)
void
videoconvert_convert_matrix16 (VideoConvert * convert)
{
int i;
int r, g, b;
int y, u, v;
guint8 *tmpline = convert->tmpline;
guint16 *tmpline = convert->tmpline16;
for (i = 0; i < convert->width; i++) {
r = tmpline[i * 4 + 1];
g = tmpline[i * 4 + 2];
b = tmpline[i * 4 + 3];
y = (47 * r + 157 * g + 16 * b + 4096) >> 8;
u = (-26 * r - 87 * g + 112 * b + 32768) >> 8;
v = (112 * r - 102 * g - 10 * b + 32768) >> 8;
y = (convert->cmatrix[0][0] * r + convert->cmatrix[0][1] * g +
convert->cmatrix[0][2] * b + convert->cmatrix[0][3]) >> 8;
u = (convert->cmatrix[1][0] * r + convert->cmatrix[1][1] * g +
convert->cmatrix[1][2] * b + convert->cmatrix[1][3]) >> 8;
v = (convert->cmatrix[2][0] * r + convert->cmatrix[2][1] * g +
convert->cmatrix[2][2] * b + convert->cmatrix[2][3]) >> 8;
tmpline[i * 4 + 1] = CLAMP (y, 0, 255);
tmpline[i * 4 + 2] = CLAMP (u, 0, 255);
tmpline[i * 4 + 3] = CLAMP (v, 0, 255);
}
}
static void
matrix_yuv_bt470_6_to_rgb (VideoConvert * convert)
{
int i;
int r, g, b;
int y, u, v;
guint8 *tmpline = convert->tmpline;
for (i = 0; i < convert->width; i++) {
y = tmpline[i * 4 + 1];
u = tmpline[i * 4 + 2];
v = tmpline[i * 4 + 3];
r = (298 * y + 409 * v - 57068) >> 8;
g = (298 * y - 100 * u - 208 * v + 34707) >> 8;
b = (298 * y + 516 * u - 70870) >> 8;
tmpline[i * 4 + 1] = CLAMP (r, 0, 255);
tmpline[i * 4 + 2] = CLAMP (g, 0, 255);
tmpline[i * 4 + 3] = CLAMP (b, 0, 255);
}
}
static void
matrix_yuv_bt709_to_rgb (VideoConvert * convert)
{
int i;
int r, g, b;
int y, u, v;
guint8 *tmpline = convert->tmpline;
for (i = 0; i < convert->width; i++) {
y = tmpline[i * 4 + 1];
u = tmpline[i * 4 + 2];
v = tmpline[i * 4 + 3];
r = (298 * y + 459 * v - 63514) >> 8;
g = (298 * y - 55 * u - 136 * v + 19681) >> 8;
b = (298 * y + 541 * u - 73988) >> 8;
tmpline[i * 4 + 1] = CLAMP (r, 0, 255);
tmpline[i * 4 + 2] = CLAMP (g, 0, 255);
tmpline[i * 4 + 3] = CLAMP (b, 0, 255);
}
}
static void
matrix_yuv_bt709_to_yuv_bt470_6 (VideoConvert * convert)
{
int i;
int r, g, b;
int y, u, v;
guint8 *tmpline = convert->tmpline;
for (i = 0; i < convert->width; i++) {
y = tmpline[i * 4 + 1];
u = tmpline[i * 4 + 2];
v = tmpline[i * 4 + 3];
r = (256 * y + 25 * u + 49 * v - 9536) >> 8;
g = (253 * u - 28 * v + 3958) >> 8;
b = (-19 * u + 252 * v + 2918) >> 8;
tmpline[i * 4 + 1] = CLAMP (r, 0, 255);
tmpline[i * 4 + 2] = CLAMP (g, 0, 255);
tmpline[i * 4 + 3] = CLAMP (b, 0, 255);
}
}
static void
matrix_yuv_bt470_6_to_yuv_bt709 (VideoConvert * convert)
{
int i;
int r, g, b;
int y, u, v;
guint8 *tmpline = convert->tmpline;
for (i = 0; i < convert->width; i++) {
y = tmpline[i * 4 + 1];
u = tmpline[i * 4 + 2];
v = tmpline[i * 4 + 3];
r = (256 * y - 30 * u - 53 * v + 10600) >> 8;
g = (261 * u + 29 * v - 4367) >> 8;
b = (19 * u + 262 * v - 3289) >> 8;
tmpline[i * 4 + 1] = CLAMP (r, 0, 255);
tmpline[i * 4 + 2] = CLAMP (g, 0, 255);
tmpline[i * 4 + 3] = CLAMP (b, 0, 255);
tmpline[i * 4 + 1] = CLAMP (y, 0, 65535);
tmpline[i * 4 + 2] = CLAMP (u, 0, 65535);
tmpline[i * 4 + 3] = CLAMP (v, 0, 65535);
}
}
@ -266,196 +186,187 @@ matrix_identity (VideoConvert * convert)
}
static void
matrix16_rgb_to_yuv_bt470_6 (VideoConvert * convert)
videoconvert_convert_compute_matrix (VideoConvert * convert)
{
int i;
int r, g, b;
int y, u, v;
guint16 *tmpline = convert->tmpline16;
GstVideoInfo *in_info, *out_info;
ColorMatrix dst;
gint i, j;
const GstVideoFormatInfo *sfinfo, *dfinfo;
gboolean use_16;
gint in_bits, out_bits;
for (i = 0; i < convert->width; i++) {
r = tmpline[i * 4 + 1];
g = tmpline[i * 4 + 2];
b = tmpline[i * 4 + 3];
in_info = &convert->in_info;
out_info = &convert->out_info;
y = (66 * r + 129 * g + 25 * b + 4096 * 256) >> 8;
u = (-38 * r - 74 * g + 112 * b + 32768 * 256) >> 8;
v = (112 * r - 94 * g - 18 * b + 32768 * 256) >> 8;
tmpline[i * 4 + 1] = CLAMP (y, 0, 65535);
tmpline[i * 4 + 2] = CLAMP (u, 0, 65535);
tmpline[i * 4 + 3] = CLAMP (v, 0, 65535);
}
}
static void
matrix16_rgb_to_yuv_bt709 (VideoConvert * convert)
{
int i;
int r, g, b;
int y, u, v;
guint16 *tmpline = convert->tmpline16;
for (i = 0; i < convert->width; i++) {
r = tmpline[i * 4 + 1];
g = tmpline[i * 4 + 2];
b = tmpline[i * 4 + 3];
y = (47 * r + 157 * g + 16 * b + 4096 * 256) >> 8;
u = (-26 * r - 87 * g + 112 * b + 32768 * 256) >> 8;
v = (112 * r - 102 * g - 10 * b + 32768 * 256) >> 8;
tmpline[i * 4 + 1] = CLAMP (y, 0, 65535);
tmpline[i * 4 + 2] = CLAMP (u, 0, 65535);
tmpline[i * 4 + 3] = CLAMP (v, 0, 65535);
}
}
static void
matrix16_yuv_bt470_6_to_rgb (VideoConvert * convert)
{
int i;
int r, g, b;
int y, u, v;
guint16 *tmpline = convert->tmpline16;
for (i = 0; i < convert->width; i++) {
y = tmpline[i * 4 + 1];
u = tmpline[i * 4 + 2];
v = tmpline[i * 4 + 3];
r = (298 * y + 409 * v - 57068 * 256) >> 8;
g = (298 * y - 100 * u - 208 * v + 34707 * 256) >> 8;
b = (298 * y + 516 * u - 70870 * 256) >> 8;
tmpline[i * 4 + 1] = CLAMP (r, 0, 65535);
tmpline[i * 4 + 2] = CLAMP (g, 0, 65535);
tmpline[i * 4 + 3] = CLAMP (b, 0, 65535);
}
}
static void
matrix16_yuv_bt709_to_rgb (VideoConvert * convert)
{
int i;
int r, g, b;
int y, u, v;
guint16 *tmpline = convert->tmpline16;
for (i = 0; i < convert->width; i++) {
y = tmpline[i * 4 + 1];
u = tmpline[i * 4 + 2];
v = tmpline[i * 4 + 3];
r = (298 * y + 459 * v - 63514 * 256) >> 8;
g = (298 * y - 55 * u - 136 * v + 19681 * 256) >> 8;
b = (298 * y + 541 * u - 73988 * 256) >> 8;
tmpline[i * 4 + 1] = CLAMP (r, 0, 65535);
tmpline[i * 4 + 2] = CLAMP (g, 0, 65535);
tmpline[i * 4 + 3] = CLAMP (b, 0, 65535);
}
}
static void
matrix16_yuv_bt709_to_yuv_bt470_6 (VideoConvert * convert)
{
int i;
int r, g, b;
int y, u, v;
guint16 *tmpline = convert->tmpline16;
for (i = 0; i < convert->width; i++) {
y = tmpline[i * 4 + 1];
u = tmpline[i * 4 + 2];
v = tmpline[i * 4 + 3];
r = (256 * y + 25 * u + 49 * v - 9536 * 256) >> 8;
g = (253 * u - 28 * v + 3958 * 256) >> 8;
b = (-19 * u + 252 * v + 2918 * 256) >> 8;
tmpline[i * 4 + 1] = CLAMP (r, 0, 65535);
tmpline[i * 4 + 2] = CLAMP (g, 0, 65535);
tmpline[i * 4 + 3] = CLAMP (b, 0, 65535);
}
}
static void
matrix16_yuv_bt470_6_to_yuv_bt709 (VideoConvert * convert)
{
int i;
int r, g, b;
int y, u, v;
guint16 *tmpline = convert->tmpline16;
for (i = 0; i < convert->width; i++) {
y = tmpline[i * 4 + 1];
u = tmpline[i * 4 + 2];
v = tmpline[i * 4 + 3];
r = (256 * y - 30 * u - 53 * v + 10600 * 256) >> 8;
g = (261 * u + 29 * v - 4367 * 256) >> 8;
b = (19 * u + 262 * v - 3289 * 256) >> 8;
tmpline[i * 4 + 1] = CLAMP (r, 0, 65535);
tmpline[i * 4 + 2] = CLAMP (g, 0, 65535);
tmpline[i * 4 + 3] = CLAMP (b, 0, 65535);
}
}
static void
matrix16_identity (VideoConvert * convert)
{
/* do nothing */
}
static void
videoconvert_convert_lookup_matrix (VideoConvert * convert)
{
GstVideoColorMatrix in_matrix, out_matrix;
in_matrix = convert->in_info.colorimetry.matrix;
out_matrix = convert->out_info.colorimetry.matrix;
if (in_matrix == out_matrix) {
GST_DEBUG ("using identity matrix");
if (in_info->colorimetry.range == out_info->colorimetry.range &&
in_info->colorimetry.matrix == out_info->colorimetry.matrix) {
GST_DEBUG ("using identity color transform");
convert->matrix = matrix_identity;
convert->matrix16 = matrix16_identity;
} else if (in_matrix == GST_VIDEO_COLOR_MATRIX_RGB
&& out_matrix == GST_VIDEO_COLOR_MATRIX_BT601) {
GST_DEBUG ("using RGB -> YUV BT470_6 matrix");
convert->matrix = matrix_rgb_to_yuv_bt470_6;
convert->matrix16 = matrix16_rgb_to_yuv_bt470_6;
} else if (in_matrix == GST_VIDEO_COLOR_MATRIX_RGB
&& out_matrix == GST_VIDEO_COLOR_MATRIX_BT709) {
GST_DEBUG ("using RGB -> YUV BT709 matrix");
convert->matrix = matrix_rgb_to_yuv_bt709;
convert->matrix16 = matrix16_rgb_to_yuv_bt709;
} else if (in_matrix == GST_VIDEO_COLOR_MATRIX_BT601
&& out_matrix == GST_VIDEO_COLOR_MATRIX_RGB) {
GST_DEBUG ("using YUV BT470_6 -> RGB matrix");
convert->matrix = matrix_yuv_bt470_6_to_rgb;
convert->matrix16 = matrix16_yuv_bt470_6_to_rgb;
} else if (in_matrix == GST_VIDEO_COLOR_MATRIX_BT709
&& out_matrix == GST_VIDEO_COLOR_MATRIX_RGB) {
GST_DEBUG ("using YUV BT709 -> RGB matrix");
convert->matrix = matrix_yuv_bt709_to_rgb;
convert->matrix16 = matrix16_yuv_bt709_to_rgb;
} else if (in_matrix == GST_VIDEO_COLOR_MATRIX_BT709
&& out_matrix == GST_VIDEO_COLOR_MATRIX_BT601) {
GST_DEBUG ("using YUV BT709 -> YUV BT470_6");
convert->matrix = matrix_yuv_bt709_to_yuv_bt470_6;
convert->matrix16 = matrix16_yuv_bt709_to_yuv_bt470_6;
} else if (in_matrix == GST_VIDEO_COLOR_MATRIX_BT601
&& out_matrix == GST_VIDEO_COLOR_MATRIX_BT709) {
GST_DEBUG ("using YUV BT470_6 -> YUV BT709");
convert->matrix = matrix_yuv_bt470_6_to_yuv_bt709;
convert->matrix16 = matrix16_yuv_bt470_6_to_yuv_bt709;
} else {
GST_DEBUG ("using identity matrix");
convert->matrix = matrix_identity;
convert->matrix16 = matrix16_identity;
convert->matrix16 = matrix_identity;
return;
}
sfinfo = in_info->finfo;
dfinfo = out_info->finfo;
in_bits =
GST_VIDEO_FORMAT_INFO_DEPTH (gst_video_format_get_info
(sfinfo->unpack_format), 0);
out_bits =
GST_VIDEO_FORMAT_INFO_DEPTH (gst_video_format_get_info
(dfinfo->unpack_format), 0);
if (in_bits == 16 || out_bits == 16)
use_16 = TRUE;
else
use_16 = FALSE;
color_matrix_set_identity (&dst);
/* 1, bring color components to [0..1.0] range */
switch (in_info->colorimetry.range) {
case GST_VIDEO_COLOR_RANGE_0_255:
if (use_16)
color_matrix_scale_components (&dst, (1 / 65535.0), (1 / 65535.0),
(1 / 65535.0));
else
color_matrix_scale_components (&dst, (1 / 255.0), (1 / 255.0),
(1 / 255.0));
break;
default:
case GST_VIDEO_COLOR_RANGE_16_235:
/* offset ans scale required to get input video black to (0.,0.,0.) */
switch (in_info->finfo->unpack_format) {
case GST_VIDEO_FORMAT_AYUV:
case GST_VIDEO_FORMAT_AYUV64:
if (use_16) {
color_matrix_offset_components (&dst, -4096, -32768, -32768);
color_matrix_scale_components (&dst, (1 / 56064.0), (1 / 57344.0),
(1 / 57344.0));
} else {
color_matrix_offset_components (&dst, -16, -128, -128);
color_matrix_scale_components (&dst, (1 / 219.0), (1 / 224.0),
(1 / 224.0));
}
break;
case GST_VIDEO_FORMAT_ARGB:
case GST_VIDEO_FORMAT_ARGB64:
if (use_16) {
color_matrix_offset_components (&dst, -4096, -4096, -4096);
color_matrix_scale_components (&dst, (1 / 56064.0), (1 / 56064.0),
(1 / 56064.0));
} else {
color_matrix_offset_components (&dst, -16, -16, -16);
color_matrix_scale_components (&dst, (1 / 219.0), (1 / 219.0),
(1 / 219.0));
}
break;
default:
break;
}
break;
}
/* 2. bring components to R'G'B' space */
switch (in_info->colorimetry.matrix) {
case GST_VIDEO_COLOR_MATRIX_RGB:
break;
case GST_VIDEO_COLOR_MATRIX_FCC:
color_matrix_YCbCr_to_RGB (&dst, 0.30, 0.11);
break;
case GST_VIDEO_COLOR_MATRIX_BT709:
color_matrix_YCbCr_to_RGB (&dst, 0.2126, 0.0722);
break;
case GST_VIDEO_COLOR_MATRIX_BT601:
color_matrix_YCbCr_to_RGB (&dst, 0.2990, 0.1140);
break;
case GST_VIDEO_COLOR_MATRIX_SMPTE240M:
color_matrix_YCbCr_to_RGB (&dst, 0.212, 0.087);
default:
break;
}
/* 3. inverse transfer function. R'G'B' to linear RGB */
/* 4. from RGB to XYZ using the primaries */
/* 5. from XYZ to RGB using the primaries */
/* 6. transfer function. linear RGB to R'G'B' */
/* 7. bring components to YCbCr space */
switch (out_info->colorimetry.matrix) {
case GST_VIDEO_COLOR_MATRIX_RGB:
break;
case GST_VIDEO_COLOR_MATRIX_FCC:
color_matrix_RGB_to_YCbCr (&dst, 0.30, 0.11);
break;
case GST_VIDEO_COLOR_MATRIX_BT709:
color_matrix_RGB_to_YCbCr (&dst, 0.2126, 0.0722);
break;
case GST_VIDEO_COLOR_MATRIX_BT601:
color_matrix_RGB_to_YCbCr (&dst, 0.2990, 0.1140);
break;
case GST_VIDEO_COLOR_MATRIX_SMPTE240M:
color_matrix_RGB_to_YCbCr (&dst, 0.212, 0.087);
default:
break;
}
/* 8, bring color components to nominal range */
switch (out_info->colorimetry.range) {
case GST_VIDEO_COLOR_RANGE_0_255:
if (use_16)
color_matrix_scale_components (&dst, 65535.0, 65535.0, 65535.0);
else
color_matrix_scale_components (&dst, 255.0, 255.0, 255.0);
break;
case GST_VIDEO_COLOR_RANGE_16_235:
default:
switch (out_info->finfo->unpack_format) {
case GST_VIDEO_FORMAT_AYUV:
case GST_VIDEO_FORMAT_AYUV64:
if (use_16) {
color_matrix_scale_components (&dst, 56064.0, 57344.0, 57344.0);
color_matrix_offset_components (&dst, 4096, 32768, 32768);
} else {
color_matrix_scale_components (&dst, 219.0, 224.0, 224.0);
color_matrix_offset_components (&dst, 16, 128, 128);
}
break;
case GST_VIDEO_FORMAT_ARGB:
case GST_VIDEO_FORMAT_ARGB64:
if (use_16) {
color_matrix_scale_components (&dst, 56064.0, 56064.0, 56064.0);
color_matrix_offset_components (&dst, 4096, 4096, 4096);
} else {
color_matrix_scale_components (&dst, 219.0, 219.0, 219.0);
color_matrix_offset_components (&dst, 16, 16, 16);
}
break;
default:
break;
}
break;
}
/* because we're doing 8-bit matrix coefficients */
color_matrix_scale_components (&dst, 256.0, 256.0, 256.0);
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
convert->cmatrix[i][j] = rint (dst.m[i][j]);
GST_DEBUG ("[%6d %6d %6d %6d]", convert->cmatrix[0][0],
convert->cmatrix[0][1], convert->cmatrix[0][2], convert->cmatrix[0][3]);
GST_DEBUG ("[%6d %6d %6d %6d]", convert->cmatrix[1][0],
convert->cmatrix[1][1], convert->cmatrix[1][2], convert->cmatrix[1][3]);
GST_DEBUG ("[%6d %6d %6d %6d]", convert->cmatrix[2][0],
convert->cmatrix[2][1], convert->cmatrix[2][2], convert->cmatrix[2][3]);
GST_DEBUG ("[%6d %6d %6d %6d]", convert->cmatrix[3][0],
convert->cmatrix[3][1], convert->cmatrix[3][2], convert->cmatrix[3][3]);
convert->matrix = videoconvert_convert_matrix;
convert->matrix16 = videoconvert_convert_matrix16;
}
static void
@ -1316,7 +1227,7 @@ static const VideoTransform transforms[] = {
#endif
};
static void
static gboolean
videoconvert_convert_lookup_fastpath (VideoConvert * convert)
{
int i;
@ -1336,7 +1247,8 @@ videoconvert_convert_lookup_fastpath (VideoConvert * convert)
(transforms[i].in_matrix == in_matrix &&
transforms[i].out_matrix == out_matrix))) {
convert->convert = transforms[i].convert;
return;
return TRUE;
}
}
return FALSE;
}

View file

@ -21,6 +21,7 @@
#define __COLORSPACE_H__
#include <gst/video/video.h>
#include "gstcms.h"
G_BEGIN_DECLS
@ -39,6 +40,8 @@ struct _VideoConvert {
gint width;
gint height;
gint cmatrix[4][4];
guint32 *palette;
ColorSpaceDitherMethod dither;