FFmpeg  4.4.8
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 "filters.h"
27 #include "formats.h"
28 #include "internal.h"
29 #include "video.h"
30 
31 typedef struct ColorCorrectContext {
32  const AVClass *class;
33 
34  float rl, bl;
35  float rh, bh;
36  float saturation;
37 
38  int depth;
39 
41  int jobnr, int nb_jobs);
43 
44 #define PROCESS() \
45  float y = yptr[x] * imax; \
46  float u = uptr[x] * imax - .5f; \
47  float v = vptr[x] * imax - .5f; \
48  float ny, nu, nv; \
49  \
50  ny = y; \
51  nu = saturation * (u + y * bd + bl); \
52  nv = saturation * (v + y * rd + rl);
53 
54 static int colorcorrect_slice8(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
55 {
56  ColorCorrectContext *s = ctx->priv;
57  AVFrame *frame = arg;
58  const int depth = s->depth;
59  const float max = (1 << depth) - 1;
60  const float imax = 1.f / max;
61  const int width = frame->width;
62  const int height = frame->height;
63  const int slice_start = ff_slice_pos(height, jobnr, nb_jobs);
64  const int slice_end = ff_slice_pos(height, jobnr + 1, nb_jobs);
65  const int ylinesize = frame->linesize[0];
66  const int ulinesize = frame->linesize[1];
67  const int vlinesize = frame->linesize[2];
68  uint8_t *yptr = frame->data[0] + slice_start * ylinesize;
69  uint8_t *uptr = frame->data[1] + slice_start * ulinesize;
70  uint8_t *vptr = frame->data[2] + slice_start * vlinesize;
71  const float saturation = s->saturation;
72  const float bl = s->bl;
73  const float rl = s->rl;
74  const float bd = s->bh - bl;
75  const float rd = s->rh - rl;
76 
77  for (int y = slice_start; y < slice_end; y++) {
78  for (int x = 0; x < width; x++) {
79  PROCESS()
80 
81  yptr[x] = av_clip_uint8( ny * max);
82  uptr[x] = av_clip_uint8((nu + 0.5f) * max);
83  vptr[x] = av_clip_uint8((nv + 0.5f) * max);
84  }
85 
86  yptr += ylinesize;
87  uptr += ulinesize;
88  vptr += vlinesize;
89  }
90 
91  return 0;
92 }
93 
94 static int colorcorrect_slice16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
95 {
96  ColorCorrectContext *s = ctx->priv;
97  AVFrame *frame = arg;
98  const int depth = s->depth;
99  const float max = (1 << depth) - 1;
100  const float imax = 1.f / max;
101  const int width = frame->width;
102  const int height = frame->height;
103  const int slice_start = ff_slice_pos(height, jobnr, nb_jobs);
104  const int slice_end = ff_slice_pos(height, jobnr + 1, nb_jobs);
105  const int ylinesize = frame->linesize[0] / 2;
106  const int ulinesize = frame->linesize[1] / 2;
107  const int vlinesize = frame->linesize[2] / 2;
108  uint16_t *yptr = (uint16_t *)frame->data[0] + slice_start * ylinesize;
109  uint16_t *uptr = (uint16_t *)frame->data[1] + slice_start * ulinesize;
110  uint16_t *vptr = (uint16_t *)frame->data[2] + slice_start * vlinesize;
111  const float saturation = s->saturation;
112  const float bl = s->bl;
113  const float rl = s->rl;
114  const float bd = s->bh - bl;
115  const float rd = s->rh - rl;
116 
117  for (int y = slice_start; y < slice_end; y++) {
118  for (int x = 0; x < width; x++) {
119  PROCESS()
120 
121  yptr[x] = av_clip_uintp2_c( ny * max, depth);
122  uptr[x] = av_clip_uintp2_c((nu + 0.5f) * max, depth);
123  vptr[x] = av_clip_uintp2_c((nv + 0.5f) * max, depth);
124  }
125 
126  yptr += ylinesize;
127  uptr += ulinesize;
128  vptr += vlinesize;
129  }
130 
131  return 0;
132 }
133 
134 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
135 {
136  AVFilterContext *ctx = inlink->dst;
137  ColorCorrectContext *s = ctx->priv;
138 
139  ctx->internal->execute(ctx, s->do_slice, frame, NULL,
141 
142  return ff_filter_frame(ctx->outputs[0], frame);
143 }
144 
146 {
147  static const enum AVPixelFormat pixel_fmts[] = {
152  };
153 
155 
156  formats = ff_make_format_list(pixel_fmts);
157  if (!formats)
158  return AVERROR(ENOMEM);
159 
161 }
162 
163 static av_cold int config_input(AVFilterLink *inlink)
164 {
165  AVFilterContext *ctx = inlink->dst;
166  ColorCorrectContext *s = ctx->priv;
168 
169  s->depth = desc->comp[0].depth;
170  s->do_slice = s->depth <= 8 ? colorcorrect_slice8 : colorcorrect_slice16;
171 
172  return 0;
173 }
174 
176  {
177  .name = "default",
178  .type = AVMEDIA_TYPE_VIDEO,
179  .needs_writable = 1,
180  .filter_frame = filter_frame,
181  .config_props = config_input,
182  },
183  { NULL }
184 };
185 
187  {
188  .name = "default",
189  .type = AVMEDIA_TYPE_VIDEO,
190  },
191  { NULL }
192 };
193 
194 #define OFFSET(x) offsetof(ColorCorrectContext, x)
195 #define VF AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
196 
197 static const AVOption colorcorrect_options[] = {
198  { "rl", "set the red shadow spot", OFFSET(rl), AV_OPT_TYPE_FLOAT, {.dbl=0}, -1, 1, VF },
199  { "bl", "set the blue shadow spot", OFFSET(bl), AV_OPT_TYPE_FLOAT, {.dbl=0}, -1, 1, VF },
200  { "rh", "set the red highlight spot", OFFSET(rh), AV_OPT_TYPE_FLOAT, {.dbl=0}, -1, 1, VF },
201  { "bh", "set the blue highlight spot", OFFSET(bh), AV_OPT_TYPE_FLOAT, {.dbl=0}, -1, 1, VF },
202  { "saturation", "set the amount of saturation", OFFSET(saturation), AV_OPT_TYPE_FLOAT, {.dbl=1}, -3, 3, VF },
203  { NULL }
204 };
205 
206 AVFILTER_DEFINE_CLASS(colorcorrect);
207 
209  .name = "colorcorrect",
210  .description = NULL_IF_CONFIG_SMALL("Adjust color white balance selectively for blacks and whites."),
211  .priv_size = sizeof(ColorCorrectContext),
212  .priv_class = &colorcorrect_class,
218 };
static const AVFilterPad inputs[]
Definition: af_acontrast.c:193
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
Definition: af_acrusher.c:336
#define av_cold
Definition: attributes.h:88
uint8_t
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1096
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
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:802
Main libavfilter public API header.
#define flags(name, subs,...)
Definition: cbs_av1.c:572
#define s(width, name)
Definition: cbs_vp9.c:257
#define FFMIN(a, b)
Definition: common.h:105
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
#define av_clip_uint8
Definition: common.h:128
#define NULL
Definition: coverity.c:32
#define max(a, b)
Definition: cuda_runtime.h:33
static AVFrame * frame
int
static int ff_slice_pos(int total, int jobnr, int nb_jobs)
Compute the boundary index for a slice when work of size total is split into nb_jobs slices.
Definition: filters.h:271
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
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:286
@ AV_OPT_TYPE_FLOAT
Definition: opt.h:228
#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
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition: avfilter.h:117
#define AVERROR(e)
Definition: error.h:43
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
for(j=16;j >0;--j)
misc image utilities
const char * arg
Definition: jacosubdec.c:66
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
const char * desc
Definition: libsvtav1.c:79
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
Handle slice ends.
Definition: mpeg12dec.c:2033
AVOptions.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2573
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:406
#define AV_PIX_FMT_YUV444P9
Definition: pixfmt.h:398
#define AV_PIX_FMT_YUVA444P10
Definition: pixfmt.h:438
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
@ 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_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
#define AV_PIX_FMT_YUVA444P16
Definition: pixfmt.h:443
#define AV_PIX_FMT_YUV444P14
Definition: pixfmt.h:409
#define AV_PIX_FMT_YUVA444P9
Definition: pixfmt.h:435
#define AV_PIX_FMT_YUVA444P12
Definition: pixfmt.h:440
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:412
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:402
formats
Definition: signature.h:48
Describe the class of an AVClass context structure.
Definition: log.h:67
An instance of a filter.
Definition: avfilter.h:341
A list of supported formats for one end of a filter link.
Definition: formats.h:65
A filter pad used for either input or output.
Definition: internal.h:54
const char * name
Pad name.
Definition: internal.h:60
Filter definition.
Definition: avfilter.h:145
const char * name
Filter name.
Definition: avfilter.h:149
AVFormatInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1699
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
int width
Definition: frame.h:376
int height
Definition: frame.h:376
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:349
AVOption.
Definition: opt.h:248
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
int(* do_slice)(AVFilterContext *s, void *arg, int jobnr, int nb_jobs)
AVFormatContext * ctx
Definition: movenc.c:48
#define height
#define width
static int colorcorrect_slice16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
static const AVOption colorcorrect_options[]
#define PROCESS()
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
static av_cold int query_formats(AVFilterContext *ctx)
AVFilter ff_vf_colorcorrect
static const AVFilterPad colorcorrect_inputs[]
#define VF
static av_cold int config_input(AVFilterLink *inlink)
#define OFFSET(x)
static const AVFilterPad colorcorrect_outputs[]
static int colorcorrect_slice8(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
AVFILTER_DEFINE_CLASS(colorcorrect)