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 ff_vaapi_vpp_make_param_buffers(avctx, VAProcFilterParameterBufferType,
88 &denoise, sizeof(denoise), 1);
93 static int sharpness_vaapi_build_filter_params(AVFilterContext *avctx)
95 VAAPIVPPContext *vpp_ctx = avctx->priv;
96 SharpnessVAAPIContext *ctx = avctx->priv;
101 uint32_t num_caps = 1;
103 VAProcFilterParameterBuffer sharpness;
105 vas = vaQueryVideoProcFilterCaps(vpp_ctx->hwctx->display, vpp_ctx->va_context,
106 VAProcFilterSharpening,
108 if (vas != VA_STATUS_SUCCESS) {
109 av_log(avctx, AV_LOG_ERROR, "Failed to query sharpness caps "
110 "context: %d (%s).\n", vas, vaErrorStr(vas));
114 sharpness.type = VAProcFilterSharpening;
115 sharpness.value = map(ctx->sharpness,
116 SHARPNESS_MIN, SHARPNESS_MAX,
117 caps.range.min_value,
118 caps.range.max_value);
119 ff_vaapi_vpp_make_param_buffers(avctx,
120 VAProcFilterParameterBufferType,
121 &sharpness, sizeof(sharpness), 1);
126 static int misc_vaapi_filter_frame(AVFilterLink *inlink, AVFrame *input_frame)
128 AVFilterContext *avctx = inlink->dst;
129 AVFilterLink *outlink = avctx->outputs[0];
130 VAAPIVPPContext *vpp_ctx = avctx->priv;
131 AVFrame *output_frame = NULL;
132 VASurfaceID input_surface, output_surface;
133 VARectangle input_region;
135 VAProcPipelineParameterBuffer params;
138 av_log(avctx, AV_LOG_DEBUG, "Filter input: %s, %ux%u (%"PRId64").\n",
139 av_get_pix_fmt_name(input_frame->format),
140 input_frame->width, input_frame->height, input_frame->pts);
142 if (vpp_ctx->va_context == VA_INVALID_ID)
143 return AVERROR(EINVAL);
145 input_surface = (VASurfaceID)(uintptr_t)input_frame->data[3];
146 av_log(avctx, AV_LOG_DEBUG, "Using surface %#x for misc vpp input.\n",
149 output_frame = ff_get_video_buffer(outlink, vpp_ctx->output_width,
150 vpp_ctx->output_height);
152 err = AVERROR(ENOMEM);
156 output_surface = (VASurfaceID)(uintptr_t)output_frame->data[3];
157 av_log(avctx, AV_LOG_DEBUG, "Using surface %#x for misc vpp output.\n",
159 memset(¶ms, 0, sizeof(params));
160 input_region = (VARectangle) {
163 .width = input_frame->width,
164 .height = input_frame->height,
167 if (vpp_ctx->nb_filter_buffers) {
168 params.filters = &vpp_ctx->filter_buffers[0];
169 params.num_filters = vpp_ctx->nb_filter_buffers;
171 params.surface = input_surface;
172 params.surface_region = &input_region;
173 params.surface_color_standard =
174 ff_vaapi_vpp_colour_standard(input_frame->colorspace);
176 params.output_region = NULL;
177 params.output_background_color = 0xff000000;
178 params.output_color_standard = params.surface_color_standard;
180 params.pipeline_flags = 0;
181 params.filter_flags = VA_FRAME_PICTURE;
183 err = ff_vaapi_vpp_render_picture(avctx, ¶ms, output_surface);
187 err = av_frame_copy_props(output_frame, input_frame);
190 av_frame_free(&input_frame);
192 av_log(avctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
193 av_get_pix_fmt_name(output_frame->format),
194 output_frame->width, output_frame->height, output_frame->pts);
196 return ff_filter_frame(outlink, output_frame);
199 av_frame_free(&input_frame);
200 av_frame_free(&output_frame);
204 static av_cold int denoise_vaapi_init(AVFilterContext *avctx)
206 VAAPIVPPContext *vpp_ctx = avctx->priv;
208 ff_vaapi_vpp_ctx_init(avctx);
209 vpp_ctx->pipeline_uninit = ff_vaapi_vpp_pipeline_uninit;
210 vpp_ctx->build_filter_params = denoise_vaapi_build_filter_params;
211 vpp_ctx->output_format = AV_PIX_FMT_NONE;
216 static av_cold int sharpness_vaapi_init(AVFilterContext *avctx)
218 VAAPIVPPContext *vpp_ctx = avctx->priv;
220 ff_vaapi_vpp_ctx_init(avctx);
221 vpp_ctx->pipeline_uninit = ff_vaapi_vpp_pipeline_uninit;
222 vpp_ctx->build_filter_params = sharpness_vaapi_build_filter_params;
223 vpp_ctx->output_format = AV_PIX_FMT_NONE;
228 #define DOFFSET(x) offsetof(DenoiseVAAPIContext, x)
229 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM)
230 static const AVOption denoise_vaapi_options[] = {
231 { "denoise", "denoise level",
232 DOFFSET(denoise), AV_OPT_TYPE_INT, { .i64 = DENOISE_DEFAULT }, DENOISE_MIN, DENOISE_MAX, .flags = FLAGS },
236 #define SOFFSET(x) offsetof(SharpnessVAAPIContext, x)
237 static const AVOption sharpness_vaapi_options[] = {
238 { "sharpness", "sharpness level",
239 SOFFSET(sharpness), AV_OPT_TYPE_INT, { .i64 = SHARPNESS_DEFAULT }, SHARPNESS_MIN, SHARPNESS_MAX, .flags = FLAGS },
243 AVFILTER_DEFINE_CLASS(denoise_vaapi);
244 AVFILTER_DEFINE_CLASS(sharpness_vaapi);
246 static const AVFilterPad misc_vaapi_inputs[] = {
249 .type = AVMEDIA_TYPE_VIDEO,
250 .filter_frame = &misc_vaapi_filter_frame,
251 .config_props = &ff_vaapi_vpp_config_input,
256 static const AVFilterPad misc_vaapi_outputs[] = {
259 .type = AVMEDIA_TYPE_VIDEO,
260 .config_props = &ff_vaapi_vpp_config_output,
265 AVFilter ff_vf_denoise_vaapi = {
266 .name = "denoise_vaapi",
267 .description = NULL_IF_CONFIG_SMALL("VAAPI VPP for de-noise"),
268 .priv_size = sizeof(DenoiseVAAPIContext),
269 .init = &denoise_vaapi_init,
270 .uninit = &ff_vaapi_vpp_ctx_uninit,
271 .query_formats = &ff_vaapi_vpp_query_formats,
272 .inputs = misc_vaapi_inputs,
273 .outputs = misc_vaapi_outputs,
274 .priv_class = &denoise_vaapi_class,
275 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
278 AVFilter ff_vf_sharpness_vaapi = {
279 .name = "sharpness_vaapi",
280 .description = NULL_IF_CONFIG_SMALL("VAAPI VPP for sharpness"),
281 .priv_size = sizeof(SharpnessVAAPIContext),
282 .init = &sharpness_vaapi_init,
283 .uninit = &ff_vaapi_vpp_ctx_uninit,
284 .query_formats = &ff_vaapi_vpp_query_formats,
285 .inputs = misc_vaapi_inputs,
286 .outputs = misc_vaapi_outputs,
287 .priv_class = &sharpness_vaapi_class,
288 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,