FFmpeg
vf_iccdetect.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022 Niklas Haas
3  * This file is part of FFmpeg.
4  *
5  * FFmpeg is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * FFmpeg is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with FFmpeg; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 /**
21  * @file
22  * filter for generating ICC profiles
23  */
24 
25 #include <lcms2.h>
26 
27 #include "libavutil/csp.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/pixdesc.h"
30 
31 #include "avfilter.h"
32 #include "fflcms2.h"
33 #include "internal.h"
34 
35 typedef struct IccDetectContext {
36  const AVClass *class;
38  int force;
39  /* (cached) detected ICC profile values */
44 
45 #define OFFSET(x) offsetof(IccDetectContext, x)
46 #define VF AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
47 
48 static const AVOption iccdetect_options[] = {
49  { "force", "overwrite existing tags", OFFSET(force), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, VF },
50  { NULL }
51 };
52 
53 AVFILTER_DEFINE_CLASS(iccdetect);
54 
56 {
57  IccDetectContext *s = avctx->priv;
58  av_buffer_unref(&s->profile);
59  ff_icc_context_uninit(&s->icc);
60 }
61 
63 {
64  IccDetectContext *s = avctx->priv;
65  return ff_icc_context_init(&s->icc, avctx);
66 }
67 
69 {
70  AVFilterContext *avctx = inlink->dst;
71  IccDetectContext *s = avctx->priv;
72  const AVFrameSideData *sd;
73  AVColorPrimariesDesc coeffs;
74  cmsHPROFILE profile;
75  int ret;
76 
78  if (!sd)
79  return ff_filter_frame(inlink->dst->outputs[0], frame);
80 
81  if (s->profile && s->profile->data == sd->buf->data) {
82  /* No change from previous ICC profile */
83  goto done;
84  }
85 
86  if ((ret = av_buffer_replace(&s->profile, sd->buf)) < 0)
87  return ret;
88  s->profile_prim = AVCOL_PRI_UNSPECIFIED;
89  s->profile_trc = AVCOL_TRC_UNSPECIFIED;
90 
91  profile = cmsOpenProfileFromMemTHR(s->icc.ctx, sd->data, sd->size);
92  if (!profile)
93  return AVERROR_INVALIDDATA;
94 
95  ret = ff_icc_profile_read_primaries(&s->icc, profile, &coeffs);
96  if (!ret)
97  ret = ff_icc_profile_detect_transfer(&s->icc, profile, &s->profile_trc);
98  cmsCloseProfile(profile);
99  if (ret < 0)
100  return ret;
101 
102  s->profile_prim = av_csp_primaries_id_from_desc(&coeffs);
103 
104 done:
105  if (s->profile_prim != AVCOL_PRI_UNSPECIFIED) {
106  if (s->force || frame->color_primaries == AVCOL_PRI_UNSPECIFIED)
107  frame->color_primaries = s->profile_prim;
108  }
109 
110  if (s->profile_trc != AVCOL_TRC_UNSPECIFIED) {
111  if (s->force || frame->color_trc == AVCOL_TRC_UNSPECIFIED)
112  frame->color_trc = s->profile_trc;
113  }
114 
115  return ff_filter_frame(inlink->dst->outputs[0], frame);
116 }
117 
118 static const AVFilterPad iccdetect_inputs[] = {
119  {
120  .name = "default",
121  .type = AVMEDIA_TYPE_VIDEO,
122  .filter_frame = iccdetect_filter_frame,
123  },
124 };
125 
126 static const AVFilterPad iccdetect_outputs[] = {
127  {
128  .name = "default",
129  .type = AVMEDIA_TYPE_VIDEO,
130  },
131 };
132 
134  .name = "iccdetect",
135  .description = NULL_IF_CONFIG_SMALL("Detect and parse ICC profiles."),
136  .priv_size = sizeof(IccDetectContext),
137  .priv_class = &iccdetect_class,
139  .init = &iccdetect_init,
143 };
ff_icc_profile_read_primaries
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...
Definition: fflcms2.c:205
iccdetect_filter_frame
static int iccdetect_filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: vf_iccdetect.c:68
IccDetectContext::profile_prim
enum AVColorPrimaries profile_prim
Definition: vf_iccdetect.c:41
opt.h
AVColorTransferCharacteristic
AVColorTransferCharacteristic
Color Transfer Characteristic.
Definition: pixfmt.h:496
IccDetectContext::profile
AVBufferRef * profile
Definition: vf_iccdetect.c:40
av_frame_get_side_data
AVFrameSideData * av_frame_get_side_data(const AVFrame *frame, enum AVFrameSideDataType type)
Definition: frame.c:684
AVColorPrimariesDesc
Struct that contains both white point location and primaries location, providing the complete descrip...
Definition: csp.h:69
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:999
AVBufferRef::data
uint8_t * data
The data buffer.
Definition: buffer.h:90
ff_icc_profile_detect_transfer
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...
Definition: fflcms2.c:252
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:325
pixdesc.h
AVFrameSideData::buf
AVBufferRef * buf
Definition: frame.h:236
AVOption
AVOption.
Definition: opt.h:251
AVCOL_TRC_UNSPECIFIED
@ AVCOL_TRC_UNSPECIFIED
Definition: pixfmt.h:499
IccDetectContext::profile_trc
enum AVColorTransferCharacteristic profile_trc
Definition: vf_iccdetect.c:42
OFFSET
#define OFFSET(x)
Definition: vf_iccdetect.c:45
AVColorPrimaries
AVColorPrimaries
Chromaticity coordinates of the source primaries.
Definition: pixfmt.h:471
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:175
iccdetect_inputs
static const AVFilterPad iccdetect_inputs[]
Definition: vf_iccdetect.c:118
init
static int init
Definition: av_tx.c:47
AVFilterContext::priv
void * priv
private data for use by the filter
Definition: avfilter.h:423
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:49
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(iccdetect)
IccDetectContext::icc
FFIccContext icc
Definition: vf_iccdetect.c:37
AVFrameSideData::size
size_t size
Definition: frame.h:234
av_cold
#define av_cold
Definition: attributes.h:90
s
#define s(width, name)
Definition: cbs_vp9.c:256
ff_icc_context_uninit
void ff_icc_context_uninit(FFIccContext *s)
Definition: fflcms2.c:43
av_csp_primaries_id_from_desc
enum AVColorPrimaries av_csp_primaries_id_from_desc(const AVColorPrimariesDesc *prm)
Detects which enum AVColorPrimaries constant corresponds to the given complete gamut description.
Definition: csp.c:105
VF
#define VF
Definition: vf_iccdetect.c:46
AVCOL_PRI_UNSPECIFIED
@ AVCOL_PRI_UNSPECIFIED
Definition: pixfmt.h:474
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:190
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
iccdetect_uninit
static av_cold void iccdetect_uninit(AVFilterContext *avctx)
Definition: vf_iccdetect.c:55
av_buffer_unref
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
Definition: buffer.c:139
AV_FRAME_DATA_ICC_PROFILE
@ AV_FRAME_DATA_ICC_PROFILE
The data contains an ICC profile as an opaque octet buffer following the format described by ISO 1507...
Definition: frame.h:144
iccdetect_outputs
static const AVFilterPad iccdetect_outputs[]
Definition: vf_iccdetect.c:126
IccDetectContext::force
int force
Definition: vf_iccdetect.c:38
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
AVFrameSideData::data
uint8_t * data
Definition: frame.h:233
fflcms2.h
csp.h
internal.h
FFIccContext
Definition: fflcms2.h:34
ff_vf_iccdetect
const AVFilter ff_vf_iccdetect
Definition: vf_iccdetect.c:133
av_buffer_replace
int av_buffer_replace(AVBufferRef **pdst, const AVBufferRef *src)
Ensure dst refers to the same data as src.
Definition: buffer.c:233
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:55
profile
int profile
Definition: mxfenc.c:2005
AVFilter
Filter definition.
Definition: avfilter.h:171
IccDetectContext
Definition: vf_iccdetect.c:35
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
ff_icc_context_init
int ff_icc_context_init(FFIccContext *s, void *avctx)
Initializes an FFIccContext.
Definition: fflcms2.c:31
avfilter.h
AVFILTER_FLAG_METADATA_ONLY
#define AVFILTER_FLAG_METADATA_ONLY
The filter is a "metadata" filter - it does not modify the frame data in any way.
Definition: avfilter.h:143
AVFilterContext
An instance of a filter.
Definition: avfilter.h:408
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:82
AVFrameSideData
Structure to hold side data for an AVFrame.
Definition: frame.h:231
iccdetect_init
static av_cold int iccdetect_init(AVFilterContext *avctx)
Definition: vf_iccdetect.c:62
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:244
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:191
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
uninit
static av_cold int uninit(AVCodecContext *avctx)
Definition: crystalhd.c:285
iccdetect_options
static const AVOption iccdetect_options[]
Definition: vf_iccdetect.c:48