2 * This file is part of FFmpeg.
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 * A hardware accelerated overlay filter based on Intel Quick Sync Video VPP
24 #include "libavutil/opt.h"
25 #include "libavutil/common.h"
26 #include "libavutil/pixdesc.h"
27 #include "libavutil/eval.h"
28 #include "libavutil/hwcontext.h"
29 #include "libavutil/avstring.h"
30 #include "libavutil/avassert.h"
31 #include "libavutil/imgutils.h"
32 #include "libavutil/mathematics.h"
44 #define OFFSET(x) offsetof(QSVOverlayContext, x)
45 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM)
52 VAR_OVERLAY_X, VAR_OX,
53 VAR_OVERLAY_Y, VAR_OY,
54 VAR_OVERLAY_W, VAR_OW,
55 VAR_OVERLAY_H, VAR_OH,
64 typedef struct QSVOverlayContext {
68 QSVVPPParam qsv_param;
69 mfxExtVPPComposite comp_conf;
70 double var_values[VAR_VARS_NB];
72 char *overlay_ox, *overlay_oy, *overlay_ow, *overlay_oh;
73 uint16_t overlay_alpha, overlay_pixel_alpha;
75 enum EOFAction eof_action; /* action to take on EOF from source */
78 AVFrame *over_prev, *over_next;
81 static const char *const var_names[] = {
82 "main_w", "W", /* input width of the main layer */
83 "main_h", "H", /* input height of the main layer */
84 "overlay_iw", /* input width of the overlay layer */
85 "overlay_ih", /* input height of the overlay layer */
86 "overlay_x", "x", /* x position of the overlay layer inside of main */
87 "overlay_y", "y", /* y position of the overlay layer inside of main */
88 "overlay_w", "w", /* output width of overlay layer */
89 "overlay_h", "h", /* output height of overlay layer */
93 static const AVOption options[] = {
94 { "x", "Overlay x position", OFFSET(overlay_ox), AV_OPT_TYPE_STRING, { .str="0"}, 0, 255, .flags = FLAGS},
95 { "y", "Overlay y position", OFFSET(overlay_oy), AV_OPT_TYPE_STRING, { .str="0"}, 0, 255, .flags = FLAGS},
96 { "w", "Overlay width", OFFSET(overlay_ow), AV_OPT_TYPE_STRING, { .str="overlay_iw"}, 0, 255, .flags = FLAGS},
97 { "h", "Overlay height", OFFSET(overlay_oh), AV_OPT_TYPE_STRING, { .str="overlay_ih*w/overlay_iw"}, 0, 255, .flags = FLAGS},
98 { "alpha", "Overlay global alpha", OFFSET(overlay_alpha), AV_OPT_TYPE_INT, { .i64 = 255}, 0, 255, .flags = FLAGS},
99 { "eof_action", "Action to take when encountering EOF from secondary input ",
100 OFFSET(eof_action), AV_OPT_TYPE_INT, { .i64 = EOF_ACTION_REPEAT },
101 EOF_ACTION_REPEAT, EOF_ACTION_ENDALL, .flags = FLAGS, "eof_action" },
102 { "repeat", "Repeat the previous frame.", 0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_REPEAT }, .flags = FLAGS, "eof_action" },
103 { "endall", "End both streams.", 0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_ENDALL }, .flags = FLAGS, "eof_action" },
107 static int eval_expr(AVFilterContext *ctx)
109 QSVOverlayContext *vpp = ctx->priv;
110 double *var_values = vpp->var_values;
112 AVExpr *ox_expr = NULL, *oy_expr = NULL;
113 AVExpr *ow_expr = NULL, *oh_expr = NULL;
115 #define PASS_EXPR(e, s) {\
116 ret = av_expr_parse(&e, s, var_names, NULL, NULL, NULL, NULL, 0, ctx); \
118 av_log(ctx, AV_LOG_ERROR, "Error when passing '%s'.\n", s);\
122 PASS_EXPR(ox_expr, vpp->overlay_ox);
123 PASS_EXPR(oy_expr, vpp->overlay_oy);
124 PASS_EXPR(ow_expr, vpp->overlay_ow);
125 PASS_EXPR(oh_expr, vpp->overlay_oh);
128 var_values[VAR_OVERLAY_W] =
129 var_values[VAR_OW] = av_expr_eval(ow_expr, var_values, NULL);
130 var_values[VAR_OVERLAY_H] =
131 var_values[VAR_OH] = av_expr_eval(oh_expr, var_values, NULL);
133 /* calc again in case ow is relative to oh */
134 var_values[VAR_OVERLAY_W] =
135 var_values[VAR_OW] = av_expr_eval(ow_expr, var_values, NULL);
137 var_values[VAR_OVERLAY_X] =
138 var_values[VAR_OX] = av_expr_eval(ox_expr, var_values, NULL);
139 var_values[VAR_OVERLAY_Y] =
140 var_values[VAR_OY] = av_expr_eval(oy_expr, var_values, NULL);
142 /* calc again in case ox is relative to oy */
143 var_values[VAR_OVERLAY_X] =
144 var_values[VAR_OX] = av_expr_eval(ox_expr, var_values, NULL);
146 /* calc overlay_w and overlay_h again incase relative to ox,oy */
147 var_values[VAR_OVERLAY_W] =
148 var_values[VAR_OW] = av_expr_eval(ow_expr, var_values, NULL);
149 var_values[VAR_OVERLAY_H] =
150 var_values[VAR_OH] = av_expr_eval(oh_expr, var_values, NULL);
151 var_values[VAR_OVERLAY_W] =
152 var_values[VAR_OW] = av_expr_eval(ow_expr, var_values, NULL);
155 av_expr_free(ox_expr);
156 av_expr_free(oy_expr);
157 av_expr_free(ow_expr);
158 av_expr_free(oh_expr);
163 static int have_alpha_planar(AVFilterLink *link)
165 enum AVPixelFormat pix_fmt;
166 const AVPixFmtDescriptor *desc;
167 AVHWFramesContext *fctx;
169 if (link->format == AV_PIX_FMT_QSV) {
170 fctx = (AVHWFramesContext *)link->hw_frames_ctx->data;
171 pix_fmt = fctx->sw_format;
174 desc = av_pix_fmt_desc_get(pix_fmt);
178 return !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA);
181 static int config_main_input(AVFilterLink *inlink)
183 AVFilterContext *ctx = inlink->dst;
184 QSVOverlayContext *vpp = ctx->priv;
185 mfxVPPCompInputStream *st = &vpp->comp_conf.InputStream[0];
187 av_log(ctx, AV_LOG_DEBUG, "Input[%d] is of %s.\n", FF_INLINK_IDX(inlink),
188 av_get_pix_fmt_name(inlink->format));
190 vpp->var_values[VAR_MAIN_iW] =
191 vpp->var_values[VAR_MW] = inlink->w;
192 vpp->var_values[VAR_MAIN_iH] =
193 vpp->var_values[VAR_MH] = inlink->h;
197 st->DstW = inlink->w;
198 st->DstH = inlink->h;
199 st->GlobalAlphaEnable = 0;
200 st->PixelAlphaEnable = 0;
205 static int config_overlay_input(AVFilterLink *inlink)
207 AVFilterContext *ctx = inlink->dst;
208 QSVOverlayContext *vpp = ctx->priv;
209 mfxVPPCompInputStream *st = &vpp->comp_conf.InputStream[1];
212 av_log(ctx, AV_LOG_DEBUG, "Input[%d] is of %s.\n", FF_INLINK_IDX(inlink),
213 av_get_pix_fmt_name(inlink->format));
215 vpp->var_values[VAR_OVERLAY_iW] = inlink->w;
216 vpp->var_values[VAR_OVERLAY_iH] = inlink->h;
218 ret = eval_expr(ctx);
222 st->DstX = vpp->var_values[VAR_OX];
223 st->DstY = vpp->var_values[VAR_OY];
224 st->DstW = vpp->var_values[VAR_OW];
225 st->DstH = vpp->var_values[VAR_OH];
226 st->GlobalAlpha = vpp->overlay_alpha;
227 st->GlobalAlphaEnable = (st->GlobalAlpha < 255);
228 st->PixelAlphaEnable = have_alpha_planar(inlink);
233 static int config_output(AVFilterLink *outlink)
235 AVFilterContext *ctx = outlink->src;
236 QSVOverlayContext *vpp = ctx->priv;
237 AVFilterLink *in0 = ctx->inputs[0];
238 AVFilterLink *in1 = ctx->inputs[1];
240 av_log(ctx, AV_LOG_DEBUG, "Output is of %s.\n", av_get_pix_fmt_name(outlink->format));
241 if ((in0->format == AV_PIX_FMT_QSV && in1->format != AV_PIX_FMT_QSV) ||
242 (in0->format != AV_PIX_FMT_QSV && in1->format == AV_PIX_FMT_QSV)) {
243 av_log(ctx, AV_LOG_ERROR, "Mixing hardware and software pixel formats is not supported.\n");
244 return AVERROR(EINVAL);
245 } else if (in0->format == AV_PIX_FMT_QSV) {
246 AVHWFramesContext *hw_frame0 = (AVHWFramesContext *)in0->hw_frames_ctx->data;
247 AVHWFramesContext *hw_frame1 = (AVHWFramesContext *)in1->hw_frames_ctx->data;
249 if (hw_frame0->device_ctx != hw_frame1->device_ctx) {
250 av_log(ctx, AV_LOG_ERROR, "Inputs with different underlying QSV devices are forbidden.\n");
251 return AVERROR(EINVAL);
255 outlink->w = vpp->var_values[VAR_MW];
256 outlink->h = vpp->var_values[VAR_MH];
257 outlink->frame_rate = in0->frame_rate;
258 outlink->time_base = av_inv_q(outlink->frame_rate);
260 return ff_qsvvpp_create(ctx, &vpp->qsv, &vpp->qsv_param);
263 static int blend_frame(AVFilterContext *ctx, AVFrame *mpic, AVFrame *opic)
266 QSVOverlayContext *vpp = ctx->priv;
267 AVFrame *opic_copy = NULL;
269 ret = ff_qsvvpp_filter_frame(vpp->qsv, ctx->inputs[0], mpic);
270 if (ret == 0 || ret == AVERROR(EAGAIN)) {
271 /* Reference the overlay frame. Because:
272 * 1. ff_qsvvpp_filter_frame will take control of the given frame
273 * 2. We need to repeat the overlay frame when 2nd input goes into EOF
275 opic_copy = av_frame_clone(opic);
277 return AVERROR(ENOMEM);
279 ret = ff_qsvvpp_filter_frame(vpp->qsv, ctx->inputs[1], opic_copy);
285 static int handle_overlay_eof(AVFilterContext *ctx)
288 QSVOverlayContext *s = ctx->priv;
289 /* Repeat previous frame on secondary input */
290 if (s->over_prev && s->eof_action == EOF_ACTION_REPEAT)
291 ret = blend_frame(ctx, s->main, s->over_prev);
292 /* End both streams */
293 else if (s->eof_action == EOF_ACTION_ENDALL)
301 static int request_frame(AVFilterLink *outlink)
303 AVFilterContext *ctx = outlink->src;
304 QSVOverlayContext *s = ctx->priv;
305 AVRational tb_main = ctx->inputs[MAIN]->time_base;
306 AVRational tb_over = ctx->inputs[OVERLAY]->time_base;
309 /* get a frame on the main input */
311 ret = ff_request_frame(ctx->inputs[MAIN]);
316 /* get a new frame on the overlay input, on EOF check setting 'eof_action' */
318 ret = ff_request_frame(ctx->inputs[OVERLAY]);
319 if (ret == AVERROR_EOF)
320 return handle_overlay_eof(ctx);
325 while (s->main->pts != AV_NOPTS_VALUE &&
326 s->over_next->pts != AV_NOPTS_VALUE &&
327 av_compare_ts(s->over_next->pts, tb_over, s->main->pts, tb_main) < 0) {
328 av_frame_free(&s->over_prev);
329 FFSWAP(AVFrame*, s->over_prev, s->over_next);
331 ret = ff_request_frame(ctx->inputs[OVERLAY]);
332 if (ret == AVERROR_EOF)
333 return handle_overlay_eof(ctx);
338 if (s->main->pts == AV_NOPTS_VALUE ||
339 s->over_next->pts == AV_NOPTS_VALUE ||
340 !av_compare_ts(s->over_next->pts, tb_over, s->main->pts, tb_main)) {
341 ret = blend_frame(ctx, s->main, s->over_next);
342 av_frame_free(&s->over_prev);
343 FFSWAP(AVFrame*, s->over_prev, s->over_next);
344 } else if (s->over_prev) {
345 ret = blend_frame(ctx, s->main, s->over_prev);
347 av_frame_free(&s->main);
348 ret = AVERROR(EAGAIN);
356 static int filter_frame_main(AVFilterLink *inlink, AVFrame *frame)
358 QSVOverlayContext *s = inlink->dst->priv;
360 av_assert0(!s->main);
366 static int filter_frame_overlay(AVFilterLink *inlink, AVFrame *frame)
368 QSVOverlayContext *s = inlink->dst->priv;
370 av_assert0(!s->over_next);
371 s->over_next = frame;
376 static int overlay_qsv_init(AVFilterContext *ctx)
378 QSVOverlayContext *vpp = ctx->priv;
380 /* fill composite config */
381 vpp->comp_conf.Header.BufferId = MFX_EXTBUFF_VPP_COMPOSITE;
382 vpp->comp_conf.Header.BufferSz = sizeof(vpp->comp_conf);
383 vpp->comp_conf.NumInputStream = ctx->nb_inputs;
384 vpp->comp_conf.InputStream = av_mallocz_array(ctx->nb_inputs,
385 sizeof(*vpp->comp_conf.InputStream));
386 if (!vpp->comp_conf.InputStream)
387 return AVERROR(ENOMEM);
389 /* initialize QSVVPP params */
390 vpp->qsv_param.filter_frame = NULL;
391 vpp->qsv_param.ext_buf = av_mallocz(sizeof(*vpp->qsv_param.ext_buf));
392 if (!vpp->qsv_param.ext_buf)
393 return AVERROR(ENOMEM);
395 vpp->qsv_param.ext_buf[0] = (mfxExtBuffer *)&vpp->comp_conf;
396 vpp->qsv_param.num_ext_buf = 1;
397 vpp->qsv_param.out_sw_format = AV_PIX_FMT_NV12;
398 vpp->qsv_param.num_crop = 0;
403 static void overlay_qsv_uninit(AVFilterContext *ctx)
405 QSVOverlayContext *vpp = ctx->priv;
407 av_frame_free(&vpp->main);
408 av_frame_free(&vpp->over_prev);
409 av_frame_free(&vpp->over_next);
410 ff_qsvvpp_free(&vpp->qsv);
411 av_freep(&vpp->comp_conf.InputStream);
412 av_freep(&vpp->qsv_param.ext_buf);
415 static int overlay_qsv_query_formats(AVFilterContext *ctx)
420 static const enum AVPixelFormat main_in_fmts[] = {
428 static const enum AVPixelFormat out_pix_fmts[] = {
434 for (i = 0; i < ctx->nb_inputs; i++) {
435 ret = ff_formats_ref(ff_make_format_list(main_in_fmts), &ctx->inputs[i]->out_formats);
440 ret = ff_formats_ref(ff_make_format_list(out_pix_fmts), &ctx->outputs[0]->in_formats);
447 static const AVClass overlay_qsv_class = {
448 .class_name = "overlay_qsv",
449 .item_name = av_default_item_name,
451 .version = LIBAVUTIL_VERSION_INT,
454 static const AVFilterPad overlay_qsv_inputs[] = {
457 .type = AVMEDIA_TYPE_VIDEO,
458 .filter_frame = filter_frame_main,
459 .config_props = config_main_input,
464 .type = AVMEDIA_TYPE_VIDEO,
465 .filter_frame = filter_frame_overlay,
466 .config_props = config_overlay_input,
472 static const AVFilterPad overlay_qsv_outputs[] = {
475 .type = AVMEDIA_TYPE_VIDEO,
476 .config_props = config_output,
477 .request_frame = request_frame,
482 AVFilter ff_vf_overlay_qsv = {
483 .name = "overlay_qsv",
484 .description = NULL_IF_CONFIG_SMALL("Quick Sync Video overlay."),
485 .priv_size = sizeof(QSVOverlayContext),
486 .query_formats = overlay_qsv_query_formats,
487 .init = overlay_qsv_init,
488 .uninit = overlay_qsv_uninit,
489 .inputs = overlay_qsv_inputs,
490 .outputs = overlay_qsv_outputs,
491 .priv_class = &overlay_qsv_class,
492 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,