• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2015 Timo Rothenpieler <timo@rothenpieler.org>
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 "libavutil/opt.h"
22 #include "libavutil/imgutils.h"
23 #include "libavutil/intreadwrite.h"
24 #include "avfilter.h"
25 #include "formats.h"
26 #include "internal.h"
27 #include "video.h"
28 
29 typedef struct ChromakeyContext {
30     const AVClass *class;
31 
32     uint8_t chromakey_rgba[4];
33     uint16_t chromakey_uv[2];
34 
35     float similarity;
36     float blend;
37 
38     int is_yuv;
39     int depth;
40     int mid;
41     int max;
42 
43     int hsub_log2;
44     int vsub_log2;
45 
46     int (*do_slice)(AVFilterContext *ctx, void *arg,
47                     int jobnr, int nb_jobs);
48 } ChromakeyContext;
49 
do_chromakey_pixel(ChromakeyContext * ctx,uint8_t u[9],uint8_t v[9])50 static uint8_t do_chromakey_pixel(ChromakeyContext *ctx, uint8_t u[9], uint8_t v[9])
51 {
52     double diff = 0.0;
53     int du, dv, i;
54 
55     for (i = 0; i < 9; ++i) {
56         du = (int)u[i] - ctx->chromakey_uv[0];
57         dv = (int)v[i] - ctx->chromakey_uv[1];
58 
59         diff += sqrt((du * du + dv * dv) / (255.0 * 255.0 * 2));
60     }
61 
62     diff /= 9.0;
63 
64     if (ctx->blend > 0.0001) {
65         return av_clipd((diff - ctx->similarity) / ctx->blend, 0.0, 1.0) * 255.0;
66     } else {
67         return (diff > ctx->similarity) ? 255 : 0;
68     }
69 }
70 
do_chromakey_pixel16(ChromakeyContext * ctx,uint16_t u[9],uint16_t v[9])71 static uint16_t do_chromakey_pixel16(ChromakeyContext *ctx, uint16_t u[9], uint16_t v[9])
72 {
73     double max = ctx->max;
74     double diff = 0.0;
75     int du, dv, i;
76 
77     for (i = 0; i < 9; ++i) {
78         du = (int)u[i] - ctx->chromakey_uv[0];
79         dv = (int)v[i] - ctx->chromakey_uv[1];
80 
81         diff += sqrt((du * du + dv * dv) / (max * max * 2));
82     }
83 
84     diff /= 9.0;
85 
86     if (ctx->blend > 0.0001) {
87         return av_clipd((diff - ctx->similarity) / ctx->blend, 0.0, 1.0) * max;
88     } else {
89         return (diff > ctx->similarity) ? max : 0;
90     }
91 }
92 
get_pixel_uv(AVFrame * frame,int hsub_log2,int vsub_log2,int x,int y,uint8_t * u,uint8_t * v)93 static av_always_inline void get_pixel_uv(AVFrame *frame, int hsub_log2, int vsub_log2, int x, int y, uint8_t *u, uint8_t *v)
94 {
95     if (x < 0 || x >= frame->width || y < 0 || y >= frame->height)
96         return;
97 
98     x >>= hsub_log2;
99     y >>= vsub_log2;
100 
101     *u = frame->data[1][frame->linesize[1] * y + x];
102     *v = frame->data[2][frame->linesize[2] * y + x];
103 }
104 
get_pixel16_uv(AVFrame * frame,int hsub_log2,int vsub_log2,int x,int y,uint16_t * u,uint16_t * v)105 static av_always_inline void get_pixel16_uv(AVFrame *frame, int hsub_log2, int vsub_log2, int x, int y, uint16_t *u, uint16_t *v)
106 {
107     if (x < 0 || x >= frame->width || y < 0 || y >= frame->height)
108         return;
109 
110     x >>= hsub_log2;
111     y >>= vsub_log2;
112 
113     *u = AV_RN16(&frame->data[1][frame->linesize[1] * y + 2 * x]);
114     *v = AV_RN16(&frame->data[2][frame->linesize[2] * y + 2 * x]);
115 }
116 
do_chromakey_slice(AVFilterContext * avctx,void * arg,int jobnr,int nb_jobs)117 static int do_chromakey_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
118 {
119     AVFrame *frame = arg;
120 
121     const int slice_start = (frame->height * jobnr) / nb_jobs;
122     const int slice_end = (frame->height * (jobnr + 1)) / nb_jobs;
123 
124     ChromakeyContext *ctx = avctx->priv;
125 
126     int x, y, xo, yo;
127     uint8_t u[9], v[9];
128 
129     memset(u, ctx->chromakey_uv[0], sizeof(u));
130     memset(v, ctx->chromakey_uv[1], sizeof(v));
131 
132     for (y = slice_start; y < slice_end; ++y) {
133         for (x = 0; x < frame->width; ++x) {
134             for (yo = 0; yo < 3; ++yo) {
135                 for (xo = 0; xo < 3; ++xo) {
136                     get_pixel_uv(frame, ctx->hsub_log2, ctx->vsub_log2, x + xo - 1, y + yo - 1, &u[yo * 3 + xo], &v[yo * 3 + xo]);
137                 }
138             }
139 
140             frame->data[3][frame->linesize[3] * y + x] = do_chromakey_pixel(ctx, u, v);
141         }
142     }
143 
144     return 0;
145 }
146 
do_chromakey16_slice(AVFilterContext * avctx,void * arg,int jobnr,int nb_jobs)147 static int do_chromakey16_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
148 {
149     AVFrame *frame = arg;
150 
151     const int slice_start = (frame->height * jobnr) / nb_jobs;
152     const int slice_end = (frame->height * (jobnr + 1)) / nb_jobs;
153 
154     ChromakeyContext *ctx = avctx->priv;
155 
156     int x, y, xo, yo;
157     uint16_t u[9], v[9];
158 
159     for (int i = 0; i < 9; i++) {
160         u[i] = ctx->chromakey_uv[0];
161         v[i] = ctx->chromakey_uv[1];
162     }
163 
164     for (y = slice_start; y < slice_end; ++y) {
165         for (x = 0; x < frame->width; ++x) {
166             uint16_t *dst = (uint16_t *)(frame->data[3] + frame->linesize[3] * y);
167 
168             for (yo = 0; yo < 3; ++yo) {
169                 for (xo = 0; xo < 3; ++xo) {
170                     get_pixel16_uv(frame, ctx->hsub_log2, ctx->vsub_log2, x + xo - 1, y + yo - 1, &u[yo * 3 + xo], &v[yo * 3 + xo]);
171                 }
172             }
173 
174             dst[x] = do_chromakey_pixel16(ctx, u, v);
175         }
176     }
177 
178     return 0;
179 }
180 
do_chromahold_slice(AVFilterContext * avctx,void * arg,int jobnr,int nb_jobs)181 static int do_chromahold_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
182 {
183     ChromakeyContext *ctx = avctx->priv;
184     AVFrame *frame = arg;
185     const int slice_start = ((frame->height >> ctx->vsub_log2) * jobnr) / nb_jobs;
186     const int slice_end = ((frame->height >> ctx->vsub_log2) * (jobnr + 1)) / nb_jobs;
187 
188     int x, y, alpha;
189 
190     for (y = slice_start; y < slice_end; ++y) {
191         for (x = 0; x < frame->width >> ctx->hsub_log2; ++x) {
192             int u = frame->data[1][frame->linesize[1] * y + x];
193             int v = frame->data[2][frame->linesize[2] * y + x];
194             double diff;
195             int du, dv;
196 
197             du = u - ctx->chromakey_uv[0];
198             dv = v - ctx->chromakey_uv[1];
199 
200             diff = sqrt((du * du + dv * dv) / (255.0 * 255.0 * 2.0));
201 
202             alpha = diff > ctx->similarity;
203             if (ctx->blend > 0.0001) {
204                 double f = 1. - av_clipd((diff - ctx->similarity) / ctx->blend, 0.0, 1.0);
205 
206                 frame->data[1][frame->linesize[1] * y + x] = 128 + (u - 128) * f;
207                 frame->data[2][frame->linesize[2] * y + x] = 128 + (v - 128) * f;
208             } else if (alpha) {
209                 frame->data[1][frame->linesize[1] * y + x] = 128;
210                 frame->data[2][frame->linesize[2] * y + x] = 128;
211             }
212         }
213     }
214 
215     return 0;
216 }
217 
do_chromahold16_slice(AVFilterContext * avctx,void * arg,int jobnr,int nb_jobs)218 static int do_chromahold16_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
219 {
220     ChromakeyContext *ctx = avctx->priv;
221     AVFrame *frame = arg;
222     const int slice_start = ((frame->height >> ctx->vsub_log2) * jobnr) / nb_jobs;
223     const int slice_end = ((frame->height >> ctx->vsub_log2) * (jobnr + 1)) / nb_jobs;
224     const int mid = ctx->mid;
225     double max = ctx->max;
226 
227     int x, y, alpha;
228 
229     for (y = slice_start; y < slice_end; ++y) {
230         for (x = 0; x < frame->width >> ctx->hsub_log2; ++x) {
231             int u = AV_RN16(&frame->data[1][frame->linesize[1] * y + 2 * x]);
232             int v = AV_RN16(&frame->data[2][frame->linesize[2] * y + 2 * x]);
233             double diff;
234             int du, dv;
235 
236             du = u - ctx->chromakey_uv[0];
237             dv = v - ctx->chromakey_uv[1];
238 
239             diff = sqrt((du * du + dv * dv) / (max * max * 2.0));
240 
241             alpha = diff > ctx->similarity;
242             if (ctx->blend > 0.0001) {
243                 double f = 1. - av_clipd((diff - ctx->similarity) / ctx->blend, 0.0, 1.0);
244 
245                 AV_WN16(&frame->data[1][frame->linesize[1] * y + 2 * x], mid + (u - mid) * f);
246                 AV_WN16(&frame->data[2][frame->linesize[2] * y + 2 * x], mid + (v - mid) * f);
247             } else if (alpha) {
248                 AV_WN16(&frame->data[1][frame->linesize[1] * y + 2 * x], mid);
249                 AV_WN16(&frame->data[2][frame->linesize[2] * y + 2 * x], mid);
250             }
251         }
252     }
253 
254     return 0;
255 }
256 
filter_frame(AVFilterLink * link,AVFrame * frame)257 static int filter_frame(AVFilterLink *link, AVFrame *frame)
258 {
259     AVFilterContext *avctx = link->dst;
260     ChromakeyContext *ctx = avctx->priv;
261     int res;
262 
263     if (res = ff_filter_execute(avctx, ctx->do_slice, frame, NULL,
264                                 FFMIN(frame->height, ff_filter_get_nb_threads(avctx))))
265         return res;
266 
267     return ff_filter_frame(avctx->outputs[0], frame);
268 }
269 
270 #define FIXNUM(x) lrint((x) * (1 << 10))
271 #define RGB_TO_U(rgb) (((- FIXNUM(0.16874) * rgb[0] - FIXNUM(0.33126) * rgb[1] + FIXNUM(0.50000) * rgb[2] + (1 << 9) - 1) >> 10) + 128)
272 #define RGB_TO_V(rgb) (((  FIXNUM(0.50000) * rgb[0] - FIXNUM(0.41869) * rgb[1] - FIXNUM(0.08131) * rgb[2] + (1 << 9) - 1) >> 10) + 128)
273 
config_output(AVFilterLink * outlink)274 static av_cold int config_output(AVFilterLink *outlink)
275 {
276     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
277     AVFilterContext *avctx = outlink->src;
278     ChromakeyContext *ctx = avctx->priv;
279     int factor;
280 
281     ctx->depth = desc->comp[0].depth;
282     ctx->mid = 1 << (ctx->depth - 1);
283     ctx->max = (1 << ctx->depth) - 1;
284 
285     factor = 1 << (ctx->depth - 8);
286 
287     if (ctx->is_yuv) {
288         ctx->chromakey_uv[0] = ctx->chromakey_rgba[1] * factor;
289         ctx->chromakey_uv[1] = ctx->chromakey_rgba[2] * factor;
290     } else {
291         ctx->chromakey_uv[0] = RGB_TO_U(ctx->chromakey_rgba) * factor;
292         ctx->chromakey_uv[1] = RGB_TO_V(ctx->chromakey_rgba) * factor;
293     }
294 
295     if (!strcmp(avctx->filter->name, "chromakey")) {
296         ctx->do_slice = ctx->depth <= 8 ? do_chromakey_slice : do_chromakey16_slice;
297     } else {
298         ctx->do_slice = ctx->depth <= 8 ? do_chromahold_slice: do_chromahold16_slice;
299     }
300 
301     return 0;
302 }
303 
config_input(AVFilterLink * inlink)304 static av_cold int config_input(AVFilterLink *inlink)
305 {
306     AVFilterContext *avctx = inlink->dst;
307     ChromakeyContext *ctx = avctx->priv;
308     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
309 
310     ctx->hsub_log2 = desc->log2_chroma_w;
311     ctx->vsub_log2 = desc->log2_chroma_h;
312 
313     return 0;
314 }
315 
process_command(AVFilterContext * ctx,const char * cmd,const char * args,char * res,int res_len,int flags)316 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
317                            char *res, int res_len, int flags)
318 {
319     int ret;
320 
321     ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
322     if (ret < 0)
323         return ret;
324 
325     return config_output(ctx->outputs[0]);
326 }
327 
328 static const AVFilterPad chromakey_inputs[] = {
329     {
330         .name           = "default",
331         .type           = AVMEDIA_TYPE_VIDEO,
332         .flags          = AVFILTERPAD_FLAG_NEEDS_WRITABLE,
333         .filter_frame   = filter_frame,
334         .config_props   = config_input,
335     },
336 };
337 
338 static const AVFilterPad chromakey_outputs[] = {
339     {
340         .name           = "default",
341         .type           = AVMEDIA_TYPE_VIDEO,
342         .config_props   = config_output,
343     },
344 };
345 
346 #define OFFSET(x) offsetof(ChromakeyContext, x)
347 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
348 
349 static const AVOption chromakey_options[] = {
350     { "color", "set the chromakey key color", OFFSET(chromakey_rgba), AV_OPT_TYPE_COLOR, { .str = "black" }, 0, 0, FLAGS },
351     { "similarity", "set the chromakey similarity value", OFFSET(similarity), AV_OPT_TYPE_FLOAT, { .dbl = 0.01 }, 0.01, 1.0, FLAGS },
352     { "blend", "set the chromakey key blend value", OFFSET(blend), AV_OPT_TYPE_FLOAT, { .dbl = 0.0 }, 0.0, 1.0, FLAGS },
353     { "yuv", "color parameter is in yuv instead of rgb", OFFSET(is_yuv), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
354     { NULL }
355 };
356 
357 static const enum AVPixelFormat chromakey_fmts[] = {
358     AV_PIX_FMT_YUVA420P,
359     AV_PIX_FMT_YUVA422P,
360     AV_PIX_FMT_YUVA444P,
361     AV_PIX_FMT_YUVA420P9,  AV_PIX_FMT_YUVA422P9,  AV_PIX_FMT_YUVA444P9,
362     AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
363     AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA444P12,
364     AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
365     AV_PIX_FMT_NONE
366 };
367 
368 AVFILTER_DEFINE_CLASS(chromakey);
369 
370 const AVFilter ff_vf_chromakey = {
371     .name          = "chromakey",
372     .description   = NULL_IF_CONFIG_SMALL("Turns a certain color into transparency. Operates on YUV colors."),
373     .priv_size     = sizeof(ChromakeyContext),
374     .priv_class    = &chromakey_class,
375     FILTER_INPUTS(chromakey_inputs),
376     FILTER_OUTPUTS(chromakey_outputs),
377     FILTER_PIXFMTS_ARRAY(chromakey_fmts),
378     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
379     .process_command = process_command,
380 };
381 
382 static const AVOption chromahold_options[] = {
383     { "color", "set the chromahold key color", OFFSET(chromakey_rgba), AV_OPT_TYPE_COLOR, { .str = "black" }, 0, 0, FLAGS },
384     { "similarity", "set the chromahold similarity value", OFFSET(similarity), AV_OPT_TYPE_FLOAT, { .dbl = 0.01 }, 0.01, 1.0, FLAGS },
385     { "blend", "set the chromahold blend value", OFFSET(blend), AV_OPT_TYPE_FLOAT, { .dbl = 0.0 }, 0.0, 1.0, FLAGS },
386     { "yuv", "color parameter is in yuv instead of rgb", OFFSET(is_yuv), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
387     { NULL }
388 };
389 
390 static const AVFilterPad chromahold_inputs[] = {
391     {
392         .name           = "default",
393         .type           = AVMEDIA_TYPE_VIDEO,
394         .flags          = AVFILTERPAD_FLAG_NEEDS_WRITABLE,
395         .filter_frame   = filter_frame,
396         .config_props   = config_input,
397     },
398 };
399 
400 static const AVFilterPad chromahold_outputs[] = {
401     {
402         .name           = "default",
403         .type           = AVMEDIA_TYPE_VIDEO,
404         .config_props   = config_output,
405     },
406 };
407 
408 static const enum AVPixelFormat hold_pixel_fmts[] = {
409     AV_PIX_FMT_YUV420P,
410     AV_PIX_FMT_YUV422P,
411     AV_PIX_FMT_YUV444P,
412     AV_PIX_FMT_YUVA420P,
413     AV_PIX_FMT_YUVA422P,
414     AV_PIX_FMT_YUVA444P,
415     AV_PIX_FMT_YUV420P9,   AV_PIX_FMT_YUV422P9,   AV_PIX_FMT_YUV444P9,
416     AV_PIX_FMT_YUV420P10,  AV_PIX_FMT_YUV422P10,  AV_PIX_FMT_YUV444P10,
417     AV_PIX_FMT_YUV444P12,  AV_PIX_FMT_YUV422P12,  AV_PIX_FMT_YUV420P12,
418     AV_PIX_FMT_YUV444P14,  AV_PIX_FMT_YUV422P14,  AV_PIX_FMT_YUV420P14,
419     AV_PIX_FMT_YUV420P16,  AV_PIX_FMT_YUV422P16,  AV_PIX_FMT_YUV444P16,
420     AV_PIX_FMT_YUVA420P9,  AV_PIX_FMT_YUVA422P9,  AV_PIX_FMT_YUVA444P9,
421     AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
422     AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA444P12,
423     AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
424     AV_PIX_FMT_NONE
425 };
426 
427 AVFILTER_DEFINE_CLASS(chromahold);
428 
429 const AVFilter ff_vf_chromahold = {
430     .name          = "chromahold",
431     .description   = NULL_IF_CONFIG_SMALL("Turns a certain color range into gray."),
432     .priv_size     = sizeof(ChromakeyContext),
433     .priv_class    = &chromahold_class,
434     FILTER_INPUTS(chromahold_inputs),
435     FILTER_OUTPUTS(chromahold_outputs),
436     FILTER_PIXFMTS_ARRAY(hold_pixel_fmts),
437     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
438     .process_command = process_command,
439 };
440