2021-01-26 05:35:30 +00:00
|
|
|
/*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "dnn_filter_common.h"
|
2021-05-06 08:46:08 +00:00
|
|
|
#include "libavutil/avstring.h"
|
|
|
|
|
|
|
|
#define MAX_SUPPORTED_OUTPUTS_NB 4
|
|
|
|
|
|
|
|
static char **separate_output_names(const char *expr, const char *val_sep, int *separated_nb)
|
|
|
|
{
|
|
|
|
char *val, **parsed_vals = NULL;
|
|
|
|
int val_num = 0;
|
|
|
|
if (!expr || !val_sep || !separated_nb) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-09-14 19:31:53 +00:00
|
|
|
parsed_vals = av_calloc(MAX_SUPPORTED_OUTPUTS_NB, sizeof(*parsed_vals));
|
2021-05-06 08:46:08 +00:00
|
|
|
if (!parsed_vals) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
val = av_get_token(&expr, val_sep);
|
|
|
|
if(val) {
|
|
|
|
parsed_vals[val_num] = val;
|
|
|
|
val_num++;
|
|
|
|
}
|
|
|
|
if (*expr) {
|
|
|
|
expr++;
|
|
|
|
}
|
|
|
|
} while(*expr);
|
|
|
|
|
|
|
|
parsed_vals[val_num] = NULL;
|
|
|
|
*separated_nb = val_num;
|
|
|
|
|
|
|
|
return parsed_vals;
|
|
|
|
}
|
2021-01-26 05:35:30 +00:00
|
|
|
|
2021-02-07 06:35:22 +00:00
|
|
|
int ff_dnn_init(DnnContext *ctx, DNNFunctionType func_type, AVFilterContext *filter_ctx)
|
2021-01-26 05:35:30 +00:00
|
|
|
{
|
|
|
|
if (!ctx->model_filename) {
|
|
|
|
av_log(filter_ctx, AV_LOG_ERROR, "model file for network is not specified\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
if (!ctx->model_inputname) {
|
|
|
|
av_log(filter_ctx, AV_LOG_ERROR, "input name of the model network is not specified\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
2021-05-06 08:46:08 +00:00
|
|
|
|
|
|
|
ctx->model_outputnames = separate_output_names(ctx->model_outputnames_string, "&", &ctx->nb_outputs);
|
|
|
|
if (!ctx->model_outputnames) {
|
|
|
|
av_log(filter_ctx, AV_LOG_ERROR, "could not parse model output names\n");
|
2021-01-26 05:35:30 +00:00
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
2023-04-30 15:38:55 +00:00
|
|
|
ctx->dnn_module = ff_get_dnn_module(ctx->backend_type, filter_ctx);
|
2021-01-26 05:35:30 +00:00
|
|
|
if (!ctx->dnn_module) {
|
|
|
|
av_log(filter_ctx, AV_LOG_ERROR, "could not create DNN module for requested backend\n");
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
if (!ctx->dnn_module->load_model) {
|
|
|
|
av_log(filter_ctx, AV_LOG_ERROR, "load_model for network is not specified\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
2021-02-07 06:35:22 +00:00
|
|
|
ctx->model = (ctx->dnn_module->load_model)(ctx->model_filename, func_type, ctx->backend_options, filter_ctx);
|
2021-01-26 05:35:30 +00:00
|
|
|
if (!ctx->model) {
|
|
|
|
av_log(filter_ctx, AV_LOG_ERROR, "could not load DNN model\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-03-01 11:23:20 +00:00
|
|
|
int ff_dnn_set_frame_proc(DnnContext *ctx, FramePrePostProc pre_proc, FramePrePostProc post_proc)
|
|
|
|
{
|
|
|
|
ctx->model->frame_pre_proc = pre_proc;
|
|
|
|
ctx->model->frame_post_proc = post_proc;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-03-09 06:51:42 +00:00
|
|
|
int ff_dnn_set_detect_post_proc(DnnContext *ctx, DetectPostProc post_proc)
|
|
|
|
{
|
|
|
|
ctx->model->detect_post_proc = post_proc;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-03-16 05:02:56 +00:00
|
|
|
int ff_dnn_set_classify_post_proc(DnnContext *ctx, ClassifyPostProc post_proc)
|
|
|
|
{
|
|
|
|
ctx->model->classify_post_proc = post_proc;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-03-02 18:05:49 +00:00
|
|
|
int ff_dnn_get_input(DnnContext *ctx, DNNData *input)
|
2021-01-26 05:35:30 +00:00
|
|
|
{
|
|
|
|
return ctx->model->get_input(ctx->model->model, input, ctx->model_inputname);
|
|
|
|
}
|
|
|
|
|
2022-03-02 18:05:49 +00:00
|
|
|
int ff_dnn_get_output(DnnContext *ctx, int input_width, int input_height, int *output_width, int *output_height)
|
2021-01-26 05:35:30 +00:00
|
|
|
{
|
|
|
|
return ctx->model->get_output(ctx->model->model, ctx->model_inputname, input_width, input_height,
|
2021-05-06 08:46:08 +00:00
|
|
|
(const char *)ctx->model_outputnames[0], output_width, output_height);
|
2021-01-26 05:35:30 +00:00
|
|
|
}
|
|
|
|
|
2022-03-02 18:05:49 +00:00
|
|
|
int ff_dnn_execute_model(DnnContext *ctx, AVFrame *in_frame, AVFrame *out_frame)
|
2021-01-26 05:35:30 +00:00
|
|
|
{
|
2021-04-01 02:06:06 +00:00
|
|
|
DNNExecBaseParams exec_params = {
|
|
|
|
.input_name = ctx->model_inputname,
|
2021-05-06 08:46:08 +00:00
|
|
|
.output_names = (const char **)ctx->model_outputnames,
|
|
|
|
.nb_output = ctx->nb_outputs,
|
2021-04-01 02:06:06 +00:00
|
|
|
.in_frame = in_frame,
|
|
|
|
.out_frame = out_frame,
|
|
|
|
};
|
|
|
|
return (ctx->dnn_module->execute_model)(ctx->model, &exec_params);
|
2021-01-26 05:35:30 +00:00
|
|
|
}
|
|
|
|
|
2022-03-02 18:05:49 +00:00
|
|
|
int ff_dnn_execute_model_classification(DnnContext *ctx, AVFrame *in_frame, AVFrame *out_frame, const char *target)
|
2021-03-16 05:02:56 +00:00
|
|
|
{
|
|
|
|
DNNExecClassificationParams class_params = {
|
|
|
|
{
|
|
|
|
.input_name = ctx->model_inputname,
|
2021-05-06 08:46:08 +00:00
|
|
|
.output_names = (const char **)ctx->model_outputnames,
|
|
|
|
.nb_output = ctx->nb_outputs,
|
2021-03-16 05:02:56 +00:00
|
|
|
.in_frame = in_frame,
|
|
|
|
.out_frame = out_frame,
|
|
|
|
},
|
|
|
|
.target = target,
|
|
|
|
};
|
2021-08-25 21:10:45 +00:00
|
|
|
return (ctx->dnn_module->execute_model)(ctx->model, &class_params.base);
|
2021-03-16 05:02:56 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 21:10:45 +00:00
|
|
|
DNNAsyncStatusType ff_dnn_get_result(DnnContext *ctx, AVFrame **in_frame, AVFrame **out_frame)
|
2021-01-26 05:35:30 +00:00
|
|
|
{
|
2021-08-25 21:10:45 +00:00
|
|
|
return (ctx->dnn_module->get_result)(ctx->model, in_frame, out_frame);
|
2021-01-26 05:35:30 +00:00
|
|
|
}
|
|
|
|
|
2022-03-02 18:05:49 +00:00
|
|
|
int ff_dnn_flush(DnnContext *ctx)
|
2021-01-26 05:35:30 +00:00
|
|
|
{
|
|
|
|
return (ctx->dnn_module->flush)(ctx->model);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ff_dnn_uninit(DnnContext *ctx)
|
|
|
|
{
|
|
|
|
if (ctx->dnn_module) {
|
|
|
|
(ctx->dnn_module->free_model)(&ctx->model);
|
|
|
|
}
|
|
|
|
}
|