2 * Copyright (c) 2013 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
21 #include "libavutil/imgutils.h"
22 #include "libavutil/eval.h"
23 #include "libavutil/opt.h"
24 #include "libavutil/pixfmt.h"
26 #include "bufferqueue.h"
29 #include "dualinput.h"
65 static const char *const var_names[] = { "X", "Y", "W", "H", "SW", "SH", "T", "N", "A", "B", "TOP", "BOTTOM", NULL };
66 enum { VAR_X, VAR_Y, VAR_W, VAR_H, VAR_SW, VAR_SH, VAR_T, VAR_N, VAR_A, VAR_B, VAR_TOP, VAR_BOTTOM, VAR_VARS_NB };
68 typedef struct FilterParams {
73 void (*blend)(const uint8_t *top, int top_linesize,
74 const uint8_t *bottom, int bottom_linesize,
75 uint8_t *dst, int dst_linesize,
76 int width, int start, int end,
77 struct FilterParams *param, double *values);
80 typedef struct ThreadData {
81 const AVFrame *top, *bottom;
91 FFDualInputContext dinput;
92 int hsub, vsub; ///< chroma subsampling values
95 enum BlendMode all_mode;
98 FilterParams params[4];
101 #define OFFSET(x) offsetof(BlendContext, x)
102 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
104 static const AVOption blend_options[] = {
105 { "c0_mode", "set component #0 blend mode", OFFSET(params[0].mode), AV_OPT_TYPE_INT, {.i64=0}, 0, BLEND_NB-1, FLAGS, "mode"},
106 { "c1_mode", "set component #1 blend mode", OFFSET(params[1].mode), AV_OPT_TYPE_INT, {.i64=0}, 0, BLEND_NB-1, FLAGS, "mode"},
107 { "c2_mode", "set component #2 blend mode", OFFSET(params[2].mode), AV_OPT_TYPE_INT, {.i64=0}, 0, BLEND_NB-1, FLAGS, "mode"},
108 { "c3_mode", "set component #3 blend mode", OFFSET(params[3].mode), AV_OPT_TYPE_INT, {.i64=0}, 0, BLEND_NB-1, FLAGS, "mode"},
109 { "all_mode", "set blend mode for all components", OFFSET(all_mode), AV_OPT_TYPE_INT, {.i64=-1},-1, BLEND_NB-1, FLAGS, "mode"},
110 { "addition", "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_ADDITION}, 0, 0, FLAGS, "mode" },
111 { "and", "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_AND}, 0, 0, FLAGS, "mode" },
112 { "average", "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_AVERAGE}, 0, 0, FLAGS, "mode" },
113 { "burn", "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_BURN}, 0, 0, FLAGS, "mode" },
114 { "darken", "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_DARKEN}, 0, 0, FLAGS, "mode" },
115 { "difference", "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_DIFFERENCE}, 0, 0, FLAGS, "mode" },
116 { "difference128", "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_DIFFERENCE128}, 0, 0, FLAGS, "mode" },
117 { "divide", "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_DIVIDE}, 0, 0, FLAGS, "mode" },
118 { "dodge", "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_DODGE}, 0, 0, FLAGS, "mode" },
119 { "exclusion", "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_EXCLUSION}, 0, 0, FLAGS, "mode" },
120 { "hardlight", "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_HARDLIGHT}, 0, 0, FLAGS, "mode" },
121 { "lighten", "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_LIGHTEN}, 0, 0, FLAGS, "mode" },
122 { "multiply", "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_MULTIPLY}, 0, 0, FLAGS, "mode" },
123 { "negation", "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_NEGATION}, 0, 0, FLAGS, "mode" },
124 { "normal", "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_NORMAL}, 0, 0, FLAGS, "mode" },
125 { "or", "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_OR}, 0, 0, FLAGS, "mode" },
126 { "overlay", "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_OVERLAY}, 0, 0, FLAGS, "mode" },
127 { "phoenix", "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_PHOENIX}, 0, 0, FLAGS, "mode" },
128 { "pinlight", "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_PINLIGHT}, 0, 0, FLAGS, "mode" },
129 { "reflect", "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_REFLECT}, 0, 0, FLAGS, "mode" },
130 { "screen", "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_SCREEN}, 0, 0, FLAGS, "mode" },
131 { "softlight", "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_SOFTLIGHT}, 0, 0, FLAGS, "mode" },
132 { "subtract", "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_SUBTRACT}, 0, 0, FLAGS, "mode" },
133 { "vividlight", "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_VIVIDLIGHT}, 0, 0, FLAGS, "mode" },
134 { "xor", "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_XOR}, 0, 0, FLAGS, "mode" },
135 { "c0_expr", "set color component #0 expression", OFFSET(params[0].expr_str), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
136 { "c1_expr", "set color component #1 expression", OFFSET(params[1].expr_str), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
137 { "c2_expr", "set color component #2 expression", OFFSET(params[2].expr_str), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
138 { "c3_expr", "set color component #3 expression", OFFSET(params[3].expr_str), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
139 { "all_expr", "set expression for all color components", OFFSET(all_expr), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
140 { "c0_opacity", "set color component #0 opacity", OFFSET(params[0].opacity), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, FLAGS },
141 { "c1_opacity", "set color component #1 opacity", OFFSET(params[1].opacity), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, FLAGS },
142 { "c2_opacity", "set color component #2 opacity", OFFSET(params[2].opacity), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, FLAGS },
143 { "c3_opacity", "set color component #3 opacity", OFFSET(params[3].opacity), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, FLAGS },
144 { "all_opacity", "set opacity for all color components", OFFSET(all_opacity), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, FLAGS},
145 { "shortest", "force termination when the shortest input terminates", OFFSET(dinput.shortest), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS },
146 { "repeatlast", "repeat last bottom frame", OFFSET(dinput.repeatlast), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS },
150 AVFILTER_DEFINE_CLASS(blend);
152 static void blend_normal(const uint8_t *top, int top_linesize,
153 const uint8_t *bottom, int bottom_linesize,
154 uint8_t *dst, int dst_linesize,
155 int width, int start, int end,
156 FilterParams *param, double *values)
158 av_image_copy_plane(dst, dst_linesize, top, top_linesize, width, end - start);
161 #define DEFINE_BLEND(name, expr) \
162 static void blend_## name(const uint8_t *top, int top_linesize, \
163 const uint8_t *bottom, int bottom_linesize, \
164 uint8_t *dst, int dst_linesize, \
165 int width, int start, int end, \
166 FilterParams *param, double *values) \
168 double opacity = param->opacity; \
171 for (i = start; i < end; i++) { \
172 for (j = 0; j < width; j++) { \
173 dst[j] = top[j] + ((expr) - top[j]) * opacity; \
175 dst += dst_linesize; \
176 top += top_linesize; \
177 bottom += bottom_linesize; \
184 #define MULTIPLY(x, a, b) ((x) * (((a) * (b)) / 255))
185 #define SCREEN(x, a, b) (255 - (x) * ((255 - (a)) * (255 - (b)) / 255))
186 #define BURN(a, b) (((a) == 0) ? (a) : FFMAX(0, 255 - ((255 - (b)) << 8) / (a)))
187 #define DODGE(a, b) (((a) == 255) ? (a) : FFMIN(255, (((b) << 8) / (255 - (a)))))
189 DEFINE_BLEND(addition, FFMIN(255, A + B))
190 DEFINE_BLEND(average, (A + B) / 2)
191 DEFINE_BLEND(subtract, FFMAX(0, A - B))
192 DEFINE_BLEND(multiply, MULTIPLY(1, A, B))
193 DEFINE_BLEND(negation, 255 - FFABS(255 - A - B))
194 DEFINE_BLEND(difference, FFABS(A - B))
195 DEFINE_BLEND(difference128, av_clip_uint8(128 + A - B))
196 DEFINE_BLEND(screen, SCREEN(1, A, B))
197 DEFINE_BLEND(overlay, (A < 128) ? MULTIPLY(2, A, B) : SCREEN(2, A, B))
198 DEFINE_BLEND(hardlight, (B < 128) ? MULTIPLY(2, B, A) : SCREEN(2, B, A))
199 DEFINE_BLEND(darken, FFMIN(A, B))
200 DEFINE_BLEND(lighten, FFMAX(A, B))
201 DEFINE_BLEND(divide, ((float)A / ((float)B) * 255))
202 DEFINE_BLEND(dodge, DODGE(A, B))
203 DEFINE_BLEND(burn, BURN(A, B))
204 DEFINE_BLEND(softlight, (A > 127) ? B + (255 - B) * (A - 127.5) / 127.5 * (0.5 - FFABS(B - 127.5) / 255): B - B * ((127.5 - A) / 127.5) * (0.5 - FFABS(B - 127.5)/255))
205 DEFINE_BLEND(exclusion, A + B - 2 * A * B / 255)
206 DEFINE_BLEND(pinlight, (B < 128) ? FFMIN(A, 2 * B) : FFMAX(A, 2 * (B - 128)))
207 DEFINE_BLEND(phoenix, FFMIN(A, B) - FFMAX(A, B) + 255)
208 DEFINE_BLEND(reflect, (B == 255) ? B : FFMIN(255, (A * A / (255 - B))))
209 DEFINE_BLEND(and, A & B)
210 DEFINE_BLEND(or, A | B)
211 DEFINE_BLEND(xor, A ^ B)
212 DEFINE_BLEND(vividlight, (B < 128) ? BURN(A, 2 * B) : DODGE(A, 2 * (B - 128)))
214 static void blend_expr(const uint8_t *top, int top_linesize,
215 const uint8_t *bottom, int bottom_linesize,
216 uint8_t *dst, int dst_linesize,
217 int width, int start, int end,
218 FilterParams *param, double *values)
220 AVExpr *e = param->e;
223 for (y = start; y < end; y++) {
225 for (x = 0; x < width; x++) {
227 values[VAR_TOP] = values[VAR_A] = top[x];
228 values[VAR_BOTTOM] = values[VAR_B] = bottom[x];
229 dst[x] = av_expr_eval(e, values, NULL);
233 bottom += bottom_linesize;
237 static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
239 ThreadData *td = arg;
240 int slice_start = (td->h * jobnr ) / nb_jobs;
241 int slice_end = (td->h * (jobnr+1)) / nb_jobs;
242 const uint8_t *top = td->top->data[td->plane];
243 const uint8_t *bottom = td->bottom->data[td->plane];
244 uint8_t *dst = td->dst->data[td->plane];
245 double values[VAR_VARS_NB];
247 values[VAR_N] = td->inlink->frame_count;
248 values[VAR_T] = td->dst->pts == AV_NOPTS_VALUE ? NAN : td->dst->pts * av_q2d(td->inlink->time_base);
249 values[VAR_W] = td->w;
250 values[VAR_H] = td->h;
251 values[VAR_SW] = td->w / (double)td->dst->width;
252 values[VAR_SH] = td->h / (double)td->dst->height;
254 td->param->blend(top + slice_start * td->top->linesize[td->plane],
255 td->top->linesize[td->plane],
256 bottom + slice_start * td->bottom->linesize[td->plane],
257 td->bottom->linesize[td->plane],
258 dst + slice_start * td->dst->linesize[td->plane],
259 td->dst->linesize[td->plane],
260 td->w, slice_start, slice_end, td->param, &values[0]);
264 static AVFrame *blend_frame(AVFilterContext *ctx, AVFrame *top_buf,
265 const AVFrame *bottom_buf)
267 BlendContext *b = ctx->priv;
268 AVFilterLink *inlink = ctx->inputs[0];
269 AVFilterLink *outlink = ctx->outputs[0];
273 dst_buf = ff_get_video_buffer(outlink, outlink->w, outlink->h);
276 av_frame_copy_props(dst_buf, top_buf);
278 for (plane = 0; plane < b->nb_planes; plane++) {
279 int hsub = plane == 1 || plane == 2 ? b->hsub : 0;
280 int vsub = plane == 1 || plane == 2 ? b->vsub : 0;
281 int outw = FF_CEIL_RSHIFT(dst_buf->width, hsub);
282 int outh = FF_CEIL_RSHIFT(dst_buf->height, vsub);
283 FilterParams *param = &b->params[plane];
284 ThreadData td = { .top = top_buf, .bottom = bottom_buf, .dst = dst_buf,
285 .w = outw, .h = outh, .param = param, .plane = plane,
288 ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(outh, ctx->graph->nb_threads));
291 av_frame_free(&top_buf);
296 static av_cold int init(AVFilterContext *ctx)
298 BlendContext *b = ctx->priv;
301 for (plane = 0; plane < FF_ARRAY_ELEMS(b->params); plane++) {
302 FilterParams *param = &b->params[plane];
304 if (b->all_mode >= 0)
305 param->mode = b->all_mode;
306 if (b->all_opacity < 1)
307 param->opacity = b->all_opacity;
309 switch (param->mode) {
310 case BLEND_ADDITION: param->blend = blend_addition; break;
311 case BLEND_AND: param->blend = blend_and; break;
312 case BLEND_AVERAGE: param->blend = blend_average; break;
313 case BLEND_BURN: param->blend = blend_burn; break;
314 case BLEND_DARKEN: param->blend = blend_darken; break;
315 case BLEND_DIFFERENCE: param->blend = blend_difference; break;
316 case BLEND_DIFFERENCE128: param->blend = blend_difference128; break;
317 case BLEND_DIVIDE: param->blend = blend_divide; break;
318 case BLEND_DODGE: param->blend = blend_dodge; break;
319 case BLEND_EXCLUSION: param->blend = blend_exclusion; break;
320 case BLEND_HARDLIGHT: param->blend = blend_hardlight; break;
321 case BLEND_LIGHTEN: param->blend = blend_lighten; break;
322 case BLEND_MULTIPLY: param->blend = blend_multiply; break;
323 case BLEND_NEGATION: param->blend = blend_negation; break;
324 case BLEND_NORMAL: param->blend = blend_normal; break;
325 case BLEND_OR: param->blend = blend_or; break;
326 case BLEND_OVERLAY: param->blend = blend_overlay; break;
327 case BLEND_PHOENIX: param->blend = blend_phoenix; break;
328 case BLEND_PINLIGHT: param->blend = blend_pinlight; break;
329 case BLEND_REFLECT: param->blend = blend_reflect; break;
330 case BLEND_SCREEN: param->blend = blend_screen; break;
331 case BLEND_SOFTLIGHT: param->blend = blend_softlight; break;
332 case BLEND_SUBTRACT: param->blend = blend_subtract; break;
333 case BLEND_VIVIDLIGHT: param->blend = blend_vividlight; break;
334 case BLEND_XOR: param->blend = blend_xor; break;
337 if (b->all_expr && !param->expr_str) {
338 param->expr_str = av_strdup(b->all_expr);
339 if (!param->expr_str)
340 return AVERROR(ENOMEM);
342 if (param->expr_str) {
343 ret = av_expr_parse(¶m->e, param->expr_str, var_names,
344 NULL, NULL, NULL, NULL, 0, ctx);
347 param->blend = blend_expr;
351 b->dinput.process = blend_frame;
355 static int query_formats(AVFilterContext *ctx)
357 static const enum AVPixelFormat pix_fmts[] = {
358 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
359 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ422P,AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ411P,
360 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
361 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE
364 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
368 static int config_output(AVFilterLink *outlink)
370 AVFilterContext *ctx = outlink->src;
371 AVFilterLink *toplink = ctx->inputs[TOP];
372 AVFilterLink *bottomlink = ctx->inputs[BOTTOM];
373 BlendContext *b = ctx->priv;
374 const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(toplink->format);
377 if (toplink->format != bottomlink->format) {
378 av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
379 return AVERROR(EINVAL);
381 if (toplink->w != bottomlink->w ||
382 toplink->h != bottomlink->h ||
383 toplink->sample_aspect_ratio.num != bottomlink->sample_aspect_ratio.num ||
384 toplink->sample_aspect_ratio.den != bottomlink->sample_aspect_ratio.den) {
385 av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
386 "(size %dx%d, SAR %d:%d) do not match the corresponding "
387 "second input link %s parameters (%dx%d, SAR %d:%d)\n",
388 ctx->input_pads[TOP].name, toplink->w, toplink->h,
389 toplink->sample_aspect_ratio.num,
390 toplink->sample_aspect_ratio.den,
391 ctx->input_pads[BOTTOM].name, bottomlink->w, bottomlink->h,
392 bottomlink->sample_aspect_ratio.num,
393 bottomlink->sample_aspect_ratio.den);
394 return AVERROR(EINVAL);
397 outlink->w = toplink->w;
398 outlink->h = toplink->h;
399 outlink->time_base = toplink->time_base;
400 outlink->sample_aspect_ratio = toplink->sample_aspect_ratio;
401 outlink->frame_rate = toplink->frame_rate;
403 b->hsub = pix_desc->log2_chroma_w;
404 b->vsub = pix_desc->log2_chroma_h;
405 b->nb_planes = av_pix_fmt_count_planes(toplink->format);
407 if ((ret = ff_dualinput_init(ctx, &b->dinput)) < 0)
413 static av_cold void uninit(AVFilterContext *ctx)
415 BlendContext *b = ctx->priv;
418 ff_dualinput_uninit(&b->dinput);
419 for (i = 0; i < FF_ARRAY_ELEMS(b->params); i++)
420 av_expr_free(b->params[i].e);
423 static int request_frame(AVFilterLink *outlink)
425 BlendContext *b = outlink->src->priv;
426 return ff_dualinput_request_frame(&b->dinput, outlink);
429 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
431 BlendContext *b = inlink->dst->priv;
432 return ff_dualinput_filter_frame(&b->dinput, inlink, buf);
435 static const AVFilterPad blend_inputs[] = {
438 .type = AVMEDIA_TYPE_VIDEO,
439 .filter_frame = filter_frame,
442 .type = AVMEDIA_TYPE_VIDEO,
443 .filter_frame = filter_frame,
448 static const AVFilterPad blend_outputs[] = {
451 .type = AVMEDIA_TYPE_VIDEO,
452 .config_props = config_output,
453 .request_frame = request_frame,
458 AVFilter ff_vf_blend = {
460 .description = NULL_IF_CONFIG_SMALL("Blend two video frames into each other."),
463 .priv_size = sizeof(BlendContext),
464 .query_formats = query_formats,
465 .inputs = blend_inputs,
466 .outputs = blend_outputs,
467 .priv_class = &blend_class,
468 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,