]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_scale_vaapi.c
Merge commit '896fe15dbb7b78de495c4a7dd75e7faec66778da'
[ffmpeg] / libavfilter / vf_scale_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
19 #include <string.h>
20
21 #include "libavutil/avassert.h"
22 #include "libavutil/mem.h"
23 #include "libavutil/opt.h"
24 #include "libavutil/pixdesc.h"
25
26 #include "avfilter.h"
27 #include "formats.h"
28 #include "internal.h"
29 #include "scale.h"
30 #include "video.h"
31 #include "vaapi_vpp.h"
32
33 typedef struct ScaleVAAPIContext {
34     VAAPIVPPContext vpp_ctx; // must be the first field
35
36     char *output_format_string;
37
38     int   mode;
39
40     char *w_expr;      // width expression string
41     char *h_expr;      // height expression string
42 } ScaleVAAPIContext;
43
44 static const char *scale_vaapi_mode_name(int mode)
45 {
46     switch (mode) {
47 #define D(name) case VA_FILTER_SCALING_ ## name: return #name
48         D(DEFAULT);
49         D(FAST);
50         D(HQ);
51         D(NL_ANAMORPHIC);
52 #undef D
53     default:
54         return "Invalid";
55     }
56 }
57
58
59 static int scale_vaapi_config_output(AVFilterLink *outlink)
60 {
61     AVFilterLink *inlink     = outlink->src->inputs[0];
62     AVFilterContext *avctx   = outlink->src;
63     VAAPIVPPContext *vpp_ctx = avctx->priv;
64     ScaleVAAPIContext *ctx   = avctx->priv;
65     int err;
66
67     if ((err = ff_scale_eval_dimensions(ctx,
68                                         ctx->w_expr, ctx->h_expr,
69                                         inlink, outlink,
70                                         &vpp_ctx->output_width, &vpp_ctx->output_height)) < 0)
71         return err;
72
73     err = ff_vaapi_vpp_config_output(outlink);
74     if (err < 0)
75         return err;
76
77     if (inlink->sample_aspect_ratio.num)
78         outlink->sample_aspect_ratio = av_mul_q((AVRational){outlink->h * inlink->w, outlink->w * inlink->h}, inlink->sample_aspect_ratio);
79     else
80         outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
81
82     return 0;
83 }
84
85 static int scale_vaapi_filter_frame(AVFilterLink *inlink, AVFrame *input_frame)
86 {
87     AVFilterContext *avctx   = inlink->dst;
88     AVFilterLink *outlink    = avctx->outputs[0];
89     VAAPIVPPContext *vpp_ctx = avctx->priv;
90     ScaleVAAPIContext *ctx   = avctx->priv;
91     AVFrame *output_frame    = NULL;
92     VASurfaceID input_surface, output_surface;
93     VAProcPipelineParameterBuffer params;
94     VARectangle input_region;
95     int err;
96
97     av_log(avctx, AV_LOG_DEBUG, "Filter input: %s, %ux%u (%"PRId64").\n",
98            av_get_pix_fmt_name(input_frame->format),
99            input_frame->width, input_frame->height, input_frame->pts);
100
101     if (vpp_ctx->va_context == VA_INVALID_ID)
102         return AVERROR(EINVAL);
103
104     input_surface = (VASurfaceID)(uintptr_t)input_frame->data[3];
105     av_log(avctx, AV_LOG_DEBUG, "Using surface %#x for scale input.\n",
106            input_surface);
107
108     output_frame = ff_get_video_buffer(outlink, vpp_ctx->output_width,
109                                        vpp_ctx->output_height);
110     if (!output_frame) {
111         err = AVERROR(ENOMEM);
112         goto fail;
113     }
114
115     output_surface = (VASurfaceID)(uintptr_t)output_frame->data[3];
116     av_log(avctx, AV_LOG_DEBUG, "Using surface %#x for scale output.\n",
117            output_surface);
118
119     memset(&params, 0, sizeof(params));
120
121     input_region = (VARectangle) {
122         .x      = input_frame->crop_left,
123         .y      = input_frame->crop_top,
124         .width  = input_frame->width -
125                  (input_frame->crop_left + input_frame->crop_right),
126         .height = input_frame->height -
127                  (input_frame->crop_top + input_frame->crop_bottom),
128     };
129
130     params.surface = input_surface;
131     params.surface_region = &input_region;
132     params.surface_color_standard =
133         ff_vaapi_vpp_colour_standard(input_frame->colorspace);
134
135     params.output_region = 0;
136     params.output_background_color = VAAPI_VPP_BACKGROUND_BLACK;
137     params.output_color_standard = params.surface_color_standard;
138
139     params.pipeline_flags = 0;
140     params.filter_flags = ctx->mode;
141
142     err = ff_vaapi_vpp_render_picture(avctx, &params, output_surface);
143     if (err < 0)
144         goto fail;
145
146     err = av_frame_copy_props(output_frame, input_frame);
147     if (err < 0)
148         goto fail;
149
150     av_frame_free(&input_frame);
151
152     av_log(avctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64"), mode: %s.\n",
153            av_get_pix_fmt_name(output_frame->format),
154            output_frame->width, output_frame->height, output_frame->pts,
155            scale_vaapi_mode_name(ctx->mode));
156
157     return ff_filter_frame(outlink, output_frame);
158
159 fail:
160     av_frame_free(&input_frame);
161     av_frame_free(&output_frame);
162     return err;
163 }
164
165 static av_cold int scale_vaapi_init(AVFilterContext *avctx)
166 {
167     VAAPIVPPContext *vpp_ctx = avctx->priv;
168     ScaleVAAPIContext *ctx   = avctx->priv;
169
170     ff_vaapi_vpp_ctx_init(avctx);
171     vpp_ctx->pipeline_uninit = ff_vaapi_vpp_pipeline_uninit;
172
173     if (ctx->output_format_string) {
174         vpp_ctx->output_format = av_get_pix_fmt(ctx->output_format_string);
175         if (vpp_ctx->output_format == AV_PIX_FMT_NONE) {
176             av_log(avctx, AV_LOG_ERROR, "Invalid output format.\n");
177             return AVERROR(EINVAL);
178         }
179     } else {
180         // Use the input format once that is configured.
181         vpp_ctx->output_format = AV_PIX_FMT_NONE;
182     }
183
184     return 0;
185 }
186
187 #define OFFSET(x) offsetof(ScaleVAAPIContext, x)
188 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM)
189 static const AVOption scale_vaapi_options[] = {
190     { "w", "Output video width",
191       OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, .flags = FLAGS },
192     { "h", "Output video height",
193       OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, .flags = FLAGS },
194     { "format", "Output video format (software format of hardware frames)",
195       OFFSET(output_format_string), AV_OPT_TYPE_STRING, .flags = FLAGS },
196     { "mode", "Scaling mode",
197       OFFSET(mode), AV_OPT_TYPE_INT, { .i64 = VA_FILTER_SCALING_HQ },
198       0, VA_FILTER_SCALING_NL_ANAMORPHIC, FLAGS, "mode" },
199         { "default", "Use the default (depend on the driver) scaling algorithm",
200           0, AV_OPT_TYPE_CONST, { .i64 = VA_FILTER_SCALING_DEFAULT }, 0, 0, FLAGS, "mode" },
201         { "fast", "Use fast scaling algorithm",
202           0, AV_OPT_TYPE_CONST, { .i64 = VA_FILTER_SCALING_FAST }, 0, 0, FLAGS, "mode" },
203         { "hq", "Use high quality scaling algorithm",
204           0, AV_OPT_TYPE_CONST, { .i64 = VA_FILTER_SCALING_HQ }, 0, 0, FLAGS,  "mode" },
205         { "nl_anamorphic", "Use nolinear anamorphic scaling algorithm",
206           0, AV_OPT_TYPE_CONST, { .i64 = VA_FILTER_SCALING_NL_ANAMORPHIC }, 0, 0, FLAGS,  "mode" },
207     { NULL },
208 };
209
210 AVFILTER_DEFINE_CLASS(scale_vaapi);
211
212 static const AVFilterPad scale_vaapi_inputs[] = {
213     {
214         .name         = "default",
215         .type         = AVMEDIA_TYPE_VIDEO,
216         .filter_frame = &scale_vaapi_filter_frame,
217         .config_props = &ff_vaapi_vpp_config_input,
218     },
219     { NULL }
220 };
221
222 static const AVFilterPad scale_vaapi_outputs[] = {
223     {
224         .name = "default",
225         .type = AVMEDIA_TYPE_VIDEO,
226         .config_props = &scale_vaapi_config_output,
227     },
228     { NULL }
229 };
230
231 AVFilter ff_vf_scale_vaapi = {
232     .name          = "scale_vaapi",
233     .description   = NULL_IF_CONFIG_SMALL("Scale to/from VAAPI surfaces."),
234     .priv_size     = sizeof(ScaleVAAPIContext),
235     .init          = &scale_vaapi_init,
236     .uninit        = &ff_vaapi_vpp_ctx_uninit,
237     .query_formats = &ff_vaapi_vpp_query_formats,
238     .inputs        = scale_vaapi_inputs,
239     .outputs       = scale_vaapi_outputs,
240     .priv_class    = &scale_vaapi_class,
241     .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
242 };