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"
35 #include "libavcodec/avcodec.h"
36 #include "libavformat/avformat.h"
39 #include "transpose.h"
41 #define OFFSET(x) offsetof(VPPContext, x)
42 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM)
44 /* number of video enhancement filters */
45 #define ENH_FILTERS_COUNT (7)
46 #define QSV_HAVE_ROTATION QSV_VERSION_ATLEAST(1, 17)
47 #define QSV_HAVE_MIRRORING QSV_VERSION_ATLEAST(1, 19)
49 typedef struct VPPContext{
54 /* Video Enhancement Algorithms */
55 mfxExtVPPDeinterlacing deinterlace_conf;
56 mfxExtVPPFrameRateConversion frc_conf;
57 mfxExtVPPDenoise denoise_conf;
58 mfxExtVPPDetail detail_conf;
59 mfxExtVPPProcAmp procamp_conf;
60 mfxExtVPPRotation rotation_conf;
61 mfxExtVPPMirroring mirroring_conf;
66 * Output sw format. AV_PIX_FMT_NONE for no conversion.
68 enum AVPixelFormat out_format;
70 AVRational framerate; /* target framerate */
71 int use_frc; /* use framerate conversion */
72 int deinterlace; /* deinterlace mode : 0=off, 1=bob, 2=advanced */
73 int denoise; /* Enable Denoise algorithm. Value [0, 100] */
74 int detail; /* Enable Detail Enhancement algorithm. */
75 /* Level is the optional, value [0, 100] */
76 int use_crop; /* 1 = use crop; 0=none */
83 int rotate; /* rotate angle : [0, 90, 180, 270] */
84 int hflip; /* flip mode : 0 = off, 1 = HORIZONTAL flip */
86 /* param for the procamp */
87 int procamp; /* enable procamp */
93 char *cx, *cy, *cw, *ch;
95 char *output_format_str;
98 static const AVOption options[] = {
99 { "deinterlace", "deinterlace mode: 0=off, 1=bob, 2=advanced", OFFSET(deinterlace), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, MFX_DEINTERLACING_ADVANCED, .flags = FLAGS, "deinterlace" },
100 { "bob", "Bob deinterlace mode.", 0, AV_OPT_TYPE_CONST, { .i64 = MFX_DEINTERLACING_BOB }, .flags = FLAGS, "deinterlace" },
101 { "advanced", "Advanced deinterlace mode. ", 0, AV_OPT_TYPE_CONST, { .i64 = MFX_DEINTERLACING_ADVANCED }, .flags = FLAGS, "deinterlace" },
103 { "denoise", "denoise level [0, 100]", OFFSET(denoise), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 100, .flags = FLAGS },
104 { "detail", "enhancement level [0, 100]", OFFSET(detail), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 100, .flags = FLAGS },
105 { "framerate", "output framerate", OFFSET(framerate), AV_OPT_TYPE_RATIONAL, { .dbl = 0.0 },0, DBL_MAX, .flags = FLAGS },
106 { "procamp", "Enable ProcAmp", OFFSET(procamp), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, .flags = FLAGS},
107 { "hue", "ProcAmp hue", OFFSET(hue), AV_OPT_TYPE_FLOAT, { .dbl = 0.0 }, -180.0, 180.0, .flags = FLAGS},
108 { "saturation", "ProcAmp saturation", OFFSET(saturation), AV_OPT_TYPE_FLOAT, { .dbl = 1.0 }, 0.0, 10.0, .flags = FLAGS},
109 { "contrast", "ProcAmp contrast", OFFSET(contrast), AV_OPT_TYPE_FLOAT, { .dbl = 1.0 }, 0.0, 10.0, .flags = FLAGS},
110 { "brightness", "ProcAmp brightness", OFFSET(brightness), AV_OPT_TYPE_FLOAT, { .dbl = 0.0 }, -100.0, 100.0, .flags = FLAGS},
112 { "transpose", "set transpose direction", OFFSET(transpose), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 6, FLAGS, "transpose"},
113 { "cclock_hflip", "rotate counter-clockwise with horizontal flip", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CCLOCK_FLIP }, .flags=FLAGS, .unit = "transpose" },
114 { "clock", "rotate clockwise", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CLOCK }, .flags=FLAGS, .unit = "transpose" },
115 { "cclock", "rotate counter-clockwise", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CCLOCK }, .flags=FLAGS, .unit = "transpose" },
116 { "clock_hflip", "rotate clockwise with horizontal flip", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CLOCK_FLIP }, .flags=FLAGS, .unit = "transpose" },
117 { "reversal", "rotate by half-turn", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_REVERSAL }, .flags=FLAGS, .unit = "transpose" },
118 { "hflip", "flip horizontally", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_HFLIP }, .flags=FLAGS, .unit = "transpose" },
119 { "vflip", "flip vertically", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_VFLIP }, .flags=FLAGS, .unit = "transpose" },
121 { "cw", "set the width crop area expression", OFFSET(cw), AV_OPT_TYPE_STRING, { .str = "iw" }, CHAR_MIN, CHAR_MAX, FLAGS },
122 { "ch", "set the height crop area expression", OFFSET(ch), AV_OPT_TYPE_STRING, { .str = "ih" }, CHAR_MIN, CHAR_MAX, FLAGS },
123 { "cx", "set the x crop area expression", OFFSET(cx), AV_OPT_TYPE_STRING, { .str = "(in_w-out_w)/2" }, CHAR_MIN, CHAR_MAX, FLAGS },
124 { "cy", "set the y crop area expression", OFFSET(cy), AV_OPT_TYPE_STRING, { .str = "(in_h-out_h)/2" }, CHAR_MIN, CHAR_MAX, FLAGS },
126 { "w", "Output video width", OFFSET(ow), AV_OPT_TYPE_STRING, { .str="cw" }, 0, 255, .flags = FLAGS },
127 { "width", "Output video width", OFFSET(ow), AV_OPT_TYPE_STRING, { .str="cw" }, 0, 255, .flags = FLAGS },
128 { "h", "Output video height", OFFSET(oh), AV_OPT_TYPE_STRING, { .str="w*ch/cw" }, 0, 255, .flags = FLAGS },
129 { "height", "Output video height", OFFSET(oh), AV_OPT_TYPE_STRING, { .str="w*ch/cw" }, 0, 255, .flags = FLAGS },
130 { "format", "Output pixel format", OFFSET(output_format_str), AV_OPT_TYPE_STRING, { .str = "same" }, .flags = FLAGS },
135 static const char *const var_names[] = {
150 VAR_oW, VAR_OUT_W, VAR_W,
151 VAR_oH, VAR_OUT_H, VAR_H,
159 static int eval_expr(AVFilterContext *ctx)
161 #define PASS_EXPR(e, s) {\
162 ret = av_expr_parse(&e, s, var_names, NULL, NULL, NULL, NULL, 0, ctx); \
164 av_log(ctx, AV_LOG_ERROR, "Error when passing '%s'.\n", s);\
168 #define CALC_EXPR(e, v, i) {\
169 i = v = av_expr_eval(e, var_values, NULL); \
171 VPPContext *vpp = ctx->priv;
172 double var_values[VAR_VARS_NB] = { NAN };
173 AVExpr *w_expr = NULL, *h_expr = NULL;
174 AVExpr *cw_expr = NULL, *ch_expr = NULL;
175 AVExpr *cx_expr = NULL, *cy_expr = NULL;
178 PASS_EXPR(cw_expr, vpp->cw);
179 PASS_EXPR(ch_expr, vpp->ch);
181 PASS_EXPR(w_expr, vpp->ow);
182 PASS_EXPR(h_expr, vpp->oh);
184 PASS_EXPR(cx_expr, vpp->cx);
185 PASS_EXPR(cy_expr, vpp->cy);
188 var_values[VAR_IN_W] = ctx->inputs[0]->w;
191 var_values[VAR_IN_H] = ctx->inputs[0]->h;
194 CALC_EXPR(cw_expr, var_values[CW], vpp->crop_w);
195 CALC_EXPR(ch_expr, var_values[CH], vpp->crop_h);
197 /* calc again in case cw is relative to ch */
198 CALC_EXPR(cw_expr, var_values[CW], vpp->crop_w);
201 var_values[VAR_OUT_W] = var_values[VAR_oW] = var_values[VAR_W],
204 var_values[VAR_OUT_H] = var_values[VAR_oH] = var_values[VAR_H],
207 /* calc again in case ow is relative to oh */
209 var_values[VAR_OUT_W] = var_values[VAR_oW] = var_values[VAR_W],
213 CALC_EXPR(cx_expr, var_values[CX], vpp->crop_x);
214 CALC_EXPR(cy_expr, var_values[CY], vpp->crop_y);
216 /* calc again in case cx is relative to cy */
217 CALC_EXPR(cx_expr, var_values[CX], vpp->crop_x);
219 if ((vpp->crop_w != var_values[VAR_iW]) || (vpp->crop_h != var_values[VAR_iH]))
223 av_expr_free(w_expr);
224 av_expr_free(h_expr);
225 av_expr_free(cw_expr);
226 av_expr_free(ch_expr);
227 av_expr_free(cx_expr);
228 av_expr_free(cy_expr);
235 static av_cold int vpp_init(AVFilterContext *ctx)
237 VPPContext *vpp = ctx->priv;
239 if (!strcmp(vpp->output_format_str, "same")) {
240 vpp->out_format = AV_PIX_FMT_NONE;
242 vpp->out_format = av_get_pix_fmt(vpp->output_format_str);
243 if (vpp->out_format == AV_PIX_FMT_NONE) {
244 av_log(ctx, AV_LOG_ERROR, "Unrecognized output pixel format: %s\n", vpp->output_format_str);
245 return AVERROR(EINVAL);
252 static int config_input(AVFilterLink *inlink)
254 AVFilterContext *ctx = inlink->dst;
255 VPPContext *vpp = ctx->priv;
258 if (vpp->framerate.den == 0 || vpp->framerate.num == 0)
259 vpp->framerate = inlink->frame_rate;
261 if (av_cmp_q(vpp->framerate, inlink->frame_rate))
264 ret = eval_expr(ctx);
266 av_log(ctx, AV_LOG_ERROR, "Fail to eval expr.\n");
270 if (vpp->out_height == 0 || vpp->out_width == 0) {
271 vpp->out_width = inlink->w;
272 vpp->out_height = inlink->h;
276 vpp->crop_x = FFMAX(vpp->crop_x, 0);
277 vpp->crop_y = FFMAX(vpp->crop_y, 0);
279 if(vpp->crop_w + vpp->crop_x > inlink->w)
280 vpp->crop_x = inlink->w - vpp->crop_w;
281 if(vpp->crop_h + vpp->crop_y > inlink->h)
282 vpp->crop_y = inlink->h - vpp->crop_h;
288 static int config_output(AVFilterLink *outlink)
290 AVFilterContext *ctx = outlink->src;
291 VPPContext *vpp = ctx->priv;
292 QSVVPPParam param = { NULL };
293 QSVVPPCrop crop = { 0 };
294 mfxExtBuffer *ext_buf[ENH_FILTERS_COUNT];
295 AVFilterLink *inlink = ctx->inputs[0];
296 enum AVPixelFormat in_format;
298 outlink->w = vpp->out_width;
299 outlink->h = vpp->out_height;
300 outlink->frame_rate = vpp->framerate;
301 outlink->time_base = av_inv_q(vpp->framerate);
303 param.filter_frame = NULL;
304 param.num_ext_buf = 0;
305 param.ext_buf = ext_buf;
307 if (inlink->format == AV_PIX_FMT_QSV) {
308 if (!inlink->hw_frames_ctx || !inlink->hw_frames_ctx->data)
309 return AVERROR(EINVAL);
311 in_format = ((AVHWFramesContext*)inlink->hw_frames_ctx->data)->sw_format;
313 in_format = inlink->format;
315 param.out_sw_format = (vpp->out_format == AV_PIX_FMT_NONE) ? in_format : vpp->out_format;
319 crop.x = vpp->crop_x;
320 crop.y = vpp->crop_y;
321 crop.w = vpp->crop_w;
322 crop.h = vpp->crop_h;
328 if (vpp->deinterlace) {
329 memset(&vpp->deinterlace_conf, 0, sizeof(mfxExtVPPDeinterlacing));
330 vpp->deinterlace_conf.Header.BufferId = MFX_EXTBUFF_VPP_DEINTERLACING;
331 vpp->deinterlace_conf.Header.BufferSz = sizeof(mfxExtVPPDeinterlacing);
332 vpp->deinterlace_conf.Mode = vpp->deinterlace == 1 ?
333 MFX_DEINTERLACING_BOB : MFX_DEINTERLACING_ADVANCED;
335 param.ext_buf[param.num_ext_buf++] = (mfxExtBuffer*)&vpp->deinterlace_conf;
339 memset(&vpp->frc_conf, 0, sizeof(mfxExtVPPFrameRateConversion));
340 vpp->frc_conf.Header.BufferId = MFX_EXTBUFF_VPP_FRAME_RATE_CONVERSION;
341 vpp->frc_conf.Header.BufferSz = sizeof(mfxExtVPPFrameRateConversion);
342 vpp->frc_conf.Algorithm = MFX_FRCALGM_DISTRIBUTED_TIMESTAMP;
344 param.ext_buf[param.num_ext_buf++] = (mfxExtBuffer*)&vpp->frc_conf;
348 memset(&vpp->denoise_conf, 0, sizeof(mfxExtVPPDenoise));
349 vpp->denoise_conf.Header.BufferId = MFX_EXTBUFF_VPP_DENOISE;
350 vpp->denoise_conf.Header.BufferSz = sizeof(mfxExtVPPDenoise);
351 vpp->denoise_conf.DenoiseFactor = vpp->denoise;
353 param.ext_buf[param.num_ext_buf++] = (mfxExtBuffer*)&vpp->denoise_conf;
357 memset(&vpp->detail_conf, 0, sizeof(mfxExtVPPDetail));
358 vpp->detail_conf.Header.BufferId = MFX_EXTBUFF_VPP_DETAIL;
359 vpp->detail_conf.Header.BufferSz = sizeof(mfxExtVPPDetail);
360 vpp->detail_conf.DetailFactor = vpp->detail;
362 param.ext_buf[param.num_ext_buf++] = (mfxExtBuffer*)&vpp->detail_conf;
366 memset(&vpp->procamp_conf, 0, sizeof(mfxExtVPPProcAmp));
367 vpp->procamp_conf.Header.BufferId = MFX_EXTBUFF_VPP_PROCAMP;
368 vpp->procamp_conf.Header.BufferSz = sizeof(mfxExtVPPProcAmp);
369 vpp->procamp_conf.Hue = vpp->hue;
370 vpp->procamp_conf.Saturation = vpp->saturation;
371 vpp->procamp_conf.Contrast = vpp->contrast;
372 vpp->procamp_conf.Brightness = vpp->brightness;
374 param.ext_buf[param.num_ext_buf++] = (mfxExtBuffer*)&vpp->procamp_conf;
377 if (vpp->transpose >= 0) {
378 #ifdef QSV_HAVE_ROTATION
379 switch (vpp->transpose) {
380 case TRANSPOSE_CCLOCK_FLIP:
381 vpp->rotate = MFX_ANGLE_270;
382 vpp->hflip = MFX_MIRRORING_HORIZONTAL;
384 case TRANSPOSE_CLOCK:
385 vpp->rotate = MFX_ANGLE_90;
386 vpp->hflip = MFX_MIRRORING_DISABLED;
388 case TRANSPOSE_CCLOCK:
389 vpp->rotate = MFX_ANGLE_270;
390 vpp->hflip = MFX_MIRRORING_DISABLED;
392 case TRANSPOSE_CLOCK_FLIP:
393 vpp->rotate = MFX_ANGLE_90;
394 vpp->hflip = MFX_MIRRORING_HORIZONTAL;
396 case TRANSPOSE_REVERSAL:
397 vpp->rotate = MFX_ANGLE_180;
398 vpp->hflip = MFX_MIRRORING_DISABLED;
400 case TRANSPOSE_HFLIP:
401 vpp->rotate = MFX_ANGLE_0;
402 vpp->hflip = MFX_MIRRORING_HORIZONTAL;
404 case TRANSPOSE_VFLIP:
405 vpp->rotate = MFX_ANGLE_180;
406 vpp->hflip = MFX_MIRRORING_HORIZONTAL;
409 av_log(ctx, AV_LOG_ERROR, "Failed to set transpose mode to %d.\n", vpp->transpose);
410 return AVERROR(EINVAL);
413 av_log(ctx, AV_LOG_WARNING, "The QSV VPP transpose option is "
414 "not supported with this MSDK version.\n");
420 #ifdef QSV_HAVE_ROTATION
421 memset(&vpp->rotation_conf, 0, sizeof(mfxExtVPPRotation));
422 vpp->rotation_conf.Header.BufferId = MFX_EXTBUFF_VPP_ROTATION;
423 vpp->rotation_conf.Header.BufferSz = sizeof(mfxExtVPPRotation);
424 vpp->rotation_conf.Angle = vpp->rotate;
426 if (MFX_ANGLE_90 == vpp->rotate || MFX_ANGLE_270 == vpp->rotate) {
427 FFSWAP(int, vpp->out_width, vpp->out_height);
428 FFSWAP(int, outlink->w, outlink->h);
429 av_log(ctx, AV_LOG_DEBUG, "Swap width and height for clock/cclock rotation.\n");
432 param.ext_buf[param.num_ext_buf++] = (mfxExtBuffer*)&vpp->rotation_conf;
434 av_log(ctx, AV_LOG_WARNING, "The QSV VPP rotate option is "
435 "not supported with this MSDK version.\n");
441 #ifdef QSV_HAVE_MIRRORING
442 memset(&vpp->mirroring_conf, 0, sizeof(mfxExtVPPMirroring));
443 vpp->mirroring_conf.Header.BufferId = MFX_EXTBUFF_VPP_MIRRORING;
444 vpp->mirroring_conf.Header.BufferSz = sizeof(mfxExtVPPMirroring);
445 vpp->mirroring_conf.Type = vpp->hflip;
447 param.ext_buf[param.num_ext_buf++] = (mfxExtBuffer*)&vpp->mirroring_conf;
449 av_log(ctx, AV_LOG_WARNING, "The QSV VPP hflip option is "
450 "not supported with this MSDK version.\n");
455 if (vpp->use_frc || vpp->use_crop || vpp->deinterlace || vpp->denoise ||
456 vpp->detail || vpp->procamp || vpp->rotate || vpp->hflip ||
457 inlink->w != outlink->w || inlink->h != outlink->h)
458 return ff_qsvvpp_create(ctx, &vpp->qsv, ¶m);
460 av_log(ctx, AV_LOG_VERBOSE, "qsv vpp pass through mode.\n");
461 if (inlink->hw_frames_ctx)
462 outlink->hw_frames_ctx = av_buffer_ref(inlink->hw_frames_ctx);
468 static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
471 AVFilterContext *ctx = inlink->dst;
472 VPPContext *vpp = inlink->dst->priv;
473 AVFilterLink *outlink = ctx->outputs[0];
476 ret = ff_qsvvpp_filter_frame(vpp->qsv, inlink, picref);
477 av_frame_free(&picref);
479 if (picref->pts != AV_NOPTS_VALUE)
480 picref->pts = av_rescale_q(picref->pts, inlink->time_base, outlink->time_base);
481 ret = ff_filter_frame(outlink, picref);
487 static int query_formats(AVFilterContext *ctx)
490 AVFilterFormats *in_fmts, *out_fmts;
491 static const enum AVPixelFormat in_pix_fmts[] = {
499 static const enum AVPixelFormat out_pix_fmts[] = {
506 in_fmts = ff_make_format_list(in_pix_fmts);
507 out_fmts = ff_make_format_list(out_pix_fmts);
508 ret = ff_formats_ref(in_fmts, &ctx->inputs[0]->out_formats);
511 ret = ff_formats_ref(out_fmts, &ctx->outputs[0]->in_formats);
518 static av_cold void vpp_uninit(AVFilterContext *ctx)
520 VPPContext *vpp = ctx->priv;
522 ff_qsvvpp_free(&vpp->qsv);
525 static const AVClass vpp_class = {
526 .class_name = "vpp_qsv",
527 .item_name = av_default_item_name,
529 .version = LIBAVUTIL_VERSION_INT,
532 static const AVFilterPad vpp_inputs[] = {
535 .type = AVMEDIA_TYPE_VIDEO,
536 .config_props = config_input,
537 .filter_frame = filter_frame,
542 static const AVFilterPad vpp_outputs[] = {
545 .type = AVMEDIA_TYPE_VIDEO,
546 .config_props = config_output,
551 AVFilter ff_vf_vpp_qsv = {
553 .description = NULL_IF_CONFIG_SMALL("Quick Sync Video VPP."),
554 .priv_size = sizeof(VPPContext),
555 .query_formats = query_formats,
557 .uninit = vpp_uninit,
558 .inputs = vpp_inputs,
559 .outputs = vpp_outputs,
560 .priv_class = &vpp_class,
561 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,