2 * Copyright (c) 2021 Paul B Mahol
4 * This file is part of FFmpeg.
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.
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.
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
23 #include "libavutil/opt.h"
24 #include "libavutil/imgutils.h"
30 typedef struct MonochromeContext {
40 int (*do_slice)(AVFilterContext *s, void *arg,
41 int jobnr, int nb_jobs);
42 int (*clear_uv)(AVFilterContext *s, void *arg,
43 int jobnr, int nb_jobs);
46 static float envelope(const float x)
48 const float beta = 0.6f;
51 const float tmp = fabsf(x / beta - 1.f);
53 return 1.f - tmp * tmp;
55 const float tmp = (1.f - x) / (1.f - beta);
57 return tmp * tmp * (3.f - 2.f * tmp);
61 static float filter(float b, float r, float u, float v, float size)
63 return expf(-av_clipf(((b - u) * (b - u) +
69 const int cx = x >> subw; \
70 float y = yptr[x] * imax; \
71 float u = uptr[cx] * imax - .5f; \
72 float v = vptr[cx] * imax - .5f; \
75 ny = filter(b, r, u, v, size); \
77 t = tt + (1.f - tt) * ihigh; \
78 ny = (1.f - t) * y + t * ny * y;
80 static int monochrome_slice8(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
82 MonochromeContext *s = ctx->priv;
84 const int depth = s->depth;
85 const int subw = s->subw;
86 const int subh = s->subh;
87 const float max = (1 << depth) - 1;
88 const float imax = 1.f / max;
89 const int width = frame->width;
90 const int height = frame->height;
91 const int slice_start = (height * jobnr) / nb_jobs;
92 const int slice_end = (height * (jobnr + 1)) / nb_jobs;
93 const int ylinesize = frame->linesize[0];
94 const int ulinesize = frame->linesize[1];
95 const int vlinesize = frame->linesize[2];
96 uint8_t *yptr = frame->data[0] + slice_start * ylinesize;
97 const float ihigh = 1.f - s->high;
98 const float size = 1.f / s->size;
99 const float b = s->b * .5f;
100 const float r = s->r * .5f;
102 for (int y = slice_start; y < slice_end; y++) {
103 const int cy = y >> subh;
104 uint8_t *uptr = frame->data[1] + cy * ulinesize;
105 uint8_t *vptr = frame->data[2] + cy * vlinesize;
107 for (int x = 0; x < width; x++) {
110 yptr[x] = av_clip_uint8(ny * max);
119 static int monochrome_slice16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
121 MonochromeContext *s = ctx->priv;
122 AVFrame *frame = arg;
123 const int depth = s->depth;
124 const int subw = s->subw;
125 const int subh = s->subh;
126 const float max = (1 << depth) - 1;
127 const float imax = 1.f / max;
128 const int width = frame->width;
129 const int height = frame->height;
130 const int slice_start = (height * jobnr) / nb_jobs;
131 const int slice_end = (height * (jobnr + 1)) / nb_jobs;
132 const int ylinesize = frame->linesize[0] / 2;
133 const int ulinesize = frame->linesize[1] / 2;
134 const int vlinesize = frame->linesize[2] / 2;
135 uint16_t *yptr = (uint16_t *)frame->data[0] + slice_start * ylinesize;
136 const float ihigh = 1.f - s->high;
137 const float size = 1.f / s->size;
138 const float b = s->b * .5f;
139 const float r = s->r * .5f;
141 for (int y = slice_start; y < slice_end; y++) {
142 const int cy = y >> subh;
143 uint16_t *uptr = (uint16_t *)frame->data[1] + cy * ulinesize;
144 uint16_t *vptr = (uint16_t *)frame->data[2] + cy * vlinesize;
146 for (int x = 0; x < width; x++) {
149 yptr[x] = av_clip_uintp2_c(ny * max, depth);
158 static int clear_slice8(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
160 MonochromeContext *s = ctx->priv;
161 AVFrame *frame = arg;
162 const int depth = s->depth;
163 const int half = 1 << (depth - 1);
164 const int subw = s->subw;
165 const int subh = s->subh;
166 const int width = AV_CEIL_RSHIFT(frame->width, subw);
167 const int height = AV_CEIL_RSHIFT(frame->height, subh);
168 const int slice_start = (height * jobnr) / nb_jobs;
169 const int slice_end = (height * (jobnr + 1)) / nb_jobs;
170 const int ulinesize = frame->linesize[1];
171 const int vlinesize = frame->linesize[2];
173 for (int y = slice_start; y < slice_end; y++) {
174 uint8_t *uptr = frame->data[1] + y * ulinesize;
175 uint8_t *vptr = frame->data[2] + y * vlinesize;
177 memset(uptr, half, width);
178 memset(vptr, half, width);
184 static int clear_slice16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
186 MonochromeContext *s = ctx->priv;
187 AVFrame *frame = arg;
188 const int depth = s->depth;
189 const int half = 1 << (depth - 1);
190 const int subw = s->subw;
191 const int subh = s->subh;
192 const int width = AV_CEIL_RSHIFT(frame->width, subw);
193 const int height = AV_CEIL_RSHIFT(frame->height, subh);
194 const int slice_start = (height * jobnr) / nb_jobs;
195 const int slice_end = (height * (jobnr + 1)) / nb_jobs;
196 const int ulinesize = frame->linesize[1] / 2;
197 const int vlinesize = frame->linesize[2] / 2;
199 for (int y = slice_start; y < slice_end; y++) {
200 uint16_t *uptr = (uint16_t *)frame->data[1] + y * ulinesize;
201 uint16_t *vptr = (uint16_t *)frame->data[2] + y * vlinesize;
203 for (int x = 0; x < width; x++) {
212 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
214 AVFilterContext *ctx = inlink->dst;
215 MonochromeContext *s = ctx->priv;
217 ctx->internal->execute(ctx, s->do_slice, frame, NULL,
218 FFMIN(frame->height, ff_filter_get_nb_threads(ctx)));
219 ctx->internal->execute(ctx, s->clear_uv, frame, NULL,
220 FFMIN(frame->height >> s->subh, ff_filter_get_nb_threads(ctx)));
222 return ff_filter_frame(ctx->outputs[0], frame);
225 static av_cold int query_formats(AVFilterContext *ctx)
227 static const enum AVPixelFormat pixel_fmts[] = {
228 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
229 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
230 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
231 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
232 AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
234 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
235 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
236 AV_PIX_FMT_YUV440P10,
237 AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
238 AV_PIX_FMT_YUV440P12,
239 AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
240 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
241 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
242 AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA444P16,
243 AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA422P16,
244 AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
248 AVFilterFormats *formats = NULL;
250 formats = ff_make_format_list(pixel_fmts);
252 return AVERROR(ENOMEM);
254 return ff_set_common_formats(ctx, formats);
257 static av_cold int config_input(AVFilterLink *inlink)
259 AVFilterContext *ctx = inlink->dst;
260 MonochromeContext *s = ctx->priv;
261 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
263 s->depth = desc->comp[0].depth;
264 s->do_slice = s->depth <= 8 ? monochrome_slice8 : monochrome_slice16;
265 s->clear_uv = s->depth <= 8 ? clear_slice8 : clear_slice16;
266 s->subw = desc->log2_chroma_w;
267 s->subh = desc->log2_chroma_h;
272 static const AVFilterPad monochrome_inputs[] = {
275 .type = AVMEDIA_TYPE_VIDEO,
277 .filter_frame = filter_frame,
278 .config_props = config_input,
283 static const AVFilterPad monochrome_outputs[] = {
286 .type = AVMEDIA_TYPE_VIDEO,
291 #define OFFSET(x) offsetof(MonochromeContext, x)
292 #define VF AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
294 static const AVOption monochrome_options[] = {
295 { "cb", "set the chroma blue spot", OFFSET(b), AV_OPT_TYPE_FLOAT, {.dbl=0},-1, 1, VF },
296 { "cr", "set the chroma red spot", OFFSET(r), AV_OPT_TYPE_FLOAT, {.dbl=0},-1, 1, VF },
297 { "size", "set the color filter size", OFFSET(size), AV_OPT_TYPE_FLOAT, {.dbl=1},.1,10, VF },
298 { "high", "set the highlights strength", OFFSET(high), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, 1, VF },
302 AVFILTER_DEFINE_CLASS(monochrome);
304 AVFilter ff_vf_monochrome = {
305 .name = "monochrome",
306 .description = NULL_IF_CONFIG_SMALL("Convert video to gray using custom color filter."),
307 .priv_size = sizeof(MonochromeContext),
308 .priv_class = &monochrome_class,
309 .query_formats = query_formats,
310 .inputs = monochrome_inputs,
311 .outputs = monochrome_outputs,
312 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
313 .process_command = ff_filter_process_command,