FFmpeg
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 "dnn_filter_common.h"
20 #include "libavutil/avstring.h"
21 #include "libavutil/mem.h"
22 #include "libavutil/opt.h"
23 
24 #define MAX_SUPPORTED_OUTPUTS_NB 4
25 
26 static char **separate_output_names(const char *expr, const char *val_sep, int *separated_nb)
27 {
28  char *val, **parsed_vals = NULL;
29  int val_num = 0;
30  if (!expr || !val_sep || !separated_nb) {
31  return NULL;
32  }
33 
34  parsed_vals = av_calloc(MAX_SUPPORTED_OUTPUTS_NB + 1, sizeof(*parsed_vals));
35  if (!parsed_vals) {
36  return NULL;
37  }
38 
39  do {
40  if (val_num >= MAX_SUPPORTED_OUTPUTS_NB) {
41  goto err;
42  }
43  val = av_get_token(&expr, val_sep);
44  if(val) {
45  parsed_vals[val_num] = val;
46  val_num++;
47  }
48  if (*expr) {
49  expr++;
50  }
51  } while(*expr);
52 
53  parsed_vals[val_num] = NULL;
54  *separated_nb = val_num;
55 
56  return parsed_vals;
57 
58 err:
59  for (int i = 0; i < val_num; i++)
60  av_free(parsed_vals[i]);
61  av_freep(&parsed_vals);
62  return NULL;
63 }
64 
65 typedef struct DnnFilterBase {
66  const AVClass *class;
69 
71  DnnFilterBase *base = filter->priv;
72  ff_dnn_init_child_class(&base->dnnctx);
73  return 0;
74 }
75 
76 void *ff_dnn_filter_child_next(void *obj, void *prev)
77 {
78  DnnFilterBase *base = obj;
79  return ff_dnn_child_next(&base->dnnctx, prev);
80 }
81 
83 {
84  DNNBackendType backend = ctx->backend_type;
85 
86  if (!ctx->model_filename) {
87  av_log(filter_ctx, AV_LOG_ERROR, "model file for network is not specified\n");
88  return AVERROR(EINVAL);
89  }
90 
91  if (backend == DNN_TH) {
92  if (ctx->model_inputname)
93  av_log(filter_ctx, AV_LOG_WARNING, "LibTorch backend do not require inputname, "\
94  "inputname will be ignored.\n");
95  if (ctx->model_outputnames)
96  av_log(filter_ctx, AV_LOG_WARNING, "LibTorch backend do not require outputname(s), "\
97  "all outputname(s) will be ignored.\n");
98  ctx->nb_outputs = 1;
99  } else if (backend == DNN_TF) {
100  if (!ctx->model_inputname) {
101  av_log(filter_ctx, AV_LOG_ERROR, "input name of the model network is not specified\n");
102  return AVERROR(EINVAL);
103  }
104  ctx->model_outputnames = separate_output_names(ctx->model_outputnames_string, "&", &ctx->nb_outputs);
105  if (!ctx->model_outputnames) {
106  av_log(filter_ctx, AV_LOG_ERROR, "could not parse model output names\n");
107  return AVERROR(EINVAL);
108  }
109  } else if (backend == DNN_ONNX) {
110  /* ONNX: input and output tensor names are optional.*/
111  if (ctx->model_outputnames_string) {
112  ctx->model_outputnames = separate_output_names(ctx->model_outputnames_string, "&", &ctx->nb_outputs);
113  if (!ctx->model_outputnames) {
114  av_log(filter_ctx, AV_LOG_ERROR, "could not parse model output names\n");
115  return AVERROR(EINVAL);
116  }
117  if (ctx->nb_outputs != 1) {
119  "ONNX backend supports a single output name only\n");
120  return AVERROR(EINVAL);
121  }
122  }
123  }
124 
125  ctx->dnn_module = ff_get_dnn_module(ctx->backend_type, filter_ctx);
126  if (!ctx->dnn_module) {
127  av_log(filter_ctx, AV_LOG_ERROR, "could not create DNN module for requested backend\n");
128  return AVERROR(ENOMEM);
129  }
130  if (!ctx->dnn_module->load_model) {
131  av_log(filter_ctx, AV_LOG_ERROR, "load_model for network is not specified\n");
132  return AVERROR(EINVAL);
133  }
134 
135  if (ctx->backend_options) {
136  void *child = NULL;
137 
139  "backend_configs is deprecated, please set backend options directly\n");
140  while (child = ff_dnn_child_next(ctx, child)) {
141  if (*(const AVClass **)child == &ctx->dnn_module->clazz) {
142  int ret = av_opt_set_from_string(child, ctx->backend_options,
143  NULL, "=", "&");
144  if (ret < 0) {
145  av_log(filter_ctx, AV_LOG_ERROR, "failed to parse options \"%s\"\n",
146  ctx->backend_options);
147  return ret;
148  }
149  }
150  }
151  }
152 
153  ctx->model = (ctx->dnn_module->load_model)(ctx, func_type, filter_ctx);
154  if (!ctx->model) {
155  av_log(filter_ctx, AV_LOG_ERROR, "could not load DNN model\n");
156  return AVERROR(EINVAL);
157  }
158 
159  return 0;
160 }
161 
163 {
164  ctx->model->frame_pre_proc = pre_proc;
165  ctx->model->frame_post_proc = post_proc;
166  return 0;
167 }
168 
170 {
171  ctx->model->detect_post_proc = post_proc;
172  return 0;
173 }
174 
176 {
177  ctx->model->classify_post_proc = post_proc;
178  return 0;
179 }
180 
182 {
183  return ctx->model->get_input(ctx->model, input, ctx->model_inputname);
184 }
185 
186 int ff_dnn_get_output(DnnContext *ctx, int input_width, int input_height, int *output_width, int *output_height)
187 {
188  char * output_name = ctx->model_outputnames && ctx->backend_type != DNN_TH ?
189  ctx->model_outputnames[0] : NULL;
190  return ctx->model->get_output(ctx->model, ctx->model_inputname, input_width, input_height,
191  (const char *)output_name, output_width, output_height);
192 }
193 
194 int ff_dnn_execute_model(DnnContext *ctx, AVFrame *in_frame, AVFrame *out_frame)
195 {
196  DNNExecBaseParams exec_params = {
197  .input_name = ctx->model_inputname,
198  .output_names = (const char **)ctx->model_outputnames,
199  .nb_output = ctx->nb_outputs,
200  .in_frame = in_frame,
201  .out_frame = out_frame,
202  };
203  return (ctx->dnn_module->execute_model)(ctx->model, &exec_params);
204 }
205 
206 int ff_dnn_execute_model_classification(DnnContext *ctx, AVFrame *in_frame, AVFrame *out_frame, const char *target)
207 {
208  DNNExecClassificationParams class_params = {
209  {
210  .input_name = ctx->model_inputname,
211  .output_names = (const char **)ctx->model_outputnames,
212  .nb_output = ctx->nb_outputs,
213  .in_frame = in_frame,
214  .out_frame = out_frame,
215  },
216  .target = target,
217  };
218  return (ctx->dnn_module->execute_model)(ctx->model, &class_params.base);
219 }
220 
222 {
223  return (ctx->dnn_module->get_result)(ctx->model, in_frame, out_frame);
224 }
225 
227 {
228  return (ctx->dnn_module->flush)(ctx->model);
229 }
230 
232 {
233  if (ctx->dnn_module) {
234  (ctx->dnn_module->free_model)(&ctx->model);
235  }
236  if (ctx->model_outputnames) {
237  for (int i = 0; i < ctx->nb_outputs; i++)
238  av_free(ctx->model_outputnames[i]);
239 
240  av_freep(&ctx->model_outputnames);
241  }
242 }
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:216
MAX_SUPPORTED_OUTPUTS_NB
#define MAX_SUPPORTED_OUTPUTS_NB
Definition: dnn_filter_common.c:24
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
DNNFunctionType
DNNFunctionType
Definition: dnn_interface.h:57
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:466
av_opt_set_from_string
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:1890
DetectPostProc
int(* DetectPostProc)(AVFrame *frame, DNNData *output, uint32_t nb, AVFilterContext *filter_ctx)
Definition: dnn_interface.h:95
separate_output_names
static char ** separate_output_names(const char *expr, const char *val_sep, int *separated_nb)
Definition: dnn_filter_common.c:26
base
uint8_t base
Definition: vp3data.h:128
DNNExecBaseParams::input_name
const char * input_name
Definition: dnn_interface.h:82
filter
void(* filter)(uint8_t *src, int stride, int qscale)
Definition: h263dsp.c:29
dnn_filter_common.h
DnnContext
Definition: dnn_interface.h:151
filter_ctx
static FilteringContext * filter_ctx
Definition: transcode.c:52
ff_dnn_filter_init_child_class
int ff_dnn_filter_init_child_class(AVFilterContext *filter)
Definition: dnn_filter_common.c:70
val
static double val(void *priv, double ch)
Definition: aeval.c:77
DNN_TF
@ DNN_TF
Definition: dnn_interface.h:36
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
DNNExecClassificationParams
Definition: dnn_interface.h:89
ff_dnn_set_detect_post_proc
int ff_dnn_set_detect_post_proc(DnnContext *ctx, DetectPostProc post_proc)
Definition: dnn_filter_common.c:169
DNNData
Definition: dnn_interface.h:70
ff_dnn_get_result
DNNAsyncStatusType ff_dnn_get_result(DnnContext *ctx, AVFrame **in_frame, AVFrame **out_frame)
Definition: dnn_filter_common.c:221
ff_dnn_init_child_class
void ff_dnn_init_child_class(DnnContext *ctx)
Definition: dnn_interface.c:104
ctx
static AVFormatContext * ctx
Definition: movenc.c:49
ff_dnn_get_input
int ff_dnn_get_input(DnnContext *ctx, DNNData *input)
Definition: dnn_filter_common.c:181
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:76
DNNExecClassificationParams::base
DNNExecBaseParams base
Definition: dnn_interface.h:90
NULL
#define NULL
Definition: coverity.c:32
ff_dnn_filter_child_next
void * ff_dnn_filter_child_next(void *obj, void *prev)
Definition: dnn_filter_common.c:76
ff_dnn_execute_model_classification
int ff_dnn_execute_model_classification(DnnContext *ctx, AVFrame *in_frame, AVFrame *out_frame, const char *target)
Definition: dnn_filter_common.c:206
ff_dnn_flush
int ff_dnn_flush(DnnContext *ctx)
Definition: dnn_filter_common.c:226
ClassifyPostProc
int(* ClassifyPostProc)(AVFrame *frame, DNNData *output, uint32_t bbox_index, AVFilterContext *filter_ctx)
Definition: dnn_interface.h:96
DNNBackendType
DNNBackendType
Definition: dnn_interface.h:35
i
#define i(width, name, range_min, range_max)
Definition: cbs_h264.c:63
DNN_ONNX
@ DNN_ONNX
Definition: dnn_interface.h:39
ff_get_dnn_module
const DNNModule * ff_get_dnn_module(DNNBackendType backend_type, void *log_ctx)
Definition: dnn_interface.c:91
input
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some input
Definition: filter_design.txt:172
ff_dnn_child_next
void * ff_dnn_child_next(DnnContext *obj, void *prev)
Definition: dnn_interface.c:114
ff_dnn_set_frame_proc
int ff_dnn_set_frame_proc(DnnContext *ctx, FramePrePostProc pre_proc, FramePrePostProc post_proc)
Definition: dnn_filter_common.c:162
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:264
DnnFilterBase::dnnctx
DnnContext dnnctx
Definition: dnn_filter_common.c:67
ret
ret
Definition: filter_design.txt:187
DnnFilterBase
Definition: dnn_filter_common.c:65
ff_dnn_get_output
int ff_dnn_get_output(DnnContext *ctx, int input_width, int input_height, int *output_width, int *output_height)
Definition: dnn_filter_common.c:186
av_get_token
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
FramePrePostProc
int(* FramePrePostProc)(AVFrame *frame, DNNData *model, AVFilterContext *filter_ctx)
Definition: dnn_interface.h:94
AVFilterContext
An instance of a filter.
Definition: avfilter.h:273
DNN_TH
@ DNN_TH
Definition: dnn_interface.h:38
mem.h
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
ff_dnn_init
int ff_dnn_init(DnnContext *ctx, DNNFunctionType func_type, AVFilterContext *filter_ctx)
Definition: dnn_filter_common.c:82
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
DNNExecBaseParams
Definition: dnn_interface.h:81
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
ff_dnn_uninit
void ff_dnn_uninit(DnnContext *ctx)
Definition: dnn_filter_common.c:231
ff_dnn_execute_model
int ff_dnn_execute_model(DnnContext *ctx, AVFrame *in_frame, AVFrame *out_frame)
Definition: dnn_filter_common.c:194
avstring.h
DNNAsyncStatusType
DNNAsyncStatusType
Definition: dnn_interface.h:50
ff_dnn_set_classify_post_proc
int ff_dnn_set_classify_post_proc(DnnContext *ctx, ClassifyPostProc post_proc)
Definition: dnn_filter_common.c:175