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 |
|
|
/** |
20 |
|
|
* @file |
21 |
|
|
* implementing an classification filter using deep learning networks. |
22 |
|
|
*/ |
23 |
|
|
|
24 |
|
|
#include "libavutil/file_open.h" |
25 |
|
|
#include "libavutil/mem.h" |
26 |
|
|
#include "libavutil/opt.h" |
27 |
|
|
#include "filters.h" |
28 |
|
|
#include "dnn_filter_common.h" |
29 |
|
|
#include "internal.h" |
30 |
|
|
#include "video.h" |
31 |
|
|
#include "libavutil/time.h" |
32 |
|
|
#include "libavutil/avstring.h" |
33 |
|
|
#include "libavutil/detection_bbox.h" |
34 |
|
|
|
35 |
|
|
typedef struct DnnClassifyContext { |
36 |
|
|
const AVClass *class; |
37 |
|
|
DnnContext dnnctx; |
38 |
|
|
float confidence; |
39 |
|
|
char *labels_filename; |
40 |
|
|
char *target; |
41 |
|
|
char **labels; |
42 |
|
|
int label_count; |
43 |
|
|
} DnnClassifyContext; |
44 |
|
|
|
45 |
|
|
#define OFFSET(x) offsetof(DnnClassifyContext, dnnctx.x) |
46 |
|
|
#define OFFSET2(x) offsetof(DnnClassifyContext, x) |
47 |
|
|
#define FLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM |
48 |
|
|
static const AVOption dnn_classify_options[] = { |
49 |
|
|
{ "dnn_backend", "DNN backend", OFFSET(backend_type), AV_OPT_TYPE_INT, { .i64 = DNN_OV }, INT_MIN, INT_MAX, FLAGS, .unit = "backend" }, |
50 |
|
|
#if (CONFIG_LIBOPENVINO == 1) |
51 |
|
|
{ "openvino", "openvino backend flag", 0, AV_OPT_TYPE_CONST, { .i64 = DNN_OV }, 0, 0, FLAGS, .unit = "backend" }, |
52 |
|
|
#endif |
53 |
|
|
DNN_COMMON_OPTIONS |
54 |
|
|
{ "confidence", "threshold of confidence", OFFSET2(confidence), AV_OPT_TYPE_FLOAT, { .dbl = 0.5 }, 0, 1, FLAGS}, |
55 |
|
|
{ "labels", "path to labels file", OFFSET2(labels_filename), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS }, |
56 |
|
|
{ "target", "which one to be classified", OFFSET2(target), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS }, |
57 |
|
|
{ NULL } |
58 |
|
|
}; |
59 |
|
|
|
60 |
|
|
AVFILTER_DEFINE_CLASS(dnn_classify); |
61 |
|
|
|
62 |
|
✗ |
static int dnn_classify_post_proc(AVFrame *frame, DNNData *output, uint32_t bbox_index, AVFilterContext *filter_ctx) |
63 |
|
|
{ |
64 |
|
✗ |
DnnClassifyContext *ctx = filter_ctx->priv; |
65 |
|
✗ |
float conf_threshold = ctx->confidence; |
66 |
|
|
AVDetectionBBoxHeader *header; |
67 |
|
|
AVDetectionBBox *bbox; |
68 |
|
|
float *classifications; |
69 |
|
|
uint32_t label_id; |
70 |
|
|
float confidence; |
71 |
|
|
AVFrameSideData *sd; |
72 |
|
✗ |
int output_size = output->dims[3] * output->dims[2] * output->dims[1]; |
73 |
|
✗ |
if (output_size <= 0) { |
74 |
|
✗ |
return -1; |
75 |
|
|
} |
76 |
|
|
|
77 |
|
✗ |
sd = av_frame_get_side_data(frame, AV_FRAME_DATA_DETECTION_BBOXES); |
78 |
|
✗ |
if (!sd) { |
79 |
|
✗ |
av_log(filter_ctx, AV_LOG_ERROR, "Cannot get side data in dnn_classify_post_proc\n"); |
80 |
|
✗ |
return -1; |
81 |
|
|
} |
82 |
|
✗ |
header = (AVDetectionBBoxHeader *)sd->data; |
83 |
|
|
|
84 |
|
✗ |
if (bbox_index == 0) { |
85 |
|
✗ |
av_strlcat(header->source, ", ", sizeof(header->source)); |
86 |
|
✗ |
av_strlcat(header->source, ctx->dnnctx.model_filename, sizeof(header->source)); |
87 |
|
|
} |
88 |
|
|
|
89 |
|
✗ |
classifications = output->data; |
90 |
|
✗ |
label_id = 0; |
91 |
|
✗ |
confidence= classifications[0]; |
92 |
|
✗ |
for (int i = 1; i < output_size; i++) { |
93 |
|
✗ |
if (classifications[i] > confidence) { |
94 |
|
✗ |
label_id = i; |
95 |
|
✗ |
confidence= classifications[i]; |
96 |
|
|
} |
97 |
|
|
} |
98 |
|
|
|
99 |
|
✗ |
if (confidence < conf_threshold) { |
100 |
|
✗ |
return 0; |
101 |
|
|
} |
102 |
|
|
|
103 |
|
✗ |
bbox = av_get_detection_bbox(header, bbox_index); |
104 |
|
✗ |
bbox->classify_confidences[bbox->classify_count] = av_make_q((int)(confidence * 10000), 10000); |
105 |
|
|
|
106 |
|
✗ |
if (ctx->labels && label_id < ctx->label_count) { |
107 |
|
✗ |
av_strlcpy(bbox->classify_labels[bbox->classify_count], ctx->labels[label_id], sizeof(bbox->classify_labels[bbox->classify_count])); |
108 |
|
|
} else { |
109 |
|
✗ |
snprintf(bbox->classify_labels[bbox->classify_count], sizeof(bbox->classify_labels[bbox->classify_count]), "%d", label_id); |
110 |
|
|
} |
111 |
|
|
|
112 |
|
✗ |
bbox->classify_count++; |
113 |
|
|
|
114 |
|
✗ |
return 0; |
115 |
|
|
} |
116 |
|
|
|
117 |
|
✗ |
static void free_classify_labels(DnnClassifyContext *ctx) |
118 |
|
|
{ |
119 |
|
✗ |
for (int i = 0; i < ctx->label_count; i++) { |
120 |
|
✗ |
av_freep(&ctx->labels[i]); |
121 |
|
|
} |
122 |
|
✗ |
ctx->label_count = 0; |
123 |
|
✗ |
av_freep(&ctx->labels); |
124 |
|
✗ |
} |
125 |
|
|
|
126 |
|
✗ |
static int read_classify_label_file(AVFilterContext *context) |
127 |
|
|
{ |
128 |
|
|
int line_len; |
129 |
|
|
FILE *file; |
130 |
|
✗ |
DnnClassifyContext *ctx = context->priv; |
131 |
|
|
|
132 |
|
✗ |
file = avpriv_fopen_utf8(ctx->labels_filename, "r"); |
133 |
|
✗ |
if (!file){ |
134 |
|
✗ |
av_log(context, AV_LOG_ERROR, "failed to open file %s\n", ctx->labels_filename); |
135 |
|
✗ |
return AVERROR(EINVAL); |
136 |
|
|
} |
137 |
|
|
|
138 |
|
✗ |
while (!feof(file)) { |
139 |
|
|
char *label; |
140 |
|
|
char buf[256]; |
141 |
|
✗ |
if (!fgets(buf, 256, file)) { |
142 |
|
✗ |
break; |
143 |
|
|
} |
144 |
|
|
|
145 |
|
✗ |
line_len = strlen(buf); |
146 |
|
✗ |
while (line_len) { |
147 |
|
✗ |
int i = line_len - 1; |
148 |
|
✗ |
if (buf[i] == '\n' || buf[i] == '\r' || buf[i] == ' ') { |
149 |
|
✗ |
buf[i] = '\0'; |
150 |
|
✗ |
line_len--; |
151 |
|
|
} else { |
152 |
|
|
break; |
153 |
|
|
} |
154 |
|
|
} |
155 |
|
|
|
156 |
|
✗ |
if (line_len == 0) // empty line |
157 |
|
✗ |
continue; |
158 |
|
|
|
159 |
|
✗ |
if (line_len >= AV_DETECTION_BBOX_LABEL_NAME_MAX_SIZE) { |
160 |
|
✗ |
av_log(context, AV_LOG_ERROR, "label %s too long\n", buf); |
161 |
|
✗ |
fclose(file); |
162 |
|
✗ |
return AVERROR(EINVAL); |
163 |
|
|
} |
164 |
|
|
|
165 |
|
✗ |
label = av_strdup(buf); |
166 |
|
✗ |
if (!label) { |
167 |
|
✗ |
av_log(context, AV_LOG_ERROR, "failed to allocate memory for label %s\n", buf); |
168 |
|
✗ |
fclose(file); |
169 |
|
✗ |
return AVERROR(ENOMEM); |
170 |
|
|
} |
171 |
|
|
|
172 |
|
✗ |
if (av_dynarray_add_nofree(&ctx->labels, &ctx->label_count, label) < 0) { |
173 |
|
✗ |
av_log(context, AV_LOG_ERROR, "failed to do av_dynarray_add\n"); |
174 |
|
✗ |
fclose(file); |
175 |
|
✗ |
av_freep(&label); |
176 |
|
✗ |
return AVERROR(ENOMEM); |
177 |
|
|
} |
178 |
|
|
} |
179 |
|
|
|
180 |
|
✗ |
fclose(file); |
181 |
|
✗ |
return 0; |
182 |
|
|
} |
183 |
|
|
|
184 |
|
✗ |
static av_cold int dnn_classify_init(AVFilterContext *context) |
185 |
|
|
{ |
186 |
|
✗ |
DnnClassifyContext *ctx = context->priv; |
187 |
|
✗ |
int ret = ff_dnn_init(&ctx->dnnctx, DFT_ANALYTICS_CLASSIFY, context); |
188 |
|
✗ |
if (ret < 0) |
189 |
|
✗ |
return ret; |
190 |
|
✗ |
ff_dnn_set_classify_post_proc(&ctx->dnnctx, dnn_classify_post_proc); |
191 |
|
|
|
192 |
|
✗ |
if (ctx->labels_filename) { |
193 |
|
✗ |
return read_classify_label_file(context); |
194 |
|
|
} |
195 |
|
✗ |
return 0; |
196 |
|
|
} |
197 |
|
|
|
198 |
|
|
static const enum AVPixelFormat pix_fmts[] = { |
199 |
|
|
AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24, |
200 |
|
|
AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAYF32, |
201 |
|
|
AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, |
202 |
|
|
AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P, |
203 |
|
|
AV_PIX_FMT_NV12, |
204 |
|
|
AV_PIX_FMT_NONE |
205 |
|
|
}; |
206 |
|
|
|
207 |
|
✗ |
static int dnn_classify_flush_frame(AVFilterLink *outlink, int64_t pts, int64_t *out_pts) |
208 |
|
|
{ |
209 |
|
✗ |
DnnClassifyContext *ctx = outlink->src->priv; |
210 |
|
|
int ret; |
211 |
|
|
DNNAsyncStatusType async_state; |
212 |
|
|
|
213 |
|
✗ |
ret = ff_dnn_flush(&ctx->dnnctx); |
214 |
|
✗ |
if (ret != 0) { |
215 |
|
✗ |
return -1; |
216 |
|
|
} |
217 |
|
|
|
218 |
|
|
do { |
219 |
|
✗ |
AVFrame *in_frame = NULL; |
220 |
|
✗ |
AVFrame *out_frame = NULL; |
221 |
|
✗ |
async_state = ff_dnn_get_result(&ctx->dnnctx, &in_frame, &out_frame); |
222 |
|
✗ |
if (async_state == DAST_SUCCESS) { |
223 |
|
✗ |
ret = ff_filter_frame(outlink, in_frame); |
224 |
|
✗ |
if (ret < 0) |
225 |
|
✗ |
return ret; |
226 |
|
✗ |
if (out_pts) |
227 |
|
✗ |
*out_pts = in_frame->pts + pts; |
228 |
|
|
} |
229 |
|
✗ |
av_usleep(5000); |
230 |
|
✗ |
} while (async_state >= DAST_NOT_READY); |
231 |
|
|
|
232 |
|
✗ |
return 0; |
233 |
|
|
} |
234 |
|
|
|
235 |
|
✗ |
static int dnn_classify_activate(AVFilterContext *filter_ctx) |
236 |
|
|
{ |
237 |
|
✗ |
AVFilterLink *inlink = filter_ctx->inputs[0]; |
238 |
|
✗ |
AVFilterLink *outlink = filter_ctx->outputs[0]; |
239 |
|
✗ |
DnnClassifyContext *ctx = filter_ctx->priv; |
240 |
|
✗ |
AVFrame *in = NULL; |
241 |
|
|
int64_t pts; |
242 |
|
|
int ret, status; |
243 |
|
✗ |
int got_frame = 0; |
244 |
|
|
int async_state; |
245 |
|
|
|
246 |
|
✗ |
FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink); |
247 |
|
|
|
248 |
|
|
do { |
249 |
|
|
// drain all input frames |
250 |
|
✗ |
ret = ff_inlink_consume_frame(inlink, &in); |
251 |
|
✗ |
if (ret < 0) |
252 |
|
✗ |
return ret; |
253 |
|
✗ |
if (ret > 0) { |
254 |
|
✗ |
if (ff_dnn_execute_model_classification(&ctx->dnnctx, in, NULL, ctx->target) != 0) { |
255 |
|
✗ |
return AVERROR(EIO); |
256 |
|
|
} |
257 |
|
|
} |
258 |
|
✗ |
} while (ret > 0); |
259 |
|
|
|
260 |
|
|
// drain all processed frames |
261 |
|
|
do { |
262 |
|
✗ |
AVFrame *in_frame = NULL; |
263 |
|
✗ |
AVFrame *out_frame = NULL; |
264 |
|
✗ |
async_state = ff_dnn_get_result(&ctx->dnnctx, &in_frame, &out_frame); |
265 |
|
✗ |
if (async_state == DAST_SUCCESS) { |
266 |
|
✗ |
ret = ff_filter_frame(outlink, in_frame); |
267 |
|
✗ |
if (ret < 0) |
268 |
|
✗ |
return ret; |
269 |
|
✗ |
got_frame = 1; |
270 |
|
|
} |
271 |
|
✗ |
} while (async_state == DAST_SUCCESS); |
272 |
|
|
|
273 |
|
|
// if frame got, schedule to next filter |
274 |
|
✗ |
if (got_frame) |
275 |
|
✗ |
return 0; |
276 |
|
|
|
277 |
|
✗ |
if (ff_inlink_acknowledge_status(inlink, &status, &pts)) { |
278 |
|
✗ |
if (status == AVERROR_EOF) { |
279 |
|
✗ |
int64_t out_pts = pts; |
280 |
|
✗ |
ret = dnn_classify_flush_frame(outlink, pts, &out_pts); |
281 |
|
✗ |
ff_outlink_set_status(outlink, status, out_pts); |
282 |
|
✗ |
return ret; |
283 |
|
|
} |
284 |
|
|
} |
285 |
|
|
|
286 |
|
✗ |
FF_FILTER_FORWARD_WANTED(outlink, inlink); |
287 |
|
|
|
288 |
|
✗ |
return 0; |
289 |
|
|
} |
290 |
|
|
|
291 |
|
✗ |
static av_cold void dnn_classify_uninit(AVFilterContext *context) |
292 |
|
|
{ |
293 |
|
✗ |
DnnClassifyContext *ctx = context->priv; |
294 |
|
✗ |
ff_dnn_uninit(&ctx->dnnctx); |
295 |
|
✗ |
free_classify_labels(ctx); |
296 |
|
✗ |
} |
297 |
|
|
|
298 |
|
|
const AVFilter ff_vf_dnn_classify = { |
299 |
|
|
.name = "dnn_classify", |
300 |
|
|
.description = NULL_IF_CONFIG_SMALL("Apply DNN classify filter to the input."), |
301 |
|
|
.priv_size = sizeof(DnnClassifyContext), |
302 |
|
|
.init = dnn_classify_init, |
303 |
|
|
.uninit = dnn_classify_uninit, |
304 |
|
|
FILTER_INPUTS(ff_video_default_filterpad), |
305 |
|
|
FILTER_OUTPUTS(ff_video_default_filterpad), |
306 |
|
|
FILTER_PIXFMTS_ARRAY(pix_fmts), |
307 |
|
|
.priv_class = &dnn_classify_class, |
308 |
|
|
.activate = dnn_classify_activate, |
309 |
|
|
}; |
310 |
|
|
|