FFmpeg
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 "formats.h"
25 #include "internal.h"
26 #include "vaapi_vpp.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 
38 typedef struct DenoiseVAAPIContext {
39  VAAPIVPPContext vpp_ctx; // must be the first field
40 
41  int denoise; // enable denoise algo.
43 
44 typedef struct SharpnessVAAPIContext {
45  VAAPIVPPContext vpp_ctx; // must be the first field
46 
47  int sharpness; // enable sharpness.
49 
50 static 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;
63  DenoiseVAAPIContext *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;
93  SharpnessVAAPIContext *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);
116  return ff_vaapi_vpp_make_param_buffers(avctx,
117  VAProcFilterParameterBufferType,
118  &sharpness, sizeof(sharpness), 1);
119 }
120 
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->va_context == VA_INVALID_ID)
135  return AVERROR(EINVAL);
136 
137  output_frame = ff_get_video_buffer(outlink, vpp_ctx->output_width,
138  vpp_ctx->output_height);
139  if (!output_frame) {
140  err = AVERROR(ENOMEM);
141  goto fail;
142  }
143 
144  err = av_frame_copy_props(output_frame, input_frame);
145  if (err < 0)
146  goto fail;
147 
148  err = ff_vaapi_vpp_init_params(avctx, &params,
149  input_frame, output_frame);
150  if (err < 0)
151  goto fail;
152 
153  if (vpp_ctx->nb_filter_buffers) {
154  params.filters = &vpp_ctx->filter_buffers[0];
155  params.num_filters = vpp_ctx->nb_filter_buffers;
156  }
157 
158  err = ff_vaapi_vpp_render_picture(avctx, &params, output_frame);
159  if (err < 0)
160  goto fail;
161 
162  av_frame_free(&input_frame);
163 
164  av_log(avctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
166  output_frame->width, output_frame->height, output_frame->pts);
167 
168  return ff_filter_frame(outlink, output_frame);
169 
170 fail:
171  av_frame_free(&input_frame);
173  return err;
174 }
175 
177 {
178  VAAPIVPPContext *vpp_ctx = avctx->priv;
179 
180  ff_vaapi_vpp_ctx_init(avctx);
183  vpp_ctx->output_format = AV_PIX_FMT_NONE;
184 
185  return 0;
186 }
187 
189 {
190  VAAPIVPPContext *vpp_ctx = avctx->priv;
191 
192  ff_vaapi_vpp_ctx_init(avctx);
195  vpp_ctx->output_format = AV_PIX_FMT_NONE;
196 
197  return 0;
198 }
199 
200 #define DOFFSET(x) offsetof(DenoiseVAAPIContext, x)
201 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM)
202 static const AVOption denoise_vaapi_options[] = {
203  { "denoise", "denoise level",
205  { NULL },
206 };
207 
208 #define SOFFSET(x) offsetof(SharpnessVAAPIContext, x)
210  { "sharpness", "sharpness level",
211  SOFFSET(sharpness), AV_OPT_TYPE_INT, { .i64 = SHARPNESS_DEFAULT }, SHARPNESS_MIN, SHARPNESS_MAX, .flags = FLAGS },
212  { NULL },
213 };
214 
215 AVFILTER_DEFINE_CLASS(denoise_vaapi);
216 AVFILTER_DEFINE_CLASS(sharpness_vaapi);
217 
218 static const AVFilterPad misc_vaapi_inputs[] = {
219  {
220  .name = "default",
221  .type = AVMEDIA_TYPE_VIDEO,
222  .filter_frame = &misc_vaapi_filter_frame,
223  .config_props = &ff_vaapi_vpp_config_input,
224  },
225 };
226 
227 static const AVFilterPad misc_vaapi_outputs[] = {
228  {
229  .name = "default",
230  .type = AVMEDIA_TYPE_VIDEO,
231  .config_props = &ff_vaapi_vpp_config_output,
232  },
233 };
234 
236  .name = "denoise_vaapi",
237  .description = NULL_IF_CONFIG_SMALL("VAAPI VPP for de-noise"),
238  .priv_size = sizeof(DenoiseVAAPIContext),
244  .priv_class = &denoise_vaapi_class,
245  .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
246 };
247 
249  .name = "sharpness_vaapi",
250  .description = NULL_IF_CONFIG_SMALL("VAAPI VPP for sharpness"),
251  .priv_size = sizeof(SharpnessVAAPIContext),
257  .priv_class = &sharpness_vaapi_class,
258  .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
259 };
ff_get_video_buffer
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:98
ff_vaapi_vpp_pipeline_uninit
void ff_vaapi_vpp_pipeline_uninit(AVFilterContext *avctx)
Definition: vaapi_vpp.c:44
ff_vaapi_vpp_ctx_init
void ff_vaapi_vpp_ctx_init(AVFilterContext *avctx)
Definition: vaapi_vpp.c:666
init
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
ff_vaapi_vpp_render_picture
int ff_vaapi_vpp_render_picture(AVFilterContext *avctx, VAProcPipelineParameterBuffer *params, AVFrame *output_frame)
Definition: vaapi_vpp.c:592
ff_vf_denoise_vaapi
const AVFilter ff_vf_denoise_vaapi
Definition: vf_misc_vaapi.c:235
FF_FILTER_FLAG_HWFRAME_AWARE
#define FF_FILTER_FLAG_HWFRAME_AWARE
The filter is aware of hardware frames, and any hardware frame context should not be automatically pr...
Definition: internal.h:371
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1018
SOFFSET
#define SOFFSET(x)
Definition: vf_misc_vaapi.c:208
sharpness_vaapi_init
static av_cold int sharpness_vaapi_init(AVFilterContext *avctx)
Definition: vf_misc_vaapi.c:188
output
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce output
Definition: filter_design.txt:225
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(denoise_vaapi)
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:112
sharpness_vaapi_build_filter_params
static int sharpness_vaapi_build_filter_params(AVFilterContext *avctx)
Definition: vf_misc_vaapi.c:90
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:303
pixdesc.h
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:396
AVFrame::width
int width
Definition: frame.h:361
ff_vf_sharpness_vaapi
const AVFilter ff_vf_sharpness_vaapi
Definition: vf_misc_vaapi.c:248
AVOption
AVOption.
Definition: opt.h:247
FILTER_QUERY_FUNC
#define FILTER_QUERY_FUNC(func)
Definition: internal.h:168
AVVAAPIDeviceContext::display
VADisplay display
The VADisplay handle, to be filled by the user.
Definition: hwcontext_vaapi.h:72
denoise_vaapi_build_filter_params
static int denoise_vaapi_build_filter_params(AVFilterContext *avctx)
Definition: vf_misc_vaapi.c:60
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:153
DenoiseVAAPIContext
Definition: vf_misc_vaapi.c:38
denoise_vaapi_options
static const AVOption denoise_vaapi_options[]
Definition: vf_misc_vaapi.c:202
sharpness_vaapi_options
static const AVOption sharpness_vaapi_options[]
Definition: vf_misc_vaapi.c:209
formats.h
VAAPIVPPContext::build_filter_params
int(* build_filter_params)(AVFilterContext *avctx)
Definition: vaapi_vpp.h:54
AVFilterContext::priv
void * priv
private data for use by the filter
Definition: avfilter.h:401
fail
#define fail()
Definition: checkasm.h:127
DENOISE_DEFAULT
#define DENOISE_DEFAULT
Definition: vf_misc_vaapi.c:31
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:50
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
SHARPNESS_DEFAULT
#define SHARPNESS_DEFAULT
Definition: vf_misc_vaapi.c:36
VAAPIVPPContext::output_width
int output_width
Definition: vaapi_vpp.h:48
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
ctx
AVFormatContext * ctx
Definition: movenc.c:48
VAAPIVPPContext::output_format
enum AVPixelFormat output_format
Definition: vaapi_vpp.h:47
ff_vaapi_vpp_make_param_buffers
int ff_vaapi_vpp_make_param_buffers(AVFilterContext *avctx, int type, const void *data, size_t size, int count)
Definition: vaapi_vpp.c:563
VAAPIVPPContext::hwctx
AVVAAPIDeviceContext * hwctx
Definition: vaapi_vpp.h:36
denoise_vaapi_init
static av_cold int denoise_vaapi_init(AVFilterContext *avctx)
Definition: vf_misc_vaapi.c:176
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:191
NULL
#define NULL
Definition: coverity.c:32
av_frame_copy_props
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:537
SHARPNESS_MIN
#define SHARPNESS_MIN
Definition: vf_misc_vaapi.c:34
ff_vaapi_vpp_config_input
int ff_vaapi_vpp_config_input(AVFilterLink *inlink)
Definition: vaapi_vpp.c:70
ff_vaapi_vpp_ctx_uninit
void ff_vaapi_vpp_ctx_uninit(AVFilterContext *avctx)
Definition: vaapi_vpp.c:680
ff_vaapi_vpp_query_formats
int ff_vaapi_vpp_query_formats(AVFilterContext *avctx)
Definition: vaapi_vpp.c:27
vaapi_vpp.h
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
misc_vaapi_filter_frame
static int misc_vaapi_filter_frame(AVFilterLink *inlink, AVFrame *input_frame)
Definition: vf_misc_vaapi.c:121
AVFrame::format
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition: frame.h:376
map
static float map(int x, int in_min, int in_max, float out_min, float out_max)
Definition: vf_misc_vaapi.c:50
output_frame
static int output_frame(H264Context *h, AVFrame *dst, H264Picture *srcp)
Definition: h264dec.c:850
DenoiseVAAPIContext::denoise
int denoise
Definition: vf_misc_vaapi.c:41
misc_vaapi_outputs
static const AVFilterPad misc_vaapi_outputs[]
Definition: vf_misc_vaapi.c:227
DOFFSET
#define DOFFSET(x)
Definition: vf_misc_vaapi.c:200
internal.h
denoise
#define denoise(...)
Definition: vf_hqdn3d.c:156
SharpnessVAAPIContext::vpp_ctx
VAAPIVPPContext vpp_ctx
Definition: vf_misc_vaapi.c:45
VAAPIVPPContext::output_height
int output_height
Definition: vaapi_vpp.h:49
VAAPIVPPContext::filter_buffers
VABufferID filter_buffers[VAProcFilterCount]
Definition: vaapi_vpp.h:51
misc_vaapi_inputs
static const AVFilterPad misc_vaapi_inputs[]
Definition: vf_misc_vaapi.c:218
FLAGS
#define FLAGS
Definition: vf_misc_vaapi.c:201
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:56
AVFilter
Filter definition.
Definition: avfilter.h:149
VAAPIVPPContext
Definition: vaapi_vpp.h:33
VAAPIVPPContext::va_context
VAContextID va_context
Definition: vaapi_vpp.h:41
AVFrame::height
int height
Definition: frame.h:361
DENOISE_MAX
#define DENOISE_MAX
Definition: vf_misc_vaapi.c:30
ff_vaapi_vpp_config_output
int ff_vaapi_vpp_config_output(AVFilterLink *outlink)
Definition: vaapi_vpp.c:95
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:224
avfilter.h
VAAPIVPPContext::pipeline_uninit
void(* pipeline_uninit)(AVFilterContext *avctx)
Definition: vaapi_vpp.h:56
AVFilterContext
An instance of a filter.
Definition: avfilter.h:386
VAAPIVPPContext::nb_filter_buffers
int nb_filter_buffers
Definition: vaapi_vpp.h:52
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
DENOISE_MIN
#define DENOISE_MIN
Definition: vf_misc_vaapi.c:29
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:192
SHARPNESS_MAX
#define SHARPNESS_MAX
Definition: vf_misc_vaapi.c:35
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
uninit
static av_cold int uninit(AVCodecContext *avctx)
Definition: crystalhd.c:279
SharpnessVAAPIContext
Definition: vf_misc_vaapi.c:44
SharpnessVAAPIContext::sharpness
int sharpness
Definition: vf_misc_vaapi.c:47
DenoiseVAAPIContext::vpp_ctx
VAAPIVPPContext vpp_ctx
Definition: vf_misc_vaapi.c:39
av_get_pix_fmt_name
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:2484
ff_vaapi_vpp_init_params
int ff_vaapi_vpp_init_params(AVFilterContext *avctx, VAProcPipelineParameterBuffer *params, const AVFrame *input_frame, AVFrame *output_frame)
Definition: vaapi_vpp.c:515
AVFilterContext::outputs
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:398