FFmpeg
Loading...
Searching...
No Matches
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
20#include "libavutil/eval.h"
21#include "libavutil/opt.h"
22
23#include "avfilter.h"
24#include "filters.h"
25#include "vaapi_vpp.h"
26#include "video.h"
27
28static 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
53
54typedef 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 ?
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
171static int pad_vaapi_filter_frame(AVFilterLink *link, AVFrame *input_frame)
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
214 if (err < 0)
215 goto fail;
216
217 av_frame_free(&input_frame);
218
219 return ff_filter_frame(outlink, output_frame);
220
221fail:
222 av_frame_free(&input_frame);
224 return err;
225}
226
228{
229 VAAPIVPPContext *vpp_ctx = avctx->priv;
230
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
241static 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
254
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
265 {
266 .name = "default",
267 .type = AVMEDIA_TYPE_VIDEO,
268 .config_props = &pad_vaapi_config_output,
269 },
270};
271
273 .p.name = "pad_vaapi",
274 .p.description = NULL_IF_CONFIG_SMALL("Pad the input video."),
275 .p.priv_class = &pad_vaapi_class,
276 .priv_size = sizeof(PadVAAPIContext),
282 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
283};
SwsAArch64OpImplParams params
Definition ops.c:51
const FFFilter ff_vf_pad_vaapi
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
Main libavfilter public API header.
@ VARS_NB
Definition boxblur.c:43
#define FLAGS
Definition cmdutils.c:598
#define NULL
Definition coverity.c:32
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
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:839
simple arithmetic expression evaluator
@ VAR_IW
Definition f_select.c:147
@ VAR_IH
Definition f_select.c:146
#define fail
Definition test.h:479
@ AV_OPT_TYPE_RATIONAL
Underlying C type is AVRational.
Definition opt.h:279
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition opt.h:275
@ AV_OPT_TYPE_COLOR
Underlying C type is uint8_t[4].
Definition opt.h:322
#define AVERROR(e)
Definition error.h:45
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:599
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
AVRational av_div_q(AVRational b, AVRational c)
Divide one rational by another.
Definition rational.c:88
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int output_frame(H264Context *h, AVFrame *dst, H264Picture *srcp)
Definition h264dec.c:860
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define FF_FILTER_FLAG_HWFRAME_AWARE
The filter is aware of hardware frames, and any hardware frame context should not be automatically pr...
Definition filters.h:208
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define FILTER_QUERY_FUNC2(func)
Definition filters.h:241
@ VAR_X
Definition vf_blend.c:55
@ VAR_Y
Definition vf_blend.c:55
#define av_cold
Definition attributes.h:117
Various defines for YUV<->RGB conversion.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define NAN
var_name
Definition noise.c:46
static const char *const var_names[]
Definition noise.c:30
AVOptions.
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ VAR_OUT_H
Definition scale_eval.c:47
@ VAR_SAR
Definition scale_eval.c:49
@ VAR_OH
Definition scale_eval.c:47
@ VAR_IN_W
Definition scale_eval.c:44
@ VAR_OW
Definition scale_eval.c:46
@ VAR_A
Definition scale_eval.c:48
@ VAR_OUT_W
Definition scale_eval.c:46
@ VAR_IN_H
Definition scale_eval.c:45
@ VAR_DAR
Definition scale_eval.c:50
An instance of a filter.
Definition avfilter.h:273
AVFilterLink ** inputs
array of pointers to input links
Definition avfilter.h:281
void * priv
private data for use by the filter
Definition avfilter.h:288
AVFilterLink ** outputs
array of pointers to output links
Definition avfilter.h:285
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
AVBufferRef * hw_frames_ctx
For hwaccel-format frames, this should be a reference to the AVHWFramesContext describing the frame.
Definition frame.h:769
AVOption.
Definition opt.h:428
Rational number (pair of numerator and denominator).
Definition rational.h:58
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
uint8_t pad_rgba[4]
VARectangle rect
AVRational aspect
VAAPIVPPContext vpp_ctx
void(* pipeline_uninit)(AVFilterContext *avctx)
Definition vaapi_vpp.h:63
enum AVPixelFormat output_format
Definition vaapi_vpp.h:52
VAContextID va_context
Definition vaapi_vpp.h:46
#define av_log(a,...)
static AVFormatContext * ctx
Definition movenc.c:49
int ff_vaapi_vpp_config_output(AVFilterLink *outlink)
Definition vaapi_vpp.c:97
int ff_vaapi_vpp_config_input(AVFilterLink *inlink)
Definition vaapi_vpp.c:71
int ff_vaapi_vpp_query_formats(const AVFilterContext *avctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition vaapi_vpp.c:29
void ff_vaapi_vpp_pipeline_uninit(AVFilterContext *avctx)
Definition vaapi_vpp.c:45
void ff_vaapi_vpp_ctx_init(AVFilterContext *avctx)
Definition vaapi_vpp.c:714
int ff_vaapi_vpp_init_params(AVFilterContext *avctx, VAProcPipelineParameterBuffer *params, const AVFrame *input_frame, AVFrame *output_frame)
Definition vaapi_vpp.c:533
int ff_vaapi_vpp_render_picture(AVFilterContext *avctx, VAProcPipelineParameterBuffer *params, AVFrame *output_frame)
Definition vaapi_vpp.c:707
void ff_vaapi_vpp_ctx_uninit(AVFilterContext *avctx)
Definition vaapi_vpp.c:728
static av_cold int pad_vaapi_init(AVFilterContext *avctx)
static const AVOption pad_vaapi_options[]
static const AVFilterPad pad_vaapi_inputs[]
static const AVFilterPad pad_vaapi_outputs[]
static int pad_vaapi_config_output(AVFilterLink *outlink)
static int pad_vaapi_filter_frame(AVFilterLink *link, AVFrame *input_frame)
#define OFFSET(x)
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition video.c:89