Commit d0f3798b authored by Ruiling Song's avatar Ruiling Song

lavfi/colorspace: move some functions to common file

These functions can be reused by other colorspace filters,
so move them to common file. No functional changes.
Signed-off-by: 's avatarRuiling Song <ruiling.song@intel.com>
parent 61cb505d
......@@ -93,6 +93,77 @@ void ff_fill_rgb2xyz_table(const struct PrimaryCoefficients *coeffs,
rgb2xyz[2][1] *= sg;
rgb2xyz[2][2] *= sb;
}
static const double ycgco_matrix[3][3] =
{
{ 0.25, 0.5, 0.25 },
{ -0.25, 0.5, -0.25 },
{ 0.5, 0, -0.5 },
};
static const double gbr_matrix[3][3] =
{
{ 0, 1, 0 },
{ 0, -0.5, 0.5 },
{ 0.5, -0.5, 0 },
};
/*
* All constants explained in e.g. https://linuxtv.org/downloads/v4l-dvb-apis/ch02s06.html
* The older ones (bt470bg/m) are also explained in their respective ITU docs
* (e.g. https://www.itu.int/dms_pubrec/itu-r/rec/bt/R-REC-BT.470-5-199802-S!!PDF-E.pdf)
* whereas the newer ones can typically be copied directly from wikipedia :)
*/
static const struct LumaCoefficients luma_coefficients[AVCOL_SPC_NB] = {
[AVCOL_SPC_FCC] = { 0.30, 0.59, 0.11 },
[AVCOL_SPC_BT470BG] = { 0.299, 0.587, 0.114 },
[AVCOL_SPC_SMPTE170M] = { 0.299, 0.587, 0.114 },
[AVCOL_SPC_BT709] = { 0.2126, 0.7152, 0.0722 },
[AVCOL_SPC_SMPTE240M] = { 0.212, 0.701, 0.087 },
[AVCOL_SPC_YCOCG] = { 0.25, 0.5, 0.25 },
[AVCOL_SPC_RGB] = { 1, 1, 1 },
[AVCOL_SPC_BT2020_NCL] = { 0.2627, 0.6780, 0.0593 },
[AVCOL_SPC_BT2020_CL] = { 0.2627, 0.6780, 0.0593 },
};
const struct LumaCoefficients *ff_get_luma_coefficients(enum AVColorSpace csp)
{
const struct LumaCoefficients *coeffs;
if (csp >= AVCOL_SPC_NB)
return NULL;
coeffs = &luma_coefficients[csp];
if (!coeffs->cr)
return NULL;
return coeffs;
}
void ff_fill_rgb2yuv_table(const struct LumaCoefficients *coeffs,
double rgb2yuv[3][3])
{
double bscale, rscale;
// special ycgco matrix
if (coeffs->cr == 0.25 && coeffs->cg == 0.5 && coeffs->cb == 0.25) {
memcpy(rgb2yuv, ycgco_matrix, sizeof(double) * 9);
return;
} else if (coeffs->cr == 1 && coeffs->cg == 1 && coeffs->cb == 1) {
memcpy(rgb2yuv, gbr_matrix, sizeof(double) * 9);
return;
}
rgb2yuv[0][0] = coeffs->cr;
rgb2yuv[0][1] = coeffs->cg;
rgb2yuv[0][2] = coeffs->cb;
bscale = 0.5 / (coeffs->cb - 1.0);
rscale = 0.5 / (coeffs->cr - 1.0);
rgb2yuv[1][0] = bscale * coeffs->cr;
rgb2yuv[1][1] = bscale * coeffs->cg;
rgb2yuv[1][2] = 0.5;
rgb2yuv[2][0] = 0.5;
rgb2yuv[2][1] = rscale * coeffs->cg;
rgb2yuv[2][2] = rscale * coeffs->cb;
}
double ff_determine_signal_peak(AVFrame *in)
{
......
......@@ -44,6 +44,10 @@ void ff_fill_rgb2xyz_table(const struct PrimaryCoefficients *coeffs,
const struct WhitepointCoefficients *wp,
double rgb2xyz[3][3]);
const struct LumaCoefficients *ff_get_luma_coefficients(enum AVColorSpace csp);
void ff_fill_rgb2yuv_table(const struct LumaCoefficients *coeffs,
double rgb2yuv[3][3]);
double ff_determine_signal_peak(AVFrame *in);
void ff_update_hdr_metadata(AVFrame *in, double peak);
......
......@@ -170,78 +170,6 @@ typedef struct ColorSpaceContext {
// FIXME dithering if bitdepth goes down?
// FIXME bitexact for fate integration?
static const double ycgco_matrix[3][3] =
{
{ 0.25, 0.5, 0.25 },
{ -0.25, 0.5, -0.25 },
{ 0.5, 0, -0.5 },
};
static const double gbr_matrix[3][3] =
{
{ 0, 1, 0 },
{ 0, -0.5, 0.5 },
{ 0.5, -0.5, 0 },
};
/*
* All constants explained in e.g. https://linuxtv.org/downloads/v4l-dvb-apis/ch02s06.html
* The older ones (bt470bg/m) are also explained in their respective ITU docs
* (e.g. https://www.itu.int/dms_pubrec/itu-r/rec/bt/R-REC-BT.470-5-199802-S!!PDF-E.pdf)
* whereas the newer ones can typically be copied directly from wikipedia :)
*/
static const struct LumaCoefficients luma_coefficients[AVCOL_SPC_NB] = {
[AVCOL_SPC_FCC] = { 0.30, 0.59, 0.11 },
[AVCOL_SPC_BT470BG] = { 0.299, 0.587, 0.114 },
[AVCOL_SPC_SMPTE170M] = { 0.299, 0.587, 0.114 },
[AVCOL_SPC_BT709] = { 0.2126, 0.7152, 0.0722 },
[AVCOL_SPC_SMPTE240M] = { 0.212, 0.701, 0.087 },
[AVCOL_SPC_YCOCG] = { 0.25, 0.5, 0.25 },
[AVCOL_SPC_RGB] = { 1, 1, 1 },
[AVCOL_SPC_BT2020_NCL] = { 0.2627, 0.6780, 0.0593 },
[AVCOL_SPC_BT2020_CL] = { 0.2627, 0.6780, 0.0593 },
};
static const struct LumaCoefficients *get_luma_coefficients(enum AVColorSpace csp)
{
const struct LumaCoefficients *coeffs;
if (csp >= AVCOL_SPC_NB)
return NULL;
coeffs = &luma_coefficients[csp];
if (!coeffs->cr)
return NULL;
return coeffs;
}
static void fill_rgb2yuv_table(const struct LumaCoefficients *coeffs,
double rgb2yuv[3][3])
{
double bscale, rscale;
// special ycgco matrix
if (coeffs->cr == 0.25 && coeffs->cg == 0.5 && coeffs->cb == 0.25) {
memcpy(rgb2yuv, ycgco_matrix, sizeof(double) * 9);
return;
} else if (coeffs->cr == 1 && coeffs->cg == 1 && coeffs->cb == 1) {
memcpy(rgb2yuv, gbr_matrix, sizeof(double) * 9);
return;
}
rgb2yuv[0][0] = coeffs->cr;
rgb2yuv[0][1] = coeffs->cg;
rgb2yuv[0][2] = coeffs->cb;
bscale = 0.5 / (coeffs->cb - 1.0);
rscale = 0.5 / (coeffs->cr - 1.0);
rgb2yuv[1][0] = bscale * coeffs->cr;
rgb2yuv[1][1] = bscale * coeffs->cg;
rgb2yuv[1][2] = 0.5;
rgb2yuv[2][0] = 0.5;
rgb2yuv[2][1] = rscale * coeffs->cg;
rgb2yuv[2][2] = rscale * coeffs->cb;
}
// FIXME I'm pretty sure gamma22/28 also have a linear toe slope, but I can't
// find any actual tables that document their real values...
// See http://www.13thmonkey.org/~boris/gammacorrection/ first graph why it matters
......@@ -669,7 +597,7 @@ static int create_filtergraph(AVFilterContext *ctx,
s->in_rng = in->color_range;
if (s->user_irng != AVCOL_RANGE_UNSPECIFIED)
s->in_rng = s->user_irng;
s->in_lumacoef = get_luma_coefficients(s->in_csp);
s->in_lumacoef = ff_get_luma_coefficients(s->in_csp);
if (!s->in_lumacoef) {
av_log(ctx, AV_LOG_ERROR,
"Unsupported input colorspace %d (%s)\n",
......@@ -682,7 +610,7 @@ static int create_filtergraph(AVFilterContext *ctx,
if (!s->out_lumacoef) {
s->out_csp = out->colorspace;
s->out_rng = out->color_range;
s->out_lumacoef = get_luma_coefficients(s->out_csp);
s->out_lumacoef = ff_get_luma_coefficients(s->out_csp);
if (!s->out_lumacoef) {
if (s->out_csp == AVCOL_SPC_UNSPECIFIED) {
if (s->user_all == CS_UNSPECIFIED) {
......@@ -724,7 +652,7 @@ static int create_filtergraph(AVFilterContext *ctx,
}
for (n = 0; n < 8; n++)
s->yuv_offset[0][n] = off;
fill_rgb2yuv_table(s->in_lumacoef, rgb2yuv);
ff_fill_rgb2yuv_table(s->in_lumacoef, rgb2yuv);
ff_matrix_invert_3x3(rgb2yuv, yuv2rgb);
bits = 1 << (in_desc->comp[0].depth - 1);
for (n = 0; n < 3; n++) {
......@@ -757,7 +685,7 @@ static int create_filtergraph(AVFilterContext *ctx,
}
for (n = 0; n < 8; n++)
s->yuv_offset[1][n] = off;
fill_rgb2yuv_table(s->out_lumacoef, rgb2yuv);
ff_fill_rgb2yuv_table(s->out_lumacoef, rgb2yuv);
bits = 1 << (29 - out_desc->comp[0].depth);
for (out_rng = s->out_y_rng, n = 0; n < 3; n++, out_rng = s->out_uv_rng) {
for (m = 0; m < 3; m++) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment