1 /*
2 * This file is part of FFmpeg.
3 *
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.
8 *
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.
13 *
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
17 */
18
19 /**
20 ** @file
21 ** Hardware accelerated common filters based on Intel Quick Sync Video VPP
22 **/
23
24 #include <float.h>
25
26 #include "libavutil/opt.h"
27 #include "libavutil/eval.h"
28 #include "libavutil/hwcontext.h"
29 #include "libavutil/hwcontext_qsv.h"
30 #include "libavutil/pixdesc.h"
31 #include "libavutil/mathematics.h"
32
33 #include "formats.h"
34 #include "internal.h"
35 #include "avfilter.h"
36 #include "filters.h"
37
38 #include "qsvvpp.h"
39 #include "transpose.h"
40
41 #define OFFSET(x) offsetof(VPPContext, x)
42 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM)
43
44 /* number of video enhancement filters */
45 #define ENH_FILTERS_COUNT (8)
46
47 typedef struct VPPContext{
48 const AVClass *class;
49
50 QSVVPPContext *qsv;
51
52 /* Video Enhancement Algorithms */
53 mfxExtVPPDeinterlacing deinterlace_conf;
54 mfxExtVPPFrameRateConversion frc_conf;
55 mfxExtVPPDenoise denoise_conf;
56 mfxExtVPPDetail detail_conf;
57 mfxExtVPPProcAmp procamp_conf;
58 mfxExtVPPRotation rotation_conf;
59 mfxExtVPPMirroring mirroring_conf;
60 mfxExtVPPScaling scale_conf;
61
62 int out_width;
63 int out_height;
64 /**
65 * Output sw format. AV_PIX_FMT_NONE for no conversion.
66 */
67 enum AVPixelFormat out_format;
68
69 AVRational framerate; /* target framerate */
70 int use_frc; /* use framerate conversion */
71 int deinterlace; /* deinterlace mode : 0=off, 1=bob, 2=advanced */
72 int denoise; /* Enable Denoise algorithm. Value [0, 100] */
73 int detail; /* Enable Detail Enhancement algorithm. */
74 /* Level is the optional, value [0, 100] */
75 int use_crop; /* 1 = use crop; 0=none */
76 int crop_w;
77 int crop_h;
78 int crop_x;
79 int crop_y;
80
81 int transpose;
82 int rotate; /* rotate angle : [0, 90, 180, 270] */
83 int hflip; /* flip mode : 0 = off, 1 = HORIZONTAL flip */
84
85 int scale_mode; /* scale mode : 0 = auto, 1 = low power, 2 = high quality */
86
87 /* param for the procamp */
88 int procamp; /* enable procamp */
89 float hue;
90 float saturation;
91 float contrast;
92 float brightness;
93
94 char *cx, *cy, *cw, *ch;
95 char *ow, *oh;
96 char *output_format_str;
97
98 int async_depth;
99 int eof;
100 } VPPContext;
101
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" },
106
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},
115
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" },
124
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 },
129
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 },
136 { "scale_mode", "scale mode: 0=auto, 1=low power, 2=high quality", OFFSET(scale_mode), AV_OPT_TYPE_INT, { .i64 = MFX_SCALING_MODE_DEFAULT }, MFX_SCALING_MODE_DEFAULT, MFX_SCALING_MODE_QUALITY, .flags = FLAGS, "scale mode" },
137 { NULL }
138 };
139
140 static const char *const var_names[] = {
141 "iw", "in_w",
142 "ih", "in_h",
143 "ow", "out_w", "w",
144 "oh", "out_h", "h",
145 "cw",
146 "ch",
147 "cx",
148 "cy",
149 NULL
150 };
151
152 enum var_name {
153 VAR_iW, VAR_IN_W,
154 VAR_iH, VAR_IN_H,
155 VAR_oW, VAR_OUT_W, VAR_W,
156 VAR_oH, VAR_OUT_H, VAR_H,
157 CW,
158 CH,
159 CX,
160 CY,
161 VAR_VARS_NB
162 };
163
eval_expr(AVFilterContext * ctx)164 static int eval_expr(AVFilterContext *ctx)
165 {
166 #define PASS_EXPR(e, s) {\
167 ret = av_expr_parse(&e, s, var_names, NULL, NULL, NULL, NULL, 0, ctx); \
168 if (ret < 0) {\
169 av_log(ctx, AV_LOG_ERROR, "Error when passing '%s'.\n", s);\
170 goto release;\
171 }\
172 }
173 #define CALC_EXPR(e, v, i) {\
174 i = v = av_expr_eval(e, var_values, NULL); \
175 }
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;
181 int ret = 0;
182
183 PASS_EXPR(cw_expr, vpp->cw);
184 PASS_EXPR(ch_expr, vpp->ch);
185
186 PASS_EXPR(w_expr, vpp->ow);
187 PASS_EXPR(h_expr, vpp->oh);
188
189 PASS_EXPR(cx_expr, vpp->cx);
190 PASS_EXPR(cy_expr, vpp->cy);
191
192 var_values[VAR_iW] =
193 var_values[VAR_IN_W] = ctx->inputs[0]->w;
194
195 var_values[VAR_iH] =
196 var_values[VAR_IN_H] = ctx->inputs[0]->h;
197
198 /* crop params */
199 CALC_EXPR(cw_expr, var_values[CW], vpp->crop_w);
200 CALC_EXPR(ch_expr, var_values[CH], vpp->crop_h);
201
202 /* calc again in case cw is relative to ch */
203 CALC_EXPR(cw_expr, var_values[CW], vpp->crop_w);
204
205 CALC_EXPR(w_expr,
206 var_values[VAR_OUT_W] = var_values[VAR_oW] = var_values[VAR_W],
207 vpp->out_width);
208 CALC_EXPR(h_expr,
209 var_values[VAR_OUT_H] = var_values[VAR_oH] = var_values[VAR_H],
210 vpp->out_height);
211
212 /* calc again in case ow is relative to oh */
213 CALC_EXPR(w_expr,
214 var_values[VAR_OUT_W] = var_values[VAR_oW] = var_values[VAR_W],
215 vpp->out_width);
216
217
218 CALC_EXPR(cx_expr, var_values[CX], vpp->crop_x);
219 CALC_EXPR(cy_expr, var_values[CY], vpp->crop_y);
220
221 /* calc again in case cx is relative to cy */
222 CALC_EXPR(cx_expr, var_values[CX], vpp->crop_x);
223
224 if ((vpp->crop_w != var_values[VAR_iW]) || (vpp->crop_h != var_values[VAR_iH]))
225 vpp->use_crop = 1;
226
227 release:
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);
234 #undef PASS_EXPR
235 #undef CALC_EXPR
236
237 return ret;
238 }
239
vpp_init(AVFilterContext * ctx)240 static av_cold int vpp_init(AVFilterContext *ctx)
241 {
242 VPPContext *vpp = ctx->priv;
243
244 if (!strcmp(vpp->output_format_str, "same")) {
245 vpp->out_format = AV_PIX_FMT_NONE;
246 } else {
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);
251 }
252 }
253
254 return 0;
255 }
256
config_input(AVFilterLink * inlink)257 static int config_input(AVFilterLink *inlink)
258 {
259 AVFilterContext *ctx = inlink->dst;
260 VPPContext *vpp = ctx->priv;
261 int ret;
262
263 if (vpp->framerate.den == 0 || vpp->framerate.num == 0)
264 vpp->framerate = inlink->frame_rate;
265
266 if (av_cmp_q(vpp->framerate, inlink->frame_rate))
267 vpp->use_frc = 1;
268
269 ret = eval_expr(ctx);
270 if (ret != 0) {
271 av_log(ctx, AV_LOG_ERROR, "Fail to eval expr.\n");
272 return ret;
273 }
274
275 if (vpp->out_height == 0 || vpp->out_width == 0) {
276 vpp->out_width = inlink->w;
277 vpp->out_height = inlink->h;
278 }
279
280 if (vpp->use_crop) {
281 vpp->crop_x = FFMAX(vpp->crop_x, 0);
282 vpp->crop_y = FFMAX(vpp->crop_y, 0);
283
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;
288 }
289
290 return 0;
291 }
292
get_mfx_version(const AVFilterContext * ctx,mfxVersion * mfx_version)293 static mfxStatus get_mfx_version(const AVFilterContext *ctx, mfxVersion *mfx_version)
294 {
295 const AVFilterLink *inlink = ctx->inputs[0];
296 AVBufferRef *device_ref;
297 AVHWDeviceContext *device_ctx;
298 AVQSVDeviceContext *device_hwctx;
299
300 if (inlink->hw_frames_ctx) {
301 AVHWFramesContext *frames_ctx = (AVHWFramesContext *)inlink->hw_frames_ctx->data;
302 device_ref = frames_ctx->device_ref;
303 } else if (ctx->hw_device_ctx) {
304 device_ref = ctx->hw_device_ctx;
305 } else {
306 // Unavailable hw context doesn't matter in pass-through mode,
307 // so don't error here but let runtime version checks fail by setting to 0.0
308 mfx_version->Major = 0;
309 mfx_version->Minor = 0;
310 return MFX_ERR_NONE;
311 }
312
313 device_ctx = (AVHWDeviceContext *)device_ref->data;
314 device_hwctx = device_ctx->hwctx;
315
316 return MFXQueryVersion(device_hwctx->session, mfx_version);
317 }
318
config_output(AVFilterLink * outlink)319 static int config_output(AVFilterLink *outlink)
320 {
321 AVFilterContext *ctx = outlink->src;
322 VPPContext *vpp = ctx->priv;
323 QSVVPPParam param = { NULL };
324 QSVVPPCrop crop = { 0 };
325 mfxExtBuffer *ext_buf[ENH_FILTERS_COUNT];
326 mfxVersion mfx_version;
327 AVFilterLink *inlink = ctx->inputs[0];
328 enum AVPixelFormat in_format;
329
330 outlink->w = vpp->out_width;
331 outlink->h = vpp->out_height;
332 outlink->frame_rate = vpp->framerate;
333 outlink->time_base = inlink->time_base;
334
335 param.filter_frame = NULL;
336 param.num_ext_buf = 0;
337 param.ext_buf = ext_buf;
338 param.async_depth = vpp->async_depth;
339
340 if (get_mfx_version(ctx, &mfx_version) != MFX_ERR_NONE) {
341 av_log(ctx, AV_LOG_ERROR, "Failed to query mfx version.\n");
342 return AVERROR(EINVAL);
343 }
344
345 if (inlink->format == AV_PIX_FMT_QSV) {
346 if (!inlink->hw_frames_ctx || !inlink->hw_frames_ctx->data)
347 return AVERROR(EINVAL);
348 else
349 in_format = ((AVHWFramesContext*)inlink->hw_frames_ctx->data)->sw_format;
350 } else
351 in_format = inlink->format;
352
353 if (vpp->out_format == AV_PIX_FMT_NONE)
354 vpp->out_format = in_format;
355 param.out_sw_format = vpp->out_format;
356
357 if (vpp->use_crop) {
358 crop.in_idx = 0;
359 crop.x = vpp->crop_x;
360 crop.y = vpp->crop_y;
361 crop.w = vpp->crop_w;
362 crop.h = vpp->crop_h;
363
364 param.num_crop = 1;
365 param.crop = &crop;
366 }
367
368 if (vpp->deinterlace) {
369 memset(&vpp->deinterlace_conf, 0, sizeof(mfxExtVPPDeinterlacing));
370 vpp->deinterlace_conf.Header.BufferId = MFX_EXTBUFF_VPP_DEINTERLACING;
371 vpp->deinterlace_conf.Header.BufferSz = sizeof(mfxExtVPPDeinterlacing);
372 vpp->deinterlace_conf.Mode = vpp->deinterlace == 1 ?
373 MFX_DEINTERLACING_BOB : MFX_DEINTERLACING_ADVANCED;
374
375 param.ext_buf[param.num_ext_buf++] = (mfxExtBuffer*)&vpp->deinterlace_conf;
376 }
377
378 if (vpp->use_frc) {
379 memset(&vpp->frc_conf, 0, sizeof(mfxExtVPPFrameRateConversion));
380 vpp->frc_conf.Header.BufferId = MFX_EXTBUFF_VPP_FRAME_RATE_CONVERSION;
381 vpp->frc_conf.Header.BufferSz = sizeof(mfxExtVPPFrameRateConversion);
382 vpp->frc_conf.Algorithm = MFX_FRCALGM_DISTRIBUTED_TIMESTAMP;
383
384 param.ext_buf[param.num_ext_buf++] = (mfxExtBuffer*)&vpp->frc_conf;
385 }
386
387 if (vpp->denoise) {
388 memset(&vpp->denoise_conf, 0, sizeof(mfxExtVPPDenoise));
389 vpp->denoise_conf.Header.BufferId = MFX_EXTBUFF_VPP_DENOISE;
390 vpp->denoise_conf.Header.BufferSz = sizeof(mfxExtVPPDenoise);
391 vpp->denoise_conf.DenoiseFactor = vpp->denoise;
392
393 param.ext_buf[param.num_ext_buf++] = (mfxExtBuffer*)&vpp->denoise_conf;
394 }
395
396 if (vpp->detail) {
397 memset(&vpp->detail_conf, 0, sizeof(mfxExtVPPDetail));
398 vpp->detail_conf.Header.BufferId = MFX_EXTBUFF_VPP_DETAIL;
399 vpp->detail_conf.Header.BufferSz = sizeof(mfxExtVPPDetail);
400 vpp->detail_conf.DetailFactor = vpp->detail;
401
402 param.ext_buf[param.num_ext_buf++] = (mfxExtBuffer*)&vpp->detail_conf;
403 }
404
405 if (vpp->procamp) {
406 memset(&vpp->procamp_conf, 0, sizeof(mfxExtVPPProcAmp));
407 vpp->procamp_conf.Header.BufferId = MFX_EXTBUFF_VPP_PROCAMP;
408 vpp->procamp_conf.Header.BufferSz = sizeof(mfxExtVPPProcAmp);
409 vpp->procamp_conf.Hue = vpp->hue;
410 vpp->procamp_conf.Saturation = vpp->saturation;
411 vpp->procamp_conf.Contrast = vpp->contrast;
412 vpp->procamp_conf.Brightness = vpp->brightness;
413
414 param.ext_buf[param.num_ext_buf++] = (mfxExtBuffer*)&vpp->procamp_conf;
415 }
416
417 if (vpp->transpose >= 0) {
418 if (QSV_RUNTIME_VERSION_ATLEAST(mfx_version, 1, 17)) {
419 switch (vpp->transpose) {
420 case TRANSPOSE_CCLOCK_FLIP:
421 vpp->rotate = MFX_ANGLE_270;
422 vpp->hflip = MFX_MIRRORING_HORIZONTAL;
423 break;
424 case TRANSPOSE_CLOCK:
425 vpp->rotate = MFX_ANGLE_90;
426 vpp->hflip = MFX_MIRRORING_DISABLED;
427 break;
428 case TRANSPOSE_CCLOCK:
429 vpp->rotate = MFX_ANGLE_270;
430 vpp->hflip = MFX_MIRRORING_DISABLED;
431 break;
432 case TRANSPOSE_CLOCK_FLIP:
433 vpp->rotate = MFX_ANGLE_90;
434 vpp->hflip = MFX_MIRRORING_HORIZONTAL;
435 break;
436 case TRANSPOSE_REVERSAL:
437 vpp->rotate = MFX_ANGLE_180;
438 vpp->hflip = MFX_MIRRORING_DISABLED;
439 break;
440 case TRANSPOSE_HFLIP:
441 vpp->rotate = MFX_ANGLE_0;
442 vpp->hflip = MFX_MIRRORING_HORIZONTAL;
443 break;
444 case TRANSPOSE_VFLIP:
445 vpp->rotate = MFX_ANGLE_180;
446 vpp->hflip = MFX_MIRRORING_HORIZONTAL;
447 break;
448 default:
449 av_log(ctx, AV_LOG_ERROR, "Failed to set transpose mode to %d.\n", vpp->transpose);
450 return AVERROR(EINVAL);
451 }
452 } else {
453 av_log(ctx, AV_LOG_WARNING, "The QSV VPP transpose option is "
454 "not supported with this MSDK version.\n");
455 vpp->transpose = 0;
456 }
457 }
458
459 if (vpp->rotate) {
460 if (QSV_RUNTIME_VERSION_ATLEAST(mfx_version, 1, 17)) {
461 memset(&vpp->rotation_conf, 0, sizeof(mfxExtVPPRotation));
462 vpp->rotation_conf.Header.BufferId = MFX_EXTBUFF_VPP_ROTATION;
463 vpp->rotation_conf.Header.BufferSz = sizeof(mfxExtVPPRotation);
464 vpp->rotation_conf.Angle = vpp->rotate;
465
466 if (MFX_ANGLE_90 == vpp->rotate || MFX_ANGLE_270 == vpp->rotate) {
467 FFSWAP(int, vpp->out_width, vpp->out_height);
468 FFSWAP(int, outlink->w, outlink->h);
469 av_log(ctx, AV_LOG_DEBUG, "Swap width and height for clock/cclock rotation.\n");
470 }
471
472 param.ext_buf[param.num_ext_buf++] = (mfxExtBuffer*)&vpp->rotation_conf;
473 } else {
474 av_log(ctx, AV_LOG_WARNING, "The QSV VPP rotate option is "
475 "not supported with this MSDK version.\n");
476 vpp->rotate = 0;
477 }
478 }
479
480 if (vpp->hflip) {
481 if (QSV_RUNTIME_VERSION_ATLEAST(mfx_version, 1, 19)) {
482 memset(&vpp->mirroring_conf, 0, sizeof(mfxExtVPPMirroring));
483 vpp->mirroring_conf.Header.BufferId = MFX_EXTBUFF_VPP_MIRRORING;
484 vpp->mirroring_conf.Header.BufferSz = sizeof(mfxExtVPPMirroring);
485 vpp->mirroring_conf.Type = vpp->hflip;
486
487 param.ext_buf[param.num_ext_buf++] = (mfxExtBuffer*)&vpp->mirroring_conf;
488 } else {
489 av_log(ctx, AV_LOG_WARNING, "The QSV VPP hflip option is "
490 "not supported with this MSDK version.\n");
491 vpp->hflip = 0;
492 }
493 }
494
495 if (inlink->w != outlink->w || inlink->h != outlink->h) {
496 if (QSV_RUNTIME_VERSION_ATLEAST(mfx_version, 1, 19)) {
497 memset(&vpp->scale_conf, 0, sizeof(mfxExtVPPScaling));
498 vpp->scale_conf.Header.BufferId = MFX_EXTBUFF_VPP_SCALING;
499 vpp->scale_conf.Header.BufferSz = sizeof(mfxExtVPPScaling);
500 vpp->scale_conf.ScalingMode = vpp->scale_mode;
501
502 param.ext_buf[param.num_ext_buf++] = (mfxExtBuffer*)&vpp->scale_conf;
503 } else
504 av_log(ctx, AV_LOG_WARNING, "The QSV VPP Scale option is "
505 "not supported with this MSDK version.\n");
506 }
507
508 if (vpp->use_frc || vpp->use_crop || vpp->deinterlace || vpp->denoise ||
509 vpp->detail || vpp->procamp || vpp->rotate || vpp->hflip ||
510 inlink->w != outlink->w || inlink->h != outlink->h || in_format != vpp->out_format)
511 return ff_qsvvpp_create(ctx, &vpp->qsv, ¶m);
512 else {
513 av_log(ctx, AV_LOG_VERBOSE, "qsv vpp pass through mode.\n");
514 if (inlink->hw_frames_ctx)
515 outlink->hw_frames_ctx = av_buffer_ref(inlink->hw_frames_ctx);
516 }
517
518 return 0;
519 }
520
activate(AVFilterContext * ctx)521 static int activate(AVFilterContext *ctx)
522 {
523 AVFilterLink *inlink = ctx->inputs[0];
524 AVFilterLink *outlink = ctx->outputs[0];
525 VPPContext *s =ctx->priv;
526 QSVVPPContext *qsv = s->qsv;
527 AVFrame *in = NULL;
528 int ret, status = 0;
529 int64_t pts = AV_NOPTS_VALUE;
530
531 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
532
533 if (!s->eof) {
534 ret = ff_inlink_consume_frame(inlink, &in);
535 if (ret < 0)
536 return ret;
537
538 if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
539 if (status == AVERROR_EOF) {
540 s->eof = 1;
541 }
542 }
543 }
544
545 if (qsv) {
546 if (in || s->eof) {
547 qsv->eof = s->eof;
548 ret = ff_qsvvpp_filter_frame(qsv, inlink, in);
549 av_frame_free(&in);
550 if (ret == AVERROR(EAGAIN))
551 goto not_ready;
552 else if (ret < 0)
553 return ret;
554
555 if (s->eof)
556 goto eof;
557
558 if (qsv->got_frame) {
559 qsv->got_frame = 0;
560 return 0;
561 }
562 }
563 } else {
564 if (in) {
565 if (in->pts != AV_NOPTS_VALUE)
566 in->pts = av_rescale_q(in->pts, inlink->time_base, outlink->time_base);
567
568 ret = ff_filter_frame(outlink, in);
569 if (ret < 0)
570 return ret;
571
572 if (s->eof)
573 goto eof;
574
575 return 0;
576 }
577 }
578
579 not_ready:
580 if (s->eof)
581 goto eof;
582
583 FF_FILTER_FORWARD_WANTED(outlink, inlink);
584
585 return FFERROR_NOT_READY;
586
587 eof:
588 ff_outlink_set_status(outlink, status, pts);
589 return 0;
590 }
591
query_formats(AVFilterContext * ctx)592 static int query_formats(AVFilterContext *ctx)
593 {
594 int ret;
595 static const enum AVPixelFormat in_pix_fmts[] = {
596 AV_PIX_FMT_YUV420P,
597 AV_PIX_FMT_NV12,
598 AV_PIX_FMT_YUYV422,
599 AV_PIX_FMT_RGB32,
600 AV_PIX_FMT_QSV,
601 AV_PIX_FMT_NONE
602 };
603 static const enum AVPixelFormat out_pix_fmts[] = {
604 AV_PIX_FMT_NV12,
605 AV_PIX_FMT_P010,
606 AV_PIX_FMT_QSV,
607 AV_PIX_FMT_NONE
608 };
609
610 ret = ff_formats_ref(ff_make_format_list(in_pix_fmts),
611 &ctx->inputs[0]->outcfg.formats);
612 if (ret < 0)
613 return ret;
614 return ff_formats_ref(ff_make_format_list(out_pix_fmts),
615 &ctx->outputs[0]->incfg.formats);
616 }
617
vpp_uninit(AVFilterContext * ctx)618 static av_cold void vpp_uninit(AVFilterContext *ctx)
619 {
620 VPPContext *vpp = ctx->priv;
621
622 ff_qsvvpp_free(&vpp->qsv);
623 }
624
625 static const AVClass vpp_class = {
626 .class_name = "vpp_qsv",
627 .item_name = av_default_item_name,
628 .option = options,
629 .version = LIBAVUTIL_VERSION_INT,
630 };
631
632 static const AVFilterPad vpp_inputs[] = {
633 {
634 .name = "default",
635 .type = AVMEDIA_TYPE_VIDEO,
636 .config_props = config_input,
637 },
638 };
639
640 static const AVFilterPad vpp_outputs[] = {
641 {
642 .name = "default",
643 .type = AVMEDIA_TYPE_VIDEO,
644 .config_props = config_output,
645 },
646 };
647
648 const AVFilter ff_vf_vpp_qsv = {
649 .name = "vpp_qsv",
650 .description = NULL_IF_CONFIG_SMALL("Quick Sync Video VPP."),
651 .priv_size = sizeof(VPPContext),
652 .init = vpp_init,
653 .uninit = vpp_uninit,
654 FILTER_INPUTS(vpp_inputs),
655 FILTER_OUTPUTS(vpp_outputs),
656 FILTER_QUERY_FUNC(query_formats),
657 .activate = activate,
658 .priv_class = &vpp_class,
659 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
660 };
661