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
20 #include "libavutil/avassert.h"
21 #include "libavutil/mem.h"
22 #include "libavutil/opt.h"
23 #include "libavutil/pixdesc.h"
28 #include "vaapi_vpp.h"
30 // Denoise min/max/default Values
32 #define DENOISE_MAX 64
33 #define DENOISE_DEFAULT 0
35 // Sharpness min/max/default values
36 #define SHARPNESS_MIN 0
37 #define SHARPNESS_MAX 64
38 #define SHARPNESS_DEFAULT 44
40 typedef struct DenoiseVAAPIContext {
41 VAAPIVPPContext vpp_ctx; // must be the first field
43 int denoise; // enable denoise algo.
44 } DenoiseVAAPIContext;
46 typedef struct SharpnessVAAPIContext {
47 VAAPIVPPContext vpp_ctx; // must be the first field
49 int sharpness; // enable sharpness.
50 } SharpnessVAAPIContext;
52 static float map(int x, int in_min, int in_max, float out_min, float out_max)
56 slope = 1.0 * (out_max - out_min) / (in_max - in_min);
57 output = out_min + slope * (x - in_min);
62 static int denoise_vaapi_build_filter_params(AVFilterContext *avctx)
64 VAAPIVPPContext *vpp_ctx = avctx->priv;
65 DenoiseVAAPIContext *ctx = avctx->priv;
70 uint32_t num_caps = 1;
72 VAProcFilterParameterBuffer denoise;
74 vas = vaQueryVideoProcFilterCaps(vpp_ctx->hwctx->display, vpp_ctx->va_context,
75 VAProcFilterNoiseReduction,
77 if (vas != VA_STATUS_SUCCESS) {
78 av_log(avctx, AV_LOG_ERROR, "Failed to query denoise caps "
79 "context: %d (%s).\n", vas, vaErrorStr(vas));
83 denoise.type = VAProcFilterNoiseReduction;
84 denoise.value = map(ctx->denoise, DENOISE_MIN, DENOISE_MAX,
86 caps.range.max_value);
87 return ff_vaapi_vpp_make_param_buffers(avctx,
88 VAProcFilterParameterBufferType,
89 &denoise, sizeof(denoise), 1);
92 static int sharpness_vaapi_build_filter_params(AVFilterContext *avctx)
94 VAAPIVPPContext *vpp_ctx = avctx->priv;
95 SharpnessVAAPIContext *ctx = avctx->priv;
100 uint32_t num_caps = 1;
102 VAProcFilterParameterBuffer sharpness;
104 vas = vaQueryVideoProcFilterCaps(vpp_ctx->hwctx->display, vpp_ctx->va_context,
105 VAProcFilterSharpening,
107 if (vas != VA_STATUS_SUCCESS) {
108 av_log(avctx, AV_LOG_ERROR, "Failed to query sharpness caps "
109 "context: %d (%s).\n", vas, vaErrorStr(vas));
113 sharpness.type = VAProcFilterSharpening;
114 sharpness.value = map(ctx->sharpness,
115 SHARPNESS_MIN, SHARPNESS_MAX,
116 caps.range.min_value,
117 caps.range.max_value);
118 return ff_vaapi_vpp_make_param_buffers(avctx,
119 VAProcFilterParameterBufferType,
120 &sharpness, sizeof(sharpness), 1);
123 static int misc_vaapi_filter_frame(AVFilterLink *inlink, AVFrame *input_frame)
125 AVFilterContext *avctx = inlink->dst;
126 AVFilterLink *outlink = avctx->outputs[0];
127 VAAPIVPPContext *vpp_ctx = avctx->priv;
128 AVFrame *output_frame = NULL;
129 VAProcPipelineParameterBuffer params;
132 av_log(avctx, AV_LOG_DEBUG, "Filter input: %s, %ux%u (%"PRId64").\n",
133 av_get_pix_fmt_name(input_frame->format),
134 input_frame->width, input_frame->height, input_frame->pts);
136 if (vpp_ctx->va_context == VA_INVALID_ID)
137 return AVERROR(EINVAL);
139 output_frame = ff_get_video_buffer(outlink, vpp_ctx->output_width,
140 vpp_ctx->output_height);
142 err = AVERROR(ENOMEM);
146 err = av_frame_copy_props(output_frame, input_frame);
150 err = ff_vaapi_vpp_init_params(avctx, ¶ms,
151 input_frame, output_frame);
155 if (vpp_ctx->nb_filter_buffers) {
156 params.filters = &vpp_ctx->filter_buffers[0];
157 params.num_filters = vpp_ctx->nb_filter_buffers;
160 err = ff_vaapi_vpp_render_picture(avctx, ¶ms, output_frame);
164 av_frame_free(&input_frame);
166 av_log(avctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
167 av_get_pix_fmt_name(output_frame->format),
168 output_frame->width, output_frame->height, output_frame->pts);
170 return ff_filter_frame(outlink, output_frame);
173 av_frame_free(&input_frame);
174 av_frame_free(&output_frame);
178 static av_cold int denoise_vaapi_init(AVFilterContext *avctx)
180 VAAPIVPPContext *vpp_ctx = avctx->priv;
182 ff_vaapi_vpp_ctx_init(avctx);
183 vpp_ctx->pipeline_uninit = ff_vaapi_vpp_pipeline_uninit;
184 vpp_ctx->build_filter_params = denoise_vaapi_build_filter_params;
185 vpp_ctx->output_format = AV_PIX_FMT_NONE;
190 static av_cold int sharpness_vaapi_init(AVFilterContext *avctx)
192 VAAPIVPPContext *vpp_ctx = avctx->priv;
194 ff_vaapi_vpp_ctx_init(avctx);
195 vpp_ctx->pipeline_uninit = ff_vaapi_vpp_pipeline_uninit;
196 vpp_ctx->build_filter_params = sharpness_vaapi_build_filter_params;
197 vpp_ctx->output_format = AV_PIX_FMT_NONE;
202 #define DOFFSET(x) offsetof(DenoiseVAAPIContext, x)
203 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM)
204 static const AVOption denoise_vaapi_options[] = {
205 { "denoise", "denoise level",
206 DOFFSET(denoise), AV_OPT_TYPE_INT, { .i64 = DENOISE_DEFAULT }, DENOISE_MIN, DENOISE_MAX, .flags = FLAGS },
210 #define SOFFSET(x) offsetof(SharpnessVAAPIContext, x)
211 static const AVOption sharpness_vaapi_options[] = {
212 { "sharpness", "sharpness level",
213 SOFFSET(sharpness), AV_OPT_TYPE_INT, { .i64 = SHARPNESS_DEFAULT }, SHARPNESS_MIN, SHARPNESS_MAX, .flags = FLAGS },
217 AVFILTER_DEFINE_CLASS(denoise_vaapi);
218 AVFILTER_DEFINE_CLASS(sharpness_vaapi);
220 static const AVFilterPad misc_vaapi_inputs[] = {
223 .type = AVMEDIA_TYPE_VIDEO,
224 .filter_frame = &misc_vaapi_filter_frame,
225 .config_props = &ff_vaapi_vpp_config_input,
230 static const AVFilterPad misc_vaapi_outputs[] = {
233 .type = AVMEDIA_TYPE_VIDEO,
234 .config_props = &ff_vaapi_vpp_config_output,
239 const AVFilter ff_vf_denoise_vaapi = {
240 .name = "denoise_vaapi",
241 .description = NULL_IF_CONFIG_SMALL("VAAPI VPP for de-noise"),
242 .priv_size = sizeof(DenoiseVAAPIContext),
243 .init = &denoise_vaapi_init,
244 .uninit = &ff_vaapi_vpp_ctx_uninit,
245 .query_formats = &ff_vaapi_vpp_query_formats,
246 .inputs = misc_vaapi_inputs,
247 .outputs = misc_vaapi_outputs,
248 .priv_class = &denoise_vaapi_class,
249 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
252 const AVFilter ff_vf_sharpness_vaapi = {
253 .name = "sharpness_vaapi",
254 .description = NULL_IF_CONFIG_SMALL("VAAPI VPP for sharpness"),
255 .priv_size = sizeof(SharpnessVAAPIContext),
256 .init = &sharpness_vaapi_init,
257 .uninit = &ff_vaapi_vpp_ctx_uninit,
258 .query_formats = &ff_vaapi_vpp_query_formats,
259 .inputs = misc_vaapi_inputs,
260 .outputs = misc_vaapi_outputs,
261 .priv_class = &sharpness_vaapi_class,
262 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,