[FFmpeg-devel] [PATCH v2] avutil/csp: create public API for colorspace structs
Andreas Rheinhardt
andreas.rheinhardt at outlook.com
Tue May 17 17:09:47 EEST 2022
Leo Izen:
> This commit moves some of the functionality from avfilter/colorspace
> into avutil/csp and exposes it as a public API so it can be used by
> libavcodec and/or libavformat.
> ---
> libavfilter/colorspace.c | 94 +-----------------------------
> libavfilter/colorspace.h | 31 ++--------
> libavfilter/fflcms2.c | 11 ++--
> libavfilter/fflcms2.h | 4 +-
> libavfilter/vf_colorspace.c | 19 +++---
> libavfilter/vf_iccdetect.c | 5 +-
> libavfilter/vf_tonemap.c | 15 +----
> libavutil/Makefile | 2 +
> libavutil/csp.c | 111 ++++++++++++++++++++++++++++++++++++
> libavutil/csp.h | 49 ++++++++++++++++
> libavutil/version.h | 4 +-
> 11 files changed, 195 insertions(+), 150 deletions(-)
> create mode 100644 libavutil/csp.c
> create mode 100644 libavutil/csp.h
>
> diff --git a/libavutil/csp.c b/libavutil/csp.c
> new file mode 100644
> index 0000000000..deecefbffc
> --- /dev/null
> +++ b/libavutil/csp.c
> @@ -0,0 +1,111 @@
> +/*
> + * Copyright (c) 2016 Ronald S. Bultje <rsbultje at gmail.com>
> + * This file is part of FFmpeg.
> + *
> + * FFmpeg 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.
> + *
> + * FFmpeg 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 FFmpeg; if not, write to the Free Software
> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
> + */
> +
> +#include "csp.h"
> +#include "frame.h"
?
> +#include "mastering_display_metadata.h"
?
> +#include "pixfmt.h"
> +
> +/*
> + * 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 AVLumaCoefficients 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 AVLumaCoefficients *av_get_luma_coefficients(enum AVColorSpace csp)
> +{
> + const AVLumaCoefficients *coeffs;
> +
> + if (csp >= AVCOL_SPC_NB)
> + return NULL;
> + coeffs = &luma_coefficients[csp];
> + if (!coeffs->cr)
> + return NULL;
> +
> + return coeffs;
> +}
> +
> +#define WP_D65 { 0.3127, 0.3290 }
> +#define WP_C { 0.3100, 0.3160 }
> +#define WP_DCI { 0.3140, 0.3510 }
> +#define WP_E { 1/3.0f, 1/3.0f }
> +
> +static const AVColorPrimariesDesc color_primaries[AVCOL_PRI_NB] = {
> + [AVCOL_PRI_BT709] = { WP_D65, { 0.640, 0.330, 0.300, 0.600, 0.150, 0.060 } },
> + [AVCOL_PRI_BT470M] = { WP_C, { 0.670, 0.330, 0.210, 0.710, 0.140, 0.080 } },
> + [AVCOL_PRI_BT470BG] = { WP_D65, { 0.640, 0.330, 0.290, 0.600, 0.150, 0.060 } },
> + [AVCOL_PRI_SMPTE170M] = { WP_D65, { 0.630, 0.340, 0.310, 0.595, 0.155, 0.070 } },
> + [AVCOL_PRI_SMPTE240M] = { WP_D65, { 0.630, 0.340, 0.310, 0.595, 0.155, 0.070 } },
> + [AVCOL_PRI_SMPTE428] = { WP_E, { 0.735, 0.265, 0.274, 0.718, 0.167, 0.009 } },
> + [AVCOL_PRI_SMPTE431] = { WP_DCI, { 0.680, 0.320, 0.265, 0.690, 0.150, 0.060 } },
> + [AVCOL_PRI_SMPTE432] = { WP_D65, { 0.680, 0.320, 0.265, 0.690, 0.150, 0.060 } },
> + [AVCOL_PRI_FILM] = { WP_C, { 0.681, 0.319, 0.243, 0.692, 0.145, 0.049 } },
> + [AVCOL_PRI_BT2020] = { WP_D65, { 0.708, 0.292, 0.170, 0.797, 0.131, 0.046 } },
> + [AVCOL_PRI_JEDEC_P22] = { WP_D65, { 0.630, 0.340, 0.295, 0.605, 0.155, 0.077 } },
> +};
> +
> +const AVColorPrimariesDesc *av_get_color_primaries(enum AVColorPrimaries prm)
> +{
> + const AVColorPrimariesDesc *p;
> +
> + if (prm >= AVCOL_PRI_NB)
> + return NULL;
> + p = &color_primaries[prm];
> + if (!p->prim.xr)
> + return NULL;
> +
> + return p;
> +}
> +
> +enum AVColorPrimaries av_detect_color_primaries(const AVColorPrimariesDesc *prm)
> +{
> + double delta;
> +
> + for (enum AVColorPrimaries p = 0; p < AVCOL_PRI_NB; p++) {
> + const AVColorPrimariesDesc *ref = &color_primaries[p];
> + if (!ref->prim.xr)
> + continue;
> +
> + delta = fabs(prm->prim.xr - ref->prim.xr) +
> + fabs(prm->prim.yr - ref->prim.yr) +
> + fabs(prm->prim.yg - ref->prim.yg) +
> + fabs(prm->prim.yg - ref->prim.yg) +
> + fabs(prm->prim.yb - ref->prim.yb) +
> + fabs(prm->prim.yb - ref->prim.yb) +
> + fabs(prm->wp.xw - ref->wp.xw) +
> + fabs(prm->wp.yw - ref->wp.yw);
> +
> + if (delta < 0.001)
> + return p;
> + }
> +
> + return AVCOL_PRI_UNSPECIFIED;
> +}
> diff --git a/libavutil/csp.h b/libavutil/csp.h
> new file mode 100644
> index 0000000000..0b4ed0028e
> --- /dev/null
> +++ b/libavutil/csp.h
> @@ -0,0 +1,49 @@
> +/*
> + * Copyright (c) 2016 Ronald S. Bultje <rsbultje at gmail.com>
> + * This file is part of FFmpeg.
> + *
> + * FFmpeg 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.
> + *
> + * FFmpeg 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 FFmpeg; if not, write to the Free Software
> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
> + */
> +
> +#ifndef AVUTIL_CSP_H
> +#define AVUTIL_CSP_H
> +
> +#include "libavutil/frame.h"
Once again: ?
> +#include "libavutil/pixfmt.h"
> +
> +typedef struct AVLumaCoefficients {
> + double cr, cg, cb;
> +} AVLumaCoefficients;
> +
> +typedef struct AVPrimaryCoefficients {
> + double xr, yr, xg, yg, xb, yb;
> +} AVPrimaryCoefficients;
> +
> +typedef struct AVWhitepointCoefficients {
> + double xw, yw;
> +} AVWhitepointCoefficients;
> +
> +typedef struct AVColorPrimariesDesc {
> + AVWhitepointCoefficients wp;
> + AVPrimaryCoefficients prim;
> +} AVColorPrimariesDesc;
> +
> +/* Returns AVCOL_PRI_UNSPECIFIED if no clear match can be identified */
> +enum AVColorPrimaries av_detect_color_primaries(const AVColorPrimariesDesc *prm);
> +
> +const AVColorPrimariesDesc *av_get_color_primaries(enum AVColorPrimaries prm);
> +const AVLumaCoefficients *av_get_luma_coefficients(enum AVColorSpace csp);
> +
> +#endif /* AVUTIL_CSP_H */
More information about the ffmpeg-devel
mailing list