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;
38 char *w_expr; // width expression string
39 char *h_expr; // height expression string
42 static int scale_vaapi_config_output(AVFilterLink *outlink)
44 AVFilterLink *inlink = outlink->src->inputs[0];
45 AVFilterContext *avctx = outlink->src;
46 VAAPIVPPContext *vpp_ctx = avctx->priv;
47 ScaleVAAPIContext *ctx = avctx->priv;
50 if ((err = ff_scale_eval_dimensions(ctx,
51 ctx->w_expr, ctx->h_expr,
53 &vpp_ctx->output_width, &vpp_ctx->output_height)) < 0)
56 err = ff_vaapi_vpp_config_output(outlink);
60 if (inlink->sample_aspect_ratio.num)
61 outlink->sample_aspect_ratio = av_mul_q((AVRational){outlink->h * inlink->w, outlink->w * inlink->h}, inlink->sample_aspect_ratio);
63 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
68 static int scale_vaapi_filter_frame(AVFilterLink *inlink, AVFrame *input_frame)
70 AVFilterContext *avctx = inlink->dst;
71 AVFilterLink *outlink = avctx->outputs[0];
72 VAAPIVPPContext *vpp_ctx = avctx->priv;
73 AVFrame *output_frame = NULL;
74 VASurfaceID input_surface, output_surface;
75 VAProcPipelineParameterBuffer params;
76 VARectangle input_region;
79 av_log(avctx, AV_LOG_DEBUG, "Filter input: %s, %ux%u (%"PRId64").\n",
80 av_get_pix_fmt_name(input_frame->format),
81 input_frame->width, input_frame->height, input_frame->pts);
83 if (vpp_ctx->va_context == VA_INVALID_ID)
84 return AVERROR(EINVAL);
86 input_surface = (VASurfaceID)(uintptr_t)input_frame->data[3];
87 av_log(avctx, AV_LOG_DEBUG, "Using surface %#x for scale input.\n",
90 output_frame = ff_get_video_buffer(outlink, vpp_ctx->output_width,
91 vpp_ctx->output_height);
93 err = AVERROR(ENOMEM);
97 output_surface = (VASurfaceID)(uintptr_t)output_frame->data[3];
98 av_log(avctx, AV_LOG_DEBUG, "Using surface %#x for scale output.\n",
101 memset(¶ms, 0, sizeof(params));
103 input_region = (VARectangle) {
104 .x = input_frame->crop_left,
105 .y = input_frame->crop_top,
106 .width = input_frame->width -
107 (input_frame->crop_left + input_frame->crop_right),
108 .height = input_frame->height -
109 (input_frame->crop_top + input_frame->crop_bottom),
112 params.surface = input_surface;
113 params.surface_region = &input_region;
114 params.surface_color_standard =
115 ff_vaapi_vpp_colour_standard(input_frame->colorspace);
117 params.output_region = 0;
118 params.output_background_color = 0xff000000;
119 params.output_color_standard = params.surface_color_standard;
121 params.pipeline_flags = 0;
122 params.filter_flags = VA_FILTER_SCALING_HQ;
124 err = ff_vaapi_vpp_render_picture(avctx, ¶ms, output_surface);
128 err = av_frame_copy_props(output_frame, input_frame);
132 av_frame_free(&input_frame);
134 av_log(avctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
135 av_get_pix_fmt_name(output_frame->format),
136 output_frame->width, output_frame->height, output_frame->pts);
138 return ff_filter_frame(outlink, output_frame);
141 av_frame_free(&input_frame);
142 av_frame_free(&output_frame);
146 static av_cold int scale_vaapi_init(AVFilterContext *avctx)
148 VAAPIVPPContext *vpp_ctx = avctx->priv;
149 ScaleVAAPIContext *ctx = avctx->priv;
151 ff_vaapi_vpp_ctx_init(avctx);
152 vpp_ctx->pipeline_uninit = ff_vaapi_vpp_pipeline_uninit;
154 if (ctx->output_format_string) {
155 vpp_ctx->output_format = av_get_pix_fmt(ctx->output_format_string);
156 if (vpp_ctx->output_format == AV_PIX_FMT_NONE) {
157 av_log(avctx, AV_LOG_ERROR, "Invalid output format.\n");
158 return AVERROR(EINVAL);
161 // Use the input format once that is configured.
162 vpp_ctx->output_format = AV_PIX_FMT_NONE;
168 #define OFFSET(x) offsetof(ScaleVAAPIContext, x)
169 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM)
170 static const AVOption scale_vaapi_options[] = {
171 { "w", "Output video width",
172 OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, .flags = FLAGS },
173 { "h", "Output video height",
174 OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, .flags = FLAGS },
175 { "format", "Output video format (software format of hardware frames)",
176 OFFSET(output_format_string), AV_OPT_TYPE_STRING, .flags = FLAGS },
180 AVFILTER_DEFINE_CLASS(scale_vaapi);
182 static const AVFilterPad scale_vaapi_inputs[] = {
185 .type = AVMEDIA_TYPE_VIDEO,
186 .filter_frame = &scale_vaapi_filter_frame,
187 .config_props = &ff_vaapi_vpp_config_input,
192 static const AVFilterPad scale_vaapi_outputs[] = {
195 .type = AVMEDIA_TYPE_VIDEO,
196 .config_props = &scale_vaapi_config_output,
201 AVFilter ff_vf_scale_vaapi = {
202 .name = "scale_vaapi",
203 .description = NULL_IF_CONFIG_SMALL("Scale to/from VAAPI surfaces."),
204 .priv_size = sizeof(ScaleVAAPIContext),
205 .init = &scale_vaapi_init,
206 .uninit = &ff_vaapi_vpp_ctx_uninit,
207 .query_formats = &ff_vaapi_vpp_query_formats,
208 .inputs = scale_vaapi_inputs,
209 .outputs = scale_vaapi_outputs,
210 .priv_class = &scale_vaapi_class,
211 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,