]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_scale_vaapi.c
Merge commit '5cb62f9d952e24fff62737a57e89cf43d9c2333a'
[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     char *w_expr;      // width expression string
39     char *h_expr;      // height expression string
40 } ScaleVAAPIContext;
41
42 static int scale_vaapi_config_output(AVFilterLink *outlink)
43 {
44     AVFilterLink *inlink     = outlink->src->inputs[0];
45     AVFilterContext *avctx   = outlink->src;
46     VAAPIVPPContext *vpp_ctx = avctx->priv;
47     ScaleVAAPIContext *ctx   = avctx->priv;
48     int err;
49
50     if ((err = ff_scale_eval_dimensions(ctx,
51                                         ctx->w_expr, ctx->h_expr,
52                                         inlink, outlink,
53                                         &vpp_ctx->output_width, &vpp_ctx->output_height)) < 0)
54         return err;
55
56     err = ff_vaapi_vpp_config_output(outlink);
57     if (err < 0)
58         return err;
59
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);
62     else
63         outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
64
65     return 0;
66 }
67
68 static int scale_vaapi_filter_frame(AVFilterLink *inlink, AVFrame *input_frame)
69 {
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;
77     int err;
78
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);
82
83     if (vpp_ctx->va_context == VA_INVALID_ID)
84         return AVERROR(EINVAL);
85
86     input_surface = (VASurfaceID)(uintptr_t)input_frame->data[3];
87     av_log(avctx, AV_LOG_DEBUG, "Using surface %#x for scale input.\n",
88            input_surface);
89
90     output_frame = ff_get_video_buffer(outlink, vpp_ctx->output_width,
91                                        vpp_ctx->output_height);
92     if (!output_frame) {
93         err = AVERROR(ENOMEM);
94         goto fail;
95     }
96
97     output_surface = (VASurfaceID)(uintptr_t)output_frame->data[3];
98     av_log(avctx, AV_LOG_DEBUG, "Using surface %#x for scale output.\n",
99            output_surface);
100
101     memset(&params, 0, sizeof(params));
102
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),
110     };
111
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);
116
117     params.output_region = 0;
118     params.output_background_color = 0xff000000;
119     params.output_color_standard = params.surface_color_standard;
120
121     params.pipeline_flags = 0;
122     params.filter_flags = VA_FILTER_SCALING_HQ;
123
124     err = ff_vaapi_vpp_render_picture(avctx, &params, output_surface);
125     if (err < 0)
126         goto fail;
127
128     err = av_frame_copy_props(output_frame, input_frame);
129     if (err < 0)
130         goto fail;
131
132     av_frame_free(&input_frame);
133
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);
137
138     return ff_filter_frame(outlink, output_frame);
139
140 fail:
141     av_frame_free(&input_frame);
142     av_frame_free(&output_frame);
143     return err;
144 }
145
146 static av_cold int scale_vaapi_init(AVFilterContext *avctx)
147 {
148     VAAPIVPPContext *vpp_ctx = avctx->priv;
149     ScaleVAAPIContext *ctx   = avctx->priv;
150
151     ff_vaapi_vpp_ctx_init(avctx);
152     vpp_ctx->pipeline_uninit = ff_vaapi_vpp_pipeline_uninit;
153
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);
159         }
160     } else {
161         // Use the input format once that is configured.
162         vpp_ctx->output_format = AV_PIX_FMT_NONE;
163     }
164
165     return 0;
166 }
167
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 },
177     { NULL },
178 };
179
180 AVFILTER_DEFINE_CLASS(scale_vaapi);
181
182 static const AVFilterPad scale_vaapi_inputs[] = {
183     {
184         .name         = "default",
185         .type         = AVMEDIA_TYPE_VIDEO,
186         .filter_frame = &scale_vaapi_filter_frame,
187         .config_props = &ff_vaapi_vpp_config_input,
188     },
189     { NULL }
190 };
191
192 static const AVFilterPad scale_vaapi_outputs[] = {
193     {
194         .name = "default",
195         .type = AVMEDIA_TYPE_VIDEO,
196         .config_props = &scale_vaapi_config_output,
197     },
198     { NULL }
199 };
200
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,
212 };