2 * This file is part of FFmpeg.
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.
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.
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
21 #include "libavutil/avassert.h"
22 #include "libavutil/mem.h"
23 #include "libavutil/opt.h"
24 #include "libavutil/pixdesc.h"
31 #include "vaapi_vpp.h"
33 typedef struct ScaleVAAPIContext {
34 VAAPIVPPContext vpp_ctx; // must be the first field
36 char *output_format_string;
40 char *w_expr; // width expression string
41 char *h_expr; // height expression string
44 static const char *scale_vaapi_mode_name(int mode)
47 #define D(name) case VA_FILTER_SCALING_ ## name: return #name
59 static int scale_vaapi_config_output(AVFilterLink *outlink)
61 AVFilterLink *inlink = outlink->src->inputs[0];
62 AVFilterContext *avctx = outlink->src;
63 VAAPIVPPContext *vpp_ctx = avctx->priv;
64 ScaleVAAPIContext *ctx = avctx->priv;
67 if ((err = ff_scale_eval_dimensions(ctx,
68 ctx->w_expr, ctx->h_expr,
70 &vpp_ctx->output_width, &vpp_ctx->output_height)) < 0)
73 err = ff_vaapi_vpp_config_output(outlink);
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);
80 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
85 static int scale_vaapi_filter_frame(AVFilterLink *inlink, AVFrame *input_frame)
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;
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);
99 if (vpp_ctx->va_context == VA_INVALID_ID)
100 return AVERROR(EINVAL);
102 output_frame = ff_get_video_buffer(outlink, vpp_ctx->output_width,
103 vpp_ctx->output_height);
105 err = AVERROR(ENOMEM);
109 err = av_frame_copy_props(output_frame, input_frame);
113 err = ff_vaapi_vpp_init_params(avctx, ¶ms,
114 input_frame, output_frame);
118 params.filter_flags |= ctx->mode;
120 err = ff_vaapi_vpp_render_picture(avctx, ¶ms, output_frame);
124 av_frame_free(&input_frame);
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));
131 return ff_filter_frame(outlink, output_frame);
134 av_frame_free(&input_frame);
135 av_frame_free(&output_frame);
139 static av_cold int scale_vaapi_init(AVFilterContext *avctx)
141 VAAPIVPPContext *vpp_ctx = avctx->priv;
142 ScaleVAAPIContext *ctx = avctx->priv;
144 ff_vaapi_vpp_ctx_init(avctx);
145 vpp_ctx->pipeline_uninit = ff_vaapi_vpp_pipeline_uninit;
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);
154 // Use the input format once that is configured.
155 vpp_ctx->output_format = AV_PIX_FMT_NONE;
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" },
184 AVFILTER_DEFINE_CLASS(scale_vaapi);
186 static const AVFilterPad scale_vaapi_inputs[] = {
189 .type = AVMEDIA_TYPE_VIDEO,
190 .filter_frame = &scale_vaapi_filter_frame,
191 .config_props = &ff_vaapi_vpp_config_input,
196 static const AVFilterPad scale_vaapi_outputs[] = {
199 .type = AVMEDIA_TYPE_VIDEO,
200 .config_props = &scale_vaapi_config_output,
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,