Line |
Branch |
Exec |
Source |
1 |
|
|
/* |
2 |
|
|
* Copyright (c) 2012 Stefano Sabatini |
3 |
|
|
* |
4 |
|
|
* This file is part of FFmpeg. |
5 |
|
|
* |
6 |
|
|
* FFmpeg is free software; you can redistribute it and/or |
7 |
|
|
* modify it under the terms of the GNU Lesser General Public |
8 |
|
|
* License as published by the Free Software Foundation; either |
9 |
|
|
* version 2.1 of the License, or (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* FFmpeg is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
|
|
* Lesser General Public License for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU Lesser General Public |
17 |
|
|
* License along with FFmpeg; if not, write to the Free Software |
18 |
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
19 |
|
|
*/ |
20 |
|
|
|
21 |
|
|
/** |
22 |
|
|
* @file |
23 |
|
|
* bounding box detection filter |
24 |
|
|
*/ |
25 |
|
|
|
26 |
|
|
#include "libavutil/opt.h" |
27 |
|
|
#include "libavutil/pixdesc.h" |
28 |
|
|
#include "libavutil/timestamp.h" |
29 |
|
|
#include "avfilter.h" |
30 |
|
|
#include "bbox.h" |
31 |
|
|
#include "filters.h" |
32 |
|
|
|
33 |
|
|
typedef struct BBoxContext { |
34 |
|
|
const AVClass *class; |
35 |
|
|
int min_val; |
36 |
|
|
int depth; |
37 |
|
|
} BBoxContext; |
38 |
|
|
|
39 |
|
|
#define OFFSET(x) offsetof(BBoxContext, x) |
40 |
|
|
#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM |
41 |
|
|
|
42 |
|
|
static const AVOption bbox_options[] = { |
43 |
|
|
{ "min_val", "set minimum luminance value for bounding box", OFFSET(min_val), AV_OPT_TYPE_INT, { .i64 = 16 }, 0, UINT16_MAX, FLAGS }, |
44 |
|
|
{ NULL } |
45 |
|
|
}; |
46 |
|
|
|
47 |
|
|
AVFILTER_DEFINE_CLASS(bbox); |
48 |
|
|
|
49 |
|
|
static const enum AVPixelFormat pix_fmts[] = { |
50 |
|
|
AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, |
51 |
|
|
AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, |
52 |
|
|
AV_PIX_FMT_GRAY16, |
53 |
|
|
AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P, |
54 |
|
|
AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, |
55 |
|
|
AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P, |
56 |
|
|
AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, |
57 |
|
|
AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P, |
58 |
|
|
AV_PIX_FMT_YUVJ411P, |
59 |
|
|
AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9, |
60 |
|
|
AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10, |
61 |
|
|
AV_PIX_FMT_YUV440P10, |
62 |
|
|
AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12, |
63 |
|
|
AV_PIX_FMT_YUV440P12, |
64 |
|
|
AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14, |
65 |
|
|
AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16, |
66 |
|
|
AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P, |
67 |
|
|
AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA444P16, |
68 |
|
|
AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA422P16, |
69 |
|
|
AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16, |
70 |
|
|
AV_PIX_FMT_NONE, |
71 |
|
|
}; |
72 |
|
|
|
73 |
|
|
#define SET_META(key, value) \ |
74 |
|
|
av_dict_set_int(metadata, key, value, 0); |
75 |
|
|
|
76 |
|
✗ |
static int filter_frame(AVFilterLink *inlink, AVFrame *frame) |
77 |
|
|
{ |
78 |
|
✗ |
FilterLink *inl = ff_filter_link(inlink); |
79 |
|
✗ |
AVFilterContext *ctx = inlink->dst; |
80 |
|
✗ |
BBoxContext *bbox = ctx->priv; |
81 |
|
|
FFBoundingBox box; |
82 |
|
|
int has_bbox, w, h; |
83 |
|
|
|
84 |
|
|
has_bbox = |
85 |
|
✗ |
ff_calculate_bounding_box(&box, |
86 |
|
✗ |
frame->data[0], frame->linesize[0], |
87 |
|
|
inlink->w, inlink->h, bbox->min_val, bbox->depth); |
88 |
|
✗ |
w = box.x2 - box.x1 + 1; |
89 |
|
✗ |
h = box.y2 - box.y1 + 1; |
90 |
|
|
|
91 |
|
✗ |
av_log(ctx, AV_LOG_INFO, |
92 |
|
|
"n:%"PRId64" pts:%s pts_time:%s", inl->frame_count_out, |
93 |
|
✗ |
av_ts2str(frame->pts), av_ts2timestr(frame->pts, &inlink->time_base)); |
94 |
|
|
|
95 |
|
✗ |
if (has_bbox) { |
96 |
|
✗ |
AVDictionary **metadata = &frame->metadata; |
97 |
|
|
|
98 |
|
✗ |
SET_META("lavfi.bbox.x1", box.x1) |
99 |
|
✗ |
SET_META("lavfi.bbox.x2", box.x2) |
100 |
|
✗ |
SET_META("lavfi.bbox.y1", box.y1) |
101 |
|
✗ |
SET_META("lavfi.bbox.y2", box.y2) |
102 |
|
✗ |
SET_META("lavfi.bbox.w", w) |
103 |
|
✗ |
SET_META("lavfi.bbox.h", h) |
104 |
|
|
|
105 |
|
✗ |
av_log(ctx, AV_LOG_INFO, |
106 |
|
|
" x1:%d x2:%d y1:%d y2:%d w:%d h:%d" |
107 |
|
|
" crop=%d:%d:%d:%d drawbox=%d:%d:%d:%d", |
108 |
|
|
box.x1, box.x2, box.y1, box.y2, w, h, |
109 |
|
|
w, h, box.x1, box.y1, /* crop params */ |
110 |
|
|
box.x1, box.y1, w, h); /* drawbox params */ |
111 |
|
|
} |
112 |
|
✗ |
av_log(ctx, AV_LOG_INFO, "\n"); |
113 |
|
|
|
114 |
|
✗ |
return ff_filter_frame(inlink->dst->outputs[0], frame); |
115 |
|
|
} |
116 |
|
|
|
117 |
|
✗ |
static int config_output(AVFilterLink *outlink) |
118 |
|
|
{ |
119 |
|
✗ |
AVFilterContext *ctx = outlink->src; |
120 |
|
✗ |
BBoxContext *s = ctx->priv; |
121 |
|
|
const AVPixFmtDescriptor *desc; |
122 |
|
|
|
123 |
|
✗ |
desc = av_pix_fmt_desc_get(outlink->format); |
124 |
|
✗ |
if (!desc) |
125 |
|
✗ |
return AVERROR_BUG; |
126 |
|
✗ |
s->depth = desc->comp[0].depth; |
127 |
|
|
|
128 |
|
✗ |
return 0; |
129 |
|
|
} |
130 |
|
|
|
131 |
|
|
static const AVFilterPad bbox_inputs[] = { |
132 |
|
|
{ |
133 |
|
|
.name = "default", |
134 |
|
|
.type = AVMEDIA_TYPE_VIDEO, |
135 |
|
|
.filter_frame = filter_frame, |
136 |
|
|
}, |
137 |
|
|
}; |
138 |
|
|
|
139 |
|
|
static const AVFilterPad bbox_outputs[] = { |
140 |
|
|
{ |
141 |
|
|
.name = "default", |
142 |
|
|
.type = AVMEDIA_TYPE_VIDEO, |
143 |
|
|
.config_props = config_output, |
144 |
|
|
}, |
145 |
|
|
}; |
146 |
|
|
|
147 |
|
|
const AVFilter ff_vf_bbox = { |
148 |
|
|
.name = "bbox", |
149 |
|
|
.description = NULL_IF_CONFIG_SMALL("Compute bounding box for each frame."), |
150 |
|
|
.priv_size = sizeof(BBoxContext), |
151 |
|
|
.priv_class = &bbox_class, |
152 |
|
|
FILTER_INPUTS(bbox_inputs), |
153 |
|
|
FILTER_OUTPUTS(bbox_outputs), |
154 |
|
|
FILTER_PIXFMTS_ARRAY(pix_fmts), |
155 |
|
|
.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_METADATA_ONLY, |
156 |
|
|
.process_command = ff_filter_process_command, |
157 |
|
|
}; |
158 |
|
|
|