forked from FFmpeg/FFmpeg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dnn: extract common functions used by different filters
Signed-off-by: Guo, Yejun <[email protected]>
- Loading branch information
Showing
6 changed files
with
201 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* 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" | ||
|
||
int ff_dnn_init(DnnContext *ctx, AVFilterContext *filter_ctx) | ||
{ | ||
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); | ||
} | ||
if (!ctx->model_outputname) { | ||
av_log(filter_ctx, AV_LOG_ERROR, "output name of the model network is not specified\n"); | ||
return AVERROR(EINVAL); | ||
} | ||
|
||
ctx->dnn_module = ff_get_dnn_module(ctx->backend_type); | ||
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); | ||
} | ||
|
||
ctx->model = (ctx->dnn_module->load_model)(ctx->model_filename, ctx->backend_options, filter_ctx); | ||
if (!ctx->model) { | ||
av_log(filter_ctx, AV_LOG_ERROR, "could not load DNN model\n"); | ||
return AVERROR(EINVAL); | ||
} | ||
|
||
if (!ctx->dnn_module->execute_model_async && ctx->async) { | ||
ctx->async = 0; | ||
av_log(filter_ctx, AV_LOG_WARNING, "this backend does not support async execution, roll back to sync.\n"); | ||
} | ||
|
||
#if !HAVE_PTHREAD_CANCEL | ||
if (ctx->async) { | ||
ctx->async = 0; | ||
av_log(filter_ctx, AV_LOG_WARNING, "pthread is not supported, roll back to sync.\n"); | ||
} | ||
#endif | ||
|
||
return 0; | ||
} | ||
|
||
DNNReturnType ff_dnn_get_input(DnnContext *ctx, DNNData *input) | ||
{ | ||
return ctx->model->get_input(ctx->model->model, input, ctx->model_inputname); | ||
} | ||
|
||
DNNReturnType ff_dnn_get_output(DnnContext *ctx, int input_width, int input_height, int *output_width, int *output_height) | ||
{ | ||
return ctx->model->get_output(ctx->model->model, ctx->model_inputname, input_width, input_height, | ||
ctx->model_outputname, output_width, output_height); | ||
} | ||
|
||
DNNReturnType ff_dnn_execute_model(DnnContext *ctx, AVFrame *in_frame, AVFrame *out_frame) | ||
{ | ||
return (ctx->dnn_module->execute_model)(ctx->model, ctx->model_inputname, in_frame, | ||
(const char **)&ctx->model_outputname, 1, out_frame); | ||
} | ||
|
||
DNNReturnType ff_dnn_execute_model_async(DnnContext *ctx, AVFrame *in_frame, AVFrame *out_frame) | ||
{ | ||
return (ctx->dnn_module->execute_model_async)(ctx->model, ctx->model_inputname, in_frame, | ||
(const char **)&ctx->model_outputname, 1, out_frame); | ||
} | ||
|
||
DNNAsyncStatusType ff_dnn_get_async_result(DnnContext *ctx, AVFrame **in_frame, AVFrame **out_frame) | ||
{ | ||
return (ctx->dnn_module->get_async_result)(ctx->model, in_frame, out_frame); | ||
} | ||
|
||
DNNReturnType ff_dnn_flush(DnnContext *ctx) | ||
{ | ||
return (ctx->dnn_module->flush)(ctx->model); | ||
} | ||
|
||
void ff_dnn_uninit(DnnContext *ctx) | ||
{ | ||
if (ctx->dnn_module) { | ||
(ctx->dnn_module->free_model)(&ctx->model); | ||
av_freep(&ctx->dnn_module); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* 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 | ||
*/ | ||
|
||
/** | ||
* @file | ||
* common functions for the dnn based filters | ||
*/ | ||
|
||
#ifndef AVFILTER_DNN_FILTER_COMMON_H | ||
#define AVFILTER_DNN_FILTER_COMMON_H | ||
|
||
#include "dnn_interface.h" | ||
|
||
typedef struct DnnContext { | ||
char *model_filename; | ||
DNNBackendType backend_type; | ||
char *model_inputname; | ||
char *model_outputname; | ||
char *backend_options; | ||
int async; | ||
|
||
DNNModule *dnn_module; | ||
DNNModel *model; | ||
} DnnContext; | ||
|
||
#define DNN_COMMON_OPTIONS \ | ||
{ "model", "path to model file", OFFSET(model_filename), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS },\ | ||
{ "input", "input name of the model", OFFSET(model_inputname), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS },\ | ||
{ "output", "output name of the model", OFFSET(model_outputname), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS },\ | ||
{ "backend_configs", "backend configs", OFFSET(backend_options), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS },\ | ||
{ "options", "backend configs", OFFSET(backend_options), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, FLAGS },\ | ||
{ "async", "use DNN async inference", OFFSET(async), AV_OPT_TYPE_BOOL, { .i64 = 1}, 0, 1, FLAGS}, | ||
|
||
|
||
int ff_dnn_init(DnnContext *ctx, AVFilterContext *filter_ctx); | ||
DNNReturnType ff_dnn_get_input(DnnContext *ctx, DNNData *input); | ||
DNNReturnType ff_dnn_get_output(DnnContext *ctx, int input_width, int input_height, int *output_width, int *output_height); | ||
DNNReturnType ff_dnn_execute_model(DnnContext *ctx, AVFrame *in_frame, AVFrame *out_frame); | ||
DNNReturnType ff_dnn_execute_model_async(DnnContext *ctx, AVFrame *in_frame, AVFrame *out_frame); | ||
DNNAsyncStatusType ff_dnn_get_async_result(DnnContext *ctx, AVFrame **in_frame, AVFrame **out_frame); | ||
DNNReturnType ff_dnn_flush(DnnContext *ctx); | ||
void ff_dnn_uninit(DnnContext *ctx); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.