2 * This file is part of FFmpeg.
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.
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.
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
19 #include "dnn_filter_common.h"
20 #include "libavutil/avstring.h"
22 #define MAX_SUPPORTED_OUTPUTS_NB 4
24 static char **separate_output_names(const char *expr, const char *val_sep, int *separated_nb)
26 char *val, **parsed_vals = NULL;
28 if (!expr || !val_sep || !separated_nb) {
32 parsed_vals = av_calloc(MAX_SUPPORTED_OUTPUTS_NB, sizeof(*parsed_vals));
38 val = av_get_token(&expr, val_sep);
40 parsed_vals[val_num] = val;
48 parsed_vals[val_num] = NULL;
49 *separated_nb = val_num;
54 int ff_dnn_init(DnnContext *ctx, DNNFunctionType func_type, AVFilterContext *filter_ctx)
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);
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);
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);
71 ctx->dnn_module = ff_get_dnn_module(ctx->backend_type, filter_ctx);
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);
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);
81 ctx->model = (ctx->dnn_module->load_model)(ctx->model_filename, func_type, ctx->backend_options, filter_ctx);
83 av_log(filter_ctx, AV_LOG_ERROR, "could not load DNN model\n");
84 return AVERROR(EINVAL);
90 int ff_dnn_set_frame_proc(DnnContext *ctx, FramePrePostProc pre_proc, FramePrePostProc post_proc)
92 ctx->model->frame_pre_proc = pre_proc;
93 ctx->model->frame_post_proc = post_proc;
97 int ff_dnn_set_detect_post_proc(DnnContext *ctx, DetectPostProc post_proc)
99 ctx->model->detect_post_proc = post_proc;
103 int ff_dnn_set_classify_post_proc(DnnContext *ctx, ClassifyPostProc post_proc)
105 ctx->model->classify_post_proc = post_proc;
109 int ff_dnn_get_input(DnnContext *ctx, DNNData *input)
111 return ctx->model->get_input(ctx->model->model, input, ctx->model_inputname);
114 int ff_dnn_get_output(DnnContext *ctx, int input_width, int input_height, int *output_width, int *output_height)
116 return ctx->model->get_output(ctx->model->model, ctx->model_inputname, input_width, input_height,
117 (const char *)ctx->model_outputnames[0], output_width, output_height);
120 int ff_dnn_execute_model(DnnContext *ctx, AVFrame *in_frame, AVFrame *out_frame)
122 DNNExecBaseParams exec_params = {
123 .input_name = ctx->model_inputname,
124 .output_names = (const char **)ctx->model_outputnames,
125 .nb_output = ctx->nb_outputs,
126 .in_frame = in_frame,
127 .out_frame = out_frame,
129 return (ctx->dnn_module->execute_model)(ctx->model, &exec_params);
132 int ff_dnn_execute_model_classification(DnnContext *ctx, AVFrame *in_frame, AVFrame *out_frame, const char *target)
134 DNNExecClassificationParams class_params = {
136 .input_name = ctx->model_inputname,
137 .output_names = (const char **)ctx->model_outputnames,
138 .nb_output = ctx->nb_outputs,
139 .in_frame = in_frame,
140 .out_frame = out_frame,
144 return (ctx->dnn_module->execute_model)(ctx->model, &class_params.base);
147 DNNAsyncStatusType ff_dnn_get_result(DnnContext *ctx, AVFrame **in_frame, AVFrame **out_frame)
149 return (ctx->dnn_module->get_result)(ctx->model, in_frame, out_frame);
152 int ff_dnn_flush(DnnContext *ctx)
154 return (ctx->dnn_module->flush)(ctx->model);
157 void ff_dnn_uninit(DnnContext *ctx)
159 if (ctx->dnn_module) {
160 (ctx->dnn_module->free_model)(&ctx->model);
162 if (ctx->model_outputnames) {
163 for (int i = 0; i < ctx->nb_outputs; i++)
164 av_free(ctx->model_outputnames[i]);
166 av_freep(&ctx->model_outputnames);