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 ** Hardware accelerated common filters based on Intel Quick Sync Video VPP
26 #include "libavutil/opt.h"
27 #include "libavutil/eval.h"
28 #include "libavutil/avassert.h"
29 #include "libavutil/pixdesc.h"
30 #include "libavutil/mathematics.h"
36 #include "libavcodec/avcodec.h"
37 #include "libavformat/avformat.h"
40 #include "transpose.h"
42 #define OFFSET(x) offsetof(VPPContext, x)
43 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM)
45 /* number of video enhancement filters */
46 #define ENH_FILTERS_COUNT (7)
47 #define QSV_HAVE_ROTATION QSV_VERSION_ATLEAST(1, 17)
48 #define QSV_HAVE_MIRRORING QSV_VERSION_ATLEAST(1, 19)
50 typedef struct VPPContext{
55 /* Video Enhancement Algorithms */
56 mfxExtVPPDeinterlacing deinterlace_conf;
57 mfxExtVPPFrameRateConversion frc_conf;
58 mfxExtVPPDenoise denoise_conf;
59 mfxExtVPPDetail detail_conf;
60 mfxExtVPPProcAmp procamp_conf;
61 mfxExtVPPRotation rotation_conf;
62 mfxExtVPPMirroring mirroring_conf;
67 * Output sw format. AV_PIX_FMT_NONE for no conversion.
69 enum AVPixelFormat out_format;
71 AVRational framerate; /* target framerate */
72 int use_frc; /* use framerate conversion */
73 int deinterlace; /* deinterlace mode : 0=off, 1=bob, 2=advanced */
74 int denoise; /* Enable Denoise algorithm. Value [0, 100] */
75 int detail; /* Enable Detail Enhancement algorithm. */
76 /* Level is the optional, value [0, 100] */
77 int use_crop; /* 1 = use crop; 0=none */
84 int rotate; /* rotate angle : [0, 90, 180, 270] */
85 int hflip; /* flip mode : 0 = off, 1 = HORIZONTAL flip */
87 /* param for the procamp */
88 int procamp; /* enable procamp */
94 char *cx, *cy, *cw, *ch;
96 char *output_format_str;
102 static const AVOption options[] = {
103 { "deinterlace", "deinterlace mode: 0=off, 1=bob, 2=advanced", OFFSET(deinterlace), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, MFX_DEINTERLACING_ADVANCED, .flags = FLAGS, "deinterlace" },
104 { "bob", "Bob deinterlace mode.", 0, AV_OPT_TYPE_CONST, { .i64 = MFX_DEINTERLACING_BOB }, .flags = FLAGS, "deinterlace" },
105 { "advanced", "Advanced deinterlace mode. ", 0, AV_OPT_TYPE_CONST, { .i64 = MFX_DEINTERLACING_ADVANCED }, .flags = FLAGS, "deinterlace" },
107 { "denoise", "denoise level [0, 100]", OFFSET(denoise), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 100, .flags = FLAGS },
108 { "detail", "enhancement level [0, 100]", OFFSET(detail), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 100, .flags = FLAGS },
109 { "framerate", "output framerate", OFFSET(framerate), AV_OPT_TYPE_RATIONAL, { .dbl = 0.0 },0, DBL_MAX, .flags = FLAGS },
110 { "procamp", "Enable ProcAmp", OFFSET(procamp), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, .flags = FLAGS},
111 { "hue", "ProcAmp hue", OFFSET(hue), AV_OPT_TYPE_FLOAT, { .dbl = 0.0 }, -180.0, 180.0, .flags = FLAGS},
112 { "saturation", "ProcAmp saturation", OFFSET(saturation), AV_OPT_TYPE_FLOAT, { .dbl = 1.0 }, 0.0, 10.0, .flags = FLAGS},
113 { "contrast", "ProcAmp contrast", OFFSET(contrast), AV_OPT_TYPE_FLOAT, { .dbl = 1.0 }, 0.0, 10.0, .flags = FLAGS},
114 { "brightness", "ProcAmp brightness", OFFSET(brightness), AV_OPT_TYPE_FLOAT, { .dbl = 0.0 }, -100.0, 100.0, .flags = FLAGS},
116 { "transpose", "set transpose direction", OFFSET(transpose), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 6, FLAGS, "transpose"},
117 { "cclock_hflip", "rotate counter-clockwise with horizontal flip", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CCLOCK_FLIP }, .flags=FLAGS, .unit = "transpose" },
118 { "clock", "rotate clockwise", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CLOCK }, .flags=FLAGS, .unit = "transpose" },
119 { "cclock", "rotate counter-clockwise", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CCLOCK }, .flags=FLAGS, .unit = "transpose" },
120 { "clock_hflip", "rotate clockwise with horizontal flip", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CLOCK_FLIP }, .flags=FLAGS, .unit = "transpose" },
121 { "reversal", "rotate by half-turn", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_REVERSAL }, .flags=FLAGS, .unit = "transpose" },
122 { "hflip", "flip horizontally", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_HFLIP }, .flags=FLAGS, .unit = "transpose" },
123 { "vflip", "flip vertically", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_VFLIP }, .flags=FLAGS, .unit = "transpose" },
125 { "cw", "set the width crop area expression", OFFSET(cw), AV_OPT_TYPE_STRING, { .str = "iw" }, 0, 0, FLAGS },
126 { "ch", "set the height crop area expression", OFFSET(ch), AV_OPT_TYPE_STRING, { .str = "ih" }, 0, 0, FLAGS },
127 { "cx", "set the x crop area expression", OFFSET(cx), AV_OPT_TYPE_STRING, { .str = "(in_w-out_w)/2" }, 0, 0, FLAGS },
128 { "cy", "set the y crop area expression", OFFSET(cy), AV_OPT_TYPE_STRING, { .str = "(in_h-out_h)/2" }, 0, 0, FLAGS },
130 { "w", "Output video width", OFFSET(ow), AV_OPT_TYPE_STRING, { .str="cw" }, 0, 255, .flags = FLAGS },
131 { "width", "Output video width", OFFSET(ow), AV_OPT_TYPE_STRING, { .str="cw" }, 0, 255, .flags = FLAGS },
132 { "h", "Output video height", OFFSET(oh), AV_OPT_TYPE_STRING, { .str="w*ch/cw" }, 0, 255, .flags = FLAGS },
133 { "height", "Output video height", OFFSET(oh), AV_OPT_TYPE_STRING, { .str="w*ch/cw" }, 0, 255, .flags = FLAGS },
134 { "format", "Output pixel format", OFFSET(output_format_str), AV_OPT_TYPE_STRING, { .str = "same" }, .flags = FLAGS },
135 { "async_depth", "Internal parallelization depth, the higher the value the higher the latency.", OFFSET(async_depth), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, .flags = FLAGS },
140 static const char *const var_names[] = {
155 VAR_oW, VAR_OUT_W, VAR_W,
156 VAR_oH, VAR_OUT_H, VAR_H,
164 static int eval_expr(AVFilterContext *ctx)
166 #define PASS_EXPR(e, s) {\
167 ret = av_expr_parse(&e, s, var_names, NULL, NULL, NULL, NULL, 0, ctx); \
169 av_log(ctx, AV_LOG_ERROR, "Error when passing '%s'.\n", s);\
173 #define CALC_EXPR(e, v, i) {\
174 i = v = av_expr_eval(e, var_values, NULL); \
176 VPPContext *vpp = ctx->priv;
177 double var_values[VAR_VARS_NB] = { NAN };
178 AVExpr *w_expr = NULL, *h_expr = NULL;
179 AVExpr *cw_expr = NULL, *ch_expr = NULL;
180 AVExpr *cx_expr = NULL, *cy_expr = NULL;
183 PASS_EXPR(cw_expr, vpp->cw);
184 PASS_EXPR(ch_expr, vpp->ch);
186 PASS_EXPR(w_expr, vpp->ow);
187 PASS_EXPR(h_expr, vpp->oh);
189 PASS_EXPR(cx_expr, vpp->cx);
190 PASS_EXPR(cy_expr, vpp->cy);
193 var_values[VAR_IN_W] = ctx->inputs[0]->w;
196 var_values[VAR_IN_H] = ctx->inputs[0]->h;
199 CALC_EXPR(cw_expr, var_values[CW], vpp->crop_w);
200 CALC_EXPR(ch_expr, var_values[CH], vpp->crop_h);
202 /* calc again in case cw is relative to ch */
203 CALC_EXPR(cw_expr, var_values[CW], vpp->crop_w);
206 var_values[VAR_OUT_W] = var_values[VAR_oW] = var_values[VAR_W],
209 var_values[VAR_OUT_H] = var_values[VAR_oH] = var_values[VAR_H],
212 /* calc again in case ow is relative to oh */
214 var_values[VAR_OUT_W] = var_values[VAR_oW] = var_values[VAR_W],
218 CALC_EXPR(cx_expr, var_values[CX], vpp->crop_x);
219 CALC_EXPR(cy_expr, var_values[CY], vpp->crop_y);
221 /* calc again in case cx is relative to cy */
222 CALC_EXPR(cx_expr, var_values[CX], vpp->crop_x);
224 if ((vpp->crop_w != var_values[VAR_iW]) || (vpp->crop_h != var_values[VAR_iH]))
228 av_expr_free(w_expr);
229 av_expr_free(h_expr);
230 av_expr_free(cw_expr);
231 av_expr_free(ch_expr);
232 av_expr_free(cx_expr);
233 av_expr_free(cy_expr);
240 static av_cold int vpp_init(AVFilterContext *ctx)
242 VPPContext *vpp = ctx->priv;
244 if (!strcmp(vpp->output_format_str, "same")) {
245 vpp->out_format = AV_PIX_FMT_NONE;
247 vpp->out_format = av_get_pix_fmt(vpp->output_format_str);
248 if (vpp->out_format == AV_PIX_FMT_NONE) {
249 av_log(ctx, AV_LOG_ERROR, "Unrecognized output pixel format: %s\n", vpp->output_format_str);
250 return AVERROR(EINVAL);
257 static int config_input(AVFilterLink *inlink)
259 AVFilterContext *ctx = inlink->dst;
260 VPPContext *vpp = ctx->priv;
263 if (vpp->framerate.den == 0 || vpp->framerate.num == 0)
264 vpp->framerate = inlink->frame_rate;
266 if (av_cmp_q(vpp->framerate, inlink->frame_rate))
269 ret = eval_expr(ctx);
271 av_log(ctx, AV_LOG_ERROR, "Fail to eval expr.\n");
275 if (vpp->out_height == 0 || vpp->out_width == 0) {
276 vpp->out_width = inlink->w;
277 vpp->out_height = inlink->h;
281 vpp->crop_x = FFMAX(vpp->crop_x, 0);
282 vpp->crop_y = FFMAX(vpp->crop_y, 0);
284 if(vpp->crop_w + vpp->crop_x > inlink->w)
285 vpp->crop_x = inlink->w - vpp->crop_w;
286 if(vpp->crop_h + vpp->crop_y > inlink->h)
287 vpp->crop_y = inlink->h - vpp->crop_h;
293 static int config_output(AVFilterLink *outlink)
295 AVFilterContext *ctx = outlink->src;
296 VPPContext *vpp = ctx->priv;
297 QSVVPPParam param = { NULL };
298 QSVVPPCrop crop = { 0 };
299 mfxExtBuffer *ext_buf[ENH_FILTERS_COUNT];
300 AVFilterLink *inlink = ctx->inputs[0];
301 enum AVPixelFormat in_format;
303 outlink->w = vpp->out_width;
304 outlink->h = vpp->out_height;
305 outlink->frame_rate = vpp->framerate;
306 outlink->time_base = av_inv_q(vpp->framerate);
308 param.filter_frame = NULL;
309 param.num_ext_buf = 0;
310 param.ext_buf = ext_buf;
311 param.async_depth = vpp->async_depth;
313 if (inlink->format == AV_PIX_FMT_QSV) {
314 if (!inlink->hw_frames_ctx || !inlink->hw_frames_ctx->data)
315 return AVERROR(EINVAL);
317 in_format = ((AVHWFramesContext*)inlink->hw_frames_ctx->data)->sw_format;
319 in_format = inlink->format;
321 if (vpp->out_format == AV_PIX_FMT_NONE)
322 vpp->out_format = in_format;
323 param.out_sw_format = vpp->out_format;
327 crop.x = vpp->crop_x;
328 crop.y = vpp->crop_y;
329 crop.w = vpp->crop_w;
330 crop.h = vpp->crop_h;
336 if (vpp->deinterlace) {
337 memset(&vpp->deinterlace_conf, 0, sizeof(mfxExtVPPDeinterlacing));
338 vpp->deinterlace_conf.Header.BufferId = MFX_EXTBUFF_VPP_DEINTERLACING;
339 vpp->deinterlace_conf.Header.BufferSz = sizeof(mfxExtVPPDeinterlacing);
340 vpp->deinterlace_conf.Mode = vpp->deinterlace == 1 ?
341 MFX_DEINTERLACING_BOB : MFX_DEINTERLACING_ADVANCED;
343 param.ext_buf[param.num_ext_buf++] = (mfxExtBuffer*)&vpp->deinterlace_conf;
347 memset(&vpp->frc_conf, 0, sizeof(mfxExtVPPFrameRateConversion));
348 vpp->frc_conf.Header.BufferId = MFX_EXTBUFF_VPP_FRAME_RATE_CONVERSION;
349 vpp->frc_conf.Header.BufferSz = sizeof(mfxExtVPPFrameRateConversion);
350 vpp->frc_conf.Algorithm = MFX_FRCALGM_DISTRIBUTED_TIMESTAMP;
352 param.ext_buf[param.num_ext_buf++] = (mfxExtBuffer*)&vpp->frc_conf;
356 memset(&vpp->denoise_conf, 0, sizeof(mfxExtVPPDenoise));
357 vpp->denoise_conf.Header.BufferId = MFX_EXTBUFF_VPP_DENOISE;
358 vpp->denoise_conf.Header.BufferSz = sizeof(mfxExtVPPDenoise);
359 vpp->denoise_conf.DenoiseFactor = vpp->denoise;
361 param.ext_buf[param.num_ext_buf++] = (mfxExtBuffer*)&vpp->denoise_conf;
365 memset(&vpp->detail_conf, 0, sizeof(mfxExtVPPDetail));
366 vpp->detail_conf.Header.BufferId = MFX_EXTBUFF_VPP_DETAIL;
367 vpp->detail_conf.Header.BufferSz = sizeof(mfxExtVPPDetail);
368 vpp->detail_conf.DetailFactor = vpp->detail;
370 param.ext_buf[param.num_ext_buf++] = (mfxExtBuffer*)&vpp->detail_conf;
374 memset(&vpp->procamp_conf, 0, sizeof(mfxExtVPPProcAmp));
375 vpp->procamp_conf.Header.BufferId = MFX_EXTBUFF_VPP_PROCAMP;
376 vpp->procamp_conf.Header.BufferSz = sizeof(mfxExtVPPProcAmp);
377 vpp->procamp_conf.Hue = vpp->hue;
378 vpp->procamp_conf.Saturation = vpp->saturation;
379 vpp->procamp_conf.Contrast = vpp->contrast;
380 vpp->procamp_conf.Brightness = vpp->brightness;
382 param.ext_buf[param.num_ext_buf++] = (mfxExtBuffer*)&vpp->procamp_conf;
385 if (vpp->transpose >= 0) {
386 #ifdef QSV_HAVE_ROTATION
387 switch (vpp->transpose) {
388 case TRANSPOSE_CCLOCK_FLIP:
389 vpp->rotate = MFX_ANGLE_270;
390 vpp->hflip = MFX_MIRRORING_HORIZONTAL;
392 case TRANSPOSE_CLOCK:
393 vpp->rotate = MFX_ANGLE_90;
394 vpp->hflip = MFX_MIRRORING_DISABLED;
396 case TRANSPOSE_CCLOCK:
397 vpp->rotate = MFX_ANGLE_270;
398 vpp->hflip = MFX_MIRRORING_DISABLED;
400 case TRANSPOSE_CLOCK_FLIP:
401 vpp->rotate = MFX_ANGLE_90;
402 vpp->hflip = MFX_MIRRORING_HORIZONTAL;
404 case TRANSPOSE_REVERSAL:
405 vpp->rotate = MFX_ANGLE_180;
406 vpp->hflip = MFX_MIRRORING_DISABLED;
408 case TRANSPOSE_HFLIP:
409 vpp->rotate = MFX_ANGLE_0;
410 vpp->hflip = MFX_MIRRORING_HORIZONTAL;
412 case TRANSPOSE_VFLIP:
413 vpp->rotate = MFX_ANGLE_180;
414 vpp->hflip = MFX_MIRRORING_HORIZONTAL;
417 av_log(ctx, AV_LOG_ERROR, "Failed to set transpose mode to %d.\n", vpp->transpose);
418 return AVERROR(EINVAL);
421 av_log(ctx, AV_LOG_WARNING, "The QSV VPP transpose option is "
422 "not supported with this MSDK version.\n");
428 #ifdef QSV_HAVE_ROTATION
429 memset(&vpp->rotation_conf, 0, sizeof(mfxExtVPPRotation));
430 vpp->rotation_conf.Header.BufferId = MFX_EXTBUFF_VPP_ROTATION;
431 vpp->rotation_conf.Header.BufferSz = sizeof(mfxExtVPPRotation);
432 vpp->rotation_conf.Angle = vpp->rotate;
434 if (MFX_ANGLE_90 == vpp->rotate || MFX_ANGLE_270 == vpp->rotate) {
435 FFSWAP(int, vpp->out_width, vpp->out_height);
436 FFSWAP(int, outlink->w, outlink->h);
437 av_log(ctx, AV_LOG_DEBUG, "Swap width and height for clock/cclock rotation.\n");
440 param.ext_buf[param.num_ext_buf++] = (mfxExtBuffer*)&vpp->rotation_conf;
442 av_log(ctx, AV_LOG_WARNING, "The QSV VPP rotate option is "
443 "not supported with this MSDK version.\n");
449 #ifdef QSV_HAVE_MIRRORING
450 memset(&vpp->mirroring_conf, 0, sizeof(mfxExtVPPMirroring));
451 vpp->mirroring_conf.Header.BufferId = MFX_EXTBUFF_VPP_MIRRORING;
452 vpp->mirroring_conf.Header.BufferSz = sizeof(mfxExtVPPMirroring);
453 vpp->mirroring_conf.Type = vpp->hflip;
455 param.ext_buf[param.num_ext_buf++] = (mfxExtBuffer*)&vpp->mirroring_conf;
457 av_log(ctx, AV_LOG_WARNING, "The QSV VPP hflip option is "
458 "not supported with this MSDK version.\n");
463 if (vpp->use_frc || vpp->use_crop || vpp->deinterlace || vpp->denoise ||
464 vpp->detail || vpp->procamp || vpp->rotate || vpp->hflip ||
465 inlink->w != outlink->w || inlink->h != outlink->h || in_format != vpp->out_format)
466 return ff_qsvvpp_create(ctx, &vpp->qsv, ¶m);
468 av_log(ctx, AV_LOG_VERBOSE, "qsv vpp pass through mode.\n");
469 if (inlink->hw_frames_ctx)
470 outlink->hw_frames_ctx = av_buffer_ref(inlink->hw_frames_ctx);
476 static int activate(AVFilterContext *ctx)
478 AVFilterLink *inlink = ctx->inputs[0];
479 AVFilterLink *outlink = ctx->outputs[0];
480 VPPContext *s =ctx->priv;
481 QSVVPPContext *qsv = s->qsv;
486 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
489 ret = ff_inlink_consume_frame(inlink, &in);
493 if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
494 if (status == AVERROR_EOF) {
503 ret = ff_qsvvpp_filter_frame(qsv, inlink, in);
507 ff_outlink_set_status(outlink, status, pts);
511 if (qsv->got_frame) {
518 if (in->pts != AV_NOPTS_VALUE)
519 in->pts = av_rescale_q(in->pts, inlink->time_base, outlink->time_base);
521 ret = ff_filter_frame(outlink, in);
527 ff_outlink_set_status(outlink, status, pts);
530 FF_FILTER_FORWARD_WANTED(outlink, inlink);
533 return FFERROR_NOT_READY;
536 static int query_formats(AVFilterContext *ctx)
539 static const enum AVPixelFormat in_pix_fmts[] = {
547 static const enum AVPixelFormat out_pix_fmts[] = {
554 ret = ff_formats_ref(ff_make_format_list(in_pix_fmts),
555 &ctx->inputs[0]->outcfg.formats);
558 return ff_formats_ref(ff_make_format_list(out_pix_fmts),
559 &ctx->outputs[0]->incfg.formats);
562 static av_cold void vpp_uninit(AVFilterContext *ctx)
564 VPPContext *vpp = ctx->priv;
566 ff_qsvvpp_free(&vpp->qsv);
569 static const AVClass vpp_class = {
570 .class_name = "vpp_qsv",
571 .item_name = av_default_item_name,
573 .version = LIBAVUTIL_VERSION_INT,
576 static const AVFilterPad vpp_inputs[] = {
579 .type = AVMEDIA_TYPE_VIDEO,
580 .config_props = config_input,
585 static const AVFilterPad vpp_outputs[] = {
588 .type = AVMEDIA_TYPE_VIDEO,
589 .config_props = config_output,
594 AVFilter ff_vf_vpp_qsv = {
596 .description = NULL_IF_CONFIG_SMALL("Quick Sync Video VPP."),
597 .priv_size = sizeof(VPPContext),
598 .query_formats = query_formats,
600 .uninit = vpp_uninit,
601 .inputs = vpp_inputs,
602 .outputs = vpp_outputs,
603 .activate = activate,
604 .priv_class = &vpp_class,
605 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,