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 
22 {
23  if (!ctx->model_filename) {
24  av_log(filter_ctx, AV_LOG_ERROR, "model file for network is not specified\n");
25  return AVERROR(EINVAL);
26  }
27  if (!ctx->model_inputname) {
28  av_log(filter_ctx, AV_LOG_ERROR, "input name of the model network is not specified\n");
29  return AVERROR(EINVAL);
30  }
31  if (!ctx->model_outputname) {
32  av_log(filter_ctx, AV_LOG_ERROR, "output name of the model network is not specified\n");
33  return AVERROR(EINVAL);
34  }
35 
36  ctx->dnn_module = ff_get_dnn_module(ctx->backend_type);
37  if (!ctx->dnn_module) {
38  av_log(filter_ctx, AV_LOG_ERROR, "could not create DNN module for requested backend\n");
39  return AVERROR(ENOMEM);
40  }
41  if (!ctx->dnn_module->load_model) {
42  av_log(filter_ctx, AV_LOG_ERROR, "load_model for network is not specified\n");
43  return AVERROR(EINVAL);
44  }
45 
46  ctx->model = (ctx->dnn_module->load_model)(ctx->model_filename, func_type, ctx->backend_options, filter_ctx);
47  if (!ctx->model) {
48  av_log(filter_ctx, AV_LOG_ERROR, "could not load DNN model\n");
49  return AVERROR(EINVAL);
50  }
51 
52  if (!ctx->dnn_module->execute_model_async && ctx->async) {
53  ctx->async = 0;
54  av_log(filter_ctx, AV_LOG_WARNING, "this backend does not support async execution, roll back to sync.\n");
55  }
56 
57 #if !HAVE_PTHREAD_CANCEL
58  if (ctx->async) {
59  ctx->async = 0;
60  av_log(filter_ctx, AV_LOG_WARNING, "pthread is not supported, roll back to sync.\n");
61  }
62 #endif
63 
64  return 0;
65 }
66 
68 {
69  return ctx->model->get_input(ctx->model->model, input, ctx->model_inputname);
70 }
71 
72 DNNReturnType ff_dnn_get_output(DnnContext *ctx, int input_width, int input_height, int *output_width, int *output_height)
73 {
74  return ctx->model->get_output(ctx->model->model, ctx->model_inputname, input_width, input_height,
75  ctx->model_outputname, output_width, output_height);
76 }
77 
79 {
80  return (ctx->dnn_module->execute_model)(ctx->model, ctx->model_inputname, in_frame,
81  (const char **)&ctx->model_outputname, 1, out_frame);
82 }
83 
85 {
86  return (ctx->dnn_module->execute_model_async)(ctx->model, ctx->model_inputname, in_frame,
87  (const char **)&ctx->model_outputname, 1, out_frame);
88 }
89 
91 {
92  return (ctx->dnn_module->get_async_result)(ctx->model, in_frame, out_frame);
93 }
94 
96 {
97  return (ctx->dnn_module->flush)(ctx->model);
98 }
99 
101 {
102  if (ctx->dnn_module) {
103  (ctx->dnn_module->free_model)(&ctx->model);
104  av_freep(&ctx->dnn_module);
105  }
106 }
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
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:48
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:72
DNNFunctionType
DNNFunctionType
Definition: dnn_interface.h:51
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
ff_dnn_get_input
DNNReturnType ff_dnn_get_input(DnnContext *ctx, DNNData *input)
Definition: dnn_filter_common.c:67
dnn_filter_common.h
DnnContext
Definition: dnn_filter_common.h:29
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
DNNReturnType
DNNReturnType
Definition: dnn_interface.h:33
DNNData
Definition: dnn_interface.h:58
ctx
AVFormatContext * ctx
Definition: movenc.c:48
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:84
ff_get_dnn_module
DNNModule * ff_get_dnn_module(DNNBackendType backend_type)
Definition: dnn_interface.c:32
AVFilterContext
An instance of a filter.
Definition: avfilter.h:341
ff_dnn_execute_model
DNNReturnType ff_dnn_execute_model(DnnContext *ctx, AVFrame *in_frame, AVFrame *out_frame)
Definition: dnn_filter_common.c:78
ff_dnn_init
int ff_dnn_init(DnnContext *ctx, DNNFunctionType func_type, AVFilterContext *filter_ctx)
Definition: dnn_filter_common.c:21
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:95
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:100
DNNAsyncStatusType
DNNAsyncStatusType
Definition: dnn_interface.h:44
ff_dnn_get_async_result
DNNAsyncStatusType ff_dnn_get_async_result(DnnContext *ctx, AVFrame **in_frame, AVFrame **out_frame)
Definition: dnn_filter_common.c:90