FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_pad_vaapi.c
Date: 2024-05-04 02:01:39
Exec Total Coverage
Lines: 0 103 0.0%
Functions: 0 3 0.0%
Branches: 0 54 0.0%

Line Branch Exec Source
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 {
42 VAR_IN_W, VAR_IW,
43 VAR_IN_H, VAR_IH,
44 VAR_OUT_W, VAR_OW,
45 VAR_OUT_H, VAR_OH,
46 VAR_X,
47 VAR_Y,
48 VAR_A,
49 VAR_SAR,
50 VAR_DAR,
51 VARS_NB
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;
62 AVRational aspect;
63
64 int w, h;
65 int x, y;
66 uint8_t pad_rgba[4];
67 } PadVAAPIContext;
68
69 static int pad_vaapi_config_output(AVFilterLink *outlink)
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
171 static 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;
177 AVFrame *output_frame = NULL;
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);
223 av_frame_free(&output_frame);
224 return err;
225 }
226
227 static av_cold int pad_vaapi_init(AVFilterContext *avctx)
228 {
229 VAAPIVPPContext *vpp_ctx = avctx->priv;
230
231 ff_vaapi_vpp_ctx_init(avctx);
232 vpp_ctx->pipeline_uninit = ff_vaapi_vpp_pipeline_uninit;
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
272 const AVFilter ff_vf_pad_vaapi = {
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,
278 .uninit = &ff_vaapi_vpp_ctx_uninit,
279 FILTER_INPUTS(pad_vaapi_inputs),
280 FILTER_OUTPUTS(pad_vaapi_outputs),
281 FILTER_QUERY_FUNC(&ff_vaapi_vpp_query_formats),
282 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
283 };
284