FFmpeg
vf_procamp_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 "internal.h"
25 #include "vaapi_vpp.h"
26 #include "video.h"
27 
28 // ProcAmp Min/Max/Default Values
29 #define BRIGHTNESS_MIN -100.0F
30 #define BRIGHTNESS_MAX 100.0F
31 #define BRIGHTNESS_DEFAULT 0.0F
32 
33 #define CONTRAST_MIN 0.0F
34 #define CONTRAST_MAX 10.0F
35 #define CONTRAST_DEFAULT 1.0F
36 
37 #define HUE_MIN -180.0F
38 #define HUE_MAX 180.0F
39 #define HUE_DEFAULT 0.0F
40 
41 #define SATURATION_MIN 0.0F
42 #define SATURATION_MAX 10.0F
43 #define SATURATION_DEFAULT 1.0F
44 
45 typedef struct ProcampVAAPIContext {
46  VAAPIVPPContext vpp_ctx; // must be the first field
47 
48  float bright;
49  float hue;
50  float saturation;
51  float contrast;
53 
54 static float map(float x, float in_min, float in_max, float out_min, float out_max)
55 {
56  double slope, output;
57 
58  slope = 1.0 * (out_max - out_min) / (in_max - in_min);
59  output = out_min + slope * (x - in_min);
60 
61  return (float)output;
62 }
63 
65 {
66  VAAPIVPPContext *vpp_ctx = avctx->priv;
67  ProcampVAAPIContext *ctx = avctx->priv;
68  VAStatus vas;
69  VAProcFilterParameterBufferColorBalance procamp_params[4];
70  VAProcFilterCapColorBalance procamp_caps[VAProcColorBalanceCount];
71  int num_caps;
72  int i = 0;
73 
74  memset(&procamp_params, 0, sizeof(procamp_params));
75  memset(&procamp_caps, 0, sizeof(procamp_caps));
76 
77  num_caps = VAProcColorBalanceCount;
78  vas = vaQueryVideoProcFilterCaps(vpp_ctx->hwctx->display, vpp_ctx->va_context,
79  VAProcFilterColorBalance, &procamp_caps, &num_caps);
80 
81  if (vas != VA_STATUS_SUCCESS) {
82  av_log(avctx, AV_LOG_ERROR, "Failed to query procamp "
83  "filter caps: %d (%s).\n", vas, vaErrorStr(vas));
84  return AVERROR(EIO);
85  }
86 
87  /* brightness */
88  procamp_params[i].type = VAProcFilterColorBalance;
89  procamp_params[i].attrib = VAProcColorBalanceBrightness;
90  procamp_params[i].value = map(ctx->bright, BRIGHTNESS_MIN, BRIGHTNESS_MAX,
91  procamp_caps[VAProcColorBalanceBrightness-1].range.min_value,
92  procamp_caps[VAProcColorBalanceBrightness-1].range.max_value);
93  i++;
94 
95  /* contrast */
96  procamp_params[i].type = VAProcFilterColorBalance;
97  procamp_params[i].attrib = VAProcColorBalanceContrast;
98  procamp_params[i].value = map(ctx->contrast, CONTRAST_MIN, CONTRAST_MAX,
99  procamp_caps[VAProcColorBalanceContrast-1].range.min_value,
100  procamp_caps[VAProcColorBalanceContrast-1].range.max_value);
101  i++;
102 
103  /* hue */
104  procamp_params[i].type = VAProcFilterColorBalance;
105  procamp_params[i].attrib = VAProcColorBalanceHue;
106  procamp_params[i].value = map(ctx->hue, HUE_MIN, HUE_MAX,
107  procamp_caps[VAProcColorBalanceHue-1].range.min_value,
108  procamp_caps[VAProcColorBalanceHue-1].range.max_value);
109  i++;
110 
111  /* saturation */
112  procamp_params[i].type = VAProcFilterColorBalance;
113  procamp_params[i].attrib = VAProcColorBalanceSaturation;
114  procamp_params[i].value = map(ctx->saturation, SATURATION_MIN, SATURATION_MAX,
115  procamp_caps[VAProcColorBalanceSaturation-1].range.min_value,
116  procamp_caps[VAProcColorBalanceSaturation-1].range.max_value);
117  i++;
118 
119  return ff_vaapi_vpp_make_param_buffers(avctx,
120  VAProcFilterParameterBufferType,
121  &procamp_params,
122  sizeof(procamp_params[0]),
123  i);
124 }
125 
127 {
128  AVFilterContext *avctx = inlink->dst;
129  AVFilterLink *outlink = avctx->outputs[0];
130  VAAPIVPPContext *vpp_ctx = avctx->priv;
132  VAProcPipelineParameterBuffer params;
133  int err;
134 
135  av_log(avctx, AV_LOG_DEBUG, "Filter input: %s, %ux%u (%"PRId64").\n",
136  av_get_pix_fmt_name(input_frame->format),
137  input_frame->width, input_frame->height, input_frame->pts);
138 
139  if (vpp_ctx->passthrough)
140  return ff_filter_frame(outlink, input_frame);
141 
142  if (vpp_ctx->va_context == VA_INVALID_ID)
143  return AVERROR(EINVAL);
144 
145  output_frame = ff_get_video_buffer(outlink, vpp_ctx->output_width,
146  vpp_ctx->output_height);
147  if (!output_frame) {
148  err = AVERROR(ENOMEM);
149  goto fail;
150  }
151 
152  err = av_frame_copy_props(output_frame, input_frame);
153  if (err < 0)
154  goto fail;
155 
156  err = ff_vaapi_vpp_init_params(avctx, &params,
157  input_frame, output_frame);
158  if (err < 0)
159  goto fail;
160 
161  params.filters = &vpp_ctx->filter_buffers[0];
162  params.num_filters = 1;
163 
164  err = ff_vaapi_vpp_render_picture(avctx, &params, output_frame);
165  if (err < 0)
166  goto fail;
167 
168  av_frame_free(&input_frame);
169 
170  av_log(avctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
172  output_frame->width, output_frame->height, output_frame->pts);
173 
174  return ff_filter_frame(outlink, output_frame);
175 
176 fail:
177  av_frame_free(&input_frame);
179  return err;
180 }
181 
183 {
184  VAAPIVPPContext *vpp_ctx = avctx->priv;
185  ProcampVAAPIContext *ctx = avctx->priv;
186  float eps = 1.0e-10f;
187 
188  ff_vaapi_vpp_ctx_init(avctx);
191  vpp_ctx->output_format = AV_PIX_FMT_NONE;
192  if (fabs(ctx->saturation - SATURATION_DEFAULT) < eps &&
193  fabs(ctx->bright - BRIGHTNESS_DEFAULT) < eps &&
194  fabs(ctx->contrast - CONTRAST_DEFAULT) < eps &&
195  fabs(ctx->hue - HUE_DEFAULT) < eps)
196  vpp_ctx->passthrough = 1;
197 
198  return 0;
199 }
200 
201 #define OFFSET(x) offsetof(ProcampVAAPIContext, x)
202 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM)
203 static const AVOption procamp_vaapi_options[] = {
204  { "b", "Output video brightness",
206  { "brightness", "Output video brightness",
208  { "s", "Output video saturation",
209  OFFSET(saturation), AV_OPT_TYPE_FLOAT, { .dbl = SATURATION_DEFAULT }, SATURATION_MIN, SATURATION_MAX, .flags = FLAGS },
210  { "saturatio", "Output video saturation",
211  OFFSET(saturation), AV_OPT_TYPE_FLOAT, { .dbl = SATURATION_DEFAULT }, SATURATION_MIN, SATURATION_MAX, .flags = FLAGS },
212  { "c", "Output video contrast",
213  OFFSET(contrast), AV_OPT_TYPE_FLOAT, { .dbl = CONTRAST_DEFAULT }, CONTRAST_MIN, CONTRAST_MAX, .flags = FLAGS },
214  { "contrast", "Output video contrast",
215  OFFSET(contrast), AV_OPT_TYPE_FLOAT, { .dbl = CONTRAST_DEFAULT }, CONTRAST_MIN, CONTRAST_MAX, .flags = FLAGS },
216  { "h", "Output video hue",
217  OFFSET(hue), AV_OPT_TYPE_FLOAT, { .dbl = HUE_DEFAULT }, HUE_MIN, HUE_MAX, .flags = FLAGS },
218  { "hue", "Output video hue",
219  OFFSET(hue), AV_OPT_TYPE_FLOAT, { .dbl = HUE_DEFAULT }, HUE_MIN, HUE_MAX, .flags = FLAGS },
220  { NULL },
221 };
222 
223 AVFILTER_DEFINE_CLASS(procamp_vaapi);
224 
226  {
227  .name = "default",
228  .type = AVMEDIA_TYPE_VIDEO,
229  .filter_frame = &procamp_vaapi_filter_frame,
230  .config_props = &ff_vaapi_vpp_config_input,
231  },
232 };
233 
235  {
236  .name = "default",
237  .type = AVMEDIA_TYPE_VIDEO,
238  .config_props = &ff_vaapi_vpp_config_output,
239  },
240 };
241 
243  .name = "procamp_vaapi",
244  .description = NULL_IF_CONFIG_SMALL("ProcAmp (color balance) adjustments for hue, saturation, brightness, contrast"),
245  .priv_size = sizeof(ProcampVAAPIContext),
251  .priv_class = &procamp_vaapi_class,
252  .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
253 };
procamp_vaapi_outputs
static const AVFilterPad procamp_vaapi_outputs[]
Definition: vf_procamp_vaapi.c:234
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:112
ff_vaapi_vpp_pipeline_uninit
void ff_vaapi_vpp_pipeline_uninit(AVFilterContext *avctx)
Definition: vaapi_vpp.c:48
ff_vaapi_vpp_ctx_init
void ff_vaapi_vpp_ctx_init(AVFilterContext *avctx)
Definition: vaapi_vpp.c:709
procamp_vaapi_filter_frame
static int procamp_vaapi_filter_frame(AVFilterLink *inlink, AVFrame *input_frame)
Definition: vf_procamp_vaapi.c:126
ProcampVAAPIContext::contrast
float contrast
Definition: vf_procamp_vaapi.c:51
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:702
CONTRAST_MAX
#define CONTRAST_MAX
Definition: vf_procamp_vaapi.c:34
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:351
ProcampVAAPIContext::hue
float hue
Definition: vf_procamp_vaapi.c:49
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1018
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
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:130
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:344
pixdesc.h
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:456
AVFrame::width
int width
Definition: frame.h:416
AVOption
AVOption.
Definition: opt.h:346
FILTER_QUERY_FUNC
#define FILTER_QUERY_FUNC(func)
Definition: internal.h:159
procamp_vaapi_options
static const AVOption procamp_vaapi_options[]
Definition: vf_procamp_vaapi.c:203
AVVAAPIDeviceContext::display
VADisplay display
The VADisplay handle, to be filled by the user.
Definition: hwcontext_vaapi.h:72
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:170
video.h
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(procamp_vaapi)
FLAGS
#define FLAGS
Definition: vf_procamp_vaapi.c:202
VAAPIVPPContext::build_filter_params
int(* build_filter_params)(AVFilterContext *avctx)
Definition: vaapi_vpp.h:61
AVFilterContext::priv
void * priv
private data for use by the filter
Definition: avfilter.h:422
fail
#define fail()
Definition: checkasm.h:179
ff_vf_procamp_vaapi
const AVFilter ff_vf_procamp_vaapi
Definition: vf_procamp_vaapi.c:242
ProcampVAAPIContext::bright
float bright
Definition: vf_procamp_vaapi.c:48
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:33
procamp_vaapi_inputs
static const AVFilterPad procamp_vaapi_inputs[]
Definition: vf_procamp_vaapi.c:225
BRIGHTNESS_MIN
#define BRIGHTNESS_MIN
Definition: vf_procamp_vaapi.c:29
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
VAAPIVPPContext::output_width
int output_width
Definition: vaapi_vpp.h:53
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:52
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:577
VAAPIVPPContext::hwctx
AVVAAPIDeviceContext * hwctx
Definition: vaapi_vpp.h:41
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:182
fabs
static __device__ float fabs(float a)
Definition: cuda_runtime.h:182
NULL
#define NULL
Definition: coverity.c:32
HUE_DEFAULT
#define HUE_DEFAULT
Definition: vf_procamp_vaapi.c:39
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:679
CONTRAST_DEFAULT
#define CONTRAST_DEFAULT
Definition: vf_procamp_vaapi.c:35
SATURATION_DEFAULT
#define SATURATION_DEFAULT
Definition: vf_procamp_vaapi.c:43
ff_vaapi_vpp_config_input
int ff_vaapi_vpp_config_input(AVFilterLink *inlink)
Definition: vaapi_vpp.c:74
ff_vaapi_vpp_ctx_uninit
void ff_vaapi_vpp_ctx_uninit(AVFilterContext *avctx)
Definition: vaapi_vpp.c:723
ff_vaapi_vpp_query_formats
int ff_vaapi_vpp_query_formats(AVFilterContext *avctx)
Definition: vaapi_vpp.c:27
vaapi_vpp.h
f
f
Definition: af_crystalizer.c:121
ProcampVAAPIContext
Definition: vf_procamp_vaapi.c:45
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:365
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:106
AVFrame::format
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition: frame.h:431
ProcampVAAPIContext::saturation
float saturation
Definition: vf_procamp_vaapi.c:50
output_frame
static int output_frame(H264Context *h, AVFrame *dst, H264Picture *srcp)
Definition: h264dec.c:874
internal.h
AV_OPT_TYPE_FLOAT
@ AV_OPT_TYPE_FLOAT
Definition: opt.h:238
HUE_MIN
#define HUE_MIN
Definition: vf_procamp_vaapi.c:37
VAAPIVPPContext::output_height
int output_height
Definition: vaapi_vpp.h:54
uninit
static void uninit(AVBSFContext *ctx)
Definition: pcm_rechunk.c:68
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
VAAPIVPPContext::filter_buffers
VABufferID filter_buffers[VAProcFilterCount]
Definition: vaapi_vpp.h:56
map
static float map(float x, float in_min, float in_max, float out_min, float out_max)
Definition: vf_procamp_vaapi.c:54
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:39
SATURATION_MIN
#define SATURATION_MIN
Definition: vf_procamp_vaapi.c:41
AVFilter
Filter definition.
Definition: avfilter.h:166
SATURATION_MAX
#define SATURATION_MAX
Definition: vf_procamp_vaapi.c:42
VAAPIVPPContext
Definition: vaapi_vpp.h:38
VAAPIVPPContext::va_context
VAContextID va_context
Definition: vaapi_vpp.h:46
ProcampVAAPIContext::vpp_ctx
VAAPIVPPContext vpp_ctx
Definition: vf_procamp_vaapi.c:46
procamp_vaapi_build_filter_params
static int procamp_vaapi_build_filter_params(AVFilterContext *avctx)
Definition: vf_procamp_vaapi.c:64
AVFrame::height
int height
Definition: frame.h:416
ff_vaapi_vpp_config_output
int ff_vaapi_vpp_config_output(AVFilterLink *outlink)
Definition: vaapi_vpp.c:99
OFFSET
#define OFFSET(x)
Definition: vf_procamp_vaapi.c:201
BRIGHTNESS_DEFAULT
#define BRIGHTNESS_DEFAULT
Definition: vf_procamp_vaapi.c:31
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
avfilter.h
VAAPIVPPContext::passthrough
int passthrough
Definition: vaapi_vpp.h:59
VAAPIVPPContext::pipeline_uninit
void(* pipeline_uninit)(AVFilterContext *avctx)
Definition: vaapi_vpp.h:63
AVFilterContext
An instance of a filter.
Definition: avfilter.h:407
CONTRAST_MIN
#define CONTRAST_MIN
Definition: vf_procamp_vaapi.c:33
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
HUE_MAX
#define HUE_MAX
Definition: vf_procamp_vaapi.c:38
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:183
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
procamp_vaapi_init
static av_cold int procamp_vaapi_init(AVFilterContext *avctx)
Definition: vf_procamp_vaapi.c:182
BRIGHTNESS_MAX
#define BRIGHTNESS_MAX
Definition: vf_procamp_vaapi.c:30
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:2882
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:528
AVFilterContext::outputs
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:419