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 
22 #define MAX_SUPPORTED_OUTPUTS_NB 4
23 
24 static char **separate_output_names(const char *expr, const char *val_sep, int *separated_nb)
25 {
26  char *val, **parsed_vals = NULL;
27  int val_num = 0;
28  if (!expr || !val_sep || !separated_nb) {
29  return NULL;
30  }
31 
32  parsed_vals = av_mallocz_array(MAX_SUPPORTED_OUTPUTS_NB, sizeof(*parsed_vals));
33  if (!parsed_vals) {
34  return NULL;
35  }
36 
37  do {
38  val = av_get_token(&expr, val_sep);
39  if(val) {
40  parsed_vals[val_num] = val;
41  val_num++;
42  }
43  if (*expr) {
44  expr++;
45  }
46  } while(*expr);
47 
48  parsed_vals[val_num] = NULL;
49  *separated_nb = val_num;
50 
51  return parsed_vals;
52 }
53 
55 {
56  if (!ctx->model_filename) {
57  av_log(filter_ctx, AV_LOG_ERROR, "model file for network is not specified\n");
58  return AVERROR(EINVAL);
59  }
60  if (!ctx->model_inputname) {
61  av_log(filter_ctx, AV_LOG_ERROR, "input name of the model network is not specified\n");
62  return AVERROR(EINVAL);
63  }
64 
65  ctx->model_outputnames = separate_output_names(ctx->model_outputnames_string, "&", &ctx->nb_outputs);
66  if (!ctx->model_outputnames) {
67  av_log(filter_ctx, AV_LOG_ERROR, "could not parse model output names\n");
68  return AVERROR(EINVAL);
69  }
70 
71  ctx->dnn_module = ff_get_dnn_module(ctx->backend_type);
72  if (!ctx->dnn_module) {
73  av_log(filter_ctx, AV_LOG_ERROR, "could not create DNN module for requested backend\n");
74  return AVERROR(ENOMEM);
75  }
76  if (!ctx->dnn_module->load_model) {
77  av_log(filter_ctx, AV_LOG_ERROR, "load_model for network is not specified\n");
78  return AVERROR(EINVAL);
79  }
80 
81  ctx->model = (ctx->dnn_module->load_model)(ctx->model_filename, func_type, ctx->backend_options, filter_ctx);
82  if (!ctx->model) {
83  av_log(filter_ctx, AV_LOG_ERROR, "could not load DNN model\n");
84  return AVERROR(EINVAL);
85  }
86 
87  if (!ctx->dnn_module->execute_model_async && ctx->async) {
88  ctx->async = 0;
89  av_log(filter_ctx, AV_LOG_WARNING, "this backend does not support async execution, roll back to sync.\n");
90  }
91 
92 #if !HAVE_PTHREAD_CANCEL
93  if (ctx->async) {
94  ctx->async = 0;
95  av_log(filter_ctx, AV_LOG_WARNING, "pthread is not supported, roll back to sync.\n");
96  }
97 #endif
98 
99  return 0;
100 }
101 
103 {
104  ctx->model->frame_pre_proc = pre_proc;
105  ctx->model->frame_post_proc = post_proc;
106  return 0;
107 }
108 
110 {
111  ctx->model->detect_post_proc = post_proc;
112  return 0;
113 }
114 
116 {
117  ctx->model->classify_post_proc = post_proc;
118  return 0;
119 }
120 
122 {
123  return ctx->model->get_input(ctx->model->model, input, ctx->model_inputname);
124 }
125 
126 DNNReturnType ff_dnn_get_output(DnnContext *ctx, int input_width, int input_height, int *output_width, int *output_height)
127 {
128  return ctx->model->get_output(ctx->model->model, ctx->model_inputname, input_width, input_height,
129  (const char *)ctx->model_outputnames[0], output_width, output_height);
130 }
131 
133 {
134  DNNExecBaseParams exec_params = {
135  .input_name = ctx->model_inputname,
136  .output_names = (const char **)ctx->model_outputnames,
137  .nb_output = ctx->nb_outputs,
138  .in_frame = in_frame,
139  .out_frame = out_frame,
140  };
141  return (ctx->dnn_module->execute_model)(ctx->model, &exec_params);
142 }
143 
145 {
146  DNNExecBaseParams exec_params = {
147  .input_name = ctx->model_inputname,
148  .output_names = (const char **)ctx->model_outputnames,
149  .nb_output = ctx->nb_outputs,
150  .in_frame = in_frame,
151  .out_frame = out_frame,
152  };
153  return (ctx->dnn_module->execute_model_async)(ctx->model, &exec_params);
154 }
155 
157 {
158  DNNExecClassificationParams class_params = {
159  {
160  .input_name = ctx->model_inputname,
161  .output_names = (const char **)ctx->model_outputnames,
162  .nb_output = ctx->nb_outputs,
163  .in_frame = in_frame,
164  .out_frame = out_frame,
165  },
166  .target = target,
167  };
168  return (ctx->dnn_module->execute_model_async)(ctx->model, &class_params.base);
169 }
170 
172 {
173  return (ctx->dnn_module->get_async_result)(ctx->model, in_frame, out_frame);
174 }
175 
177 {
178  return (ctx->dnn_module->flush)(ctx->model);
179 }
180 
182 {
183  if (ctx->dnn_module) {
184  (ctx->dnn_module->free_model)(&ctx->model);
185  av_freep(&ctx->dnn_module);
186  }
187 }
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
MAX_SUPPORTED_OUTPUTS_NB
#define MAX_SUPPORTED_OUTPUTS_NB
Definition: dnn_filter_common.c:22
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
filter_ctx
static FilteringContext * filter_ctx
Definition: transcoding.c:49
ff_dnn_get_output
DNNReturnType ff_dnn_get_output(DnnContext *ctx, int input_width, int input_height, int *output_width, int *output_height)
Definition: dnn_filter_common.c:126
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:151
DNNFunctionType
DNNFunctionType
Definition: dnn_interface.h:52
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:303
DetectPostProc
int(* DetectPostProc)(AVFrame *frame, DNNData *output, uint32_t nb, AVFilterContext *filter_ctx)
Definition: dnn_interface.h:81
separate_output_names
static char ** separate_output_names(const char *expr, const char *val_sep, int *separated_nb)
Definition: dnn_filter_common.c:24
av_mallocz_array
void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.c:196
DNNExecBaseParams::input_name
const char * input_name
Definition: dnn_interface.h:68
ff_dnn_get_input
DNNReturnType ff_dnn_get_input(DnnContext *ctx, DNNData *input)
Definition: dnn_filter_common.c:121
dnn_filter_common.h
DnnContext
Definition: dnn_filter_common.h:29
val
static double val(void *priv, double ch)
Definition: aeval.c:75
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
DNNExecClassificationParams
Definition: dnn_interface.h:75
ff_dnn_set_detect_post_proc
int ff_dnn_set_detect_post_proc(DnnContext *ctx, DetectPostProc post_proc)
Definition: dnn_filter_common.c:109
DNNReturnType
DNNReturnType
Definition: dnn_interface.h:33
DNNData
Definition: dnn_interface.h:59
ctx
AVFormatContext * ctx
Definition: movenc.c:48
DNNExecClassificationParams::base
DNNExecBaseParams base
Definition: dnn_interface.h:76
NULL
#define NULL
Definition: coverity.c:32
ClassifyPostProc
int(* ClassifyPostProc)(AVFrame *frame, DNNData *output, uint32_t bbox_index, AVFilterContext *filter_ctx)
Definition: dnn_interface.h:82
ff_dnn_execute_model_classification
DNNReturnType ff_dnn_execute_model_classification(DnnContext *ctx, AVFrame *in_frame, AVFrame *out_frame, const char *target)
Definition: dnn_filter_common.c:156
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_execute_model_async
DNNReturnType ff_dnn_execute_model_async(DnnContext *ctx, AVFrame *in_frame, AVFrame *out_frame)
Definition: dnn_filter_common.c:144
ff_dnn_set_frame_proc
int ff_dnn_set_frame_proc(DnnContext *ctx, FramePrePostProc pre_proc, FramePrePostProc post_proc)
Definition: dnn_filter_common.c:102
ff_get_dnn_module
DNNModule * ff_get_dnn_module(DNNBackendType backend_type)
Definition: dnn_interface.c:32
FramePrePostProc
int(* FramePrePostProc)(AVFrame *frame, DNNData *model, AVFilterContext *filter_ctx)
Definition: dnn_interface.h:80
AVFilterContext
An instance of a filter.
Definition: avfilter.h:333
ff_dnn_execute_model
DNNReturnType ff_dnn_execute_model(DnnContext *ctx, AVFrame *in_frame, AVFrame *out_frame)
Definition: dnn_filter_common.c:132
ff_dnn_init
int ff_dnn_init(DnnContext *ctx, DNNFunctionType func_type, AVFilterContext *filter_ctx)
Definition: dnn_filter_common.c:54
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
ff_dnn_flush
DNNReturnType ff_dnn_flush(DnnContext *ctx)
Definition: dnn_filter_common.c:176
DNNExecBaseParams
Definition: dnn_interface.h:67
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
ff_dnn_uninit
void ff_dnn_uninit(DnnContext *ctx)
Definition: dnn_filter_common.c:181
avstring.h
DNNAsyncStatusType
DNNAsyncStatusType
Definition: dnn_interface.h:45
ff_dnn_get_async_result
DNNAsyncStatusType ff_dnn_get_async_result(DnnContext *ctx, AVFrame **in_frame, AVFrame **out_frame)
Definition: dnn_filter_common.c:171
ff_dnn_set_classify_post_proc
int ff_dnn_set_classify_post_proc(DnnContext *ctx, ClassifyPostProc post_proc)
Definition: dnn_filter_common.c:115