FFmpeg
Loading...
Searching...
No Matches
vf_misc_vaapi.c
Go to the documentation of this file.
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#include <string.h>
19
20#include "libavutil/opt.h"
21#include "libavutil/pixdesc.h"
22
23#include "avfilter.h"
24#include "filters.h"
25#include "vaapi_vpp.h"
26#include "video.h"
27
28// Denoise min/max/default Values
29#define DENOISE_MIN 0
30#define DENOISE_MAX 64
31#define DENOISE_DEFAULT 0
32
33// Sharpness min/max/default values
34#define SHARPNESS_MIN 0
35#define SHARPNESS_MAX 64
36#define SHARPNESS_DEFAULT 44
37
38typedef struct DenoiseVAAPIContext {
39 VAAPIVPPContext vpp_ctx; // must be the first field
40
41 int denoise; // enable denoise algo.
43
44typedef struct SharpnessVAAPIContext {
45 VAAPIVPPContext vpp_ctx; // must be the first field
46
47 int sharpness; // enable sharpness.
49
50static float map(int x, int in_min, int in_max, float out_min, float out_max)
51{
52 double slope, output;
53
54 slope = 1.0 * (out_max - out_min) / (in_max - in_min);
55 output = out_min + slope * (x - in_min);
56
57 return (float)output;
58}
59
61{
62 VAAPIVPPContext *vpp_ctx = avctx->priv;
64
65 VAProcFilterCap caps;
66
67 VAStatus vas;
68 uint32_t num_caps = 1;
69
70 VAProcFilterParameterBuffer denoise;
71
72 vas = vaQueryVideoProcFilterCaps(vpp_ctx->hwctx->display, vpp_ctx->va_context,
73 VAProcFilterNoiseReduction,
74 &caps, &num_caps);
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));
78 return AVERROR(EIO);
79 }
80
81 denoise.type = VAProcFilterNoiseReduction;
82 denoise.value = map(ctx->denoise, DENOISE_MIN, DENOISE_MAX,
83 caps.range.min_value,
84 caps.range.max_value);
86 VAProcFilterParameterBufferType,
87 &denoise, sizeof(denoise), 1);
88}
89
91{
92 VAAPIVPPContext *vpp_ctx = avctx->priv;
94
95 VAProcFilterCap caps;
96
97 VAStatus vas;
98 uint32_t num_caps = 1;
99
100 VAProcFilterParameterBuffer sharpness;
101
102 vas = vaQueryVideoProcFilterCaps(vpp_ctx->hwctx->display, vpp_ctx->va_context,
103 VAProcFilterSharpening,
104 &caps, &num_caps);
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));
108 return AVERROR(EIO);
109 }
110
111 sharpness.type = VAProcFilterSharpening;
112 sharpness.value = map(ctx->sharpness,
114 caps.range.min_value,
115 caps.range.max_value);
117 VAProcFilterParameterBufferType,
118 &sharpness, sizeof(sharpness), 1);
119}
120
121static int misc_vaapi_filter_frame(AVFilterLink *inlink, AVFrame *input_frame)
122{
123 AVFilterContext *avctx = inlink->dst;
124 AVFilterLink *outlink = avctx->outputs[0];
125 VAAPIVPPContext *vpp_ctx = avctx->priv;
127 VAProcPipelineParameterBuffer params;
128 int err;
129
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);
133
134 if (vpp_ctx->passthrough)
135 return ff_filter_frame(outlink, input_frame);
136
137 if (vpp_ctx->va_context == VA_INVALID_ID)
138 return AVERROR(EINVAL);
139
141 vpp_ctx->output_height);
142 if (!output_frame) {
143 err = AVERROR(ENOMEM);
144 goto fail;
145 }
146
147 err = av_frame_copy_props(output_frame, input_frame);
148 if (err < 0)
149 goto fail;
150
151 err = ff_vaapi_vpp_init_params(avctx, &params,
152 input_frame, output_frame);
153 if (err < 0)
154 goto fail;
155
156 if (vpp_ctx->nb_filter_buffers) {
157 params.filters = &vpp_ctx->filter_buffers[0];
158 params.num_filters = vpp_ctx->nb_filter_buffers;
159 }
160
162 if (err < 0)
163 goto fail;
164
165 av_frame_free(&input_frame);
166
167 av_log(avctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
169 output_frame->width, output_frame->height, output_frame->pts);
170
171 return ff_filter_frame(outlink, output_frame);
172
173fail:
174 av_frame_free(&input_frame);
176 return err;
177}
178
180{
181 VAAPIVPPContext *vpp_ctx = avctx->priv;
182 DenoiseVAAPIContext *ctx = avctx->priv;
183
188 if (ctx->denoise == DENOISE_DEFAULT)
189 vpp_ctx->passthrough = 1;
190
191 return 0;
192}
193
195{
196 VAAPIVPPContext *vpp_ctx = avctx->priv;
198
203 if (ctx->sharpness == SHARPNESS_DEFAULT)
204 vpp_ctx->passthrough = 1;
205
206 return 0;
207}
208
209#define DOFFSET(x) offsetof(DenoiseVAAPIContext, x)
210#define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM)
212 { "denoise", "denoise level",
214 { NULL },
215};
216
217#define SOFFSET(x) offsetof(SharpnessVAAPIContext, x)
219 { "sharpness", "sharpness level",
220 SOFFSET(sharpness), AV_OPT_TYPE_INT, { .i64 = SHARPNESS_DEFAULT }, SHARPNESS_MIN, SHARPNESS_MAX, .flags = FLAGS },
221 { NULL },
222};
223
225AVFILTER_DEFINE_CLASS(sharpness_vaapi);
226
228 {
229 .name = "default",
230 .type = AVMEDIA_TYPE_VIDEO,
231 .filter_frame = &misc_vaapi_filter_frame,
232 .config_props = &ff_vaapi_vpp_config_input,
233 },
234};
235
237 {
238 .name = "default",
239 .type = AVMEDIA_TYPE_VIDEO,
240 .config_props = &ff_vaapi_vpp_config_output,
241 },
242};
243
245 .p.name = "denoise_vaapi",
246 .p.description = NULL_IF_CONFIG_SMALL("VAAPI VPP for de-noise"),
247 .p.priv_class = &denoise_vaapi_class,
248 .priv_size = sizeof(DenoiseVAAPIContext),
254 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
255};
256
258 .p.name = "sharpness_vaapi",
259 .p.description = NULL_IF_CONFIG_SMALL("VAAPI VPP for sharpness"),
260 .p.priv_class = &sharpness_vaapi_class,
261 .priv_size = sizeof(SharpnessVAAPIContext),
267 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
268};
SwsAArch64OpImplParams params
Definition ops.c:51
const FFFilter ff_vf_denoise_vaapi
const FFFilter ff_vf_sharpness_vaapi
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
Main libavfilter public API header.
#define FLAGS
Definition cmdutils.c:598
#define NULL
Definition coverity.c:32
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
#define fail
Definition test.h:479
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
#define AVERROR(e)
Definition error.h:45
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:599
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
const VDPAUPixFmtMap * map
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int output_frame(H264Context *h, AVFrame *dst, H264Picture *srcp)
Definition h264dec.c:860
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define FF_FILTER_FLAG_HWFRAME_AWARE
The filter is aware of hardware frames, and any hardware frame context should not be automatically pr...
Definition filters.h:208
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define FILTER_QUERY_FUNC2(func)
Definition filters.h:241
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
AVOptions.
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition pixdesc.c:3380
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
An instance of a filter.
Definition avfilter.h:273
void * priv
private data for use by the filter
Definition avfilter.h:288
AVFilterLink ** outputs
array of pointers to output links
Definition avfilter.h:285
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition frame.h:574
int width
Definition frame.h:544
int height
Definition frame.h:544
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition frame.h:559
AVOption.
Definition opt.h:428
VADisplay display
The VADisplay handle, to be filled by the user.
VAAPIVPPContext vpp_ctx
VAAPIVPPContext vpp_ctx
VABufferID filter_buffers[VAProcFilterCount]
Definition vaapi_vpp.h:56
int nb_filter_buffers
Definition vaapi_vpp.h:57
void(* pipeline_uninit)(AVFilterContext *avctx)
Definition vaapi_vpp.h:63
enum AVPixelFormat output_format
Definition vaapi_vpp.h:52
int(* build_filter_params)(AVFilterContext *avctx)
Definition vaapi_vpp.h:61
VAContextID va_context
Definition vaapi_vpp.h:46
AVVAAPIDeviceContext * hwctx
Definition vaapi_vpp.h:41
#define av_log(a,...)
static AVFormatContext * ctx
Definition movenc.c:49
int ff_vaapi_vpp_config_output(AVFilterLink *outlink)
Definition vaapi_vpp.c:97
int ff_vaapi_vpp_config_input(AVFilterLink *inlink)
Definition vaapi_vpp.c:71
int ff_vaapi_vpp_query_formats(const AVFilterContext *avctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition vaapi_vpp.c:29
void ff_vaapi_vpp_pipeline_uninit(AVFilterContext *avctx)
Definition vaapi_vpp.c:45
void ff_vaapi_vpp_ctx_init(AVFilterContext *avctx)
Definition vaapi_vpp.c:714
int ff_vaapi_vpp_make_param_buffers(AVFilterContext *avctx, int type, const void *data, size_t size, int count)
Definition vaapi_vpp.c:582
int ff_vaapi_vpp_init_params(AVFilterContext *avctx, VAProcPipelineParameterBuffer *params, const AVFrame *input_frame, AVFrame *output_frame)
Definition vaapi_vpp.c:533
int ff_vaapi_vpp_render_picture(AVFilterContext *avctx, VAProcPipelineParameterBuffer *params, AVFrame *output_frame)
Definition vaapi_vpp.c:707
void ff_vaapi_vpp_ctx_uninit(AVFilterContext *avctx)
Definition vaapi_vpp.c:728
#define denoise(...)
Definition vf_hqdn3d.c:155
static int denoise_vaapi_build_filter_params(AVFilterContext *avctx)
#define SHARPNESS_MIN
#define SHARPNESS_DEFAULT
#define DENOISE_MAX
#define DENOISE_DEFAULT
static const AVOption sharpness_vaapi_options[]
#define DOFFSET(x)
static av_cold int sharpness_vaapi_init(AVFilterContext *avctx)
#define SHARPNESS_MAX
static const AVFilterPad misc_vaapi_outputs[]
static av_cold int denoise_vaapi_init(AVFilterContext *avctx)
#define SOFFSET(x)
static const AVFilterPad misc_vaapi_inputs[]
static const AVOption denoise_vaapi_options[]
#define DENOISE_MIN
static int misc_vaapi_filter_frame(AVFilterLink *inlink, AVFrame *input_frame)
static int sharpness_vaapi_build_filter_params(AVFilterContext *avctx)
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition video.c:89