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"
40 #define OFFSET(x) offsetof(VPPContext, x)
41 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM)
43 /* number of video enhancement filters */
44 #define ENH_FILTERS_COUNT (5)
46 typedef struct VPPContext{
51 /* Video Enhancement Algorithms */
52 mfxExtVPPDeinterlacing deinterlace_conf;
53 mfxExtVPPFrameRateConversion frc_conf;
54 mfxExtVPPDenoise denoise_conf;
55 mfxExtVPPDetail detail_conf;
56 mfxExtVPPProcAmp procamp_conf;
61 * Output sw format. AV_PIX_FMT_NONE for no conversion.
63 enum AVPixelFormat out_format;
65 AVRational framerate; /* target framerate */
66 int use_frc; /* use framerate conversion */
67 int deinterlace; /* deinterlace mode : 0=off, 1=bob, 2=advanced */
68 int denoise; /* Enable Denoise algorithm. Value [0, 100] */
69 int detail; /* Enable Detail Enhancement algorithm. */
70 /* Level is the optional, value [0, 100] */
71 int use_crop; /* 1 = use crop; 0=none */
77 /* param for the procamp */
78 int procamp; /* enable procamp */
84 char *cx, *cy, *cw, *ch;
86 char *output_format_str;
89 static const AVOption options[] = {
90 { "deinterlace", "deinterlace mode: 0=off, 1=bob, 2=advanced", OFFSET(deinterlace), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, MFX_DEINTERLACING_ADVANCED, .flags = FLAGS, "deinterlace" },
91 { "bob", "Bob deinterlace mode.", 0, AV_OPT_TYPE_CONST, { .i64 = MFX_DEINTERLACING_BOB }, .flags = FLAGS, "deinterlace" },
92 { "advanced", "Advanced deinterlace mode. ", 0, AV_OPT_TYPE_CONST, { .i64 = MFX_DEINTERLACING_ADVANCED }, .flags = FLAGS, "deinterlace" },
94 { "denoise", "denoise level [0, 100]", OFFSET(denoise), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 100, .flags = FLAGS },
95 { "detail", "enhancement level [0, 100]", OFFSET(detail), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 100, .flags = FLAGS },
96 { "framerate", "output framerate", OFFSET(framerate), AV_OPT_TYPE_RATIONAL, { .dbl = 0.0 },0, DBL_MAX, .flags = FLAGS },
97 { "procamp", "Enable ProcAmp", OFFSET(procamp), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, .flags = FLAGS},
98 { "hue", "ProcAmp hue", OFFSET(hue), AV_OPT_TYPE_FLOAT, { .dbl = 0.0 }, -180.0, 180.0, .flags = FLAGS},
99 { "saturation", "ProcAmp saturation", OFFSET(saturation), AV_OPT_TYPE_FLOAT, { .dbl = 1.0 }, 0.0, 10.0, .flags = FLAGS},
100 { "contrast", "ProcAmp contrast", OFFSET(contrast), AV_OPT_TYPE_FLOAT, { .dbl = 1.0 }, 0.0, 10.0, .flags = FLAGS},
101 { "brightness", "ProcAmp brightness", OFFSET(brightness), AV_OPT_TYPE_FLOAT, { .dbl = 0.0 }, -100.0, 100.0, .flags = FLAGS},
103 { "cw", "set the width crop area expression", OFFSET(cw), AV_OPT_TYPE_STRING, { .str = "iw" }, CHAR_MIN, CHAR_MAX, FLAGS },
104 { "ch", "set the height crop area expression", OFFSET(ch), AV_OPT_TYPE_STRING, { .str = "ih" }, CHAR_MIN, CHAR_MAX, FLAGS },
105 { "cx", "set the x crop area expression", OFFSET(cx), AV_OPT_TYPE_STRING, { .str = "(in_w-out_w)/2" }, CHAR_MIN, CHAR_MAX, FLAGS },
106 { "cy", "set the y crop area expression", OFFSET(cy), AV_OPT_TYPE_STRING, { .str = "(in_h-out_h)/2" }, CHAR_MIN, CHAR_MAX, FLAGS },
108 { "w", "Output video width", OFFSET(ow), AV_OPT_TYPE_STRING, { .str="cw" }, 0, 255, .flags = FLAGS },
109 { "width", "Output video width", OFFSET(ow), AV_OPT_TYPE_STRING, { .str="cw" }, 0, 255, .flags = FLAGS },
110 { "h", "Output video height", OFFSET(oh), AV_OPT_TYPE_STRING, { .str="w*ch/cw" }, 0, 255, .flags = FLAGS },
111 { "height", "Output video height", OFFSET(oh), AV_OPT_TYPE_STRING, { .str="w*ch/cw" }, 0, 255, .flags = FLAGS },
112 { "format", "Output pixel format", OFFSET(output_format_str), AV_OPT_TYPE_STRING, { .str = "same" }, .flags = FLAGS },
117 static const char *const var_names[] = {
132 VAR_oW, VAR_OUT_W, VAR_W,
133 VAR_oH, VAR_OUT_H, VAR_H,
141 static int eval_expr(AVFilterContext *ctx)
143 #define PASS_EXPR(e, s) {\
144 ret = av_expr_parse(&e, s, var_names, NULL, NULL, NULL, NULL, 0, ctx); \
146 av_log(ctx, AV_LOG_ERROR, "Error when passing '%s'.\n", s);\
150 #define CALC_EXPR(e, v, i) {\
151 i = v = av_expr_eval(e, var_values, NULL); \
153 VPPContext *vpp = ctx->priv;
154 double var_values[VAR_VARS_NB] = { NAN };
155 AVExpr *w_expr = NULL, *h_expr = NULL;
156 AVExpr *cw_expr = NULL, *ch_expr = NULL;
157 AVExpr *cx_expr = NULL, *cy_expr = NULL;
160 PASS_EXPR(cw_expr, vpp->cw);
161 PASS_EXPR(ch_expr, vpp->ch);
163 PASS_EXPR(w_expr, vpp->ow);
164 PASS_EXPR(h_expr, vpp->oh);
166 PASS_EXPR(cx_expr, vpp->cx);
167 PASS_EXPR(cy_expr, vpp->cy);
170 var_values[VAR_IN_W] = ctx->inputs[0]->w;
173 var_values[VAR_IN_H] = ctx->inputs[0]->h;
176 CALC_EXPR(cw_expr, var_values[CW], vpp->crop_w);
177 CALC_EXPR(ch_expr, var_values[CH], vpp->crop_h);
179 /* calc again in case cw is relative to ch */
180 CALC_EXPR(cw_expr, var_values[CW], vpp->crop_w);
183 var_values[VAR_OUT_W] = var_values[VAR_oW] = var_values[VAR_W],
186 var_values[VAR_OUT_H] = var_values[VAR_oH] = var_values[VAR_H],
189 /* calc again in case ow is relative to oh */
191 var_values[VAR_OUT_W] = var_values[VAR_oW] = var_values[VAR_W],
195 CALC_EXPR(cx_expr, var_values[CX], vpp->crop_x);
196 CALC_EXPR(cy_expr, var_values[CY], vpp->crop_y);
198 /* calc again in case cx is relative to cy */
199 CALC_EXPR(cx_expr, var_values[CX], vpp->crop_x);
201 if ((vpp->crop_w != var_values[VAR_iW]) || (vpp->crop_h != var_values[VAR_iH]))
205 av_expr_free(w_expr);
206 av_expr_free(h_expr);
207 av_expr_free(cw_expr);
208 av_expr_free(ch_expr);
209 av_expr_free(cx_expr);
210 av_expr_free(cy_expr);
217 static av_cold int vpp_init(AVFilterContext *ctx)
219 VPPContext *vpp = ctx->priv;
221 if (!strcmp(vpp->output_format_str, "same")) {
222 vpp->out_format = AV_PIX_FMT_NONE;
224 vpp->out_format = av_get_pix_fmt(vpp->output_format_str);
225 if (vpp->out_format == AV_PIX_FMT_NONE) {
226 av_log(ctx, AV_LOG_ERROR, "Unrecognized output pixel format: %s\n", vpp->output_format_str);
227 return AVERROR(EINVAL);
234 static int config_input(AVFilterLink *inlink)
236 AVFilterContext *ctx = inlink->dst;
237 VPPContext *vpp = ctx->priv;
240 if (vpp->framerate.den == 0 || vpp->framerate.num == 0)
241 vpp->framerate = inlink->frame_rate;
243 if (av_cmp_q(vpp->framerate, inlink->frame_rate))
246 ret = eval_expr(ctx);
248 av_log(ctx, AV_LOG_ERROR, "Fail to eval expr.\n");
252 if (vpp->out_height == 0 || vpp->out_width == 0) {
253 vpp->out_width = inlink->w;
254 vpp->out_height = inlink->h;
258 vpp->crop_x = FFMAX(vpp->crop_x, 0);
259 vpp->crop_y = FFMAX(vpp->crop_y, 0);
261 if(vpp->crop_w + vpp->crop_x > inlink->w)
262 vpp->crop_x = inlink->w - vpp->crop_w;
263 if(vpp->crop_h + vpp->crop_y > inlink->h)
264 vpp->crop_y = inlink->h - vpp->crop_h;
270 static int config_output(AVFilterLink *outlink)
272 AVFilterContext *ctx = outlink->src;
273 VPPContext *vpp = ctx->priv;
274 QSVVPPParam param = { NULL };
275 QSVVPPCrop crop = { 0 };
276 mfxExtBuffer *ext_buf[ENH_FILTERS_COUNT];
277 AVFilterLink *inlink = ctx->inputs[0];
278 enum AVPixelFormat in_format;
280 outlink->w = vpp->out_width;
281 outlink->h = vpp->out_height;
282 outlink->frame_rate = vpp->framerate;
283 outlink->time_base = av_inv_q(vpp->framerate);
285 param.filter_frame = NULL;
286 param.num_ext_buf = 0;
287 param.ext_buf = ext_buf;
289 if (inlink->format == AV_PIX_FMT_QSV) {
290 if (!inlink->hw_frames_ctx || !inlink->hw_frames_ctx->data)
291 return AVERROR(EINVAL);
293 in_format = ((AVHWFramesContext*)inlink->hw_frames_ctx->data)->sw_format;
295 in_format = inlink->format;
297 param.out_sw_format = (vpp->out_format == AV_PIX_FMT_NONE) ? in_format : vpp->out_format;
301 crop.x = vpp->crop_x;
302 crop.y = vpp->crop_y;
303 crop.w = vpp->crop_w;
304 crop.h = vpp->crop_h;
310 if (vpp->deinterlace) {
311 memset(&vpp->deinterlace_conf, 0, sizeof(mfxExtVPPDeinterlacing));
312 vpp->deinterlace_conf.Header.BufferId = MFX_EXTBUFF_VPP_DEINTERLACING;
313 vpp->deinterlace_conf.Header.BufferSz = sizeof(mfxExtVPPDeinterlacing);
314 vpp->deinterlace_conf.Mode = vpp->deinterlace == 1 ?
315 MFX_DEINTERLACING_BOB : MFX_DEINTERLACING_ADVANCED;
317 param.ext_buf[param.num_ext_buf++] = (mfxExtBuffer*)&vpp->deinterlace_conf;
321 memset(&vpp->frc_conf, 0, sizeof(mfxExtVPPFrameRateConversion));
322 vpp->frc_conf.Header.BufferId = MFX_EXTBUFF_VPP_FRAME_RATE_CONVERSION;
323 vpp->frc_conf.Header.BufferSz = sizeof(mfxExtVPPFrameRateConversion);
324 vpp->frc_conf.Algorithm = MFX_FRCALGM_DISTRIBUTED_TIMESTAMP;
326 param.ext_buf[param.num_ext_buf++] = (mfxExtBuffer*)&vpp->frc_conf;
330 memset(&vpp->denoise_conf, 0, sizeof(mfxExtVPPDenoise));
331 vpp->denoise_conf.Header.BufferId = MFX_EXTBUFF_VPP_DENOISE;
332 vpp->denoise_conf.Header.BufferSz = sizeof(mfxExtVPPDenoise);
333 vpp->denoise_conf.DenoiseFactor = vpp->denoise;
335 param.ext_buf[param.num_ext_buf++] = (mfxExtBuffer*)&vpp->denoise_conf;
339 memset(&vpp->detail_conf, 0, sizeof(mfxExtVPPDetail));
340 vpp->detail_conf.Header.BufferId = MFX_EXTBUFF_VPP_DETAIL;
341 vpp->detail_conf.Header.BufferSz = sizeof(mfxExtVPPDetail);
342 vpp->detail_conf.DetailFactor = vpp->detail;
344 param.ext_buf[param.num_ext_buf++] = (mfxExtBuffer*)&vpp->detail_conf;
348 memset(&vpp->procamp_conf, 0, sizeof(mfxExtVPPProcAmp));
349 vpp->procamp_conf.Header.BufferId = MFX_EXTBUFF_VPP_PROCAMP;
350 vpp->procamp_conf.Header.BufferSz = sizeof(mfxExtVPPProcAmp);
351 vpp->procamp_conf.Hue = vpp->hue;
352 vpp->procamp_conf.Saturation = vpp->saturation;
353 vpp->procamp_conf.Contrast = vpp->contrast;
354 vpp->procamp_conf.Brightness = vpp->brightness;
356 param.ext_buf[param.num_ext_buf++] = (mfxExtBuffer*)&vpp->procamp_conf;
359 if (vpp->use_frc || vpp->use_crop || vpp->deinterlace || vpp->denoise ||
360 vpp->detail || vpp->procamp || inlink->w != outlink->w || inlink->h != outlink->h)
361 return ff_qsvvpp_create(ctx, &vpp->qsv, ¶m);
363 av_log(ctx, AV_LOG_VERBOSE, "qsv vpp pass through mode.\n");
364 if (inlink->hw_frames_ctx)
365 outlink->hw_frames_ctx = av_buffer_ref(inlink->hw_frames_ctx);
371 static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
374 AVFilterContext *ctx = inlink->dst;
375 VPPContext *vpp = inlink->dst->priv;
376 AVFilterLink *outlink = ctx->outputs[0];
379 ret = ff_qsvvpp_filter_frame(vpp->qsv, inlink, picref);
380 av_frame_free(&picref);
382 if (picref->pts != AV_NOPTS_VALUE)
383 picref->pts = av_rescale_q(picref->pts, inlink->time_base, outlink->time_base);
384 ret = ff_filter_frame(outlink, picref);
390 static int query_formats(AVFilterContext *ctx)
393 AVFilterFormats *in_fmts, *out_fmts;
394 static const enum AVPixelFormat in_pix_fmts[] = {
402 static const enum AVPixelFormat out_pix_fmts[] = {
409 in_fmts = ff_make_format_list(in_pix_fmts);
410 out_fmts = ff_make_format_list(out_pix_fmts);
411 ret = ff_formats_ref(in_fmts, &ctx->inputs[0]->out_formats);
414 ret = ff_formats_ref(out_fmts, &ctx->outputs[0]->in_formats);
421 static av_cold void vpp_uninit(AVFilterContext *ctx)
423 VPPContext *vpp = ctx->priv;
425 ff_qsvvpp_free(&vpp->qsv);
428 static const AVClass vpp_class = {
429 .class_name = "vpp_qsv",
430 .item_name = av_default_item_name,
432 .version = LIBAVUTIL_VERSION_INT,
435 static const AVFilterPad vpp_inputs[] = {
438 .type = AVMEDIA_TYPE_VIDEO,
439 .config_props = config_input,
440 .filter_frame = filter_frame,
445 static const AVFilterPad vpp_outputs[] = {
448 .type = AVMEDIA_TYPE_VIDEO,
449 .config_props = config_output,
454 AVFilter ff_vf_vpp_qsv = {
456 .description = NULL_IF_CONFIG_SMALL("Quick Sync Video VPP."),
457 .priv_size = sizeof(VPPContext),
458 .query_formats = query_formats,
460 .uninit = vpp_uninit,
461 .inputs = vpp_inputs,
462 .outputs = vpp_outputs,
463 .priv_class = &vpp_class,
464 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,