From patchwork Tue Apr 19 12:46:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Niklas Haas X-Patchwork-Id: 35349 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:3b9e:b0:7d:cfb5:dc7c with SMTP id b30csp328661pzh; Tue, 19 Apr 2022 05:48:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwL5d5euEN3fjcGDy7lpiTEA5eRIUFqqZb3i0HqDIFyEgb3tfYtHcGZL9vugruMIQvu/g6Y X-Received: by 2002:a17:907:72cc:b0:6e8:a2c1:3744 with SMTP id du12-20020a17090772cc00b006e8a2c13744mr13261343ejc.726.1650372510164; Tue, 19 Apr 2022 05:48:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650372510; cv=none; d=google.com; s=arc-20160816; b=s2Gydfm+GnxvPBU4sdgORd4wwKvEo6zMLEtRLrksgBgrg4AdzoWx/Jc+hD2kId4r6o 1WbRKwX7LXR4F9IrUfEh3Jn7pma5KPCeTbxGfvaVvh1PkhWghh6MgLQrHuSS6YcRbzV1 xch2i23TOY29ytIdHLf6Eas2Fn+TJQKcDgkPSZeTQKfz4TKad0nkGL0J4ec3bEkSj0iQ 1B4GqP4BuD2Eb9GVTKChah3qndPH4HDpE6B5NLUONZnGgAGGM6DZxFvolzO2iCsKnvDH JXMW8eg0V9hzmD725NNInGWfeuGoS0gRL1WjejZvrSBp0yZ+DTjFOUvP1bJmCErm8WmC fpDA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:references:in-reply-to :message-id:date:to:from:dkim-signature:delivered-to; bh=pTapCcVclr/q3uw0NiFsdo/AP5UDkdNUlb1KaflG+kc=; b=OjcP+rCclv4XZHvVuQJ/TWKYVpPMTmKe4UIPNcIwjWEMEm1d7hiRTRTfAsuFTQk+Xq BQtNr2k1Y5KBew/k5FWspFAvxLAWUPTxsy1XpOt5Id8sLzWSNB+nw4XX3E+UKAS3mbT8 GUIxowmpGAF7n9/FH1PoiteXA5drB81rPPp+YLtuEZmbWAge6laG3tj3VOteHxSaqRbz JXdcmcI8dAQIbyQ1pNzUkXsojXugq+iK270AgEyGddJND4wr4pNaGahDjJNp6V45AWdQ wI9CpGJmYzOwLJ/MhNK+REpF0Av31KnCSdtnuYON17HZqnnE0ydVVRkmD7hYHEpjxMNB K9Aw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@haasn.xyz header.s=mail header.b=uOZsp1mL; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id o5-20020aa7dd45000000b0041e97a8af57si7704585edw.63.2022.04.19.05.48.29; Tue, 19 Apr 2022 05:48:30 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@haasn.xyz header.s=mail header.b=uOZsp1mL; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 3CEA068B380; Tue, 19 Apr 2022 15:47:43 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from haasn.dev (haasn.dev [78.46.187.166]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id A6D4168B34E for ; Tue, 19 Apr 2022 15:47:32 +0300 (EEST) Received: from haasn.dev (unknown [10.30.0.2]) by haasn.dev (Postfix) with ESMTP id 5FA8849E5F; Tue, 19 Apr 2022 14:47:32 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=haasn.xyz; s=mail; t=1650372452; bh=Z5828lygFJWE8fYYQGPNhHCOAKe4i4TB6CktSBYfTZk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uOZsp1mL02vvIZ9ius9hLU6GIxwV3STQmOiuJ9V98QhVm2mPmYb3bvmiI3wRUuAGd E5J/WPSj5H0/G4ovJtapqUqrFt6nu+GqCDmgENwUQyRzS++0pPV6RWzAVD4RWnhUuB EnhQqIzBDVSqXmyxGolKRZKUSDi87U3YXWmJNce0= From: Niklas Haas To: ffmpeg-devel@ffmpeg.org Date: Tue, 19 Apr 2022 14:46:57 +0200 Message-Id: <20220419124656.121638-5-ffmpeg@haasn.xyz> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220419124656.121638-1-ffmpeg@haasn.xyz> References: <20220419124656.121638-1-ffmpeg@haasn.xyz> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 5/5] lavfi: add vf_iccdetect for parsing ICC profiles 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: uFSDQ/c4g2IP From: Niklas Haas This filter is designed to parse embedded ICC profiles and attempt extracting colorspace tags from them, updating the AVFrame metadata accordingly. This is intentionally made a separate filter, rather than being part of libavcodec itself, so that it's an opt-in behavior for the time being. This also gives the user more flexibility to e.g. first attach an ICC profile and then also set the colorspace tags from it. This makes #9673 possible, though not automatic. Signed-off-by: Niklas Haas --- configure | 1 + doc/filters.texi | 14 ++++ libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/vf_iccdetect.c | 142 +++++++++++++++++++++++++++++++++++++ 5 files changed, 159 insertions(+) create mode 100644 libavfilter/vf_iccdetect.c diff --git a/configure b/configure index 44f8ff738e..612c894f1b 100755 --- a/configure +++ b/configure @@ -3661,6 +3661,7 @@ gblur_vulkan_filter_deps="vulkan spirv_compiler" hflip_vulkan_filter_deps="vulkan spirv_compiler" histeq_filter_deps="gpl" hqdn3d_filter_deps="gpl" +iccdetect_filter_deps="lcms2" iccgen_filter_deps="lcms2" interlace_filter_deps="gpl" kerndeint_filter_deps="gpl" diff --git a/doc/filters.texi b/doc/filters.texi index f29845890e..85b1586486 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -14342,6 +14342,20 @@ By default value is 0. The @code{hysteresis} filter also supports the @ref{framesync} options. +@section iccdetect + +Detect the colorspace from an embedded ICC profile (if present), and update +the frame's tags accordingly. + +This filter accepts the following options: + +@table @option +@item force +If true, the frame's existing colorspace tags will always be overridden by +values detected from an ICC profile. Otherwise, they will only be assigned if +they contain @code{unknown}. Enabled by default. +@end table + @section iccgen Generate ICC profiles and attach them to frames. diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 1f2b03fa9c..2c131e6af5 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -321,6 +321,7 @@ OBJS-$(CONFIG_HWMAP_FILTER) += vf_hwmap.o OBJS-$(CONFIG_HWUPLOAD_CUDA_FILTER) += vf_hwupload_cuda.o OBJS-$(CONFIG_HWUPLOAD_FILTER) += vf_hwupload.o OBJS-$(CONFIG_HYSTERESIS_FILTER) += vf_hysteresis.o framesync.o +OBJS-$(CONFIG_ICCDETECT_FILTER) += vf_iccdetect.o fflcms2.o colorspace.o OBJS-$(CONFIG_ICCGEN_FILTER) += vf_iccgen.o fflcms2.o colorspace.o OBJS-$(CONFIG_IDENTITY_FILTER) += vf_identity.o OBJS-$(CONFIG_IDET_FILTER) += vf_idet.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 212615019d..ebe12520b5 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -304,6 +304,7 @@ extern const AVFilter ff_vf_hwmap; extern const AVFilter ff_vf_hwupload; extern const AVFilter ff_vf_hwupload_cuda; extern const AVFilter ff_vf_hysteresis; +extern const AVFilter ff_vf_iccdetect; extern const AVFilter ff_vf_iccgen; extern const AVFilter ff_vf_identity; extern const AVFilter ff_vf_idet; diff --git a/libavfilter/vf_iccdetect.c b/libavfilter/vf_iccdetect.c new file mode 100644 index 0000000000..fb7871f035 --- /dev/null +++ b/libavfilter/vf_iccdetect.c @@ -0,0 +1,142 @@ +/* + * Copyright (c) 2022 Niklas Haas + * 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 + */ + +/** + * @file + * filter for generating ICC profiles + */ + +#include + +#include "libavutil/opt.h" +#include "libavutil/pixdesc.h" + +#include "avfilter.h" +#include "fflcms2.h" +#include "internal.h" + +typedef struct IccDetectContext { + const AVClass *class; + FFIccContext icc; + int force; + /* (cached) detected ICC profile values */ + AVBufferRef *profile; + enum AVColorPrimaries profile_prim; + enum AVColorTransferCharacteristic profile_trc; +} IccDetectContext; + +#define OFFSET(x) offsetof(IccDetectContext, x) +#define VF AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM + +static const AVOption iccdetect_options[] = { + { "force", "overwrite existing tags", OFFSET(force), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, VF }, + { NULL } +}; + +AVFILTER_DEFINE_CLASS(iccdetect); + +static av_cold void iccdetect_uninit(AVFilterContext *avctx) +{ + IccDetectContext *s = avctx->priv; + av_buffer_unref(&s->profile); + ff_icc_context_uninit(&s->icc); +} + +static av_cold int iccdetect_init(AVFilterContext *avctx) +{ + IccDetectContext *s = avctx->priv; + return ff_icc_context_init(&s->icc, avctx); +} + +static int iccdetect_filter_frame(AVFilterLink *inlink, AVFrame *frame) +{ + AVFilterContext *avctx = inlink->dst; + IccDetectContext *s = avctx->priv; + const AVFrameSideData *sd; + struct ColorPrimaries coeffs; + cmsHPROFILE profile; + int ret; + + sd = av_frame_get_side_data(frame, AV_FRAME_DATA_ICC_PROFILE); + if (!sd) + return ff_filter_frame(inlink->dst->outputs[0], frame); + + if (s->profile && s->profile->data == sd->buf->data) { + /* No change from previous ICC profile */ + goto done; + } + + if ((ret = av_buffer_replace(&s->profile, sd->buf)) < 0) + return ret; + s->profile_prim = AVCOL_PRI_UNSPECIFIED; + s->profile_trc = AVCOL_TRC_UNSPECIFIED; + + profile = cmsOpenProfileFromMemTHR(s->icc.ctx, sd->data, sd->size); + if (!profile) + return AVERROR_INVALIDDATA; + + ret = ff_icc_profile_read_primaries(&s->icc, profile, &coeffs); + if (!ret) + ret = ff_icc_profile_detect_transfer(&s->icc, profile, &s->profile_trc); + cmsCloseProfile(profile); + if (ret < 0) + return ret; + + s->profile_prim = ff_detect_color_primaries(&coeffs); + +done: + if (s->profile_prim != AVCOL_PRI_UNSPECIFIED) { + if (s->force || frame->color_primaries == AVCOL_PRI_UNSPECIFIED) + frame->color_primaries = s->profile_prim; + } + + if (s->profile_trc != AVCOL_TRC_UNSPECIFIED) { + if (s->force || frame->color_trc == AVCOL_TRC_UNSPECIFIED) + frame->color_trc = s->profile_trc; + } + + return ff_filter_frame(inlink->dst->outputs[0], frame); +} + +static const AVFilterPad iccdetect_inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .filter_frame = iccdetect_filter_frame, + }, +}; + +static const AVFilterPad iccdetect_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + }, +}; + +const AVFilter ff_vf_iccdetect = { + .name = "iccdetect", + .description = NULL_IF_CONFIG_SMALL("Detect and parse ICC profiles."), + .priv_size = sizeof(IccDetectContext), + .priv_class = &iccdetect_class, + .flags = AVFILTER_FLAG_METADATA_ONLY, + .init = &iccdetect_init, + .uninit = &iccdetect_uninit, + FILTER_INPUTS(iccdetect_inputs), + FILTER_OUTPUTS(iccdetect_outputs), +};