FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_drawbox_vaapi.c
Date: 2024-05-03 15:42:48
Exec Total Coverage
Lines: 0 163 0.0%
Functions: 0 4 0.0%
Branches: 0 68 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_h", "ih",
30 "in_w", "iw",
31 "x",
32 "y",
33 "h",
34 "w",
35 "t",
36 "fill",
37 NULL
38 };
39
40 enum var_name {
41 VAR_IN_H, VAR_IH,
42 VAR_IN_W, VAR_IW,
43 VAR_X,
44 VAR_Y,
45 VAR_H,
46 VAR_W,
47 VAR_T,
48 VAR_MAX,
49 VARS_NB
50 };
51
52 static const int NUM_EXPR_EVALS = 5;
53
54 typedef struct DrawboxVAAPIContext {
55 VAAPIVPPContext vpp_ctx; // must be the first field
56 VARectangle outer_rect, inner_rect;
57
58 /* The hardware frame context containing the frames for outer_rect. */
59 AVBufferRef *outer_frames_ref;
60 AVHWFramesContext *outer_frames;
61 AVFrame *outer_frame;
62
63 char *x_expr;
64 char *y_expr;
65 char *w_expr;
66 char *h_expr;
67 char *t_expr;
68
69 int w, h;
70 int x, y;
71 int replace;
72 uint32_t thickness;
73 uint8_t drawbox_rgba[4];
74
75 int fill;
76
77 } DrawboxVAAPIContext;
78
79 static int drawbox_vaapi_config_output(AVFilterLink *outlink)
80 {
81 AVFilterContext *avctx = outlink->src;
82 AVFilterLink *inlink = avctx->inputs[0];
83 DrawboxVAAPIContext *ctx = avctx->priv;
84 VAAPIVPPContext *vpp_ctx = avctx->priv;
85 double var_values[VARS_NB], res;
86 int ret, i;
87 char *expr;
88
89 var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
90 var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
91 var_values[VAR_X] = NAN;
92 var_values[VAR_Y] = NAN;
93 var_values[VAR_H] = NAN;
94 var_values[VAR_W] = NAN;
95 var_values[VAR_T] = NAN;
96
97 for (i = 0; i <= NUM_EXPR_EVALS; i++) {
98 /* evaluate expressions, fail on last iteration */
99 var_values[VAR_MAX] = inlink->w;
100 if ((ret = av_expr_parse_and_eval(&res, (expr = ctx->x_expr),
101 var_names, var_values,
102 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0 && i == NUM_EXPR_EVALS)
103 goto fail;
104 ctx->x = var_values[VAR_X] = res;
105
106 var_values[VAR_MAX] = inlink->h;
107 if ((ret = av_expr_parse_and_eval(&res, (expr = ctx->y_expr),
108 var_names, var_values,
109 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0 && i == NUM_EXPR_EVALS)
110 goto fail;
111 ctx->y = var_values[VAR_Y] = res;
112
113 var_values[VAR_MAX] = inlink->w - ctx->x;
114 if ((ret = av_expr_parse_and_eval(&res, (expr = ctx->w_expr),
115 var_names, var_values,
116 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0 && i == NUM_EXPR_EVALS)
117 goto fail;
118 ctx->w = var_values[VAR_W] = res;
119
120 var_values[VAR_MAX] = inlink->h - ctx->y;
121 if ((ret = av_expr_parse_and_eval(&res, (expr = ctx->h_expr),
122 var_names, var_values,
123 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0 && i == NUM_EXPR_EVALS)
124 goto fail;
125 ctx->h = var_values[VAR_H] = res;
126
127 var_values[VAR_MAX] = INT_MAX;
128 if ((ret = av_expr_parse_and_eval(&res, (expr = ctx->t_expr),
129 var_names, var_values,
130 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0 && i == NUM_EXPR_EVALS)
131 goto fail;
132 ctx->thickness = var_values[VAR_T] = res;
133 }
134
135 /* Sanity check */
136 ctx->w = (ctx->w > 0) ? ctx->w : inlink->w;
137 ctx->h = (ctx->h > 0) ? ctx->h : inlink->h;
138 if (ctx->x + ctx->w > inlink->w)
139 ctx->w = inlink->w - ctx->x;
140 if (ctx->y + ctx->h > inlink->h)
141 ctx->h = inlink->h - ctx->y;
142
143 ctx->outer_rect.x = ctx->x;
144 ctx->outer_rect.y = ctx->y;
145 ctx->outer_rect.width = ctx->w;
146 ctx->outer_rect.height = ctx->h;
147
148 if (ctx->outer_rect.width <= ctx->thickness * 2 ||
149 ctx->outer_rect.height <= ctx->thickness * 2) {
150 ctx->fill = 1;
151 } else {
152 ctx->fill = 0;
153 ctx->inner_rect.x = ctx->outer_rect.x + ctx->thickness;
154 ctx->inner_rect.y = ctx->outer_rect.y + ctx->thickness;
155 ctx->inner_rect.width = ctx->outer_rect.width - ctx->thickness * 2;
156 ctx->inner_rect.height = ctx->outer_rect.height - ctx->thickness * 2;
157 }
158
159 vpp_ctx->output_width = inlink->w;
160 vpp_ctx->output_height = inlink->h;
161
162 ret = ff_vaapi_vpp_config_output(outlink);
163 if (ret < 0)
164 return ret;
165
166 ctx->outer_frames_ref = av_hwframe_ctx_alloc(vpp_ctx->device_ref);
167 if (!ctx->outer_frames_ref) {
168 return AVERROR(ENOMEM);
169 }
170
171 ctx->outer_frames = (AVHWFramesContext*)ctx->outer_frames_ref->data;
172
173 ctx->outer_frames->format = AV_PIX_FMT_VAAPI;
174 ctx->outer_frames->sw_format = vpp_ctx->input_frames->sw_format;
175 ctx->outer_frames->width = ctx->outer_rect.width;
176 ctx->outer_frames->height = ctx->outer_rect.height;
177
178 return av_hwframe_ctx_init(ctx->outer_frames_ref);
179
180 fail:
181 av_log(avctx, AV_LOG_ERROR,
182 "Error when evaluating the expression '%s'.\n",
183 expr);
184 return ret;
185 }
186
187 static int drawbox_vaapi_filter_frame(AVFilterLink *link, AVFrame *input_frame)
188 {
189 AVFilterContext *avctx = link->dst;
190 AVFilterLink *outlink = avctx->outputs[0];
191 VAAPIVPPContext *vpp_ctx = avctx->priv;
192 DrawboxVAAPIContext *drawbox_ctx = avctx->priv;
193 AVFrame *output_frame = NULL;
194 VAProcPipelineParameterBuffer box_params;
195 VAProcPipelineParameterBuffer params[3];
196 VABlendState blend_state = {
197 .flags = VA_BLEND_GLOBAL_ALPHA,
198 };
199 VARectangle box[4];
200 int err, nb_params = 0;
201
202 if (!input_frame->hw_frames_ctx ||
203 vpp_ctx->va_context == VA_INVALID_ID) {
204 err = AVERROR(EINVAL);
205 goto fail;
206 }
207
208 if (!drawbox_ctx->outer_frame) {
209 drawbox_ctx->outer_frame = av_frame_alloc();
210 if (!drawbox_ctx->outer_frame) {
211 err = AVERROR(ENOMEM);
212 goto fail;
213 }
214
215 err = av_hwframe_get_buffer(drawbox_ctx->outer_frames_ref, drawbox_ctx->outer_frame, 0);
216 if (err < 0) {
217 err = AVERROR(ENOMEM);
218 goto fail;
219 }
220
221 /* Create image for the outer rect */
222 err = ff_vaapi_vpp_init_params(avctx, &box_params,
223 input_frame, drawbox_ctx->outer_frame);
224 if (err < 0)
225 goto fail;
226
227 blend_state.global_alpha = 0.0f;
228 box_params.surface_region = &drawbox_ctx->outer_rect;
229 box_params.blend_state = &blend_state;
230 box_params.output_background_color = (drawbox_ctx->drawbox_rgba[3] << 24 |
231 drawbox_ctx->drawbox_rgba[0] << 16 |
232 drawbox_ctx->drawbox_rgba[1] << 8 |
233 drawbox_ctx->drawbox_rgba[2]);
234
235 err = ff_vaapi_vpp_render_picture(avctx, &box_params, drawbox_ctx->outer_frame);
236 if (err < 0)
237 goto fail;
238 }
239
240 /* Draw outer & inner rects on the input video, then we can get a box*/
241 output_frame = ff_get_video_buffer(outlink, outlink->w, outlink->h);
242 if (!output_frame) {
243 err = AVERROR(ENOMEM);
244 goto fail;
245 }
246
247 err = av_frame_copy_props(output_frame, input_frame);
248 if (err < 0)
249 goto fail;
250
251 err = ff_vaapi_vpp_init_params(avctx, &params[nb_params],
252 input_frame, output_frame);
253 if (err < 0)
254 goto fail;
255
256 box[0].x = 0;
257 box[0].y = 0;
258 box[0].width = link->w;
259 box[0].height = link->h;
260 params[nb_params].surface_region = &box[0];
261 params[nb_params].output_background_color = 0;
262 nb_params++;
263
264 err = ff_vaapi_vpp_init_params(avctx, &params[nb_params],
265 drawbox_ctx->outer_frame, output_frame);
266 if (err < 0)
267 goto fail;
268
269 box[1] = drawbox_ctx->outer_rect;
270 if (drawbox_ctx->drawbox_rgba[3] != 255 && !drawbox_ctx->replace) {
271 blend_state.global_alpha = (float)drawbox_ctx->drawbox_rgba[3] / 255;
272 params[nb_params].blend_state = &blend_state;
273 }
274 params[nb_params].output_region = &box[1];
275 params[nb_params].output_background_color = 0;
276 nb_params++;
277
278 if (!drawbox_ctx->fill) {
279 box[3] = box[2] = drawbox_ctx->inner_rect;
280 params[nb_params] = params[0];
281 params[nb_params].surface_region = &box[2];
282 params[nb_params].output_region = &box[3];
283 params[nb_params].output_background_color = 0;
284 nb_params++;
285 }
286
287 err = ff_vaapi_vpp_render_pictures(avctx, params, nb_params, output_frame);
288 if (err < 0)
289 goto fail;
290
291 av_frame_free(&input_frame);
292
293 return ff_filter_frame(outlink, output_frame);
294
295 fail:
296 av_frame_free(&input_frame);
297 av_frame_free(&output_frame);
298 return err;
299 }
300
301 static av_cold int drawbox_vaapi_init(AVFilterContext *avctx)
302 {
303 VAAPIVPPContext *vpp_ctx = avctx->priv;
304
305 ff_vaapi_vpp_ctx_init(avctx);
306 vpp_ctx->pipeline_uninit = ff_vaapi_vpp_pipeline_uninit;
307 vpp_ctx->output_format = AV_PIX_FMT_NONE;
308
309 return 0;
310 }
311
312 static av_cold void drawbox_vaapi_uninit(AVFilterContext *avctx)
313 {
314 DrawboxVAAPIContext *ctx = avctx->priv;
315
316 av_frame_free(&ctx->outer_frame);
317 av_buffer_unref(&ctx->outer_frames_ref);
318 ff_vaapi_vpp_ctx_uninit(avctx);
319 }
320
321 #define OFFSET(x) offsetof(DrawboxVAAPIContext, x)
322 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
323
324 static const AVOption drawbox_vaapi_options[] = {
325 { "x", "set horizontal position of the left box edge", OFFSET(x_expr), AV_OPT_TYPE_STRING, { .str="0" }, 0, 0, FLAGS },
326 { "y", "set vertical position of the top box edge", OFFSET(y_expr), AV_OPT_TYPE_STRING, { .str="0" }, 0, 0, FLAGS },
327 { "width", "set width of the box", OFFSET(w_expr), AV_OPT_TYPE_STRING, { .str="0" }, 0, 0, FLAGS },
328 { "w", "set width of the box", OFFSET(w_expr), AV_OPT_TYPE_STRING, { .str="0" }, 0, 0, FLAGS },
329 { "height", "set height of the box", OFFSET(h_expr), AV_OPT_TYPE_STRING, { .str="0" }, 0, 0, FLAGS },
330 { "h", "set height of the box", OFFSET(h_expr), AV_OPT_TYPE_STRING, { .str="0" }, 0, 0, FLAGS },
331 { "color", "set color of the box", OFFSET(drawbox_rgba), AV_OPT_TYPE_COLOR, { .str = "black" }, 0, 0, FLAGS },
332 { "c", "set color of the box", OFFSET(drawbox_rgba), AV_OPT_TYPE_COLOR, { .str = "black" }, 0, 0, FLAGS },
333 { "thickness", "set the box thickness", OFFSET(t_expr), AV_OPT_TYPE_STRING, { .str="3" }, 0, 0, FLAGS },
334 { "t", "set the box thickness", OFFSET(t_expr), AV_OPT_TYPE_STRING, { .str="3" }, 0, 0, FLAGS },
335 { "replace", "replace color", OFFSET(replace), AV_OPT_TYPE_BOOL, { .i64=0 }, 0, 1, FLAGS },
336 { NULL }
337 };
338
339 AVFILTER_DEFINE_CLASS(drawbox_vaapi);
340
341 static const AVFilterPad drawbox_vaapi_inputs[] = {
342 {
343 .name = "default",
344 .type = AVMEDIA_TYPE_VIDEO,
345 .filter_frame = drawbox_vaapi_filter_frame,
346 .config_props = &ff_vaapi_vpp_config_input,
347 },
348 };
349
350 static const AVFilterPad drawbox_vaapi_outputs[] = {
351 {
352 .name = "default",
353 .type = AVMEDIA_TYPE_VIDEO,
354 .config_props = &drawbox_vaapi_config_output,
355 },
356 };
357
358 const AVFilter ff_vf_drawbox_vaapi = {
359 .name = "drawbox_vaapi",
360 .description = NULL_IF_CONFIG_SMALL("Draw a colored box on the input video."),
361 .priv_size = sizeof(DrawboxVAAPIContext),
362 .priv_class = &drawbox_vaapi_class,
363 .init = &drawbox_vaapi_init,
364 .uninit = &drawbox_vaapi_uninit,
365 FILTER_INPUTS(drawbox_vaapi_inputs),
366 FILTER_OUTPUTS(drawbox_vaapi_outputs),
367 FILTER_QUERY_FUNC(&ff_vaapi_vpp_query_formats),
368 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
369 };
370