FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/dnn_filter_common.c
Date: 2024-04-19 17:50:32
Exec Total Coverage
Lines: 0 89 0.0%
Functions: 0 12 0.0%
Branches: 0 44 0.0%

Line Branch Exec Source
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
23 #define MAX_SUPPORTED_OUTPUTS_NB 4
24
25 static char **separate_output_names(const char *expr, const char *val_sep, int *separated_nb)
26 {
27 char *val, **parsed_vals = NULL;
28 int val_num = 0;
29 if (!expr || !val_sep || !separated_nb) {
30 return NULL;
31 }
32
33 parsed_vals = av_calloc(MAX_SUPPORTED_OUTPUTS_NB, sizeof(*parsed_vals));
34 if (!parsed_vals) {
35 return NULL;
36 }
37
38 do {
39 val = av_get_token(&expr, val_sep);
40 if(val) {
41 parsed_vals[val_num] = val;
42 val_num++;
43 }
44 if (*expr) {
45 expr++;
46 }
47 } while(*expr);
48
49 parsed_vals[val_num] = NULL;
50 *separated_nb = val_num;
51
52 return parsed_vals;
53 }
54
55 int ff_dnn_init(DnnContext *ctx, DNNFunctionType func_type, AVFilterContext *filter_ctx)
56 {
57 DNNBackendType backend = ctx->backend_type;
58
59 if (!ctx->model_filename) {
60 av_log(filter_ctx, AV_LOG_ERROR, "model file for network is not specified\n");
61 return AVERROR(EINVAL);
62 }
63
64 if (backend == DNN_TH) {
65 if (ctx->model_inputname)
66 av_log(filter_ctx, AV_LOG_WARNING, "LibTorch backend do not require inputname, "\
67 "inputname will be ignored.\n");
68 if (ctx->model_outputnames)
69 av_log(filter_ctx, AV_LOG_WARNING, "LibTorch backend do not require outputname(s), "\
70 "all outputname(s) will be ignored.\n");
71 ctx->nb_outputs = 1;
72 } else if (backend == DNN_TF) {
73 if (!ctx->model_inputname) {
74 av_log(filter_ctx, AV_LOG_ERROR, "input name of the model network is not specified\n");
75 return AVERROR(EINVAL);
76 }
77 ctx->model_outputnames = separate_output_names(ctx->model_outputnames_string, "&", &ctx->nb_outputs);
78 if (!ctx->model_outputnames) {
79 av_log(filter_ctx, AV_LOG_ERROR, "could not parse model output names\n");
80 return AVERROR(EINVAL);
81 }
82 }
83
84 ctx->dnn_module = ff_get_dnn_module(ctx->backend_type, filter_ctx);
85 if (!ctx->dnn_module) {
86 av_log(filter_ctx, AV_LOG_ERROR, "could not create DNN module for requested backend\n");
87 return AVERROR(ENOMEM);
88 }
89 if (!ctx->dnn_module->load_model) {
90 av_log(filter_ctx, AV_LOG_ERROR, "load_model for network is not specified\n");
91 return AVERROR(EINVAL);
92 }
93
94 ctx->model = (ctx->dnn_module->load_model)(ctx->model_filename, func_type, ctx->backend_options, filter_ctx);
95 if (!ctx->model) {
96 av_log(filter_ctx, AV_LOG_ERROR, "could not load DNN model\n");
97 return AVERROR(EINVAL);
98 }
99
100 return 0;
101 }
102
103 int ff_dnn_set_frame_proc(DnnContext *ctx, FramePrePostProc pre_proc, FramePrePostProc post_proc)
104 {
105 ctx->model->frame_pre_proc = pre_proc;
106 ctx->model->frame_post_proc = post_proc;
107 return 0;
108 }
109
110 int ff_dnn_set_detect_post_proc(DnnContext *ctx, DetectPostProc post_proc)
111 {
112 ctx->model->detect_post_proc = post_proc;
113 return 0;
114 }
115
116 int ff_dnn_set_classify_post_proc(DnnContext *ctx, ClassifyPostProc post_proc)
117 {
118 ctx->model->classify_post_proc = post_proc;
119 return 0;
120 }
121
122 int ff_dnn_get_input(DnnContext *ctx, DNNData *input)
123 {
124 return ctx->model->get_input(ctx->model->model, input, ctx->model_inputname);
125 }
126
127 int ff_dnn_get_output(DnnContext *ctx, int input_width, int input_height, int *output_width, int *output_height)
128 {
129 char * output_name = ctx->model_outputnames && ctx->backend_type != DNN_TH ?
130 ctx->model_outputnames[0] : NULL;
131 return ctx->model->get_output(ctx->model->model, ctx->model_inputname, input_width, input_height,
132 (const char *)output_name, output_width, output_height);
133 }
134
135 int ff_dnn_execute_model(DnnContext *ctx, AVFrame *in_frame, AVFrame *out_frame)
136 {
137 DNNExecBaseParams exec_params = {
138 .input_name = ctx->model_inputname,
139 .output_names = (const char **)ctx->model_outputnames,
140 .nb_output = ctx->nb_outputs,
141 .in_frame = in_frame,
142 .out_frame = out_frame,
143 };
144 return (ctx->dnn_module->execute_model)(ctx->model, &exec_params);
145 }
146
147 int ff_dnn_execute_model_classification(DnnContext *ctx, AVFrame *in_frame, AVFrame *out_frame, const char *target)
148 {
149 DNNExecClassificationParams class_params = {
150 {
151 .input_name = ctx->model_inputname,
152 .output_names = (const char **)ctx->model_outputnames,
153 .nb_output = ctx->nb_outputs,
154 .in_frame = in_frame,
155 .out_frame = out_frame,
156 },
157 .target = target,
158 };
159 return (ctx->dnn_module->execute_model)(ctx->model, &class_params.base);
160 }
161
162 DNNAsyncStatusType ff_dnn_get_result(DnnContext *ctx, AVFrame **in_frame, AVFrame **out_frame)
163 {
164 return (ctx->dnn_module->get_result)(ctx->model, in_frame, out_frame);
165 }
166
167 int ff_dnn_flush(DnnContext *ctx)
168 {
169 return (ctx->dnn_module->flush)(ctx->model);
170 }
171
172 void ff_dnn_uninit(DnnContext *ctx)
173 {
174 if (ctx->dnn_module) {
175 (ctx->dnn_module->free_model)(&ctx->model);
176 }
177 if (ctx->model_outputnames) {
178 for (int i = 0; i < ctx->nb_outputs; i++)
179 av_free(ctx->model_outputnames[i]);
180
181 av_freep(&ctx->model_outputnames);
182 }
183 }
184