History

avfilter: add colorcorrect filter

Paul B Mahol committed 5 years ago

Commit f6621a81 parent b7251ae6 files · +254 −1Browse files

Unified · Split

6 files
  1. Changelog +1 −0
  2. doc/filters.texi +33 −0
  3. libavfilter/Makefile +1 −0
  4. libavfilter/allfilters.c +1 −0
  5. libavfilter/version.h +1 −1
  6. libavfilter/vf_colorcorrect.c +217 −0
Changelog+1−0
@@ -63,6 +63,7 @@ version <next>: · 62 unmodified lines
63- colortemperature filter63- colortemperature filter
64- colorcontrast filter64- colorcontrast filter
65- PFM encoder65- PFM encoder
66- colorcorrect filter
66 67
67 68
68version 4.3:69version 4.3:
doc/filters.texi+33−0
@@ -8056,6 +8056,39 @@ Set the amount of preserving lightness. Default value is 0.0. Allowed range is f · 8055 unmodified lines
8056 8056
8057This filter supports the all above options as @ref{commands}.8057This filter supports the all above options as @ref{commands}.
8058 8058
8059@section colorcorrect
8060
8061Adjust color white balance selectively for blacks and whites.
8062This filter operates in YUV colorspace.
8063
8064The filter accepts the following options:
8065
8066@table @option
8067@item rl
8068Set the red shadow spot. Allowed range is from -1.0 to 1.0.
8069Default value is 0.
8070
8071@item bl
8072Set the blue shadow spot. Allowed range is from -1.0 to 1.0.
8073Default value is 0.
8074
8075@item rh
8076Set the red highlight spot. Allowed range is from -1.0 to 1.0.
8077Default value is 0.
8078
8079@item bh
8080Set the red highlight spot. Allowed range is from -1.0 to 1.0.
8081Default value is 0.
8082
8083@item saturation
8084Set the amount of saturation. Allowed range is from -3.0 to 3.0.
8085Default value is 1.
8086@end table
8087
8088@subsection Commands
8089
8090This filter supports the all above options as @ref{commands}.
8091
8059@section colorchannelmixer8092@section colorchannelmixer
8060 8093
8061Adjust video input frames by re-mixing color channels.8094Adjust video input frames by re-mixing color channels.
libavfilter/Makefile+1−0
@@ -197,6 +197,7 @@ OBJS-$(CONFIG_CODECVIEW_FILTER) += vf_codecview.o qp_table.o · 196 unmodified lines
197OBJS-$(CONFIG_COLORBALANCE_FILTER) += vf_colorbalance.o197OBJS-$(CONFIG_COLORBALANCE_FILTER) += vf_colorbalance.o
198OBJS-$(CONFIG_COLORCHANNELMIXER_FILTER) += vf_colorchannelmixer.o198OBJS-$(CONFIG_COLORCHANNELMIXER_FILTER) += vf_colorchannelmixer.o
199OBJS-$(CONFIG_COLORCONTRAST_FILTER) += vf_colorcontrast.o199OBJS-$(CONFIG_COLORCONTRAST_FILTER) += vf_colorcontrast.o
200OBJS-$(CONFIG_COLORCORRECT_FILTER) += vf_colorcorrect.o
200OBJS-$(CONFIG_COLORKEY_FILTER) += vf_colorkey.o201OBJS-$(CONFIG_COLORKEY_FILTER) += vf_colorkey.o
201OBJS-$(CONFIG_COLORKEY_OPENCL_FILTER) += vf_colorkey_opencl.o opencl.o \202OBJS-$(CONFIG_COLORKEY_OPENCL_FILTER) += vf_colorkey_opencl.o opencl.o \
202 opencl/colorkey.o203 opencl/colorkey.o
libavfilter/allfilters.c+1−0
@@ -187,6 +187,7 @@ extern AVFilter ff_vf_codecview; · 186 unmodified lines
187extern AVFilter ff_vf_colorbalance;187extern AVFilter ff_vf_colorbalance;
188extern AVFilter ff_vf_colorchannelmixer;188extern AVFilter ff_vf_colorchannelmixer;
189extern AVFilter ff_vf_colorcontrast;189extern AVFilter ff_vf_colorcontrast;
190extern AVFilter ff_vf_colorcorrect;
190extern AVFilter ff_vf_colorkey;191extern AVFilter ff_vf_colorkey;
191extern AVFilter ff_vf_colorkey_opencl;192extern AVFilter ff_vf_colorkey_opencl;
192extern AVFilter ff_vf_colorhold;193extern AVFilter ff_vf_colorhold;
libavfilter/version.h+1−1
@@ -30,7 +30,7 @@ · 29 unmodified lines
30#include "libavutil/version.h"30#include "libavutil/version.h"
31 31
32#define LIBAVFILTER_VERSION_MAJOR 732#define LIBAVFILTER_VERSION_MAJOR 7
33#define LIBAVFILTER_VERSION_MINOR 10133#define LIBAVFILTER_VERSION_MINOR 102
34#define LIBAVFILTER_VERSION_MICRO 10034#define LIBAVFILTER_VERSION_MICRO 100
35 35
36 36
libavfilter/vf_colorcorrect.c+217−0
@@ -0,0 +1,217 @@
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
30typedef struct ColorCorrectContext {
31 const AVClass *class;
32
33 float rl, bl;
34 float rh, bh;
35 float saturation;
36
37 int depth;
38
39 int (*do_slice)(AVFilterContext *s, void *arg,
40 int jobnr, int nb_jobs);
41} ColorCorrectContext;
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
53static 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
93static 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
133static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
134{
135 AVFilterContext *ctx = inlink->dst;
136 ColorCorrectContext *s = ctx->priv;
137
138 ctx->internal->execute(ctx, s->do_slice, frame, NULL,
139 FFMIN(frame->height, ff_filter_get_nb_threads(ctx)));
140
141 return ff_filter_frame(ctx->outputs[0], frame);
142}
143
144static av_cold int query_formats(AVFilterContext *ctx)
145{
146 static const enum AVPixelFormat pixel_fmts[] = {
147 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
148 AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV444P16,
149 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA444P16,
150 AV_PIX_FMT_NONE
151 };
152
153 AVFilterFormats *formats = NULL;
154
155 formats = ff_make_format_list(pixel_fmts);
156 if (!formats)
157 return AVERROR(ENOMEM);
158
159 return ff_set_common_formats(ctx, formats);
160}
161
162static av_cold int config_input(AVFilterLink *inlink)
163{
164 AVFilterContext *ctx = inlink->dst;
165 ColorCorrectContext *s = ctx->priv;
166 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
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
174static const AVFilterPad colorcorrect_inputs[] = {
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
185static const AVFilterPad colorcorrect_outputs[] = {
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
196static 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
205AVFILTER_DEFINE_CLASS(colorcorrect);
206
207AVFilter ff_vf_colorcorrect = {
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,
212 .query_formats = query_formats,
213 .inputs = colorcorrect_inputs,
214 .outputs = colorcorrect_outputs,
215 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
216 .process_command = ff_filter_process_command,
217};

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t