FFmpeg  4.4.8
vf_monochrome.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 MonochromeContext {
32  const AVClass *class;
33 
34  float b, r;
35  float size;
36  float high;
37 
38  int depth;
39  int subw, subh;
40 
42  int jobnr, int nb_jobs);
44  int jobnr, int nb_jobs);
46 
47 static float envelope(const float x)
48 {
49  const float beta = 0.6f;
50 
51  if (x < beta) {
52  const float tmp = fabsf(x / beta - 1.f);
53 
54  return 1.f - tmp * tmp;
55  } else {
56  const float tmp = (1.f - x) / (1.f - beta);
57 
58  return tmp * tmp * (3.f - 2.f * tmp);
59  }
60 }
61 
62 static float filter(float b, float r, float u, float v, float size)
63 {
64  return expf(-av_clipf(((b - u) * (b - u) +
65  (r - v) * (r - v)) *
66  size, 0.f, 1.f));
67 }
68 
69 #define PROCESS() \
70  const int cx = x >> subw; \
71  float y = yptr[x] * imax; \
72  float u = uptr[cx] * imax - .5f; \
73  float v = vptr[cx] * imax - .5f; \
74  float tt, t, ny; \
75  \
76  ny = filter(b, r, u, v, size); \
77  tt = envelope(y); \
78  t = tt + (1.f - tt) * ihigh; \
79  ny = (1.f - t) * y + t * ny * y;
80 
81 static int monochrome_slice8(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
82 {
83  MonochromeContext *s = ctx->priv;
84  AVFrame *frame = arg;
85  const int depth = s->depth;
86  const int subw = s->subw;
87  const int subh = s->subh;
88  const float max = (1 << depth) - 1;
89  const float imax = 1.f / max;
90  const int width = frame->width;
91  const int height = frame->height;
92  const int slice_start = ff_slice_pos(height, jobnr, nb_jobs);
93  const int slice_end = ff_slice_pos(height, jobnr + 1, nb_jobs);
94  const int ylinesize = frame->linesize[0];
95  const int ulinesize = frame->linesize[1];
96  const int vlinesize = frame->linesize[2];
97  uint8_t *yptr = frame->data[0] + slice_start * ylinesize;
98  const float ihigh = 1.f - s->high;
99  const float size = 1.f / s->size;
100  const float b = s->b * .5f;
101  const float r = s->r * .5f;
102 
103  for (int y = slice_start; y < slice_end; y++) {
104  const int cy = y >> subh;
105  uint8_t *uptr = frame->data[1] + cy * ulinesize;
106  uint8_t *vptr = frame->data[2] + cy * vlinesize;
107 
108  for (int x = 0; x < width; x++) {
109  PROCESS()
110 
111  yptr[x] = av_clip_uint8(ny * max);
112  }
113 
114  yptr += ylinesize;
115  }
116 
117  return 0;
118 }
119 
120 static int monochrome_slice16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
121 {
122  MonochromeContext *s = ctx->priv;
123  AVFrame *frame = arg;
124  const int depth = s->depth;
125  const int subw = s->subw;
126  const int subh = s->subh;
127  const float max = (1 << depth) - 1;
128  const float imax = 1.f / max;
129  const int width = frame->width;
130  const int height = frame->height;
131  const int slice_start = ff_slice_pos(height, jobnr, nb_jobs);
132  const int slice_end = ff_slice_pos(height, jobnr + 1, nb_jobs);
133  const int ylinesize = frame->linesize[0] / 2;
134  const int ulinesize = frame->linesize[1] / 2;
135  const int vlinesize = frame->linesize[2] / 2;
136  uint16_t *yptr = (uint16_t *)frame->data[0] + slice_start * ylinesize;
137  const float ihigh = 1.f - s->high;
138  const float size = 1.f / s->size;
139  const float b = s->b * .5f;
140  const float r = s->r * .5f;
141 
142  for (int y = slice_start; y < slice_end; y++) {
143  const int cy = y >> subh;
144  uint16_t *uptr = (uint16_t *)frame->data[1] + cy * ulinesize;
145  uint16_t *vptr = (uint16_t *)frame->data[2] + cy * vlinesize;
146 
147  for (int x = 0; x < width; x++) {
148  PROCESS()
149 
150  yptr[x] = av_clip_uintp2_c(ny * max, depth);
151  }
152 
153  yptr += ylinesize;
154  }
155 
156  return 0;
157 }
158 
159 static int clear_slice8(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
160 {
161  MonochromeContext *s = ctx->priv;
162  AVFrame *frame = arg;
163  const int depth = s->depth;
164  const int half = 1 << (depth - 1);
165  const int subw = s->subw;
166  const int subh = s->subh;
167  const int width = AV_CEIL_RSHIFT(frame->width, subw);
168  const int height = AV_CEIL_RSHIFT(frame->height, subh);
169  const int slice_start = ff_slice_pos(height, jobnr, nb_jobs);
170  const int slice_end = ff_slice_pos(height, jobnr + 1, nb_jobs);
171  const int ulinesize = frame->linesize[1];
172  const int vlinesize = frame->linesize[2];
173 
174  for (int y = slice_start; y < slice_end; y++) {
175  uint8_t *uptr = frame->data[1] + y * ulinesize;
176  uint8_t *vptr = frame->data[2] + y * vlinesize;
177 
178  memset(uptr, half, width);
179  memset(vptr, half, width);
180  }
181 
182  return 0;
183 }
184 
185 static int clear_slice16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
186 {
187  MonochromeContext *s = ctx->priv;
188  AVFrame *frame = arg;
189  const int depth = s->depth;
190  const int half = 1 << (depth - 1);
191  const int subw = s->subw;
192  const int subh = s->subh;
193  const int width = AV_CEIL_RSHIFT(frame->width, subw);
194  const int height = AV_CEIL_RSHIFT(frame->height, subh);
195  const int slice_start = ff_slice_pos(height, jobnr, nb_jobs);
196  const int slice_end = ff_slice_pos(height, jobnr + 1, nb_jobs);
197  const int ulinesize = frame->linesize[1] / 2;
198  const int vlinesize = frame->linesize[2] / 2;
199 
200  for (int y = slice_start; y < slice_end; y++) {
201  uint16_t *uptr = (uint16_t *)frame->data[1] + y * ulinesize;
202  uint16_t *vptr = (uint16_t *)frame->data[2] + y * vlinesize;
203 
204  for (int x = 0; x < width; x++) {
205  uptr[x] = half;
206  vptr[x] = half;
207  }
208  }
209 
210  return 0;
211 }
212 
213 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
214 {
215  AVFilterContext *ctx = inlink->dst;
216  MonochromeContext *s = ctx->priv;
217 
218  ctx->internal->execute(ctx, s->do_slice, frame, NULL,
220  ctx->internal->execute(ctx, s->clear_uv, frame, NULL,
222 
223  return ff_filter_frame(ctx->outputs[0], frame);
224 }
225 
227 {
228  static const enum AVPixelFormat pixel_fmts[] = {
247  };
248 
250 
251  formats = ff_make_format_list(pixel_fmts);
252  if (!formats)
253  return AVERROR(ENOMEM);
254 
256 }
257 
258 static av_cold int config_input(AVFilterLink *inlink)
259 {
260  AVFilterContext *ctx = inlink->dst;
261  MonochromeContext *s = ctx->priv;
263 
264  s->depth = desc->comp[0].depth;
265  s->do_slice = s->depth <= 8 ? monochrome_slice8 : monochrome_slice16;
266  s->clear_uv = s->depth <= 8 ? clear_slice8 : clear_slice16;
267  s->subw = desc->log2_chroma_w;
268  s->subh = desc->log2_chroma_h;
269 
270  return 0;
271 }
272 
273 static const AVFilterPad monochrome_inputs[] = {
274  {
275  .name = "default",
276  .type = AVMEDIA_TYPE_VIDEO,
277  .needs_writable = 1,
278  .filter_frame = filter_frame,
279  .config_props = config_input,
280  },
281  { NULL }
282 };
283 
284 static const AVFilterPad monochrome_outputs[] = {
285  {
286  .name = "default",
287  .type = AVMEDIA_TYPE_VIDEO,
288  },
289  { NULL }
290 };
291 
292 #define OFFSET(x) offsetof(MonochromeContext, x)
293 #define VF AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
294 
295 static const AVOption monochrome_options[] = {
296  { "cb", "set the chroma blue spot", OFFSET(b), AV_OPT_TYPE_FLOAT, {.dbl=0},-1, 1, VF },
297  { "cr", "set the chroma red spot", OFFSET(r), AV_OPT_TYPE_FLOAT, {.dbl=0},-1, 1, VF },
298  { "size", "set the color filter size", OFFSET(size), AV_OPT_TYPE_FLOAT, {.dbl=1},.1,10, VF },
299  { "high", "set the highlights strength", OFFSET(high), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, 1, VF },
300  { NULL }
301 };
302 
304 
306  .name = "monochrome",
307  .description = NULL_IF_CONFIG_SMALL("Convert video to gray using custom color filter."),
308  .priv_size = sizeof(MonochromeContext),
309  .priv_class = &monochrome_class,
315 };
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 u(width, name, range_min, range_max)
Definition: cbs_h2645.c:264
#define s(width, name)
Definition: cbs_vp9.c:257
#define FFMIN(a, b)
Definition: common.h:105
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:58
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 av_clipf
Definition: common.h:170
#define NULL
Definition: coverity.c:32
static __device__ float fabsf(float a)
Definition: cuda_runtime.h:181
#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
#define expf(x)
Definition: libm.h:283
const char * desc
Definition: libsvtav1.c:79
static uint8_t half(int a, int b)
Definition: mobiclip.c:541
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_YUV420P16
Definition: pixfmt.h:410
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:406
#define AV_PIX_FMT_YUV444P9
Definition: pixfmt.h:398
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:399
#define AV_PIX_FMT_YUV440P12
Definition: pixfmt.h:405
#define AV_PIX_FMT_YUV422P9
Definition: pixfmt.h:397
#define AV_PIX_FMT_YUVA444P10
Definition: pixfmt.h:438
#define AV_PIX_FMT_YUVA420P16
Definition: pixfmt.h:441
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:403
#define AV_PIX_FMT_YUVA420P10
Definition: pixfmt.h:436
#define AV_PIX_FMT_YUVA422P9
Definition: pixfmt.h:434
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:404
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:400
#define AV_PIX_FMT_YUV420P9
Definition: pixfmt.h:396
#define AV_PIX_FMT_YUVA420P9
Definition: pixfmt.h:433
#define AV_PIX_FMT_YUVA422P10
Definition: pixfmt.h:437
#define AV_PIX_FMT_YUV420P14
Definition: pixfmt.h:407
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:99
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
@ AV_PIX_FMT_YUVA420P
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:101
@ AV_PIX_FMT_YUVJ440P
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV440P and setting color_range
Definition: pixfmt.h:100
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:72
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:73
@ 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_YUVJ411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples) full scale (JPEG), deprecated in favor ...
Definition: pixfmt.h:258
@ AV_PIX_FMT_YUVJ422P
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition: pixfmt.h:79
@ AV_PIX_FMT_YUVA422P
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition: pixfmt.h:176
@ 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
@ AV_PIX_FMT_YUVJ420P
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:78
#define AV_PIX_FMT_YUVA422P12
Definition: pixfmt.h:439
#define AV_PIX_FMT_YUV422P14
Definition: pixfmt.h:408
#define AV_PIX_FMT_YUV422P16
Definition: pixfmt.h:411
#define AV_PIX_FMT_YUV440P10
Definition: pixfmt.h:401
#define AV_PIX_FMT_YUVA444P16
Definition: pixfmt.h:443
#define AV_PIX_FMT_YUVA422P16
Definition: pixfmt.h:442
#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)
Definition: vf_monochrome.c:41
int(* clear_uv)(AVFilterContext *s, void *arg, int jobnr, int nb_jobs)
Definition: vf_monochrome.c:43
static uint8_t tmp[11]
Definition: aes_ctr.c:27
AVFormatContext * ctx
Definition: movenc.c:48
#define height
#define width
int size
const char * b
Definition: vf_curves.c:119
const char * r
Definition: vf_curves.c:117
AVFilter ff_vf_monochrome
AVFILTER_DEFINE_CLASS(monochrome)
static int clear_slice8(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
static float filter(float b, float r, float u, float v, float size)
Definition: vf_monochrome.c:62
#define PROCESS()
Definition: vf_monochrome.c:69
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
static av_cold int query_formats(AVFilterContext *ctx)
static const AVFilterPad monochrome_inputs[]
static const AVFilterPad monochrome_outputs[]
#define VF
static int clear_slice16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
static av_cold int config_input(AVFilterLink *inlink)
static int monochrome_slice8(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_monochrome.c:81
static const AVOption monochrome_options[]
#define OFFSET(x)
static int monochrome_slice16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
static float envelope(const float x)
Definition: vf_monochrome.c:47