| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2008 Affine Systems, Inc (Michael Sullivan, Bobby Impollonia) | ||
| 3 | * Copyright (c) 2013 Andrey Utkin <andrey.krieger.utkin gmail com> | ||
| 4 | * | ||
| 5 | * This file is part of FFmpeg. | ||
| 6 | * | ||
| 7 | * FFmpeg is free software; you can redistribute it and/or | ||
| 8 | * modify it under the terms of the GNU Lesser General Public | ||
| 9 | * License as published by the Free Software Foundation; either | ||
| 10 | * version 2.1 of the License, or (at your option) any later version. | ||
| 11 | * | ||
| 12 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 15 | * Lesser General Public License for more details. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU Lesser General Public | ||
| 18 | * License along with FFmpeg; if not, write to the Free Software | ||
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 20 | */ | ||
| 21 | |||
| 22 | /** | ||
| 23 | * @file | ||
| 24 | * Box and grid drawing filters. Also a nice template for a filter | ||
| 25 | * that needs to write in the input frame. | ||
| 26 | */ | ||
| 27 | |||
| 28 | #include "config_components.h" | ||
| 29 | |||
| 30 | #include "libavutil/colorspace.h" | ||
| 31 | #include "libavutil/common.h" | ||
| 32 | #include "libavutil/opt.h" | ||
| 33 | #include "libavutil/eval.h" | ||
| 34 | #include "libavutil/pixdesc.h" | ||
| 35 | #include "libavutil/parseutils.h" | ||
| 36 | #include "libavutil/detection_bbox.h" | ||
| 37 | #include "avfilter.h" | ||
| 38 | #include "drawutils.h" | ||
| 39 | #include "filters.h" | ||
| 40 | #include "video.h" | ||
| 41 | |||
| 42 | static const char *const var_names[] = { | ||
| 43 | "dar", | ||
| 44 | "hsub", "vsub", | ||
| 45 | "in_h", "ih", ///< height of the input video | ||
| 46 | "in_w", "iw", ///< width of the input video | ||
| 47 | "sar", | ||
| 48 | "x", | ||
| 49 | "y", | ||
| 50 | "h", ///< height of the rendered box | ||
| 51 | "w", ///< width of the rendered box | ||
| 52 | "t", | ||
| 53 | "fill", | ||
| 54 | NULL | ||
| 55 | }; | ||
| 56 | |||
| 57 | enum { Y, U, V, A }; | ||
| 58 | enum { R, G, B }; | ||
| 59 | |||
| 60 | enum var_name { | ||
| 61 | VAR_DAR, | ||
| 62 | VAR_HSUB, VAR_VSUB, | ||
| 63 | VAR_IN_H, VAR_IH, | ||
| 64 | VAR_IN_W, VAR_IW, | ||
| 65 | VAR_SAR, | ||
| 66 | VAR_X, | ||
| 67 | VAR_Y, | ||
| 68 | VAR_H, | ||
| 69 | VAR_W, | ||
| 70 | VAR_T, | ||
| 71 | VAR_MAX, | ||
| 72 | VARS_NB | ||
| 73 | }; | ||
| 74 | |||
| 75 | struct DrawBoxContext; | ||
| 76 | |||
| 77 | typedef int (*PixelBelongsToRegion)(struct DrawBoxContext *s, int x, int y); | ||
| 78 | |||
| 79 | typedef struct DrawBoxContext { | ||
| 80 | const AVClass *class; | ||
| 81 | int x, y, w, h; | ||
| 82 | int thickness; | ||
| 83 | char *color_str; | ||
| 84 | uint8_t rgba_map[4]; | ||
| 85 | uint8_t rgba_color[4]; | ||
| 86 | unsigned char yuv_color[4]; | ||
| 87 | int invert_color; ///< invert luma color | ||
| 88 | int vsub, hsub; ///< chroma subsampling | ||
| 89 | char *x_expr, *y_expr; ///< expression for x and y | ||
| 90 | char *w_expr, *h_expr; ///< expression for width and height | ||
| 91 | char *t_expr; ///< expression for thickness | ||
| 92 | char *box_source_string; ///< string for box data source | ||
| 93 | int have_alpha; | ||
| 94 | int replace; | ||
| 95 | int step; | ||
| 96 | enum AVFrameSideDataType box_source; | ||
| 97 | |||
| 98 | void (*draw_region)(AVFrame *frame, struct DrawBoxContext *ctx, int left, int top, int right, int down, | ||
| 99 | PixelBelongsToRegion pixel_belongs_to_region); | ||
| 100 | } DrawBoxContext; | ||
| 101 | |||
| 102 | static const int NUM_EXPR_EVALS = 5; | ||
| 103 | |||
| 104 | #define ASSIGN_THREE_CHANNELS \ | ||
| 105 | row[0] = frame->data[0] + y * frame->linesize[0]; \ | ||
| 106 | row[1] = frame->data[1] + (y >> ctx->vsub) * frame->linesize[1]; \ | ||
| 107 | row[2] = frame->data[2] + (y >> ctx->vsub) * frame->linesize[2]; | ||
| 108 | |||
| 109 | #define ASSIGN_FOUR_CHANNELS \ | ||
| 110 | ASSIGN_THREE_CHANNELS \ | ||
| 111 | row[3] = frame->data[3] + y * frame->linesize[3]; | ||
| 112 | |||
| 113 | 1226 | static void draw_region(AVFrame *frame, DrawBoxContext *ctx, int left, int top, int right, int down, | |
| 114 | PixelBelongsToRegion pixel_belongs_to_region) | ||
| 115 | { | ||
| 116 | unsigned char *row[4]; | ||
| 117 | int x, y; | ||
| 118 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1226 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
1226 | if (ctx->have_alpha && ctx->replace) { |
| 119 | ✗ | for (y = top; y < down; y++) { | |
| 120 | ✗ | ASSIGN_FOUR_CHANNELS | |
| 121 | ✗ | if (ctx->invert_color) { | |
| 122 | ✗ | for (x = left; x < right; x++) | |
| 123 | ✗ | if (pixel_belongs_to_region(ctx, x, y)) | |
| 124 | ✗ | row[0][x] = 0xff - row[0][x]; | |
| 125 | } else { | ||
| 126 | ✗ | for (x = left; x < right; x++) { | |
| 127 | ✗ | if (pixel_belongs_to_region(ctx, x, y)) { | |
| 128 | ✗ | row[0][x ] = ctx->yuv_color[Y]; | |
| 129 | ✗ | row[1][x >> ctx->hsub] = ctx->yuv_color[U]; | |
| 130 | ✗ | row[2][x >> ctx->hsub] = ctx->yuv_color[V]; | |
| 131 | ✗ | row[3][x ] = ctx->yuv_color[A]; | |
| 132 | } | ||
| 133 | } | ||
| 134 | } | ||
| 135 | } | ||
| 136 | } else { | ||
| 137 |
2/2✓ Branch 0 taken 5952 times.
✓ Branch 1 taken 1226 times.
|
7178 | for (y = top; y < down; y++) { |
| 138 | 5952 | ASSIGN_THREE_CHANNELS | |
| 139 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5952 times.
|
5952 | if (ctx->invert_color) { |
| 140 | ✗ | for (x = left; x < right; x++) | |
| 141 | ✗ | if (pixel_belongs_to_region(ctx, x, y)) | |
| 142 | ✗ | row[0][x] = 0xff - row[0][x]; | |
| 143 | } else { | ||
| 144 |
2/2✓ Branch 0 taken 321504 times.
✓ Branch 1 taken 5952 times.
|
327456 | for (x = left; x < right; x++) { |
| 145 | 321504 | double alpha = (double)ctx->yuv_color[A] / 255; | |
| 146 | |||
| 147 |
2/2✓ Branch 1 taken 50904 times.
✓ Branch 2 taken 270600 times.
|
321504 | if (pixel_belongs_to_region(ctx, x, y)) { |
| 148 | 50904 | row[0][x ] = (1 - alpha) * row[0][x ] + alpha * ctx->yuv_color[Y]; | |
| 149 | 50904 | row[1][x >> ctx->hsub] = (1 - alpha) * row[1][x >> ctx->hsub] + alpha * ctx->yuv_color[U]; | |
| 150 | 50904 | row[2][x >> ctx->hsub] = (1 - alpha) * row[2][x >> ctx->hsub] + alpha * ctx->yuv_color[V]; | |
| 151 | } | ||
| 152 | } | ||
| 153 | } | ||
| 154 | } | ||
| 155 | } | ||
| 156 | 1226 | } | |
| 157 | |||
| 158 | #define ASSIGN_THREE_CHANNELS_PACKED \ | ||
| 159 | row[0] = frame->data[0] + y * frame->linesize[0] + ctx->rgba_map[0]; \ | ||
| 160 | row[1] = frame->data[0] + y * frame->linesize[0] + ctx->rgba_map[1]; \ | ||
| 161 | row[2] = frame->data[0] + y * frame->linesize[0] + ctx->rgba_map[2]; | ||
| 162 | |||
| 163 | #define ASSIGN_FOUR_CHANNELS_PACKED \ | ||
| 164 | ASSIGN_THREE_CHANNELS_PACKED \ | ||
| 165 | row[3] = frame->data[0] + y * frame->linesize[0] + ctx->rgba_map[3]; | ||
| 166 | |||
| 167 | ✗ | static void draw_region_rgb_packed(AVFrame *frame, DrawBoxContext *ctx, int left, int top, int right, int down, | |
| 168 | PixelBelongsToRegion pixel_belongs_to_region) | ||
| 169 | { | ||
| 170 | ✗ | const int C = ctx->step; | |
| 171 | uint8_t *row[4]; | ||
| 172 | |||
| 173 | ✗ | if (ctx->have_alpha && ctx->replace) { | |
| 174 | ✗ | for (int y = top; y < down; y++) { | |
| 175 | ✗ | ASSIGN_FOUR_CHANNELS_PACKED | |
| 176 | ✗ | if (ctx->invert_color) { | |
| 177 | ✗ | for (int x = left; x < right; x++) | |
| 178 | ✗ | if (pixel_belongs_to_region(ctx, x, y)) { | |
| 179 | ✗ | row[0][x*C] = 0xff - row[0][x*C]; | |
| 180 | ✗ | row[1][x*C] = 0xff - row[1][x*C]; | |
| 181 | ✗ | row[2][x*C] = 0xff - row[2][x*C]; | |
| 182 | } | ||
| 183 | } else { | ||
| 184 | ✗ | for (int x = left; x < right; x++) { | |
| 185 | ✗ | if (pixel_belongs_to_region(ctx, x, y)) { | |
| 186 | ✗ | row[0][x*C] = ctx->rgba_color[R]; | |
| 187 | ✗ | row[1][x*C] = ctx->rgba_color[G]; | |
| 188 | ✗ | row[2][x*C] = ctx->rgba_color[B]; | |
| 189 | ✗ | row[3][x*C] = ctx->rgba_color[A]; | |
| 190 | } | ||
| 191 | } | ||
| 192 | } | ||
| 193 | } | ||
| 194 | } else { | ||
| 195 | ✗ | for (int y = top; y < down; y++) { | |
| 196 | ✗ | ASSIGN_THREE_CHANNELS_PACKED | |
| 197 | ✗ | if (ctx->invert_color) { | |
| 198 | ✗ | for (int x = left; x < right; x++) | |
| 199 | ✗ | if (pixel_belongs_to_region(ctx, x, y)) { | |
| 200 | ✗ | row[0][x*C] = 0xff - row[0][x*C]; | |
| 201 | ✗ | row[1][x*C] = 0xff - row[1][x*C]; | |
| 202 | ✗ | row[2][x*C] = 0xff - row[2][x*C]; | |
| 203 | } | ||
| 204 | } else { | ||
| 205 | ✗ | for (int x = left; x < right; x++) { | |
| 206 | ✗ | float alpha = (float)ctx->rgba_color[A] / 255.f; | |
| 207 | |||
| 208 | ✗ | if (pixel_belongs_to_region(ctx, x, y)) { | |
| 209 | ✗ | row[0][x*C] = (1.f - alpha) * row[0][x*C] + alpha * ctx->rgba_color[R]; | |
| 210 | ✗ | row[1][x*C] = (1.f - alpha) * row[1][x*C] + alpha * ctx->rgba_color[G]; | |
| 211 | ✗ | row[2][x*C] = (1.f - alpha) * row[2][x*C] + alpha * ctx->rgba_color[B]; | |
| 212 | } | ||
| 213 | } | ||
| 214 | } | ||
| 215 | } | ||
| 216 | } | ||
| 217 | ✗ | } | |
| 218 | |||
| 219 | ✗ | static enum AVFrameSideDataType box_source_string_parse(const char *box_source_string) | |
| 220 | { | ||
| 221 | ✗ | av_assert0(box_source_string); | |
| 222 | ✗ | if (!strcmp(box_source_string, "side_data_detection_bboxes")) { | |
| 223 | ✗ | return AV_FRAME_DATA_DETECTION_BBOXES; | |
| 224 | } else { | ||
| 225 | // will support side_data_regions_of_interest next | ||
| 226 | ✗ | return AVERROR(EINVAL); | |
| 227 | } | ||
| 228 | } | ||
| 229 | |||
| 230 | 8 | static av_cold int init(AVFilterContext *ctx) | |
| 231 | { | ||
| 232 | 8 | DrawBoxContext *s = ctx->priv; | |
| 233 | |||
| 234 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | if (s->box_source_string) { |
| 235 | ✗ | s->box_source = box_source_string_parse(s->box_source_string); | |
| 236 | ✗ | if ((int)s->box_source < 0) { | |
| 237 | ✗ | av_log(ctx, AV_LOG_ERROR, "Error box source: %s\n",s->box_source_string); | |
| 238 | ✗ | return AVERROR(EINVAL); | |
| 239 | } | ||
| 240 | } | ||
| 241 | |||
| 242 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | if (!strcmp(s->color_str, "invert")) |
| 243 | ✗ | s->invert_color = 1; | |
| 244 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
|
8 | else if (av_parse_color(s->rgba_color, s->color_str, -1, ctx) < 0) |
| 245 | ✗ | return AVERROR(EINVAL); | |
| 246 | |||
| 247 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | if (!s->invert_color) { |
| 248 | 8 | s->yuv_color[Y] = RGB_TO_Y_CCIR(s->rgba_color[0], s->rgba_color[1], s->rgba_color[2]); | |
| 249 | 8 | s->yuv_color[U] = RGB_TO_U_CCIR(s->rgba_color[0], s->rgba_color[1], s->rgba_color[2], 0); | |
| 250 | 8 | s->yuv_color[V] = RGB_TO_V_CCIR(s->rgba_color[0], s->rgba_color[1], s->rgba_color[2], 0); | |
| 251 | 8 | s->yuv_color[A] = s->rgba_color[3]; | |
| 252 | } | ||
| 253 | |||
| 254 | 8 | return 0; | |
| 255 | } | ||
| 256 | |||
| 257 | static const enum AVPixelFormat pix_fmts[] = { | ||
| 258 | AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P, | ||
| 259 | AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, | ||
| 260 | AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P, | ||
| 261 | AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUVJ440P, | ||
| 262 | AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P, | ||
| 263 | AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24, | ||
| 264 | AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA, | ||
| 265 | AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR, | ||
| 266 | AV_PIX_FMT_0RGB, AV_PIX_FMT_0BGR, | ||
| 267 | AV_PIX_FMT_RGB0, AV_PIX_FMT_BGR0, | ||
| 268 | AV_PIX_FMT_NONE | ||
| 269 | }; | ||
| 270 | |||
| 271 | 4 | static int config_input(AVFilterLink *inlink) | |
| 272 | { | ||
| 273 | 4 | AVFilterContext *ctx = inlink->dst; | |
| 274 | 4 | DrawBoxContext *s = ctx->priv; | |
| 275 | 4 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); | |
| 276 | double var_values[VARS_NB], res; | ||
| 277 | char *expr; | ||
| 278 | int ret; | ||
| 279 | int i; | ||
| 280 | |||
| 281 | 4 | ff_fill_rgba_map(s->rgba_map, inlink->format); | |
| 282 | |||
| 283 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | if (!(desc->flags & AV_PIX_FMT_FLAG_RGB)) |
| 284 | 4 | s->draw_region = draw_region; | |
| 285 | else | ||
| 286 | ✗ | s->draw_region = draw_region_rgb_packed; | |
| 287 | |||
| 288 | 4 | s->step = av_get_padded_bits_per_pixel(desc) >> 3; | |
| 289 | 4 | s->hsub = desc->log2_chroma_w; | |
| 290 | 4 | s->vsub = desc->log2_chroma_h; | |
| 291 | 4 | s->have_alpha = desc->flags & AV_PIX_FMT_FLAG_ALPHA; | |
| 292 | |||
| 293 | 4 | var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h; | |
| 294 | 4 | var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w; | |
| 295 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ? av_q2d(inlink->sample_aspect_ratio) : 1; |
| 296 | 4 | var_values[VAR_DAR] = (double)inlink->w / inlink->h * var_values[VAR_SAR]; | |
| 297 | 4 | var_values[VAR_HSUB] = s->hsub; | |
| 298 | 4 | var_values[VAR_VSUB] = s->vsub; | |
| 299 | 4 | var_values[VAR_X] = NAN; | |
| 300 | 4 | var_values[VAR_Y] = NAN; | |
| 301 | 4 | var_values[VAR_H] = NAN; | |
| 302 | 4 | var_values[VAR_W] = NAN; | |
| 303 | 4 | var_values[VAR_T] = NAN; | |
| 304 | |||
| 305 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 4 times.
|
28 | for (i = 0; i <= NUM_EXPR_EVALS; i++) { |
| 306 | /* evaluate expressions, fail on last iteration */ | ||
| 307 | 24 | var_values[VAR_MAX] = inlink->w; | |
| 308 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
|
24 | if ((ret = av_expr_parse_and_eval(&res, (expr = s->x_expr), |
| 309 | var_names, var_values, | ||
| 310 | ✗ | NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0 && i == NUM_EXPR_EVALS) | |
| 311 | ✗ | goto fail; | |
| 312 | 24 | s->x = var_values[VAR_X] = res; | |
| 313 | |||
| 314 | 24 | var_values[VAR_MAX] = inlink->h; | |
| 315 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
|
24 | if ((ret = av_expr_parse_and_eval(&res, (expr = s->y_expr), |
| 316 | var_names, var_values, | ||
| 317 | ✗ | NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0 && i == NUM_EXPR_EVALS) | |
| 318 | ✗ | goto fail; | |
| 319 | 24 | s->y = var_values[VAR_Y] = res; | |
| 320 | |||
| 321 | 24 | var_values[VAR_MAX] = inlink->w - s->x; | |
| 322 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
|
24 | if ((ret = av_expr_parse_and_eval(&res, (expr = s->w_expr), |
| 323 | var_names, var_values, | ||
| 324 | ✗ | NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0 && i == NUM_EXPR_EVALS) | |
| 325 | ✗ | goto fail; | |
| 326 | 24 | s->w = var_values[VAR_W] = res; | |
| 327 | |||
| 328 | 24 | var_values[VAR_MAX] = inlink->h - s->y; | |
| 329 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
|
24 | if ((ret = av_expr_parse_and_eval(&res, (expr = s->h_expr), |
| 330 | var_names, var_values, | ||
| 331 | ✗ | NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0 && i == NUM_EXPR_EVALS) | |
| 332 | ✗ | goto fail; | |
| 333 | 24 | s->h = var_values[VAR_H] = res; | |
| 334 | |||
| 335 | 24 | var_values[VAR_MAX] = INT_MAX; | |
| 336 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
|
24 | if ((ret = av_expr_parse_and_eval(&res, (expr = s->t_expr), |
| 337 | var_names, var_values, | ||
| 338 | ✗ | NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0 && i == NUM_EXPR_EVALS) | |
| 339 | ✗ | goto fail; | |
| 340 | 24 | s->thickness = var_values[VAR_T] = res; | |
| 341 | } | ||
| 342 | |||
| 343 | /* if w or h are zero, use the input w/h */ | ||
| 344 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
|
4 | s->w = (s->w > 0) ? s->w : inlink->w; |
| 345 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
|
4 | s->h = (s->h > 0) ? s->h : inlink->h; |
| 346 | |||
| 347 | /* sanity check width and height */ | ||
| 348 |
2/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
|
4 | if (s->w < 0 || s->h < 0) { |
| 349 | ✗ | av_log(ctx, AV_LOG_ERROR, "Size values less than 0 are not acceptable.\n"); | |
| 350 | ✗ | return AVERROR(EINVAL); | |
| 351 | } | ||
| 352 | |||
| 353 | 4 | av_log(ctx, AV_LOG_VERBOSE, "x:%d y:%d w:%d h:%d color:0x%02X%02X%02X%02X\n", | |
| 354 | s->x, s->y, s->w, s->h, | ||
| 355 | 4 | s->yuv_color[Y], s->yuv_color[U], s->yuv_color[V], s->yuv_color[A]); | |
| 356 | |||
| 357 | 4 | return 0; | |
| 358 | |||
| 359 | ✗ | fail: | |
| 360 | ✗ | av_log(ctx, AV_LOG_ERROR, | |
| 361 | "Error when evaluating the expression '%s'.\n", | ||
| 362 | expr); | ||
| 363 | ✗ | return ret; | |
| 364 | } | ||
| 365 | |||
| 366 | 321504 | static av_pure av_always_inline int pixel_belongs_to_box(DrawBoxContext *s, int x, int y) | |
| 367 | { | ||
| 368 |
2/2✓ Branch 0 taken 290400 times.
✓ Branch 1 taken 13200 times.
|
303600 | return (y - s->y < s->thickness) || (s->y + s->h - 1 - y < s->thickness) || |
| 369 |
6/6✓ Branch 0 taken 303600 times.
✓ Branch 1 taken 17904 times.
✓ Branch 2 taken 280500 times.
✓ Branch 3 taken 9900 times.
✓ Branch 4 taken 9900 times.
✓ Branch 5 taken 270600 times.
|
625104 | (x - s->x < s->thickness) || (s->x + s->w - 1 - x < s->thickness); |
| 370 | } | ||
| 371 | |||
| 372 | 1226 | static int filter_frame(AVFilterLink *inlink, AVFrame *frame) | |
| 373 | { | ||
| 374 | 1226 | AVFilterContext *ctx = inlink->dst; | |
| 375 | 1226 | DrawBoxContext *s = ctx->priv; | |
| 376 | 1226 | const AVDetectionBBoxHeader *header = NULL; | |
| 377 | const AVDetectionBBox *bbox; | ||
| 378 | AVFrameSideData *sd; | ||
| 379 | 1226 | int loop = 1; | |
| 380 | |||
| 381 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1226 times.
|
1226 | if (s->box_source == AV_FRAME_DATA_DETECTION_BBOXES) { |
| 382 | ✗ | sd = av_frame_get_side_data(frame, AV_FRAME_DATA_DETECTION_BBOXES); | |
| 383 | ✗ | if (sd) { | |
| 384 | ✗ | header = (AVDetectionBBoxHeader *)sd->data; | |
| 385 | ✗ | loop = header->nb_bboxes; | |
| 386 | } else { | ||
| 387 | ✗ | av_log(ctx, AV_LOG_WARNING, "No detection bboxes.\n"); | |
| 388 | ✗ | return ff_filter_frame(inlink->dst->outputs[0], frame); | |
| 389 | } | ||
| 390 | } | ||
| 391 | |||
| 392 |
2/2✓ Branch 0 taken 1226 times.
✓ Branch 1 taken 1226 times.
|
2452 | for (int i = 0; i < loop; i++) { |
| 393 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1226 times.
|
1226 | if (header) { |
| 394 | ✗ | bbox = av_get_detection_bbox(header, i); | |
| 395 | ✗ | s->y = bbox->y; | |
| 396 | ✗ | s->x = bbox->x; | |
| 397 | ✗ | s->h = bbox->h; | |
| 398 | ✗ | s->w = bbox->w; | |
| 399 | } | ||
| 400 | |||
| 401 | 1226 | s->draw_region(frame, s, FFMAX(s->x, 0), FFMAX(s->y, 0), FFMIN(s->x + s->w, frame->width), | |
| 402 | 1226 | FFMIN(s->y + s->h, frame->height), pixel_belongs_to_box); | |
| 403 | } | ||
| 404 | |||
| 405 | 1226 | return ff_filter_frame(inlink->dst->outputs[0], frame); | |
| 406 | } | ||
| 407 | |||
| 408 | ✗ | static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags) | |
| 409 | { | ||
| 410 | ✗ | AVFilterLink *inlink = ctx->inputs[0]; | |
| 411 | ✗ | DrawBoxContext *s = ctx->priv; | |
| 412 | ✗ | int old_x = s->x; | |
| 413 | ✗ | int old_y = s->y; | |
| 414 | ✗ | int old_w = s->w; | |
| 415 | ✗ | int old_h = s->h; | |
| 416 | ✗ | int old_t = s->thickness; | |
| 417 | ✗ | int old_r = s->replace; | |
| 418 | int ret; | ||
| 419 | |||
| 420 | ✗ | ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags); | |
| 421 | ✗ | if (ret < 0) | |
| 422 | ✗ | return ret; | |
| 423 | |||
| 424 | ✗ | ret = init(ctx); | |
| 425 | ✗ | if (ret < 0) | |
| 426 | ✗ | goto end; | |
| 427 | ✗ | ret = config_input(inlink); | |
| 428 | ✗ | end: | |
| 429 | ✗ | if (ret < 0) { | |
| 430 | ✗ | s->x = old_x; | |
| 431 | ✗ | s->y = old_y; | |
| 432 | ✗ | s->w = old_w; | |
| 433 | ✗ | s->h = old_h; | |
| 434 | ✗ | s->thickness = old_t; | |
| 435 | ✗ | s->replace = old_r; | |
| 436 | } | ||
| 437 | |||
| 438 | ✗ | return ret; | |
| 439 | } | ||
| 440 | |||
| 441 | #define OFFSET(x) offsetof(DrawBoxContext, x) | ||
| 442 | #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM | ||
| 443 | |||
| 444 | #if CONFIG_DRAWBOX_FILTER | ||
| 445 | |||
| 446 | static const AVOption drawbox_options[] = { | ||
| 447 | { "x", "set horizontal position of the left box edge", OFFSET(x_expr), AV_OPT_TYPE_STRING, { .str="0" }, 0, 0, FLAGS }, | ||
| 448 | { "y", "set vertical position of the top box edge", OFFSET(y_expr), AV_OPT_TYPE_STRING, { .str="0" }, 0, 0, FLAGS }, | ||
| 449 | { "width", "set width of the box", OFFSET(w_expr), AV_OPT_TYPE_STRING, { .str="0" }, 0, 0, FLAGS }, | ||
| 450 | { "w", "set width of the box", OFFSET(w_expr), AV_OPT_TYPE_STRING, { .str="0" }, 0, 0, FLAGS }, | ||
| 451 | { "height", "set height of the box", OFFSET(h_expr), AV_OPT_TYPE_STRING, { .str="0" }, 0, 0, FLAGS }, | ||
| 452 | { "h", "set height of the box", OFFSET(h_expr), AV_OPT_TYPE_STRING, { .str="0" }, 0, 0, FLAGS }, | ||
| 453 | { "color", "set color of the box", OFFSET(color_str), AV_OPT_TYPE_STRING, { .str = "black" }, 0, 0, FLAGS }, | ||
| 454 | { "c", "set color of the box", OFFSET(color_str), AV_OPT_TYPE_STRING, { .str = "black" }, 0, 0, FLAGS }, | ||
| 455 | { "thickness", "set the box thickness", OFFSET(t_expr), AV_OPT_TYPE_STRING, { .str="3" }, 0, 0, FLAGS }, | ||
| 456 | { "t", "set the box thickness", OFFSET(t_expr), AV_OPT_TYPE_STRING, { .str="3" }, 0, 0, FLAGS }, | ||
| 457 | { "replace", "replace color & alpha", OFFSET(replace), AV_OPT_TYPE_BOOL, { .i64=0 }, 0, 1, FLAGS }, | ||
| 458 | { "box_source","use data from bounding box in side data", OFFSET(box_source_string), AV_OPT_TYPE_STRING, { .str=NULL }, 0, 1, FLAGS }, | ||
| 459 | { NULL } | ||
| 460 | }; | ||
| 461 | |||
| 462 | AVFILTER_DEFINE_CLASS(drawbox); | ||
| 463 | |||
| 464 | static const AVFilterPad drawbox_inputs[] = { | ||
| 465 | { | ||
| 466 | .name = "default", | ||
| 467 | .type = AVMEDIA_TYPE_VIDEO, | ||
| 468 | .flags = AVFILTERPAD_FLAG_NEEDS_WRITABLE, | ||
| 469 | .config_props = config_input, | ||
| 470 | .filter_frame = filter_frame, | ||
| 471 | }, | ||
| 472 | }; | ||
| 473 | |||
| 474 | const FFFilter ff_vf_drawbox = { | ||
| 475 | .p.name = "drawbox", | ||
| 476 | .p.description = NULL_IF_CONFIG_SMALL("Draw a colored box on the input video."), | ||
| 477 | .p.priv_class = &drawbox_class, | ||
| 478 | .p.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, | ||
| 479 | .priv_size = sizeof(DrawBoxContext), | ||
| 480 | .init = init, | ||
| 481 | FILTER_INPUTS(drawbox_inputs), | ||
| 482 | FILTER_OUTPUTS(ff_video_default_filterpad), | ||
| 483 | FILTER_PIXFMTS_ARRAY(pix_fmts), | ||
| 484 | .process_command = process_command, | ||
| 485 | }; | ||
| 486 | #endif /* CONFIG_DRAWBOX_FILTER */ | ||
| 487 | |||
| 488 | #if CONFIG_DRAWGRID_FILTER | ||
| 489 | ✗ | static av_pure av_always_inline int pixel_belongs_to_grid(DrawBoxContext *drawgrid, int x, int y) | |
| 490 | { | ||
| 491 | // x is horizontal (width) coord, | ||
| 492 | // y is vertical (height) coord | ||
| 493 | int x_modulo; | ||
| 494 | int y_modulo; | ||
| 495 | |||
| 496 | // Abstract from the offset | ||
| 497 | ✗ | x -= drawgrid->x; | |
| 498 | ✗ | y -= drawgrid->y; | |
| 499 | |||
| 500 | ✗ | x_modulo = x % drawgrid->w; | |
| 501 | ✗ | y_modulo = y % drawgrid->h; | |
| 502 | |||
| 503 | // If x or y got negative, fix values to preserve logics | ||
| 504 | ✗ | if (x_modulo < 0) | |
| 505 | ✗ | x_modulo += drawgrid->w; | |
| 506 | ✗ | if (y_modulo < 0) | |
| 507 | ✗ | y_modulo += drawgrid->h; | |
| 508 | |||
| 509 | ✗ | return x_modulo < drawgrid->thickness // Belongs to vertical line | |
| 510 | ✗ | || y_modulo < drawgrid->thickness; // Belongs to horizontal line | |
| 511 | } | ||
| 512 | |||
| 513 | ✗ | static int drawgrid_filter_frame(AVFilterLink *inlink, AVFrame *frame) | |
| 514 | { | ||
| 515 | ✗ | DrawBoxContext *drawgrid = inlink->dst->priv; | |
| 516 | |||
| 517 | ✗ | drawgrid->draw_region(frame, drawgrid, 0, 0, frame->width, frame->height, pixel_belongs_to_grid); | |
| 518 | |||
| 519 | ✗ | return ff_filter_frame(inlink->dst->outputs[0], frame); | |
| 520 | } | ||
| 521 | |||
| 522 | static const AVOption drawgrid_options[] = { | ||
| 523 | { "x", "set horizontal offset", OFFSET(x_expr), AV_OPT_TYPE_STRING, { .str="0" }, 0, 0, FLAGS }, | ||
| 524 | { "y", "set vertical offset", OFFSET(y_expr), AV_OPT_TYPE_STRING, { .str="0" }, 0, 0, FLAGS }, | ||
| 525 | { "width", "set width of grid cell", OFFSET(w_expr), AV_OPT_TYPE_STRING, { .str="0" }, 0, 0, FLAGS }, | ||
| 526 | { "w", "set width of grid cell", OFFSET(w_expr), AV_OPT_TYPE_STRING, { .str="0" }, 0, 0, FLAGS }, | ||
| 527 | { "height", "set height of grid cell", OFFSET(h_expr), AV_OPT_TYPE_STRING, { .str="0" }, 0, 0, FLAGS }, | ||
| 528 | { "h", "set height of grid cell", OFFSET(h_expr), AV_OPT_TYPE_STRING, { .str="0" }, 0, 0, FLAGS }, | ||
| 529 | { "color", "set color of the grid", OFFSET(color_str), AV_OPT_TYPE_STRING, { .str = "black" }, 0, 0, FLAGS }, | ||
| 530 | { "c", "set color of the grid", OFFSET(color_str), AV_OPT_TYPE_STRING, { .str = "black" }, 0, 0, FLAGS }, | ||
| 531 | { "thickness", "set grid line thickness", OFFSET(t_expr), AV_OPT_TYPE_STRING, {.str="1"}, 0, 0, FLAGS }, | ||
| 532 | { "t", "set grid line thickness", OFFSET(t_expr), AV_OPT_TYPE_STRING, {.str="1"}, 0, 0, FLAGS }, | ||
| 533 | { "replace", "replace color & alpha", OFFSET(replace), AV_OPT_TYPE_BOOL, { .i64=0 }, 0, 1, FLAGS }, | ||
| 534 | { NULL } | ||
| 535 | }; | ||
| 536 | |||
| 537 | AVFILTER_DEFINE_CLASS(drawgrid); | ||
| 538 | |||
| 539 | static const AVFilterPad drawgrid_inputs[] = { | ||
| 540 | { | ||
| 541 | .name = "default", | ||
| 542 | .type = AVMEDIA_TYPE_VIDEO, | ||
| 543 | .flags = AVFILTERPAD_FLAG_NEEDS_WRITABLE, | ||
| 544 | .config_props = config_input, | ||
| 545 | .filter_frame = drawgrid_filter_frame, | ||
| 546 | }, | ||
| 547 | }; | ||
| 548 | |||
| 549 | const FFFilter ff_vf_drawgrid = { | ||
| 550 | .p.name = "drawgrid", | ||
| 551 | .p.description = NULL_IF_CONFIG_SMALL("Draw a colored grid on the input video."), | ||
| 552 | .p.priv_class = &drawgrid_class, | ||
| 553 | .p.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, | ||
| 554 | .priv_size = sizeof(DrawBoxContext), | ||
| 555 | .init = init, | ||
| 556 | FILTER_INPUTS(drawgrid_inputs), | ||
| 557 | FILTER_OUTPUTS(ff_video_default_filterpad), | ||
| 558 | FILTER_PIXFMTS_ARRAY(pix_fmts), | ||
| 559 | .process_command = process_command, | ||
| 560 | }; | ||
| 561 | |||
| 562 | #endif /* CONFIG_DRAWGRID_FILTER */ | ||
| 563 |