FFmpeg  4.4.9
vf_scale_vaapi.c
Go to the documentation of this file.
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_eval.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 
37 
38  int mode;
39 
40  char *w_expr; // width expression string
41  char *h_expr; // height expression string
42 
45 
51 
57 
58 static const char *scale_vaapi_mode_name(int mode)
59 {
60  switch (mode) {
61 #define D(name) case VA_FILTER_SCALING_ ## name: return #name
62  D(DEFAULT);
63  D(FAST);
64  D(HQ);
65  D(NL_ANAMORPHIC);
66 #undef D
67  default:
68  return "Invalid";
69  }
70 }
71 
72 
74 {
75  AVFilterLink *inlink = outlink->src->inputs[0];
76  AVFilterContext *avctx = outlink->src;
77  VAAPIVPPContext *vpp_ctx = avctx->priv;
78  ScaleVAAPIContext *ctx = avctx->priv;
79  int err;
80 
81  if ((err = ff_scale_eval_dimensions(ctx,
82  ctx->w_expr, ctx->h_expr,
83  inlink, outlink,
84  &vpp_ctx->output_width, &vpp_ctx->output_height)) < 0)
85  return err;
86 
87  err = ff_scale_adjust_dimensions(inlink, &vpp_ctx->output_width, &vpp_ctx->output_height,
88  ctx->force_original_aspect_ratio,
89  ctx->force_divisible_by);
90  if (err < 0)
91  return err;
92 
93  err = ff_vaapi_vpp_config_output(outlink);
94  if (err < 0)
95  return err;
96 
97  if (inlink->sample_aspect_ratio.num)
98  outlink->sample_aspect_ratio = av_mul_q((AVRational){outlink->h * inlink->w, outlink->w * inlink->h}, inlink->sample_aspect_ratio);
99  else
100  outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
101 
102  return 0;
103 }
104 
105 static int scale_vaapi_filter_frame(AVFilterLink *inlink, AVFrame *input_frame)
106 {
107  AVFilterContext *avctx = inlink->dst;
108  AVFilterLink *outlink = avctx->outputs[0];
109  VAAPIVPPContext *vpp_ctx = avctx->priv;
110  ScaleVAAPIContext *ctx = avctx->priv;
112  VAProcPipelineParameterBuffer params;
113  int err;
114 
115  av_log(avctx, AV_LOG_DEBUG, "Filter input: %s, %ux%u (%"PRId64").\n",
116  av_get_pix_fmt_name(input_frame->format),
117  input_frame->width, input_frame->height, input_frame->pts);
118 
119  if (vpp_ctx->va_context == VA_INVALID_ID)
120  return AVERROR(EINVAL);
121 
122  output_frame = ff_get_video_buffer(outlink, vpp_ctx->output_width,
123  vpp_ctx->output_height);
124  if (!output_frame) {
125  err = AVERROR(ENOMEM);
126  goto fail;
127  }
128 
129  err = av_frame_copy_props(output_frame, input_frame);
130  if (err < 0)
131  goto fail;
132 
133  if (ctx->colour_primaries != AVCOL_PRI_UNSPECIFIED)
134  output_frame->color_primaries = ctx->colour_primaries;
135  if (ctx->colour_transfer != AVCOL_TRC_UNSPECIFIED)
136  output_frame->color_trc = ctx->colour_transfer;
137  if (ctx->colour_matrix != AVCOL_SPC_UNSPECIFIED)
138  output_frame->colorspace = ctx->colour_matrix;
139  if (ctx->colour_range != AVCOL_RANGE_UNSPECIFIED)
140  output_frame->color_range = ctx->colour_range;
141  if (ctx->chroma_location != AVCHROMA_LOC_UNSPECIFIED)
142  output_frame->chroma_location = ctx->chroma_location;
143 
144  err = ff_vaapi_vpp_init_params(avctx, &params,
145  input_frame, output_frame);
146  if (err < 0)
147  goto fail;
148 
149  params.filter_flags |= ctx->mode;
150 
151  err = ff_vaapi_vpp_render_picture(avctx, &params, output_frame);
152  if (err < 0)
153  goto fail;
154 
155  av_frame_free(&input_frame);
156 
157  av_log(avctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64"), mode: %s.\n",
159  output_frame->width, output_frame->height, output_frame->pts,
160  scale_vaapi_mode_name(ctx->mode));
161 
162  return ff_filter_frame(outlink, output_frame);
163 
164 fail:
165  av_frame_free(&input_frame);
167  return err;
168 }
169 
171 {
172  VAAPIVPPContext *vpp_ctx = avctx->priv;
173  ScaleVAAPIContext *ctx = avctx->priv;
174 
175  ff_vaapi_vpp_ctx_init(avctx);
177 
178  if (ctx->output_format_string) {
179  vpp_ctx->output_format = av_get_pix_fmt(ctx->output_format_string);
180  if (vpp_ctx->output_format == AV_PIX_FMT_NONE) {
181  av_log(avctx, AV_LOG_ERROR, "Invalid output format.\n");
182  return AVERROR(EINVAL);
183  }
184  } else {
185  // Use the input format once that is configured.
186  vpp_ctx->output_format = AV_PIX_FMT_NONE;
187  }
188 
189 #define STRING_OPTION(var_name, func_name, default_value) do { \
190  if (ctx->var_name ## _string) { \
191  int var = av_ ## func_name ## _from_name(ctx->var_name ## _string); \
192  if (var < 0) { \
193  av_log(avctx, AV_LOG_ERROR, "Invalid %s.\n", #var_name); \
194  return AVERROR(EINVAL); \
195  } \
196  ctx->var_name = var; \
197  } else { \
198  ctx->var_name = default_value; \
199  } \
200  } while (0)
201 
203  STRING_OPTION(colour_transfer, color_transfer, AVCOL_TRC_UNSPECIFIED);
204  STRING_OPTION(colour_matrix, color_space, AVCOL_SPC_UNSPECIFIED);
205  STRING_OPTION(chroma_location, chroma_location, AVCHROMA_LOC_UNSPECIFIED);
206 
207  return 0;
208 }
209 
210 #define OFFSET(x) offsetof(ScaleVAAPIContext, x)
211 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM)
212 static const AVOption scale_vaapi_options[] = {
213  { "w", "Output video width",
214  OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, .flags = FLAGS },
215  { "h", "Output video height",
216  OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, .flags = FLAGS },
217  { "format", "Output video format (software format of hardware frames)",
218  OFFSET(output_format_string), AV_OPT_TYPE_STRING, .flags = FLAGS },
219  { "mode", "Scaling mode",
220  OFFSET(mode), AV_OPT_TYPE_INT, { .i64 = VA_FILTER_SCALING_HQ },
221  0, VA_FILTER_SCALING_NL_ANAMORPHIC, FLAGS, "mode" },
222  { "default", "Use the default (depend on the driver) scaling algorithm",
223  0, AV_OPT_TYPE_CONST, { .i64 = VA_FILTER_SCALING_DEFAULT }, 0, 0, FLAGS, "mode" },
224  { "fast", "Use fast scaling algorithm",
225  0, AV_OPT_TYPE_CONST, { .i64 = VA_FILTER_SCALING_FAST }, 0, 0, FLAGS, "mode" },
226  { "hq", "Use high quality scaling algorithm",
227  0, AV_OPT_TYPE_CONST, { .i64 = VA_FILTER_SCALING_HQ }, 0, 0, FLAGS, "mode" },
228  { "nl_anamorphic", "Use nolinear anamorphic scaling algorithm",
229  0, AV_OPT_TYPE_CONST, { .i64 = VA_FILTER_SCALING_NL_ANAMORPHIC }, 0, 0, FLAGS, "mode" },
230 
231  // These colour properties match the ones of the same name in vf_scale.
232  { "out_color_matrix", "Output colour matrix coefficient set",
233  OFFSET(colour_matrix_string), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = FLAGS },
234  { "out_range", "Output colour range",
235  OFFSET(colour_range), AV_OPT_TYPE_INT, { .i64 = AVCOL_RANGE_UNSPECIFIED },
237  { "full", "Full range",
238  0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_RANGE_JPEG }, 0, 0, FLAGS, "range" },
239  { "limited", "Limited range",
240  0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_RANGE_MPEG }, 0, 0, FLAGS, "range" },
241  { "jpeg", "Full range",
242  0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_RANGE_JPEG }, 0, 0, FLAGS, "range" },
243  { "mpeg", "Limited range",
244  0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_RANGE_MPEG }, 0, 0, FLAGS, "range" },
245  { "tv", "Limited range",
246  0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_RANGE_MPEG }, 0, 0, FLAGS, "range" },
247  { "pc", "Full range",
248  0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_RANGE_JPEG }, 0, 0, FLAGS, "range" },
249  // These colour properties are new here.
250  { "out_color_primaries", "Output colour primaries",
251  OFFSET(colour_primaries_string), AV_OPT_TYPE_STRING,
252  { .str = NULL }, .flags = FLAGS },
253  { "out_color_transfer", "Output colour transfer characteristics",
254  OFFSET(colour_transfer_string), AV_OPT_TYPE_STRING,
255  { .str = NULL }, .flags = FLAGS },
256  { "out_chroma_location", "Output chroma sample location",
257  OFFSET(chroma_location_string), AV_OPT_TYPE_STRING,
258  { .str = NULL }, .flags = FLAGS },
259  { "force_original_aspect_ratio", "decrease or increase w/h if necessary to keep the original AR", OFFSET(force_original_aspect_ratio), AV_OPT_TYPE_INT, { .i64 = 0}, 0, 2, FLAGS, "force_oar" },
260  { "disable", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, 0, 0, FLAGS, "force_oar" },
261  { "decrease", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 0, FLAGS, "force_oar" },
262  { "increase", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 2 }, 0, 0, FLAGS, "force_oar" },
263  { "force_divisible_by", "enforce that the output resolution is divisible by a defined integer when force_original_aspect_ratio is used", OFFSET(force_divisible_by), AV_OPT_TYPE_INT, { .i64 = 1}, 1, 256, FLAGS },
264 
265  { NULL },
266 };
267 
269 
270 static const AVFilterPad scale_vaapi_inputs[] = {
271  {
272  .name = "default",
273  .type = AVMEDIA_TYPE_VIDEO,
274  .filter_frame = &scale_vaapi_filter_frame,
275  .config_props = &ff_vaapi_vpp_config_input,
276  },
277  { NULL }
278 };
279 
281  {
282  .name = "default",
283  .type = AVMEDIA_TYPE_VIDEO,
284  .config_props = &scale_vaapi_config_output,
285  },
286  { NULL }
287 };
288 
290  .name = "scale_vaapi",
291  .description = NULL_IF_CONFIG_SMALL("Scale to/from VAAPI surfaces."),
292  .priv_size = sizeof(ScaleVAAPIContext),
298  .priv_class = &scale_vaapi_class,
299  .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
300 };
static int query_formats(AVFilterContext *ctx)
Definition: aeval.c:244
static const AVFilterPad inputs[]
Definition: af_acontrast.c:193
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
#define av_cold
Definition: attributes.h:88
simple assert() macros that are a bit more flexible than ISO C assert().
#define DEFAULT
Definition: avdct.c:28
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1099
Main libavfilter public API header.
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
#define fail()
Definition: checkasm.h:133
#define NULL
Definition: coverity.c:32
static av_cold int uninit(AVCodecContext *avctx)
Definition: crystalhd.c:279
mode
Use these values in ebur128_init (or'ed).
Definition: ebur128.h:83
@ AV_OPT_TYPE_CONST
Definition: opt.h:234
@ AV_OPT_TYPE_INT
Definition: opt.h:225
@ AV_OPT_TYPE_STRING
Definition: opt.h:229
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:658
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:215
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition: rational.c:80
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
static int output_frame(H264Context *h, AVFrame *dst, H264Picture *srcp)
Definition: h264dec.c:830
#define FF_FILTER_FLAG_HWFRAME_AWARE
The filter is aware of hardware frames, and any hardware frame context should not be automatically pr...
Definition: internal.h:339
static const struct ColorPrimaries color_primaries[AVCOL_PRI_NB]
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
Memory handling functions.
AVOptions.
enum AVPixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
Definition: pixdesc.c:2501
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition: pixdesc.c:2489
AVChromaLocation
Location of chroma samples.
Definition: pixfmt.h:605
@ AVCHROMA_LOC_UNSPECIFIED
Definition: pixfmt.h:606
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition: pixfmt.h:569
@ AVCOL_RANGE_UNSPECIFIED
Definition: pixfmt.h:552
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:586
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
AVColorPrimaries
Chromaticity coordinates of the source primaries.
Definition: pixfmt.h:458
@ AVCOL_PRI_UNSPECIFIED
Definition: pixfmt.h:461
AVColorTransferCharacteristic
Color Transfer Characteristic.
Definition: pixfmt.h:483
@ AVCOL_TRC_UNSPECIFIED
Definition: pixfmt.h:486
AVColorSpace
YUV colorspace type.
Definition: pixfmt.h:512
@ AVCOL_SPC_UNSPECIFIED
Definition: pixfmt.h:515
int ff_scale_adjust_dimensions(AVFilterLink *inlink, int *ret_w, int *ret_h, int force_original_aspect_ratio, int force_divisible_by)
Transform evaluated width and height obtained from ff_scale_eval_dimensions into actual target width ...
Definition: scale_eval.c:125
int ff_scale_eval_dimensions(void *log_ctx, const char *w_expr, const char *h_expr, AVFilterLink *inlink, AVFilterLink *outlink, int *ret_w, int *ret_h)
Parse and evaluate string expressions for width and height.
Definition: scale_eval.c:73
An instance of a filter.
Definition: avfilter.h:341
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:349
void * priv
private data for use by the filter
Definition: avfilter.h:356
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:353
A filter pad used for either input or output.
Definition: internal.h:54
const char * name
Pad name.
Definition: internal.h:60
Filter definition.
Definition: avfilter.h:145
const char * name
Filter name.
Definition: avfilter.h:149
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:411
int width
Definition: frame.h:376
int height
Definition: frame.h:376
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition: frame.h:391
AVOption.
Definition: opt.h:248
Rational number (pair of numerator and denominator).
Definition: rational.h:58
int num
Numerator.
Definition: rational.h:59
enum AVChromaLocation chroma_location
char * chroma_location_string
enum AVColorPrimaries colour_primaries
char * colour_transfer_string
char * output_format_string
char * colour_matrix_string
char * colour_primaries_string
enum AVColorSpace colour_matrix
enum AVColorTransferCharacteristic colour_transfer
VAAPIVPPContext vpp_ctx
void(* pipeline_uninit)(AVFilterContext *avctx)
Definition: vaapi_vpp.h:56
enum AVPixelFormat output_format
Definition: vaapi_vpp.h:47
VAContextID va_context
Definition: vaapi_vpp.h:41
#define av_log(a,...)
AVFormatContext * ctx
Definition: movenc.c:48
int ff_vaapi_vpp_config_output(AVFilterLink *outlink)
Definition: vaapi_vpp.c:95
int ff_vaapi_vpp_config_input(AVFilterLink *inlink)
Definition: vaapi_vpp.c:70
void ff_vaapi_vpp_pipeline_uninit(AVFilterContext *avctx)
Definition: vaapi_vpp.c:44
void ff_vaapi_vpp_ctx_init(AVFilterContext *avctx)
Definition: vaapi_vpp.c:666
int ff_vaapi_vpp_init_params(AVFilterContext *avctx, VAProcPipelineParameterBuffer *params, const AVFrame *input_frame, AVFrame *output_frame)
Definition: vaapi_vpp.c:515
int ff_vaapi_vpp_render_picture(AVFilterContext *avctx, VAProcPipelineParameterBuffer *params, AVFrame *output_frame)
Definition: vaapi_vpp.c:592
void ff_vaapi_vpp_ctx_uninit(AVFilterContext *avctx)
Definition: vaapi_vpp.c:680
int ff_vaapi_vpp_query_formats(AVFilterContext *avctx)
Definition: vaapi_vpp.c:27
#define D(name)
AVFilter ff_vf_scale_vaapi
static const AVFilterPad scale_vaapi_outputs[]
static int scale_vaapi_config_output(AVFilterLink *outlink)
static int scale_vaapi_filter_frame(AVFilterLink *inlink, AVFrame *input_frame)
#define FLAGS
static const AVFilterPad scale_vaapi_inputs[]
AVFILTER_DEFINE_CLASS(scale_vaapi)
static av_cold int scale_vaapi_init(AVFilterContext *avctx)
static const AVOption scale_vaapi_options[]
#define OFFSET(x)
#define STRING_OPTION(var_name, func_name, default_value)
static const char * scale_vaapi_mode_name(int mode)
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:104