FFmpeg
vf_colorcorrect.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021 Paul B Mahol
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include <float.h>
22 
23 #include "libavutil/opt.h"
24 #include "libavutil/imgutils.h"
25 #include "avfilter.h"
26 #include "formats.h"
27 #include "internal.h"
28 #include "video.h"
29 
30 typedef struct ColorCorrectContext {
31  const AVClass *class;
32 
33  float rl, bl;
34  float rh, bh;
35  float saturation;
36 
37  int depth;
38 
40  int jobnr, int nb_jobs);
42 
43 #define PROCESS() \
44  float y = yptr[x] * imax; \
45  float u = uptr[x] * imax - .5f; \
46  float v = vptr[x] * imax - .5f; \
47  float ny, nu, nv; \
48  \
49  ny = y; \
50  nu = saturation * (u + y * bd + bl); \
51  nv = saturation * (v + y * rd + rl);
52 
53 static int colorcorrect_slice8(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
54 {
55  ColorCorrectContext *s = ctx->priv;
56  AVFrame *frame = arg;
57  const int depth = s->depth;
58  const float max = (1 << depth) - 1;
59  const float imax = 1.f / max;
60  const int width = frame->width;
61  const int height = frame->height;
62  const int slice_start = (height * jobnr) / nb_jobs;
63  const int slice_end = (height * (jobnr + 1)) / nb_jobs;
64  const int ylinesize = frame->linesize[0];
65  const int ulinesize = frame->linesize[1];
66  const int vlinesize = frame->linesize[2];
67  uint8_t *yptr = frame->data[0] + slice_start * ylinesize;
68  uint8_t *uptr = frame->data[1] + slice_start * ulinesize;
69  uint8_t *vptr = frame->data[2] + slice_start * vlinesize;
70  const float saturation = s->saturation;
71  const float bl = s->bl;
72  const float rl = s->rl;
73  const float bd = s->bh - bl;
74  const float rd = s->rh - rl;
75 
76  for (int y = slice_start; y < slice_end; y++) {
77  for (int x = 0; x < width; x++) {
78  PROCESS()
79 
80  yptr[x] = av_clip_uint8( ny * max);
81  uptr[x] = av_clip_uint8((nu + 0.5f) * max);
82  vptr[x] = av_clip_uint8((nv + 0.5f) * max);
83  }
84 
85  yptr += ylinesize;
86  uptr += ulinesize;
87  vptr += vlinesize;
88  }
89 
90  return 0;
91 }
92 
93 static int colorcorrect_slice16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
94 {
95  ColorCorrectContext *s = ctx->priv;
96  AVFrame *frame = arg;
97  const int depth = s->depth;
98  const float max = (1 << depth) - 1;
99  const float imax = 1.f / max;
100  const int width = frame->width;
101  const int height = frame->height;
102  const int slice_start = (height * jobnr) / nb_jobs;
103  const int slice_end = (height * (jobnr + 1)) / nb_jobs;
104  const int ylinesize = frame->linesize[0] / 2;
105  const int ulinesize = frame->linesize[1] / 2;
106  const int vlinesize = frame->linesize[2] / 2;
107  uint16_t *yptr = (uint16_t *)frame->data[0] + slice_start * ylinesize;
108  uint16_t *uptr = (uint16_t *)frame->data[1] + slice_start * ulinesize;
109  uint16_t *vptr = (uint16_t *)frame->data[2] + slice_start * vlinesize;
110  const float saturation = s->saturation;
111  const float bl = s->bl;
112  const float rl = s->rl;
113  const float bd = s->bh - bl;
114  const float rd = s->rh - rl;
115 
116  for (int y = slice_start; y < slice_end; y++) {
117  for (int x = 0; x < width; x++) {
118  PROCESS()
119 
120  yptr[x] = av_clip_uintp2_c( ny * max, depth);
121  uptr[x] = av_clip_uintp2_c((nu + 0.5f) * max, depth);
122  vptr[x] = av_clip_uintp2_c((nv + 0.5f) * max, depth);
123  }
124 
125  yptr += ylinesize;
126  uptr += ulinesize;
127  vptr += vlinesize;
128  }
129 
130  return 0;
131 }
132 
134 {
135  AVFilterContext *ctx = inlink->dst;
136  ColorCorrectContext *s = ctx->priv;
137 
138  ctx->internal->execute(ctx, s->do_slice, frame, NULL,
140 
141  return ff_filter_frame(ctx->outputs[0], frame);
142 }
143 
145 {
146  static const enum AVPixelFormat pixel_fmts[] = {
151  };
152 
154 
155  formats = ff_make_format_list(pixel_fmts);
156  if (!formats)
157  return AVERROR(ENOMEM);
158 
160 }
161 
163 {
164  AVFilterContext *ctx = inlink->dst;
165  ColorCorrectContext *s = ctx->priv;
167 
168  s->depth = desc->comp[0].depth;
169  s->do_slice = s->depth <= 8 ? colorcorrect_slice8 : colorcorrect_slice16;
170 
171  return 0;
172 }
173 
175  {
176  .name = "default",
177  .type = AVMEDIA_TYPE_VIDEO,
178  .needs_writable = 1,
179  .filter_frame = filter_frame,
180  .config_props = config_input,
181  },
182  { NULL }
183 };
184 
186  {
187  .name = "default",
188  .type = AVMEDIA_TYPE_VIDEO,
189  },
190  { NULL }
191 };
192 
193 #define OFFSET(x) offsetof(ColorCorrectContext, x)
194 #define VF AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
195 
196 static const AVOption colorcorrect_options[] = {
197  { "rl", "set the red shadow spot", OFFSET(rl), AV_OPT_TYPE_FLOAT, {.dbl=0}, -1, 1, VF },
198  { "bl", "set the blue shadow spot", OFFSET(bl), AV_OPT_TYPE_FLOAT, {.dbl=0}, -1, 1, VF },
199  { "rh", "set the red highlight spot", OFFSET(rh), AV_OPT_TYPE_FLOAT, {.dbl=0}, -1, 1, VF },
200  { "bh", "set the blue highlight spot", OFFSET(bh), AV_OPT_TYPE_FLOAT, {.dbl=0}, -1, 1, VF },
201  { "saturation", "set the amount of saturation", OFFSET(saturation), AV_OPT_TYPE_FLOAT, {.dbl=1}, -3, 3, VF },
202  { NULL }
203 };
204 
205 AVFILTER_DEFINE_CLASS(colorcorrect);
206 
208  .name = "colorcorrect",
209  .description = NULL_IF_CONFIG_SMALL("Adjust color white balance selectively for blacks and whites."),
210  .priv_size = sizeof(ColorCorrectContext),
211  .priv_class = &colorcorrect_class,
217 };
formats
formats
Definition: signature.h:48
VF
#define VF
Definition: vf_colorcorrect.c:194
query_formats
static av_cold int query_formats(AVFilterContext *ctx)
Definition: vf_colorcorrect.c:144
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
ff_make_format_list
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:286
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1096
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2573
colorcorrect_slice16
static int colorcorrect_slice16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_colorcorrect.c:93
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
colorcorrect_slice8
static int colorcorrect_slice8(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_colorcorrect.c:53
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
av_clip_uintp2_c
static av_always_inline av_const unsigned av_clip_uintp2_c(int a, int p)
Clip a signed integer to an unsigned power of two range.
Definition: common.h:302
AVOption
AVOption.
Definition: opt.h:248
colorcorrect_inputs
static const AVFilterPad colorcorrect_inputs[]
Definition: vf_colorcorrect.c:174
float.h
max
#define max(a, b)
Definition: cuda_runtime.h:33
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:149
video.h
AVFormatContext::internal
AVFormatInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1699
colorcorrect_options
static const AVOption colorcorrect_options[]
Definition: vf_colorcorrect.c:196
AVFilterFormats
A list of supported formats for one end of a filter link.
Definition: formats.h:65
formats.h
ColorCorrectContext::do_slice
int(* do_slice)(AVFilterContext *s, void *arg, int jobnr, int nb_jobs)
Definition: vf_colorcorrect.c:39
config_input
static av_cold int config_input(AVFilterLink *inlink)
Definition: vf_colorcorrect.c:162
AV_PIX_FMT_YUVA444P16
#define AV_PIX_FMT_YUVA444P16
Definition: pixfmt.h:443
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:54
AV_PIX_FMT_YUV444P10
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:402
ColorCorrectContext
Definition: vf_colorcorrect.c:30
av_cold
#define av_cold
Definition: attributes.h:90
ff_set_common_formats
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:587
width
#define width
s
#define s(width, name)
Definition: cbs_vp9.c:257
AV_PIX_FMT_YUV444P16
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:412
slice_end
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
Handle slice ends.
Definition: mpeg12dec.c:2033
outputs
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
AV_PIX_FMT_YUVA444P12
#define AV_PIX_FMT_YUVA444P12
Definition: pixfmt.h:440
ctx
AVFormatContext * ctx
Definition: movenc.c:48
f
#define f(width, name)
Definition: cbs_vp9.c:255
AV_PIX_FMT_YUVJ444P
@ AV_PIX_FMT_YUVJ444P
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition: pixfmt.h:80
arg
const char * arg
Definition: jacosubdec.c:66
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
NULL
#define NULL
Definition: coverity.c:32
inputs
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 inputs
Definition: filter_design.txt:243
for
for(j=16;j >0;--j)
Definition: h264pred_template.c:469
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
process_command
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
Definition: af_acrusher.c:336
ColorCorrectContext::bl
float bl
Definition: vf_colorcorrect.c:33
ColorCorrectContext::rh
float rh
Definition: vf_colorcorrect.c:34
AV_PIX_FMT_YUV444P12
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:406
ff_filter_process_command
int ff_filter_process_command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Generic processing of user supplied commands that are set in the same way as the filter options.
Definition: avfilter.c:882
height
#define height
FFMIN
#define FFMIN(a, b)
Definition: common.h:105
AV_PIX_FMT_YUVA444P
@ AV_PIX_FMT_YUVA444P
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:177
AV_PIX_FMT_YUVA444P10
#define AV_PIX_FMT_YUVA444P10
Definition: pixfmt.h:438
ff_vf_colorcorrect
AVFilter ff_vf_colorcorrect
Definition: vf_colorcorrect.c:207
internal.h
AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
Definition: avfilter.h:126
AV_OPT_TYPE_FLOAT
@ AV_OPT_TYPE_FLOAT
Definition: opt.h:228
ColorCorrectContext::saturation
float saturation
Definition: vf_colorcorrect.c:35
ff_filter_get_nb_threads
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:802
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: vf_colorcorrect.c:133
uint8_t
uint8_t
Definition: audio_convert.c:194
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:60
AV_PIX_FMT_YUV444P9
#define AV_PIX_FMT_YUV444P9
Definition: pixfmt.h:398
AVFilter
Filter definition.
Definition: avfilter.h:145
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
AV_PIX_FMT_YUVA444P9
#define AV_PIX_FMT_YUVA444P9
Definition: pixfmt.h:435
ColorCorrectContext::depth
int depth
Definition: vf_colorcorrect.c:37
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
avfilter.h
av_clip_uint8
#define av_clip_uint8
Definition: common.h:128
AV_PIX_FMT_YUV444P
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
AVFilterContext
An instance of a filter.
Definition: avfilter.h:341
ColorCorrectContext::bh
float bh
Definition: vf_colorcorrect.c:34
AVFILTER_FLAG_SLICE_THREADS
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition: avfilter.h:117
desc
const char * desc
Definition: libsvtav1.c:79
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
colorcorrect_outputs
static const AVFilterPad colorcorrect_outputs[]
Definition: vf_colorcorrect.c:185
imgutils.h
PROCESS
#define PROCESS()
Definition: vf_colorcorrect.c:43
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(colorcorrect)
AV_PIX_FMT_YUV444P14
#define AV_PIX_FMT_YUV444P14
Definition: pixfmt.h:409
int
int
Definition: ffmpeg_filter.c:170
OFFSET
#define OFFSET(x)
Definition: vf_colorcorrect.c:193
ColorCorrectContext::rl
float rl
Definition: vf_colorcorrect.c:33