32 memset(
s, 0,
sizeof(*
s));
34 s->ctx = cmsCreateContext(
NULL,
s);
38 cmsSetLogErrorHandlerTHR(
s->ctx,
log_cb);
45 cmsFreeToneCurve(
s->curves[
i]);
46 cmsDeleteContext(
s->ctx);
47 memset(
s, 0,
sizeof(*
s));
51 cmsToneCurve **out_curve)
53 if ((
unsigned)trc < AVCOL_TRC_NB && s->
curves[trc])
58 s->curves[trc] = cmsBuildGamma(
s->ctx, 1.0);
61 s->curves[trc] = cmsBuildGamma(
s->ctx, 2.2);
64 s->curves[trc] = cmsBuildGamma(
s->ctx, 2.8);
70 s->curves[trc] = cmsBuildParametricToneCurve(
s->ctx, 4, (
double[5]) {
73 1 - 1/1.099296826809442,
75 4.5 * 0.018053968510807,
79 s->curves[trc] = cmsBuildParametricToneCurve(
s->ctx, 4, (
double[5]) {
88 s->curves[trc] = cmsBuildParametricToneCurve(
s->ctx, 8, (
double[5]) {
97 s->curves[trc] = cmsBuildParametricToneCurve(
s->ctx, 8, (
double[5]) {
106 s->curves[trc] = cmsBuildParametricToneCurve(
s->ctx, 4, (
double[5]) {
115 s->curves[trc] = cmsBuildParametricToneCurve(
s->ctx, 2, (
double[3]) {
117 pow(52.37/48.0, 1/2.6),
139 *out_curve =
s->curves[trc];
146 cmsHPROFILE *out_profile)
148 cmsToneCurve *tonecurve;
154 if ((ret =
get_curve(
s, color_trc, &tonecurve)) < 0)
157 *out_profile = cmsCreateRGBProfileTHR(
s->ctx,
158 &(cmsCIExyY) { av_q2d(prim->wp.x), av_q2d(prim->wp.y), 1.0 },
160 .Red = { av_q2d(prim->prim.r.x), av_q2d(prim->prim.r.y), 1.0 },
161 .Green = { av_q2d(prim->prim.g.x), av_q2d(prim->prim.g.y), 1.0 },
162 .Blue = { av_q2d(prim->prim.b.x), av_q2d(prim->prim.b.y), 1.0 },
164 (cmsToneCurve *[3]) { tonecurve, tonecurve, tonecurve }
172 cmsUInt32Number
size;
197 double k = 1.0 / (
XYZ.X +
XYZ.Y +
XYZ.Z);
213 cmsCIEXYZ *white,
fixed;
219 if (cmsGetEncodedICCversion(
profile) >= 0x4000000) {
220 switch (cmsGetHeaderRenderingIntent(
profile)) {
221 case INTENT_RELATIVE_COLORIMETRIC:
222 case INTENT_ABSOLUTE_COLORIMETRIC: ;
227 if (!(white = cmsReadTag(
profile, cmsSigMediaWhitePointTag)))
233 "profile media white point tag (expected %.4f %.4f, "
242 if (!cmsWriteTag(
profile, cmsSigMediaWhitePointTag, &
fixed))
256 static const uint8_t testprimaries[4][3] = {
260 { 0xFF, 0xFF, 0xFF },
265 cmsFloat64Number prev_adapt;
270 xyz = cmsCreateXYZProfileTHR(
s->ctx);
275 prev_adapt = cmsSetAdaptationStateTHR(
s->ctx, 0.0);
276 tf = cmsCreateTransformTHR(
s->ctx,
profile, TYPE_RGB_8, xyz, TYPE_XYZ_DBL,
277 INTENT_ABSOLUTE_COLORIMETRIC,
281 cmsFLAGS_NOOPTIMIZE |
282 cmsFLAGS_LOWRESPRECALC |
283 cmsFLAGS_GRIDPOINTS(2));
284 cmsSetAdaptationStateTHR(
s->ctx, prev_adapt);
285 cmsCloseProfile(xyz);
291 cmsDoTransform(tf, testprimaries,
dst, 4);
292 cmsDeleteTransform(tf);
304 static const uint8_t testramp[16][3] = {
326 cmsToneCurve *tonecurve;
333 ref = cmsCreateGrayProfileTHR(
s->ctx, cmsD50_xyY(), tonecurve);
337 tf = cmsCreateTransformTHR(
s->ctx,
profile, TYPE_RGB_8,
ref, TYPE_GRAY_DBL,
338 INTENT_RELATIVE_COLORIMETRIC,
339 cmsFLAGS_NOCACHE | cmsFLAGS_NOOPTIMIZE);
340 cmsCloseProfile(
ref);
347 cmsDeleteTransform(tf);
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
static AVFormatContext * ctx
#define i(width, name, range_min, range_max)
Colorspace value utility functions for libavutil.
static __device__ float fabs(float a)
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
AVBufferRef * av_buffer_alloc(size_t size)
Allocate an AVBuffer of the given size using av_malloc().
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
#define AVERROR_EXTERNAL
Generic error in an external library.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
AVFrameSideData * av_frame_new_side_data_from_buf(AVFrame *frame, enum AVFrameSideDataType type, AVBufferRef *buf)
Add a new side data to a frame from an existing AVBufferRef.
@ AV_FRAME_DATA_ICC_PROFILE
The data contains an ICC profile as an opaque octet buffer following the format described by ISO 1507...
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
AVCIExy AVWhitepointCoefficients
Struct defining white point location in terms of CIE 1931 chromaticity x and y.
const AVColorPrimariesDesc * av_csp_primaries_desc_from_id(enum AVColorPrimaries prm)
Retrieves a complete gamut description from an enum constant describing the color primaries.
AVRational av_add_q(AVRational b, AVRational c)
Add two rationals.
static AVRational av_make_q(int num, int den)
Create an AVRational.
AVRational av_d2q(double d, int max)
Convert a double precision floating point number to a rational.
static double av_q2d(AVRational a)
Convert an AVRational to a double.
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
AVRational av_sub_q(AVRational b, AVRational c)
Subtract one rational from another.
AVRational av_div_q(AVRational b, AVRational c)
Divide one rational by another.
#define fixed(width, name, value)
int ff_icc_profile_attach(FFIccContext *s, cmsHPROFILE profile, AVFrame *frame)
Attach an ICC profile to a frame.
int ff_icc_profile_generate(FFIccContext *s, enum AVColorPrimaries color_prim, enum AVColorTransferCharacteristic color_trc, cmsHPROFILE *out_profile)
Generate an ICC profile for a given combination of color primaries and transfer function.
void ff_icc_context_uninit(FFIccContext *s)
static void log_cb(cmsContext ctx, cmsUInt32Number error, const char *str)
static const AVCIExy wp_d50
static int get_curve(FFIccContext *s, enum AVColorTransferCharacteristic trc, cmsToneCurve **out_curve)
static av_always_inline void XYZ_xy(cmsCIEXYZ XYZ, AVCIExy *xy)
int ff_icc_profile_sanitize(FFIccContext *s, cmsHPROFILE profile)
Sanitize an ICC profile to try and fix badly broken values.
static av_always_inline AVRational abs_sub_q(AVRational r1, AVRational r2)
int ff_icc_profile_read_primaries(FFIccContext *s, cmsHPROFILE profile, AVColorPrimariesDesc *out_primaries)
Read the color primaries and white point coefficients encoded by an ICC profile, and return the raw v...
int ff_icc_context_init(FFIccContext *s, void *avctx)
Initializes an FFIccContext.
int ff_icc_profile_detect_transfer(FFIccContext *s, cmsHPROFILE profile, enum AVColorTransferCharacteristic *out_trc)
Attempt detecting the transfer characteristic that best approximates the transfer function encoded by...
Various functions for dealing with ICC profiles.
AVColorPrimaries
Chromaticity coordinates of the source primaries.
AVColorTransferCharacteristic
Color Transfer Characteristic.
@ AVCOL_TRC_SMPTE170M
also ITU-R BT601-6 525 or 625 / ITU-R BT1358 525 or 625 / ITU-R BT1700 NTSC
@ AVCOL_TRC_SMPTE2084
SMPTE ST 2084 for 10-, 12-, 14- and 16-bit systems.
@ AVCOL_TRC_GAMMA22
also ITU-R BT470M / ITU-R BT1700 625 PAL & SECAM
@ AVCOL_TRC_BT1361_ECG
ITU-R BT1361 Extended Colour Gamut.
@ AVCOL_TRC_LOG
"Logarithmic transfer characteristic (100:1 range)"
@ AVCOL_TRC_IEC61966_2_4
IEC 61966-2-4.
@ AVCOL_TRC_LINEAR
"Linear transfer characteristics"
@ AVCOL_TRC_GAMMA28
also ITU-R BT470BG
@ AVCOL_TRC_ARIB_STD_B67
ARIB STD-B67, known as "Hybrid log-gamma".
@ AVCOL_TRC_SMPTE428
SMPTE ST 428-1.
@ AVCOL_TRC_LOG_SQRT
"Logarithmic transfer characteristic (100 * Sqrt(10) : 1 range)"
@ AVCOL_TRC_BT2020_12
ITU-R BT2020 for 12-bit system.
@ AVCOL_TRC_IEC61966_2_1
IEC 61966-2-1 (sRGB or sYCC)
@ AVCOL_TRC_BT2020_10
ITU-R BT2020 for 10-bit system.
@ AVCOL_TRC_BT709
also ITU-R BT1361
@ AVCOL_TRC_NB
Not part of ABI.
#define FF_ARRAY_ELEMS(a)
A reference to a data buffer.
uint8_t * data
The data buffer.
size_t size
Size of data in bytes.
Struct containing chromaticity x and y values for the standard CIE 1931 chromaticity definition.
Struct that contains both white point location and primaries location, providing the complete descrip...
AVWhitepointCoefficients wp
AVPrimaryCoefficients prim
This structure describes decoded (raw) audio or video data.
Struct defining the red, green, and blue primary locations in terms of CIE 1931 chromaticity x and y.
Rational number (pair of numerator and denominator).
static void error(const char *err)
static int ref[MAX_W *MAX_W]
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
static const Curve curves[]