]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_procamp_vaapi.c
avfilter/Makefile: skip compiling vaapi_vpp.h when vaapi is not enabled
[ffmpeg] / libavfilter / vf_procamp_vaapi.c
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/avassert.h"
21 #include "libavutil/mem.h"
22 #include "libavutil/opt.h"
23 #include "libavutil/pixdesc.h"
24
25 #include "avfilter.h"
26 #include "formats.h"
27 #include "internal.h"
28 #include "vaapi_vpp.h"
29
30 // ProcAmp Min/Max/Default Values
31 #define BRIGHTNESS_MIN     -100.0F
32 #define BRIGHTNESS_MAX      100.0F
33 #define BRIGHTNESS_DEFAULT    0.0F
34
35 #define CONTRAST_MIN          0.0F
36 #define CONTRAST_MAX         10.0F
37 #define CONTRAST_DEFAULT      1.0F
38
39 #define HUE_MIN            -180.0F
40 #define HUE_MAX             180.0F
41 #define HUE_DEFAULT           0.0F
42
43 #define SATURATION_MIN        0.0F
44 #define SATURATION_MAX       10.0F
45 #define SATURATION_DEFAULT    1.0F
46
47 #define EPSILON               0.00001F
48
49 typedef struct ProcampVAAPIContext {
50     VAAPIVPPContext vpp_ctx; // must be the first fileld
51
52     float bright;
53     float hue;
54     float saturation;
55     float contrast;
56 } ProcampVAAPIContext;
57
58 static float map(float x, float in_min, float in_max, float out_min, float out_max)
59 {
60     double slope, output;
61
62     slope = 1.0 * (out_max - out_min) / (in_max - in_min);
63     output = out_min + slope * (x - in_min);
64
65     return (float)output;
66 }
67
68 static int fequal(float a, float b)
69 {
70     return fabs(a-b) < EPSILON;
71 }
72
73 static int procamp_vaapi_build_filter_params(AVFilterContext *avctx)
74 {
75     VAAPIVPPContext *vpp_ctx = avctx->priv;
76     ProcampVAAPIContext *ctx = avctx->priv;
77     VAStatus vas;
78     VAProcFilterParameterBufferColorBalance procamp_params[4];
79     VAProcFilterCapColorBalance procamp_caps[VAProcColorBalanceCount];
80     int num_caps;
81     int i = 0;
82
83     memset(&procamp_params, 0, sizeof(procamp_params));
84     memset(&procamp_caps, 0, sizeof(procamp_caps));
85
86     num_caps = VAProcColorBalanceCount;
87     vas = vaQueryVideoProcFilterCaps(vpp_ctx->hwctx->display, vpp_ctx->va_context,
88                                      VAProcFilterColorBalance, &procamp_caps, &num_caps);
89
90     if (vas != VA_STATUS_SUCCESS) {
91         av_log(avctx, AV_LOG_ERROR, "Failed to query procamp "
92                "filter caps: %d (%s).\n", vas, vaErrorStr(vas));
93         return AVERROR(EIO);
94     }
95
96     if (!fequal(ctx->bright, BRIGHTNESS_DEFAULT)) {
97         procamp_params[i].type   = VAProcFilterColorBalance;
98         procamp_params[i].attrib = VAProcColorBalanceBrightness;
99         procamp_params[i].value  = map(ctx->bright, BRIGHTNESS_MIN, BRIGHTNESS_MAX,
100                                        procamp_caps[VAProcColorBalanceBrightness-1].range.min_value,
101                                        procamp_caps[VAProcColorBalanceBrightness-1].range.max_value);
102         i++;
103     }
104
105     if (!fequal(ctx->contrast, CONTRAST_DEFAULT)) {
106         procamp_params[i].type   = VAProcFilterColorBalance;
107         procamp_params[i].attrib = VAProcColorBalanceContrast;
108         procamp_params[i].value  = map(ctx->contrast, CONTRAST_MIN, CONTRAST_MAX,
109                                        procamp_caps[VAProcColorBalanceContrast-1].range.min_value,
110                                        procamp_caps[VAProcColorBalanceContrast-1].range.max_value);
111         i++;
112     }
113
114     if (!fequal(ctx->hue, HUE_DEFAULT)) {
115         procamp_params[i].type   = VAProcFilterColorBalance;
116         procamp_params[i].attrib = VAProcColorBalanceHue;
117         procamp_params[i].value  = map(ctx->hue, HUE_MIN, HUE_MAX,
118                                        procamp_caps[VAProcColorBalanceHue-1].range.min_value,
119                                        procamp_caps[VAProcColorBalanceHue-1].range.max_value);
120         i++;
121     }
122
123     if (!fequal(ctx->saturation, SATURATION_DEFAULT)) {
124         procamp_params[i].type   = VAProcFilterColorBalance;
125         procamp_params[i].attrib = VAProcColorBalanceSaturation;
126         procamp_params[i].value  = map(ctx->saturation, SATURATION_MIN, SATURATION_MAX,
127                                        procamp_caps[VAProcColorBalanceSaturation-1].range.min_value,
128                                        procamp_caps[VAProcColorBalanceSaturation-1].range.max_value);
129         i++;
130     }
131
132     return ff_vaapi_vpp_make_param_buffers(avctx,
133                                            VAProcFilterParameterBufferType,
134                                            &procamp_params,
135                                            sizeof(procamp_params[0]),
136                                            i);
137 }
138
139 static int procamp_vaapi_filter_frame(AVFilterLink *inlink, AVFrame *input_frame)
140 {
141     AVFilterContext *avctx   = inlink->dst;
142     AVFilterLink *outlink    = avctx->outputs[0];
143     VAAPIVPPContext *vpp_ctx = avctx->priv;
144     AVFrame *output_frame = NULL;
145     VASurfaceID input_surface, output_surface;
146     VAProcPipelineParameterBuffer params;
147     VARectangle input_region;
148     int err;
149
150     av_log(avctx, AV_LOG_DEBUG, "Filter input: %s, %ux%u (%"PRId64").\n",
151            av_get_pix_fmt_name(input_frame->format),
152            input_frame->width, input_frame->height, input_frame->pts);
153
154     if (vpp_ctx->va_context == VA_INVALID_ID)
155         return AVERROR(EINVAL);
156
157     input_surface = (VASurfaceID)(uintptr_t)input_frame->data[3];
158     av_log(avctx, AV_LOG_DEBUG, "Using surface %#x for procamp input.\n",
159            input_surface);
160
161     output_frame = ff_get_video_buffer(outlink, vpp_ctx->output_width,
162                                        vpp_ctx->output_height);
163     if (!output_frame) {
164         err = AVERROR(ENOMEM);
165         goto fail;
166     }
167
168     output_surface = (VASurfaceID)(uintptr_t)output_frame->data[3];
169     av_log(avctx, AV_LOG_DEBUG, "Using surface %#x for procamp output.\n",
170            output_surface);
171     memset(&params, 0, sizeof(params));
172     input_region = (VARectangle) {
173         .x      = 0,
174         .y      = 0,
175         .width  = input_frame->width,
176         .height = input_frame->height,
177     };
178
179     params.surface = input_surface;
180     params.surface_region = &input_region;
181     params.surface_color_standard =
182         ff_vaapi_vpp_colour_standard(input_frame->colorspace);
183
184     params.output_region = NULL;
185     params.output_background_color = 0xff000000;
186     params.output_color_standard = params.surface_color_standard;
187
188     params.pipeline_flags = 0;
189     params.filter_flags = VA_FRAME_PICTURE;
190
191     params.filters     = &vpp_ctx->filter_buffers[0];
192     params.num_filters = 1;
193
194     err = ff_vaapi_vpp_render_picture(avctx, &params, output_surface);
195     if (err < 0)
196         goto fail;
197
198     err = av_frame_copy_props(output_frame, input_frame);
199     if (err < 0)
200         goto fail;
201     av_frame_free(&input_frame);
202
203     av_log(avctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
204            av_get_pix_fmt_name(output_frame->format),
205            output_frame->width, output_frame->height, output_frame->pts);
206
207     return ff_filter_frame(outlink, output_frame);
208
209 fail:
210     av_frame_free(&input_frame);
211     av_frame_free(&output_frame);
212     return err;
213 }
214
215 static av_cold int procamp_vaapi_init(AVFilterContext *avctx)
216 {
217     VAAPIVPPContext *vpp_ctx = avctx->priv;
218
219     ff_vaapi_vpp_ctx_init(avctx);
220     vpp_ctx->pipeline_uninit     = ff_vaapi_vpp_pipeline_uninit;
221     vpp_ctx->build_filter_params = procamp_vaapi_build_filter_params;
222     vpp_ctx->output_format       = AV_PIX_FMT_NONE;
223
224     return 0;
225 }
226
227 #define OFFSET(x) offsetof(ProcampVAAPIContext, x)
228 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM)
229 static const AVOption procamp_vaapi_options[] = {
230     { "b", "Output video brightness",
231       OFFSET(bright),  AV_OPT_TYPE_FLOAT, { .dbl = BRIGHTNESS_DEFAULT }, BRIGHTNESS_MIN, BRIGHTNESS_MAX, .flags = FLAGS },
232     { "brightness", "Output video brightness",
233       OFFSET(bright),  AV_OPT_TYPE_FLOAT, { .dbl = BRIGHTNESS_DEFAULT }, BRIGHTNESS_MIN, BRIGHTNESS_MAX, .flags = FLAGS },
234     { "s", "Output video saturation",
235       OFFSET(saturation), AV_OPT_TYPE_FLOAT, { .dbl = SATURATION_DEFAULT }, SATURATION_MIN, SATURATION_MAX, .flags = FLAGS },
236     { "saturatio", "Output video saturation",
237       OFFSET(saturation), AV_OPT_TYPE_FLOAT, { .dbl = SATURATION_DEFAULT }, SATURATION_MIN, SATURATION_MAX, .flags = FLAGS },
238     { "c", "Output video contrast",
239       OFFSET(contrast),  AV_OPT_TYPE_FLOAT, { .dbl = CONTRAST_DEFAULT }, CONTRAST_MIN, CONTRAST_MAX, .flags = FLAGS },
240     { "contrast", "Output video contrast",
241       OFFSET(contrast),  AV_OPT_TYPE_FLOAT, { .dbl = CONTRAST_DEFAULT }, CONTRAST_MIN, CONTRAST_MAX, .flags = FLAGS },
242     { "h", "Output video hue",
243       OFFSET(hue), AV_OPT_TYPE_FLOAT, { .dbl = HUE_DEFAULT }, HUE_MIN, HUE_MAX, .flags = FLAGS },
244     { "hue", "Output video hue",
245       OFFSET(hue), AV_OPT_TYPE_FLOAT, { .dbl = HUE_DEFAULT }, HUE_MIN, HUE_MAX, .flags = FLAGS },
246     { NULL },
247 };
248
249 AVFILTER_DEFINE_CLASS(procamp_vaapi);
250
251 static const AVFilterPad procamp_vaapi_inputs[] = {
252     {
253         .name         = "default",
254         .type         = AVMEDIA_TYPE_VIDEO,
255         .filter_frame = &procamp_vaapi_filter_frame,
256         .config_props = &ff_vaapi_vpp_config_input,
257     },
258     { NULL }
259 };
260
261 static const AVFilterPad procamp_vaapi_outputs[] = {
262     {
263         .name = "default",
264         .type = AVMEDIA_TYPE_VIDEO,
265         .config_props = &ff_vaapi_vpp_config_output,
266     },
267     { NULL }
268 };
269
270 AVFilter ff_vf_procamp_vaapi = {
271     .name          = "procamp_vaapi",
272     .description   = NULL_IF_CONFIG_SMALL("ProcAmp (color balance) adjustments for hue, saturation, brightness, contrast"),
273     .priv_size     = sizeof(ProcampVAAPIContext),
274     .init          = &procamp_vaapi_init,
275     .uninit        = &ff_vaapi_vpp_ctx_uninit,
276     .query_formats = &ff_vaapi_vpp_query_formats,
277     .inputs        = procamp_vaapi_inputs,
278     .outputs       = procamp_vaapi_outputs,
279     .priv_class    = &procamp_vaapi_class,
280     .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
281 };