From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id 9404D47B4A for ; Mon, 2 Oct 2023 22:30:28 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 7EE2668CB17; Tue, 3 Oct 2023 01:30:26 +0300 (EEST) Received: from haasn.dev (haasn.dev [78.46.187.166]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id CCF0568CAB0 for ; Tue, 3 Oct 2023 01:30:20 +0300 (EEST) Received: from haasn.dev (unknown [10.30.0.2]) by haasn.dev (Postfix) with ESMTP id 8FE1C4BC75; Tue, 3 Oct 2023 00:30:20 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=haasn.xyz; s=mail; t=1696285820; bh=UqafSmlTYIlLF8mPoTQtgnyPooERtoa1pHP+dX8xDX4=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=c3dB9ECcaxRxsw8wUD/Un9miGqlx4A2RDqha/2HoPnO5IG8Un5wCNja5haMjFSONF PXx8e9LoWID5V05EkH9Q8IEXOgij/ud9rozUYmmYSouRsmtq63ljhns7C753hxoUzS t+oCVjy7H96Xp6PEE1z9FfzI0FnIoFemgwq2NUs0= Date: Tue, 3 Oct 2023 00:30:20 +0200 Message-ID: <20231003003020.GD66943@haasn.xyz> From: Niklas Haas To: ffmpeg-devel@ffmpeg.org In-Reply-To: <20230928151041.57953-1-ffmpeg@haasn.xyz> References: <20230928151041.57953-1-ffmpeg@haasn.xyz> MIME-Version: 1.0 Content-Disposition: inline Subject: Re: [FFmpeg-devel] [PATCH v2 1/3] avcodec/fflcms2: add ff_icc_profile_sanitize X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Niklas Haas Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: On Thu, 28 Sep 2023 17:10:39 +0200 Niklas Haas wrote: > From: Niklas Haas > > Buggy ICCv4 profiles are unfortunately used in the wild, and it's quite > easy to work around them by just forcing the white point to the correct > value. Display a warning just in case. > > See-Also: https://trac.ffmpeg.org/ticket/9673 > --- > libavcodec/fflcms2.c | 51 ++++++++++++++++++++++++++++++++++++++++++++ > libavcodec/fflcms2.h | 7 ++++++ > 2 files changed, 58 insertions(+) > > diff --git a/libavcodec/fflcms2.c b/libavcodec/fflcms2.c > index 5443f178bc9..3b67e62d3af 100644 > --- a/libavcodec/fflcms2.c > +++ b/libavcodec/fflcms2.c > @@ -201,6 +201,57 @@ static av_always_inline void XYZ_xy(cmsCIEXYZ XYZ, AVCIExy *xy) > xy->y = av_d2q(k * XYZ.Y, 100000); > } > > +static av_always_inline AVRational abs_sub_q(AVRational r1, AVRational r2) > +{ > + AVRational diff = av_sub_q(r1, r2); > + /* denominator assumed to be positive */ > + return av_make_q(abs(diff.num), diff.den); > +} > + > +static const AVCIExy wp_d50 = { {3457, 10000}, {3585, 10000} }; /* CIE D50 */ > + > +int ff_icc_profile_sanitize(FFIccContext *s, cmsHPROFILE profile) > +{ > + cmsCIEXYZ *white, fixed; > + AVCIExy wpxy; > + AVRational diff, z; > + if (!profile) > + return 0; > + > + if (cmsGetEncodedICCversion(profile) >= 0x4000000) { // ICC v4 > + switch (cmsGetHeaderRenderingIntent(profile)) { > + case INTENT_RELATIVE_COLORIMETRIC: > + case INTENT_ABSOLUTE_COLORIMETRIC: ; > + /* ICC v4 colorimetric profiles are specified to always use D50 > + * media white point, anything else is a violation of the spec. > + * Sadly, such profiles are incredibly common (Apple...), so make > + * an effort to fix them. */ > + if (!(white = cmsReadTag(profile, cmsSigMediaWhitePointTag))) > + return AVERROR_INVALIDDATA; > + XYZ_xy(*white, &wpxy); > + diff = av_add_q(abs_sub_q(wpxy.x, wp_d50.x), abs_sub_q(wpxy.y, wp_d50.y)); > + if (av_cmp_q(diff, av_make_q(1, 1000)) > 0) { > + av_log(s->avctx, AV_LOG_WARNING, "Invalid colorimetric ICCv4 " > + "profile media white point tag (expected %.4f %.4f, " > + "got %.4f %.4f)\n", > + av_q2d(wp_d50.x), av_q2d(wp_d50.y), > + av_q2d(wpxy.x), av_q2d(wpxy.y)); > + /* x+y+z = 1 */ > + z = av_sub_q(av_sub_q(av_make_q(1, 1), wp_d50.x), wp_d50.y); > + fixed.X = av_q2d(av_div_q(wp_d50.x, wp_d50.y)) * white->Y; > + fixed.Y = white->Y; > + fixed.Z = av_q2d(av_div_q(z, wp_d50.y)) * white->Y; > + if (!cmsWriteTag(profile, cmsSigMediaWhitePointTag, &fixed)) > + return AVERROR_EXTERNAL; > + } > + break; > + default: break; > + } > + } > + > + return 0; > +} > + > int ff_icc_profile_read_primaries(FFIccContext *s, cmsHPROFILE profile, > AVColorPrimariesDesc *out_primaries) > { > diff --git a/libavcodec/fflcms2.h b/libavcodec/fflcms2.h > index af63c9a13c8..b54173e50e2 100644 > --- a/libavcodec/fflcms2.h > +++ b/libavcodec/fflcms2.h > @@ -65,6 +65,13 @@ int ff_icc_profile_generate(FFIccContext *s, > */ > int ff_icc_profile_attach(FFIccContext *s, cmsHPROFILE profile, AVFrame *frame); > > +/** > + * Sanitize an ICC profile to try and fix badly broken values. > + * > + * Returns 0 on success, or a negative error code. > + */ > +int ff_icc_profile_sanitize(FFIccContext *s, cmsHPROFILE profile); > + > /** > * Read the color primaries and white point coefficients encoded by an ICC > * profile, and return the raw values in `out_primaries`. > -- > 2.42.0 > Merged as beac4a99..0d596776 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".