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 VASurfaceID input_surface, output_surface;
93 VAProcPipelineParameterBuffer params;
94 VARectangle input_region;
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);
101 if (vpp_ctx->va_context == VA_INVALID_ID)
102 return AVERROR(EINVAL);
104 input_surface = (VASurfaceID)(uintptr_t)input_frame->data[3];
105 av_log(avctx, AV_LOG_DEBUG, "Using surface %#x for scale input.\n",
108 output_frame = ff_get_video_buffer(outlink, vpp_ctx->output_width,
109 vpp_ctx->output_height);
111 err = AVERROR(ENOMEM);
115 output_surface = (VASurfaceID)(uintptr_t)output_frame->data[3];
116 av_log(avctx, AV_LOG_DEBUG, "Using surface %#x for scale output.\n",
119 memset(¶ms, 0, sizeof(params));
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),
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);
135 params.output_region = 0;
136 params.output_background_color = VAAPI_VPP_BACKGROUND_BLACK;
137 params.output_color_standard = params.surface_color_standard;
139 params.pipeline_flags = 0;
140 params.filter_flags = ctx->mode;
142 err = ff_vaapi_vpp_render_picture(avctx, ¶ms, output_surface);
146 err = av_frame_copy_props(output_frame, input_frame);
150 av_frame_free(&input_frame);
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));
157 return ff_filter_frame(outlink, output_frame);
160 av_frame_free(&input_frame);
161 av_frame_free(&output_frame);
165 static av_cold int scale_vaapi_init(AVFilterContext *avctx)
167 VAAPIVPPContext *vpp_ctx = avctx->priv;
168 ScaleVAAPIContext *ctx = avctx->priv;
170 ff_vaapi_vpp_ctx_init(avctx);
171 vpp_ctx->pipeline_uninit = ff_vaapi_vpp_pipeline_uninit;
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);
180 // Use the input format once that is configured.
181 vpp_ctx->output_format = AV_PIX_FMT_NONE;
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" },
210 AVFILTER_DEFINE_CLASS(scale_vaapi);
212 static const AVFilterPad scale_vaapi_inputs[] = {
215 .type = AVMEDIA_TYPE_VIDEO,
216 .filter_frame = &scale_vaapi_filter_frame,
217 .config_props = &ff_vaapi_vpp_config_input,
222 static const AVFilterPad scale_vaapi_outputs[] = {
225 .type = AVMEDIA_TYPE_VIDEO,
226 .config_props = &scale_vaapi_config_output,
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,