FFmpeg
vf_pad_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 "libavutil/colorspace.h"
20 #include "libavutil/eval.h"
21 #include "libavutil/opt.h"
22 
23 #include "avfilter.h"
24 #include "internal.h"
25 #include "vaapi_vpp.h"
26 #include "video.h"
27 
28 static const char *const var_names[] = {
29  "in_w", "iw",
30  "in_h", "ih",
31  "out_w", "ow",
32  "out_h", "oh",
33  "x",
34  "y",
35  "a",
36  "sar",
37  "dar",
38  NULL
39 };
40 
41 enum var_name {
52 };
53 
54 typedef struct PadVAAPIContext {
55  VAAPIVPPContext vpp_ctx; // must be the first field
56  VARectangle rect;
57 
58  char *w_expr;
59  char *h_expr;
60  char *x_expr;
61  char *y_expr;
63 
64  int w, h;
65  int x, y;
66  uint8_t pad_rgba[4];
68 
70 {
71  AVFilterContext *avctx = outlink->src;
72  AVFilterLink *inlink = avctx->inputs[0];
73  PadVAAPIContext *ctx = avctx->priv;
74  VAAPIVPPContext *vpp_ctx = avctx->priv;
75  AVRational adjusted_aspect = ctx->aspect;
76  double var_values[VARS_NB], res;
77  int err, ret;
78  char *expr;
79 
80  var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
81  var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
82  var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
83  var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
84  var_values[VAR_A] = (double) inlink->w / inlink->h;
85  var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
86  (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
87  var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
88 
89  av_expr_parse_and_eval(&res, (expr = ctx->w_expr),
90  var_names, var_values,
91  NULL, NULL, NULL, NULL, NULL, 0, ctx);
92  ctx->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
93  if ((ret = av_expr_parse_and_eval(&res, (expr = ctx->h_expr),
94  var_names, var_values,
95  NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
96  return ret;
97  ctx->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
98  if (!ctx->h)
99  var_values[VAR_OUT_H] = var_values[VAR_OH] = ctx->h = inlink->h;
100 
101  /* evaluate the width again, as it may depend on the evaluated output height */
102  if ((ret = av_expr_parse_and_eval(&res, (expr = ctx->w_expr),
103  var_names, var_values,
104  NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
105  return ret;
106  ctx->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
107  if (!ctx->w)
108  var_values[VAR_OUT_W] = var_values[VAR_OW] = ctx->w = inlink->w;
109 
110  if (adjusted_aspect.num && adjusted_aspect.den) {
111  adjusted_aspect = av_div_q(adjusted_aspect, inlink->sample_aspect_ratio);
112  if (ctx->h < av_rescale(ctx->w, adjusted_aspect.den, adjusted_aspect.num)) {
113  ctx->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = av_rescale(ctx->w, adjusted_aspect.den, adjusted_aspect.num);
114  } else {
115  ctx->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = av_rescale(ctx->h, adjusted_aspect.num, adjusted_aspect.den);
116  }
117  }
118 
119  /* evaluate x and y */
120  av_expr_parse_and_eval(&res, (expr = ctx->x_expr),
121  var_names, var_values,
122  NULL, NULL, NULL, NULL, NULL, 0, ctx);
123  ctx->x = var_values[VAR_X] = res;
124  if ((ret = av_expr_parse_and_eval(&res, (expr = ctx->y_expr),
125  var_names, var_values,
126  NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
127  return ret;
128  ctx->y = var_values[VAR_Y] = res;
129  /* evaluate x again, as it may depend on the evaluated y value */
130  if ((ret = av_expr_parse_and_eval(&res, (expr = ctx->x_expr),
131  var_names, var_values,
132  NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
133  return ret;
134  ctx->x = var_values[VAR_X] = res;
135 
136  if (ctx->x < 0 || ctx->x + inlink->w > ctx->w)
137  ctx->x = var_values[VAR_X] = (ctx->w - inlink->w) / 2;
138  if (ctx->y < 0 || ctx->y + inlink->h > ctx->h)
139  ctx->y = var_values[VAR_Y] = (ctx->h - inlink->h) / 2;
140 
141  /* sanity check params */
142  if (ctx->w < inlink->w || ctx->h < inlink->h) {
143  av_log(ctx, AV_LOG_ERROR, "Padded dimensions cannot be smaller than input dimensions.\n");
144  return AVERROR(EINVAL);
145  }
146 
147  if (ctx->w > avctx->inputs[0]->w) {
148  vpp_ctx->output_width = ctx->w;
149  } else {
150  vpp_ctx->output_width = avctx->inputs[0]->w;
151  }
152 
153  if (ctx->h > avctx->inputs[0]->h) {
154  vpp_ctx->output_height = ctx->h;
155  } else {
156  vpp_ctx->output_height = avctx->inputs[0]->h;
157  }
158 
159  if (ctx->x + avctx->inputs[0]->w > vpp_ctx->output_width ||
160  ctx->y + avctx->inputs[0]->h > vpp_ctx->output_height) {
161  return AVERROR(EINVAL);
162  }
163 
164  err = ff_vaapi_vpp_config_output(outlink);
165  if (err < 0)
166  return err;
167 
168  return 0;
169 }
170 
172 {
173  AVFilterContext *avctx = link->dst;
174  AVFilterLink *outlink = avctx->outputs[0];
175  VAAPIVPPContext *vpp_ctx = avctx->priv;
176  PadVAAPIContext *pad_ctx = avctx->priv;
178  VAProcPipelineParameterBuffer params;
179  int err;
180 
181  if (!input_frame->hw_frames_ctx ||
182  vpp_ctx->va_context == VA_INVALID_ID) {
183  err = AVERROR(EINVAL);
184  goto fail;
185  }
186 
187  output_frame = ff_get_video_buffer(outlink, outlink->w, outlink->h);
188  if (!output_frame) {
189  err = AVERROR(ENOMEM);
190  goto fail;
191  }
192 
193  err = av_frame_copy_props(output_frame, input_frame);
194  if (err < 0)
195  goto fail;
196 
197  err = ff_vaapi_vpp_init_params(avctx, &params,
198  input_frame, output_frame);
199  if (err < 0)
200  goto fail;
201 
202  pad_ctx->rect.x = pad_ctx->x;
203  pad_ctx->rect.y = pad_ctx->y;
204  pad_ctx->rect.width = link->w;
205  pad_ctx->rect.height = link->h;
206  params.output_region = &pad_ctx->rect;
207 
208  params.output_background_color = (pad_ctx->pad_rgba[3] << 24 |
209  pad_ctx->pad_rgba[0] << 16 |
210  pad_ctx->pad_rgba[1] << 8 |
211  pad_ctx->pad_rgba[2]);
212 
213  err = ff_vaapi_vpp_render_picture(avctx, &params, output_frame);
214  if (err < 0)
215  goto fail;
216 
217  av_frame_free(&input_frame);
218 
219  return ff_filter_frame(outlink, output_frame);
220 
221 fail:
222  av_frame_free(&input_frame);
224  return err;
225 }
226 
228 {
229  VAAPIVPPContext *vpp_ctx = avctx->priv;
230 
231  ff_vaapi_vpp_ctx_init(avctx);
233  vpp_ctx->output_format = AV_PIX_FMT_NONE;
234 
235  return 0;
236 }
237 
238 #define OFFSET(x) offsetof(PadVAAPIContext, x)
239 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
240 
241 static const AVOption pad_vaapi_options[] = {
242  { "width", "set the pad area width", OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, 0, 0, FLAGS },
243  { "w", "set the pad area width", OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, 0, 0, FLAGS },
244  { "height", "set the pad area height", OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, 0, 0, FLAGS },
245  { "h", "set the pad area height", OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, 0, 0, FLAGS },
246  { "x", "set the x offset for the input image position", OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str = "0"}, 0, INT16_MAX, FLAGS },
247  { "y", "set the y offset for the input image position", OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str = "0"}, 0, INT16_MAX, FLAGS },
248  { "color", "set the color of the padded area border", OFFSET(pad_rgba), AV_OPT_TYPE_COLOR, { .str = "black" }, 0, 0, FLAGS },
249  { "aspect", "pad to fit an aspect instead of a resolution", OFFSET(aspect), AV_OPT_TYPE_RATIONAL, {.dbl = 0}, 0, INT16_MAX, FLAGS },
250  { NULL }
251 };
252 
253 AVFILTER_DEFINE_CLASS(pad_vaapi);
254 
255 static const AVFilterPad pad_vaapi_inputs[] = {
256  {
257  .name = "default",
258  .type = AVMEDIA_TYPE_VIDEO,
259  .filter_frame = pad_vaapi_filter_frame,
260  .config_props = &ff_vaapi_vpp_config_input,
261  },
262 };
263 
264 static const AVFilterPad pad_vaapi_outputs[] = {
265  {
266  .name = "default",
267  .type = AVMEDIA_TYPE_VIDEO,
268  .config_props = &pad_vaapi_config_output,
269  },
270 };
271 
273  .name = "pad_vaapi",
274  .description = NULL_IF_CONFIG_SMALL("Pad the input video."),
275  .priv_size = sizeof(PadVAAPIContext),
276  .priv_class = &pad_vaapi_class,
277  .init = &pad_vaapi_init,
282  .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
283 };
VAR_Y
@ VAR_Y
Definition: vf_pad_vaapi.c:47
ff_get_video_buffer
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:112
ff_vaapi_vpp_pipeline_uninit
void ff_vaapi_vpp_pipeline_uninit(AVFilterContext *avctx)
Definition: vaapi_vpp.c:49
ff_vaapi_vpp_ctx_init
void ff_vaapi_vpp_ctx_init(AVFilterContext *avctx)
Definition: vaapi_vpp.c:715
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
var_name
var_name
Definition: noise.c:47
ff_vaapi_vpp_render_picture
int ff_vaapi_vpp_render_picture(AVFilterContext *avctx, VAProcPipelineParameterBuffer *params, AVFrame *output_frame)
Definition: vaapi_vpp.c:708
FF_FILTER_FLAG_HWFRAME_AWARE
#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:351
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1015
VAR_OW
@ VAR_OW
Definition: vf_pad_vaapi.c:44
av_div_q
AVRational av_div_q(AVRational b, AVRational c)
Divide one rational by another.
Definition: rational.c:88
PadVAAPIContext::w_expr
char * w_expr
Definition: vf_pad_vaapi.c:58
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:160
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:374
AVOption
AVOption.
Definition: opt.h:357
FILTER_QUERY_FUNC
#define FILTER_QUERY_FUNC(func)
Definition: internal.h:159
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:170
PadVAAPIContext::y_expr
char * y_expr
Definition: vf_pad_vaapi.c:61
AV_OPT_TYPE_RATIONAL
@ AV_OPT_TYPE_RATIONAL
Definition: opt.h:250
video.h
VAR_SAR
@ VAR_SAR
Definition: vf_pad_vaapi.c:49
PadVAAPIContext::h_expr
char * h_expr
Definition: vf_pad_vaapi.c:59
VAR_X
@ VAR_X
Definition: vf_pad_vaapi.c:46
AVFilterContext::priv
void * priv
private data for use by the filter
Definition: avfilter.h:422
fail
#define fail()
Definition: checkasm.h:185
PadVAAPIContext
Definition: vf_pad_vaapi.c:54
PadVAAPIContext::aspect
AVRational aspect
Definition: vf_pad_vaapi.c:62
FLAGS
#define FLAGS
Definition: vf_pad_vaapi.c:239
AVRational::num
int num
Numerator.
Definition: rational.h:59
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:33
VAR_IN_H
@ VAR_IN_H
Definition: vf_pad_vaapi.c:43
colorspace.h
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
VAAPIVPPContext::output_width
int output_width
Definition: vaapi_vpp.h:53
ctx
AVFormatContext * ctx
Definition: movenc.c:49
VAAPIVPPContext::output_format
enum AVPixelFormat output_format
Definition: vaapi_vpp.h:52
VAR_OUT_W
@ VAR_OUT_W
Definition: vf_pad_vaapi.c:44
var_names
static const char *const var_names[]
Definition: vf_pad_vaapi.c:28
PadVAAPIContext::rect
VARectangle rect
Definition: vf_pad_vaapi.c:56
NAN
#define NAN
Definition: mathematics.h:115
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:182
link
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a link
Definition: filter_design.txt:23
VAR_OUT_H
@ VAR_OUT_H
Definition: vf_pad_vaapi.c:45
NULL
#define NULL
Definition: coverity.c:32
av_frame_copy_props
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:709
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
AV_OPT_TYPE_COLOR
@ AV_OPT_TYPE_COLOR
Definition: opt.h:260
AVFilterContext::inputs
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:415
double
double
Definition: af_crystalizer.c:131
ff_vaapi_vpp_config_input
int ff_vaapi_vpp_config_input(AVFilterLink *inlink)
Definition: vaapi_vpp.c:75
PadVAAPIContext::y
int y
Definition: vf_pad_vaapi.c:65
ff_vaapi_vpp_ctx_uninit
void ff_vaapi_vpp_ctx_uninit(AVFilterContext *avctx)
Definition: vaapi_vpp.c:729
VAR_DAR
@ VAR_DAR
Definition: vf_pad_vaapi.c:50
pad_vaapi_options
static const AVOption pad_vaapi_options[]
Definition: vf_pad_vaapi.c:241
ff_vaapi_vpp_query_formats
int ff_vaapi_vpp_query_formats(AVFilterContext *avctx)
Definition: vaapi_vpp.c:28
pad_vaapi_outputs
static const AVFilterPad pad_vaapi_outputs[]
Definition: vf_pad_vaapi.c:264
PadVAAPIContext::w
int w
Definition: vf_pad_vaapi.c:64
pad_vaapi_init
static av_cold int pad_vaapi_init(AVFilterContext *avctx)
Definition: vf_pad_vaapi.c:227
eval.h
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(pad_vaapi)
vaapi_vpp.h
OFFSET
#define OFFSET(x)
Definition: vf_pad_vaapi.c:238
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:366
ff_vf_pad_vaapi
const AVFilter ff_vf_pad_vaapi
Definition: vf_pad_vaapi.c:272
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
av_expr_parse_and_eval
int av_expr_parse_and_eval(double *d, const char *s, const char *const *const_names, const double *const_values, const char *const *func1_names, double(*const *funcs1)(void *, double), const char *const *func2_names, double(*const *funcs2)(void *, double, double), void *opaque, int log_offset, void *log_ctx)
Parse and evaluate an expression.
Definition: eval.c:803
PadVAAPIContext::h
int h
Definition: vf_pad_vaapi.c:64
PadVAAPIContext::x
int x
Definition: vf_pad_vaapi.c:65
PadVAAPIContext::vpp_ctx
VAAPIVPPContext vpp_ctx
Definition: vf_pad_vaapi.c:55
output_frame
static int output_frame(H264Context *h, AVFrame *dst, H264Picture *srcp)
Definition: h264dec.c:875
pad_vaapi_config_output
static int pad_vaapi_config_output(AVFilterLink *outlink)
Definition: vf_pad_vaapi.c:69
internal.h
VAAPIVPPContext::output_height
int output_height
Definition: vaapi_vpp.h:54
uninit
static void uninit(AVBSFContext *ctx)
Definition: pcm_rechunk.c:68
VAR_IW
@ VAR_IW
Definition: vf_pad_vaapi.c:42
VAR_IH
@ VAR_IH
Definition: vf_pad_vaapi.c:43
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:39
av_rescale
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:129
AVFilter
Filter definition.
Definition: avfilter.h:166
ret
ret
Definition: filter_design.txt:187
VAAPIVPPContext
Definition: vaapi_vpp.h:38
VAAPIVPPContext::va_context
VAContextID va_context
Definition: vaapi_vpp.h:46
PadVAAPIContext::pad_rgba
uint8_t pad_rgba[4]
Definition: vf_pad_vaapi.c:66
AVFrame::hw_frames_ctx
AVBufferRef * hw_frames_ctx
For hwaccel-format frames, this should be a reference to the AVHWFramesContext describing the frame.
Definition: frame.h:725
ff_vaapi_vpp_config_output
int ff_vaapi_vpp_config_output(AVFilterLink *outlink)
Definition: vaapi_vpp.c:100
AVRational::den
int den
Denominator.
Definition: rational.h:60
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
avfilter.h
VAAPIVPPContext::pipeline_uninit
void(* pipeline_uninit)(AVFilterContext *avctx)
Definition: vaapi_vpp.h:63
AVFilterContext
An instance of a filter.
Definition: avfilter.h:407
pad_vaapi_inputs
static const AVFilterPad pad_vaapi_inputs[]
Definition: vf_pad_vaapi.c:255
PadVAAPIContext::x_expr
char * x_expr
Definition: vf_pad_vaapi.c:60
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
VAR_OH
@ VAR_OH
Definition: vf_pad_vaapi.c:45
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:183
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
VAR_A
@ VAR_A
Definition: vf_pad_vaapi.c:48
VAR_IN_W
@ VAR_IN_W
Definition: vf_pad_vaapi.c:42
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Definition: opt.h:249
pad_vaapi_filter_frame
static int pad_vaapi_filter_frame(AVFilterLink *link, AVFrame *input_frame)
Definition: vf_pad_vaapi.c:171
VARS_NB
@ VARS_NB
Definition: vf_pad_vaapi.c:51
ff_vaapi_vpp_init_params
int ff_vaapi_vpp_init_params(AVFilterContext *avctx, VAProcPipelineParameterBuffer *params, const AVFrame *input_frame, AVFrame *output_frame)
Definition: vaapi_vpp.c:534
AVFilterContext::outputs
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:419