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 #include "video.h"
35 
36 typedef struct IccDetectContext {
37  const AVClass *class;
39  int force;
40  /* (cached) detected ICC profile values */
45 
46 #define OFFSET(x) offsetof(IccDetectContext, x)
47 #define VF AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
48 
49 static const AVOption iccdetect_options[] = {
50  { "force", "overwrite existing tags", OFFSET(force), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, VF },
51  { NULL }
52 };
53 
54 AVFILTER_DEFINE_CLASS(iccdetect);
55 
57 {
58  IccDetectContext *s = avctx->priv;
59  av_buffer_unref(&s->profile);
60  ff_icc_context_uninit(&s->icc);
61 }
62 
64 {
65  IccDetectContext *s = avctx->priv;
66  return ff_icc_context_init(&s->icc, avctx);
67 }
68 
70 {
71  AVFilterContext *avctx = inlink->dst;
72  IccDetectContext *s = avctx->priv;
73  const AVFrameSideData *sd;
74  AVColorPrimariesDesc coeffs;
75  cmsHPROFILE profile;
76  int ret;
77 
79  if (!sd)
80  return ff_filter_frame(inlink->dst->outputs[0], frame);
81 
82  if (s->profile && s->profile->data == sd->buf->data) {
83  /* No change from previous ICC profile */
84  goto done;
85  }
86 
87  if ((ret = av_buffer_replace(&s->profile, sd->buf)) < 0)
88  return ret;
89  s->profile_prim = AVCOL_PRI_UNSPECIFIED;
90  s->profile_trc = AVCOL_TRC_UNSPECIFIED;
91 
92  profile = cmsOpenProfileFromMemTHR(s->icc.ctx, sd->data, sd->size);
93  if (!profile)
94  return AVERROR_INVALIDDATA;
95 
97  if (!ret)
98  ret = ff_icc_profile_read_primaries(&s->icc, profile, &coeffs);
99  if (!ret)
100  ret = ff_icc_profile_detect_transfer(&s->icc, profile, &s->profile_trc);
101  cmsCloseProfile(profile);
102  if (ret < 0)
103  return ret;
104 
105  s->profile_prim = av_csp_primaries_id_from_desc(&coeffs);
106 
107 done:
108  if (s->profile_prim != AVCOL_PRI_UNSPECIFIED) {
109  if (s->force || frame->color_primaries == AVCOL_PRI_UNSPECIFIED)
110  frame->color_primaries = s->profile_prim;
111  }
112 
113  if (s->profile_trc != AVCOL_TRC_UNSPECIFIED) {
114  if (s->force || frame->color_trc == AVCOL_TRC_UNSPECIFIED)
115  frame->color_trc = s->profile_trc;
116  }
117 
118  return ff_filter_frame(inlink->dst->outputs[0], frame);
119 }
120 
121 static const AVFilterPad iccdetect_inputs[] = {
122  {
123  .name = "default",
124  .type = AVMEDIA_TYPE_VIDEO,
125  .filter_frame = iccdetect_filter_frame,
126  },
127 };
128 
130  .name = "iccdetect",
131  .description = NULL_IF_CONFIG_SMALL("Detect and parse ICC profiles."),
132  .priv_size = sizeof(IccDetectContext),
133  .priv_class = &iccdetect_class,
135  .init = &iccdetect_init,
139 };
iccdetect_filter_frame
static int iccdetect_filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: vf_iccdetect.c:69
IccDetectContext::profile_prim
enum AVColorPrimaries profile_prim
Definition: vf_iccdetect.c:42
opt.h
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:255
AVColorTransferCharacteristic
AVColorTransferCharacteristic
Color Transfer Characteristic.
Definition: pixfmt.h:580
IccDetectContext::profile
AVBufferRef * profile
Definition: vf_iccdetect.c:41
av_frame_get_side_data
AVFrameSideData * av_frame_get_side_data(const AVFrame *frame, enum AVFrameSideDataType type)
Definition: frame.c:947
AVColorPrimariesDesc
Struct that contains both white point location and primaries location, providing the complete descrip...
Definition: csp.h:78
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1015
AVBufferRef::data
uint8_t * data
The data buffer.
Definition: buffer.h:90
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:374
pixdesc.h
AVFrameSideData::buf
AVBufferRef * buf
Definition: frame.h:255
AVOption
AVOption.
Definition: opt.h:346
AVCOL_TRC_UNSPECIFIED
@ AVCOL_TRC_UNSPECIFIED
Definition: pixfmt.h:583
IccDetectContext::profile_trc
enum AVColorTransferCharacteristic profile_trc
Definition: vf_iccdetect.c:43
OFFSET
#define OFFSET(x)
Definition: vf_iccdetect.c:46
AVColorPrimaries
AVColorPrimaries
Chromaticity coordinates of the source primaries.
Definition: pixfmt.h:555
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:170
fflcms2.h
video.h
iccdetect_inputs
static const AVFilterPad iccdetect_inputs[]
Definition: vf_iccdetect.c:121
AVFilterContext::priv
void * priv
private data for use by the filter
Definition: avfilter.h:422
ff_icc_context_init
int ff_icc_context_init(FFIccContext *s, void *avctx)
Initializes an FFIccContext.
Definition: fflcms2.c:30
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:33
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(iccdetect)
IccDetectContext::icc
FFIccContext icc
Definition: vf_iccdetect.c:38
AVFrameSideData::size
size_t size
Definition: frame.h:253
av_cold
#define av_cold
Definition: attributes.h:90
ff_video_default_filterpad
const AVFilterPad ff_video_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_VIDEO.
Definition: video.c:37
s
#define s(width, name)
Definition: cbs_vp9.c:198
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:110
VF
#define VF
Definition: vf_iccdetect.c:47
ff_icc_context_uninit
void ff_icc_context_uninit(FFIccContext *s)
Definition: fflcms2.c:42
AVCOL_PRI_UNSPECIFIED
@ AVCOL_PRI_UNSPECIFIED
Definition: pixfmt.h:558
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:182
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:56
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
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:302
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:366
IccDetectContext::force
int force
Definition: vf_iccdetect.c:39
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:94
AVFrameSideData::data
uint8_t * data
Definition: frame.h:252
csp.h
internal.h
uninit
static void uninit(AVBSFContext *ctx)
Definition: pcm_rechunk.c:68
FFIccContext
Definition: fflcms2.h:34
ff_vf_iccdetect
const AVFilter ff_vf_iccdetect
Definition: vf_iccdetect.c:129
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:39
profile
int profile
Definition: mxfenc.c:2227
AVFilter
Filter definition.
Definition: avfilter.h:166
IccDetectContext
Definition: vf_iccdetect.c:36
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
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:133
AVFilterContext
An instance of a filter.
Definition: avfilter.h:407
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:82
ff_icc_profile_sanitize
int ff_icc_profile_sanitize(FFIccContext *s, cmsHPROFILE profile)
Sanitize an ICC profile to try and fix badly broken values.
Definition: fflcms2.c:213
AVFrameSideData
Structure to hold side data for an AVFrame.
Definition: frame.h:250
iccdetect_init
static av_cold int iccdetect_init(AVFilterContext *avctx)
Definition: vf_iccdetect.c:63
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:251
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:183
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:474
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
iccdetect_options
static const AVOption iccdetect_options[]
Definition: vf_iccdetect.c:49