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/opt.h"
21 #include "libavutil/pixdesc.h"
25 #include "vaapi_vpp.h"
28 // Denoise min/max/default Values
30 #define DENOISE_MAX 64
31 #define DENOISE_DEFAULT 0
33 // Sharpness min/max/default values
34 #define SHARPNESS_MIN 0
35 #define SHARPNESS_MAX 64
36 #define SHARPNESS_DEFAULT 44
38 typedef struct DenoiseVAAPIContext {
39 VAAPIVPPContext vpp_ctx; // must be the first field
41 int denoise; // enable denoise algo.
42 } DenoiseVAAPIContext;
44 typedef struct SharpnessVAAPIContext {
45 VAAPIVPPContext vpp_ctx; // must be the first field
47 int sharpness; // enable sharpness.
48 } SharpnessVAAPIContext;
50 static float map(int x, int in_min, int in_max, float out_min, float out_max)
54 slope = 1.0 * (out_max - out_min) / (in_max - in_min);
55 output = out_min + slope * (x - in_min);
60 static int denoise_vaapi_build_filter_params(AVFilterContext *avctx)
62 VAAPIVPPContext *vpp_ctx = avctx->priv;
63 DenoiseVAAPIContext *ctx = avctx->priv;
68 uint32_t num_caps = 1;
70 VAProcFilterParameterBuffer denoise;
72 vas = vaQueryVideoProcFilterCaps(vpp_ctx->hwctx->display, vpp_ctx->va_context,
73 VAProcFilterNoiseReduction,
75 if (vas != VA_STATUS_SUCCESS) {
76 av_log(avctx, AV_LOG_ERROR, "Failed to query denoise caps "
77 "context: %d (%s).\n", vas, vaErrorStr(vas));
81 denoise.type = VAProcFilterNoiseReduction;
82 denoise.value = map(ctx->denoise, DENOISE_MIN, DENOISE_MAX,
84 caps.range.max_value);
85 return ff_vaapi_vpp_make_param_buffers(avctx,
86 VAProcFilterParameterBufferType,
87 &denoise, sizeof(denoise), 1);
90 static int sharpness_vaapi_build_filter_params(AVFilterContext *avctx)
92 VAAPIVPPContext *vpp_ctx = avctx->priv;
93 SharpnessVAAPIContext *ctx = avctx->priv;
98 uint32_t num_caps = 1;
100 VAProcFilterParameterBuffer sharpness;
102 vas = vaQueryVideoProcFilterCaps(vpp_ctx->hwctx->display, vpp_ctx->va_context,
103 VAProcFilterSharpening,
105 if (vas != VA_STATUS_SUCCESS) {
106 av_log(avctx, AV_LOG_ERROR, "Failed to query sharpness caps "
107 "context: %d (%s).\n", vas, vaErrorStr(vas));
111 sharpness.type = VAProcFilterSharpening;
112 sharpness.value = map(ctx->sharpness,
113 SHARPNESS_MIN, SHARPNESS_MAX,
114 caps.range.min_value,
115 caps.range.max_value);
116 return ff_vaapi_vpp_make_param_buffers(avctx,
117 VAProcFilterParameterBufferType,
118 &sharpness, sizeof(sharpness), 1);
121 static int misc_vaapi_filter_frame(AVFilterLink *inlink, AVFrame *input_frame)
123 AVFilterContext *avctx = inlink->dst;
124 AVFilterLink *outlink = avctx->outputs[0];
125 VAAPIVPPContext *vpp_ctx = avctx->priv;
126 AVFrame *output_frame = NULL;
127 VAProcPipelineParameterBuffer params;
130 av_log(avctx, AV_LOG_DEBUG, "Filter input: %s, %ux%u (%"PRId64").\n",
131 av_get_pix_fmt_name(input_frame->format),
132 input_frame->width, input_frame->height, input_frame->pts);
134 if (vpp_ctx->passthrough)
135 return ff_filter_frame(outlink, input_frame);
137 if (vpp_ctx->va_context == VA_INVALID_ID)
138 return AVERROR(EINVAL);
140 output_frame = ff_get_video_buffer(outlink, vpp_ctx->output_width,
141 vpp_ctx->output_height);
143 err = AVERROR(ENOMEM);
147 err = av_frame_copy_props(output_frame, input_frame);
151 err = ff_vaapi_vpp_init_params(avctx, ¶ms,
152 input_frame, output_frame);
156 if (vpp_ctx->nb_filter_buffers) {
157 params.filters = &vpp_ctx->filter_buffers[0];
158 params.num_filters = vpp_ctx->nb_filter_buffers;
161 err = ff_vaapi_vpp_render_picture(avctx, ¶ms, output_frame);
165 av_frame_free(&input_frame);
167 av_log(avctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
168 av_get_pix_fmt_name(output_frame->format),
169 output_frame->width, output_frame->height, output_frame->pts);
171 return ff_filter_frame(outlink, output_frame);
174 av_frame_free(&input_frame);
175 av_frame_free(&output_frame);
179 static av_cold int denoise_vaapi_init(AVFilterContext *avctx)
181 VAAPIVPPContext *vpp_ctx = avctx->priv;
182 DenoiseVAAPIContext *ctx = avctx->priv;
184 ff_vaapi_vpp_ctx_init(avctx);
185 vpp_ctx->pipeline_uninit = ff_vaapi_vpp_pipeline_uninit;
186 vpp_ctx->build_filter_params = denoise_vaapi_build_filter_params;
187 vpp_ctx->output_format = AV_PIX_FMT_NONE;
188 if (ctx->denoise == DENOISE_DEFAULT)
189 vpp_ctx->passthrough = 1;
194 static av_cold int sharpness_vaapi_init(AVFilterContext *avctx)
196 VAAPIVPPContext *vpp_ctx = avctx->priv;
197 SharpnessVAAPIContext *ctx = avctx->priv;
199 ff_vaapi_vpp_ctx_init(avctx);
200 vpp_ctx->pipeline_uninit = ff_vaapi_vpp_pipeline_uninit;
201 vpp_ctx->build_filter_params = sharpness_vaapi_build_filter_params;
202 vpp_ctx->output_format = AV_PIX_FMT_NONE;
203 if (ctx->sharpness == SHARPNESS_DEFAULT)
204 vpp_ctx->passthrough = 1;
209 #define DOFFSET(x) offsetof(DenoiseVAAPIContext, x)
210 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM)
211 static const AVOption denoise_vaapi_options[] = {
212 { "denoise", "denoise level",
213 DOFFSET(denoise), AV_OPT_TYPE_INT, { .i64 = DENOISE_DEFAULT }, DENOISE_MIN, DENOISE_MAX, .flags = FLAGS },
217 #define SOFFSET(x) offsetof(SharpnessVAAPIContext, x)
218 static const AVOption sharpness_vaapi_options[] = {
219 { "sharpness", "sharpness level",
220 SOFFSET(sharpness), AV_OPT_TYPE_INT, { .i64 = SHARPNESS_DEFAULT }, SHARPNESS_MIN, SHARPNESS_MAX, .flags = FLAGS },
224 AVFILTER_DEFINE_CLASS(denoise_vaapi);
225 AVFILTER_DEFINE_CLASS(sharpness_vaapi);
227 static const AVFilterPad misc_vaapi_inputs[] = {
230 .type = AVMEDIA_TYPE_VIDEO,
231 .filter_frame = &misc_vaapi_filter_frame,
232 .config_props = &ff_vaapi_vpp_config_input,
236 static const AVFilterPad misc_vaapi_outputs[] = {
239 .type = AVMEDIA_TYPE_VIDEO,
240 .config_props = &ff_vaapi_vpp_config_output,
244 const AVFilter ff_vf_denoise_vaapi = {
245 .name = "denoise_vaapi",
246 .description = NULL_IF_CONFIG_SMALL("VAAPI VPP for de-noise"),
247 .priv_size = sizeof(DenoiseVAAPIContext),
248 .init = &denoise_vaapi_init,
249 .uninit = &ff_vaapi_vpp_ctx_uninit,
250 FILTER_INPUTS(misc_vaapi_inputs),
251 FILTER_OUTPUTS(misc_vaapi_outputs),
252 FILTER_QUERY_FUNC(&ff_vaapi_vpp_query_formats),
253 .priv_class = &denoise_vaapi_class,
254 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
257 const AVFilter ff_vf_sharpness_vaapi = {
258 .name = "sharpness_vaapi",
259 .description = NULL_IF_CONFIG_SMALL("VAAPI VPP for sharpness"),
260 .priv_size = sizeof(SharpnessVAAPIContext),
261 .init = &sharpness_vaapi_init,
262 .uninit = &ff_vaapi_vpp_ctx_uninit,
263 FILTER_INPUTS(misc_vaapi_inputs),
264 FILTER_OUTPUTS(misc_vaapi_outputs),
265 FILTER_QUERY_FUNC(&ff_vaapi_vpp_query_formats),
266 .priv_class = &sharpness_vaapi_class,
267 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,