]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_scale_vaapi.c
lavfi/vaapi: Factorise out common code for parameter buffer setup
[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     VAProcPipelineParameterBuffer params;
93     int err;
94
95     av_log(avctx, AV_LOG_DEBUG, "Filter input: %s, %ux%u (%"PRId64").\n",
96            av_get_pix_fmt_name(input_frame->format),
97            input_frame->width, input_frame->height, input_frame->pts);
98
99     if (vpp_ctx->va_context == VA_INVALID_ID)
100         return AVERROR(EINVAL);
101
102     output_frame = ff_get_video_buffer(outlink, vpp_ctx->output_width,
103                                        vpp_ctx->output_height);
104     if (!output_frame) {
105         err = AVERROR(ENOMEM);
106         goto fail;
107     }
108
109     err = ff_vaapi_vpp_init_params(avctx, &params,
110                                    input_frame, output_frame);
111     if (err < 0)
112         goto fail;
113
114     params.filter_flags |= ctx->mode;
115
116     err = ff_vaapi_vpp_render_picture(avctx, &params, output_frame);
117     if (err < 0)
118         goto fail;
119
120     err = av_frame_copy_props(output_frame, input_frame);
121     if (err < 0)
122         goto fail;
123
124     av_frame_free(&input_frame);
125
126     av_log(avctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64"), mode: %s.\n",
127            av_get_pix_fmt_name(output_frame->format),
128            output_frame->width, output_frame->height, output_frame->pts,
129            scale_vaapi_mode_name(ctx->mode));
130
131     return ff_filter_frame(outlink, output_frame);
132
133 fail:
134     av_frame_free(&input_frame);
135     av_frame_free(&output_frame);
136     return err;
137 }
138
139 static av_cold int scale_vaapi_init(AVFilterContext *avctx)
140 {
141     VAAPIVPPContext *vpp_ctx = avctx->priv;
142     ScaleVAAPIContext *ctx   = avctx->priv;
143
144     ff_vaapi_vpp_ctx_init(avctx);
145     vpp_ctx->pipeline_uninit = ff_vaapi_vpp_pipeline_uninit;
146
147     if (ctx->output_format_string) {
148         vpp_ctx->output_format = av_get_pix_fmt(ctx->output_format_string);
149         if (vpp_ctx->output_format == AV_PIX_FMT_NONE) {
150             av_log(avctx, AV_LOG_ERROR, "Invalid output format.\n");
151             return AVERROR(EINVAL);
152         }
153     } else {
154         // Use the input format once that is configured.
155         vpp_ctx->output_format = AV_PIX_FMT_NONE;
156     }
157
158     return 0;
159 }
160
161 #define OFFSET(x) offsetof(ScaleVAAPIContext, x)
162 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM)
163 static const AVOption scale_vaapi_options[] = {
164     { "w", "Output video width",
165       OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, .flags = FLAGS },
166     { "h", "Output video height",
167       OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, .flags = FLAGS },
168     { "format", "Output video format (software format of hardware frames)",
169       OFFSET(output_format_string), AV_OPT_TYPE_STRING, .flags = FLAGS },
170     { "mode", "Scaling mode",
171       OFFSET(mode), AV_OPT_TYPE_INT, { .i64 = VA_FILTER_SCALING_HQ },
172       0, VA_FILTER_SCALING_NL_ANAMORPHIC, FLAGS, "mode" },
173         { "default", "Use the default (depend on the driver) scaling algorithm",
174           0, AV_OPT_TYPE_CONST, { .i64 = VA_FILTER_SCALING_DEFAULT }, 0, 0, FLAGS, "mode" },
175         { "fast", "Use fast scaling algorithm",
176           0, AV_OPT_TYPE_CONST, { .i64 = VA_FILTER_SCALING_FAST }, 0, 0, FLAGS, "mode" },
177         { "hq", "Use high quality scaling algorithm",
178           0, AV_OPT_TYPE_CONST, { .i64 = VA_FILTER_SCALING_HQ }, 0, 0, FLAGS,  "mode" },
179         { "nl_anamorphic", "Use nolinear anamorphic scaling algorithm",
180           0, AV_OPT_TYPE_CONST, { .i64 = VA_FILTER_SCALING_NL_ANAMORPHIC }, 0, 0, FLAGS,  "mode" },
181     { NULL },
182 };
183
184 AVFILTER_DEFINE_CLASS(scale_vaapi);
185
186 static const AVFilterPad scale_vaapi_inputs[] = {
187     {
188         .name         = "default",
189         .type         = AVMEDIA_TYPE_VIDEO,
190         .filter_frame = &scale_vaapi_filter_frame,
191         .config_props = &ff_vaapi_vpp_config_input,
192     },
193     { NULL }
194 };
195
196 static const AVFilterPad scale_vaapi_outputs[] = {
197     {
198         .name = "default",
199         .type = AVMEDIA_TYPE_VIDEO,
200         .config_props = &scale_vaapi_config_output,
201     },
202     { NULL }
203 };
204
205 AVFilter ff_vf_scale_vaapi = {
206     .name          = "scale_vaapi",
207     .description   = NULL_IF_CONFIG_SMALL("Scale to/from VAAPI surfaces."),
208     .priv_size     = sizeof(ScaleVAAPIContext),
209     .init          = &scale_vaapi_init,
210     .uninit        = &ff_vaapi_vpp_ctx_uninit,
211     .query_formats = &ff_vaapi_vpp_query_formats,
212     .inputs        = scale_vaapi_inputs,
213     .outputs       = scale_vaapi_outputs,
214     .priv_class    = &scale_vaapi_class,
215     .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
216 };