FFmpeg
Loading...
Searching...
No Matches
dnn_filter_common.c
Go to the documentation of this file.
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 "config.h"
20#include "dnn_filter_common.h"
21#include "libavutil/avstring.h"
22#include "libavutil/mem.h"
23#include "libavutil/opt.h"
24#include "libavutil/hwcontext.h"
25
26#define MAX_SUPPORTED_OUTPUTS_NB 4
27
28static char **separate_output_names(const char *expr, const char *val_sep, int *separated_nb)
29{
30 char *val, **parsed_vals = NULL;
31 int val_num = 0;
32 if (!expr || !val_sep || !separated_nb) {
33 return NULL;
34 }
35
36 parsed_vals = av_calloc(MAX_SUPPORTED_OUTPUTS_NB + 1, sizeof(*parsed_vals));
37 if (!parsed_vals) {
38 return NULL;
39 }
40
41 do {
42 if (val_num >= MAX_SUPPORTED_OUTPUTS_NB) {
43 goto err;
44 }
45 val = av_get_token(&expr, val_sep);
46 if(val) {
47 parsed_vals[val_num] = val;
48 val_num++;
49 }
50 if (*expr) {
51 expr++;
52 }
53 } while(*expr);
54
55 parsed_vals[val_num] = NULL;
56 *separated_nb = val_num;
57
58 return parsed_vals;
59
60err:
61 for (int i = 0; i < val_num; i++)
62 av_free(parsed_vals[i]);
63 av_freep(&parsed_vals);
64 return NULL;
65}
66
67typedef struct DnnFilterBase {
68 const AVClass *class;
71
77
78void *ff_dnn_filter_child_next(void *obj, void *prev)
79{
80 DnnFilterBase *base = obj;
81 return ff_dnn_child_next(&base->dnnctx, prev);
82}
83
85{
86 DNNBackendType backend = ctx->backend_type;
87
88 if (!ctx->model_filename) {
89 av_log(filter_ctx, AV_LOG_ERROR, "model file for network is not specified\n");
90 return AVERROR(EINVAL);
91 }
92
93 if (backend == DNN_TH) {
94 if (ctx->model_inputname)
95 av_log(filter_ctx, AV_LOG_WARNING, "LibTorch backend do not require inputname, "\
96 "inputname will be ignored.\n");
97 if (ctx->model_outputnames)
98 av_log(filter_ctx, AV_LOG_WARNING, "LibTorch backend do not require outputname(s), "\
99 "all outputname(s) will be ignored.\n");
100 ctx->nb_outputs = 1;
101 } else if (backend == DNN_TF) {
102 if (!ctx->model_inputname) {
103 av_log(filter_ctx, AV_LOG_ERROR, "input name of the model network is not specified\n");
104 return AVERROR(EINVAL);
105 }
106 ctx->model_outputnames = separate_output_names(ctx->model_outputnames_string, "&", &ctx->nb_outputs);
107 if (!ctx->model_outputnames) {
108 av_log(filter_ctx, AV_LOG_ERROR, "could not parse model output names\n");
109 return AVERROR(EINVAL);
110 }
111 } else if (backend == DNN_ONNX) {
112 /* ONNX: input and output tensor names are optional.*/
113 if (ctx->model_outputnames_string) {
114 ctx->model_outputnames = separate_output_names(ctx->model_outputnames_string, "&", &ctx->nb_outputs);
115 if (!ctx->model_outputnames) {
116 av_log(filter_ctx, AV_LOG_ERROR, "could not parse model output names\n");
117 return AVERROR(EINVAL);
118 }
119 if (ctx->nb_outputs != 1) {
121 "ONNX backend supports a single output name only\n");
122 return AVERROR(EINVAL);
123 }
124 }
125 }
126
127 ctx->dnn_module = ff_get_dnn_module(ctx->backend_type, filter_ctx);
128 if (!ctx->dnn_module) {
129 av_log(filter_ctx, AV_LOG_ERROR, "could not create DNN module for requested backend\n");
130 return AVERROR(ENOMEM);
131 }
132 if (!ctx->dnn_module->load_model) {
133 av_log(filter_ctx, AV_LOG_ERROR, "load_model for network is not specified\n");
134 return AVERROR(EINVAL);
135 }
136
137 if (ctx->backend_options) {
138 void *child = NULL;
139
141 "backend_configs is deprecated, please set backend options directly\n");
142 while (child = ff_dnn_child_next(ctx, child)) {
143 if (*(const AVClass **)child == &ctx->dnn_module->clazz) {
144 int ret = av_opt_set_from_string(child, ctx->backend_options,
145 NULL, "=", "&");
146 if (ret < 0) {
147 av_log(filter_ctx, AV_LOG_ERROR, "failed to parse options \"%s\"\n",
148 ctx->backend_options);
149 return ret;
150 }
151 }
152 }
153 }
154
155 ctx->model = (ctx->dnn_module->load_model)(ctx, func_type, filter_ctx);
156 if (!ctx->model) {
157 av_log(filter_ctx, AV_LOG_ERROR, "could not load DNN model\n");
158 return AVERROR(EINVAL);
159 }
160
161 return 0;
162}
163
165{
166 ctx->model->frame_pre_proc = pre_proc;
167 ctx->model->frame_post_proc = post_proc;
168 return 0;
169}
170
172{
173 ctx->model->detect_post_proc = post_proc;
174 return 0;
175}
176
178{
179 ctx->model->classify_post_proc = post_proc;
180 return 0;
181}
182
184{
185 return ctx->model->get_input(ctx->model, input, ctx->model_inputname);
186}
187
188int ff_dnn_get_output(DnnContext *ctx, int input_width, int input_height, int *output_width, int *output_height)
189{
190 char * output_name = ctx->model_outputnames && ctx->backend_type != DNN_TH ?
191 ctx->model_outputnames[0] : NULL;
192 return ctx->model->get_output(ctx->model, ctx->model_inputname, input_width, input_height,
193 (const char *)output_name, output_width, output_height);
194}
195
197{
198 DNNExecBaseParams exec_params = {
199 .input_name = ctx->model_inputname,
200 .output_names = (const char **)ctx->model_outputnames,
201 .nb_output = ctx->nb_outputs,
202 .in_frame = in_frame,
203 .out_frame = out_frame,
204 };
205 return (ctx->dnn_module->execute_model)(ctx->model, &exec_params);
206}
207
208int ff_dnn_execute_model_classification(DnnContext *ctx, AVFrame *in_frame, AVFrame *out_frame, const char *target)
209{
210 DNNExecClassificationParams class_params = {
211 {
212 .input_name = ctx->model_inputname,
213 .output_names = (const char **)ctx->model_outputnames,
214 .nb_output = ctx->nb_outputs,
215 .in_frame = in_frame,
216 .out_frame = out_frame,
217 },
218 .target = target,
219 };
220 return (ctx->dnn_module->execute_model)(ctx->model, &class_params.base);
221}
222
224{
225 return (ctx->dnn_module->get_result)(ctx->model, in_frame, out_frame);
226}
227
229{
230 return (ctx->dnn_module->flush)(ctx->model);
231}
232
234{
235 if (ctx->dnn_module) {
236 (ctx->dnn_module->free_model)(&ctx->model);
237 }
238 if (ctx->model_outputnames) {
239 for (int i = 0; i < ctx->nb_outputs; i++)
240 av_free(ctx->model_outputnames[i]);
241
242 av_freep(&ctx->model_outputnames);
243 }
244}
245
246#if CONFIG_CUDA
247int ff_dnn_zero_copy_supported_cuda(DnnContext *ctx, const AVFilterLink *inlink)
248{
249 AVBufferRef *hw_frames_ref = avfilter_link_get_hw_frames_ctx((AVFilterLink *)inlink);
250 AVHWFramesContext *hw_frames_ctx;
251
252 if (!hw_frames_ref)
253 return 0;
254
255 hw_frames_ctx = (AVHWFramesContext *)hw_frames_ref->data;
256
257 if (inlink->format == AV_PIX_FMT_CUDA) {
258 if (ctx->batch_size > 1) {
259 av_log(inlink->dst, AV_LOG_ERROR, "CUDA zero-copy currently does not support batching.\n");
260 av_buffer_unref(&hw_frames_ref);
261 return AVERROR(EINVAL);
262 }
263
264 if (ctx->backend_type == DNN_TH) {
265 switch (hw_frames_ctx->sw_format) {
266 case AV_PIX_FMT_RGB24:
267 case AV_PIX_FMT_BGR24:
268 case AV_PIX_FMT_RGB0:
269 case AV_PIX_FMT_0RGB:
270 case AV_PIX_FMT_BGR0:
271 case AV_PIX_FMT_0BGR:
272 break;
273 default:
274 av_log(inlink->dst, AV_LOG_ERROR,
275 "Zero-copy CUDA path currently only supports RGB24/BGR24 or RGB0/BGR0 variants.\n");
276 av_buffer_unref(&hw_frames_ref);
277 return AVERROR(EINVAL);
278 }
279 }
280 }
281
282 av_buffer_unref(&hw_frames_ref);
283 return 0;
284}
285#endif
static double val(void *priv, double ch)
Definition aeval.c:77
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define NULL
Definition coverity.c:32
static char ** separate_output_names(const char *expr, const char *val_sep, int *separated_nb)
void * ff_dnn_filter_child_next(void *obj, void *prev)
int ff_dnn_set_detect_post_proc(DnnContext *ctx, DetectPostProc post_proc)
void ff_dnn_uninit(DnnContext *ctx)
DNNAsyncStatusType ff_dnn_get_result(DnnContext *ctx, AVFrame **in_frame, AVFrame **out_frame)
int ff_dnn_execute_model(DnnContext *ctx, AVFrame *in_frame, AVFrame *out_frame)
int ff_dnn_set_classify_post_proc(DnnContext *ctx, ClassifyPostProc post_proc)
int ff_dnn_init(DnnContext *ctx, DNNFunctionType func_type, AVFilterContext *filter_ctx)
int ff_dnn_get_input(DnnContext *ctx, DNNData *input)
#define MAX_SUPPORTED_OUTPUTS_NB
int ff_dnn_get_output(DnnContext *ctx, int input_width, int input_height, int *output_width, int *output_height)
int ff_dnn_flush(DnnContext *ctx)
int ff_dnn_set_frame_proc(DnnContext *ctx, FramePrePostProc pre_proc, FramePrePostProc post_proc)
int ff_dnn_filter_init_child_class(AVFilterContext *filter)
int ff_dnn_execute_model_classification(DnnContext *ctx, AVFrame *in_frame, AVFrame *out_frame, const char *target)
common functions for the dnn based filters
void * ff_dnn_child_next(DnnContext *obj, void *prev)
const DNNModule * ff_get_dnn_module(DNNBackendType backend_type, void *log_ctx)
void ff_dnn_init_child_class(DnnContext *ctx)
int(* FramePrePostProc)(AVFrame *frame, DNNData *model, AVFilterContext *filter_ctx)
DNNAsyncStatusType
int(* DetectPostProc)(AVFrame *frame, DNNData *output, uint32_t nb, AVFilterContext *filter_ctx)
DNNBackendType
@ DNN_ONNX
@ DNN_TH
@ DNN_TF
DNNFunctionType
int(* ClassifyPostProc)(AVFrame *frame, DNNData *output, uint32_t bbox_index, AVFilterContext *filter_ctx)
AVBufferRef * avfilter_link_get_hw_frames_ctx(AVFilterLink *link)
Get the hardware frames context of a filter link.
Definition avfilter.c:998
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
Definition buffer.c:139
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
char * av_get_token(const char **buf, const char *term)
Unescape the given string until a non escaped terminating char, and return the token corresponding to...
Definition avstring.c:143
int av_opt_set_from_string(void *ctx, const char *opts, const char *const *shorthand, const char *key_val_sep, const char *pairs_sep)
Parse the key-value pairs list in opts.
Definition opt.c:1973
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
AVOptions.
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition pixfmt.h:75
@ AV_PIX_FMT_BGR0
packed BGR 8:8:8, 32bpp, BGRXBGRX... X=unused/undefined
Definition pixfmt.h:265
@ AV_PIX_FMT_CUDA
HW acceleration through CUDA.
Definition pixfmt.h:260
@ AV_PIX_FMT_0BGR
packed BGR 8:8:8, 32bpp, XBGRXBGR... X=unused/undefined
Definition pixfmt.h:264
@ AV_PIX_FMT_RGB0
packed RGB 8:8:8, 32bpp, RGBXRGBX... X=unused/undefined
Definition pixfmt.h:263
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition pixfmt.h:76
@ AV_PIX_FMT_0RGB
packed RGB 8:8:8, 32bpp, XRGBXRGB... X=unused/undefined
Definition pixfmt.h:262
A reference to a data buffer.
Definition buffer.h:82
uint8_t * data
The data buffer.
Definition buffer.h:90
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This struct describes a set or pool of "hardware" frames (i.e.
Definition hwcontext.h:118
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition hwcontext.h:213
#define av_free(p)
#define av_freep(p)
#define av_log(a,...)
void(* filter)(uint8_t *src, ptrdiff_t stride, int qscale)
Definition h263dsp.c:29
static AVFormatContext * ctx
Definition movenc.c:49
static FilteringContext * filter_ctx
Definition transcode.c:52
uint8_t base
Definition vp3data.h:128