2021-04-01 02:06:06 +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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* DNN common functions different backends.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "dnn_backend_common.h"
|
|
|
|
|
2021-08-08 10:55:39 +00:00
|
|
|
#define DNN_ASYNC_SUCCESS (void *)0
|
|
|
|
#define DNN_ASYNC_FAIL (void *)-1
|
|
|
|
|
2021-04-01 02:06:06 +00:00
|
|
|
int ff_check_exec_params(void *ctx, DNNBackendType backend, DNNFunctionType func_type, DNNExecBaseParams *exec_params)
|
|
|
|
{
|
|
|
|
if (!exec_params) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "exec_params is null when execute model.\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!exec_params->in_frame) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "in frame is NULL when execute model.\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
2021-08-25 21:10:45 +00:00
|
|
|
if (!exec_params->out_frame && func_type == DFT_PROCESS_FRAME) {
|
2021-04-01 02:06:06 +00:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "out frame is NULL when execute model.\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (exec_params->nb_output != 1 && backend != DNN_TF) {
|
|
|
|
// currently, the filter does not need multiple outputs,
|
|
|
|
// so we just pending the support until we really need it.
|
|
|
|
avpriv_report_missing_feature(ctx, "multiple outputs");
|
2022-03-02 18:05:55 +00:00
|
|
|
return AVERROR(ENOSYS);
|
2021-04-01 02:06:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2021-06-05 18:08:06 +00:00
|
|
|
|
2022-03-02 18:05:55 +00:00
|
|
|
int ff_dnn_fill_task(TaskItem *task, DNNExecBaseParams *exec_params, void *backend_model, int async, int do_ioproc) {
|
2021-06-05 18:08:06 +00:00
|
|
|
if (task == NULL || exec_params == NULL || backend_model == NULL)
|
2022-03-02 18:05:55 +00:00
|
|
|
return AVERROR(EINVAL);
|
2021-06-05 18:08:06 +00:00
|
|
|
if (do_ioproc != 0 && do_ioproc != 1)
|
2022-03-02 18:05:55 +00:00
|
|
|
return AVERROR(EINVAL);
|
2021-06-05 18:08:06 +00:00
|
|
|
if (async != 0 && async != 1)
|
2022-03-02 18:05:55 +00:00
|
|
|
return AVERROR(EINVAL);
|
2021-06-05 18:08:06 +00:00
|
|
|
|
|
|
|
task->do_ioproc = do_ioproc;
|
|
|
|
task->async = async;
|
|
|
|
task->input_name = exec_params->input_name;
|
|
|
|
task->in_frame = exec_params->in_frame;
|
|
|
|
task->out_frame = exec_params->out_frame;
|
|
|
|
task->model = backend_model;
|
|
|
|
task->nb_output = exec_params->nb_output;
|
|
|
|
task->output_names = exec_params->output_names;
|
|
|
|
|
2022-03-02 18:05:56 +00:00
|
|
|
return 0;
|
2021-06-05 18:08:06 +00:00
|
|
|
}
|
2021-08-08 10:55:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Thread routine for async execution.
|
|
|
|
* @param args pointer to DNNAsyncExecModule module
|
|
|
|
*/
|
|
|
|
static void *async_thread_routine(void *args)
|
|
|
|
{
|
|
|
|
DNNAsyncExecModule *async_module = args;
|
|
|
|
void *request = async_module->args;
|
|
|
|
|
2022-03-02 18:05:56 +00:00
|
|
|
if (async_module->start_inference(request) != 0) {
|
2021-08-08 10:55:39 +00:00
|
|
|
return DNN_ASYNC_FAIL;
|
|
|
|
}
|
2021-08-08 10:55:31 +00:00
|
|
|
async_module->callback(request);
|
2021-08-08 10:55:39 +00:00
|
|
|
return DNN_ASYNC_SUCCESS;
|
2021-08-08 10:55:31 +00:00
|
|
|
}
|
|
|
|
|
2022-03-02 18:05:55 +00:00
|
|
|
int ff_dnn_async_module_cleanup(DNNAsyncExecModule *async_module)
|
2021-08-08 10:55:31 +00:00
|
|
|
{
|
2021-08-08 10:55:39 +00:00
|
|
|
void *status = 0;
|
2021-08-08 10:55:31 +00:00
|
|
|
if (!async_module) {
|
2022-03-02 18:05:55 +00:00
|
|
|
return AVERROR(EINVAL);
|
2021-08-08 10:55:31 +00:00
|
|
|
}
|
|
|
|
#if HAVE_PTHREAD_CANCEL
|
2021-08-08 10:55:39 +00:00
|
|
|
pthread_join(async_module->thread_id, &status);
|
|
|
|
if (status == DNN_ASYNC_FAIL) {
|
|
|
|
av_log(NULL, AV_LOG_ERROR, "Last Inference Failed.\n");
|
2022-03-02 18:05:55 +00:00
|
|
|
return DNN_GENERIC_ERROR;
|
2021-08-08 10:55:39 +00:00
|
|
|
}
|
2021-08-08 10:55:31 +00:00
|
|
|
#endif
|
|
|
|
async_module->start_inference = NULL;
|
|
|
|
async_module->callback = NULL;
|
|
|
|
async_module->args = NULL;
|
2022-03-02 18:05:56 +00:00
|
|
|
return 0;
|
2021-08-08 10:55:31 +00:00
|
|
|
}
|
|
|
|
|
2022-03-02 18:05:55 +00:00
|
|
|
int ff_dnn_start_inference_async(void *ctx, DNNAsyncExecModule *async_module)
|
2021-08-08 10:55:31 +00:00
|
|
|
{
|
|
|
|
int ret;
|
2021-08-08 10:55:39 +00:00
|
|
|
void *status = 0;
|
2021-08-08 10:55:31 +00:00
|
|
|
|
|
|
|
if (!async_module) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "async_module is null when starting async inference.\n");
|
2022-03-02 18:05:55 +00:00
|
|
|
return AVERROR(EINVAL);
|
2021-08-08 10:55:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if HAVE_PTHREAD_CANCEL
|
2021-08-08 10:55:39 +00:00
|
|
|
pthread_join(async_module->thread_id, &status);
|
|
|
|
if (status == DNN_ASYNC_FAIL) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Unable to start inference as previous inference failed.\n");
|
2022-03-02 18:05:55 +00:00
|
|
|
return DNN_GENERIC_ERROR;
|
2021-08-08 10:55:39 +00:00
|
|
|
}
|
2021-08-08 10:55:31 +00:00
|
|
|
ret = pthread_create(&async_module->thread_id, NULL, async_thread_routine, async_module);
|
|
|
|
if (ret != 0) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Unable to start async inference.\n");
|
2022-03-02 18:05:55 +00:00
|
|
|
return ret;
|
2021-08-08 10:55:31 +00:00
|
|
|
}
|
|
|
|
#else
|
2022-03-02 18:05:55 +00:00
|
|
|
ret = async_module->start_inference(async_module->args);
|
2022-03-02 18:05:56 +00:00
|
|
|
if (ret != 0) {
|
2022-03-02 18:05:55 +00:00
|
|
|
return ret;
|
2021-08-08 10:55:31 +00:00
|
|
|
}
|
|
|
|
async_module->callback(async_module->args);
|
|
|
|
#endif
|
2022-03-02 18:05:56 +00:00
|
|
|
return 0;
|
2021-08-08 10:55:31 +00:00
|
|
|
}
|
2021-08-08 10:55:32 +00:00
|
|
|
|
2021-08-25 21:10:45 +00:00
|
|
|
DNNAsyncStatusType ff_dnn_get_result_common(Queue *task_queue, AVFrame **in, AVFrame **out)
|
2021-08-08 10:55:32 +00:00
|
|
|
{
|
|
|
|
TaskItem *task = ff_queue_peek_front(task_queue);
|
|
|
|
|
|
|
|
if (!task) {
|
|
|
|
return DAST_EMPTY_QUEUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (task->inference_done != task->inference_todo) {
|
|
|
|
return DAST_NOT_READY;
|
|
|
|
}
|
|
|
|
|
|
|
|
*in = task->in_frame;
|
|
|
|
*out = task->out_frame;
|
|
|
|
ff_queue_pop_front(task_queue);
|
|
|
|
av_freep(&task);
|
|
|
|
|
|
|
|
return DAST_SUCCESS;
|
|
|
|
}
|
2021-08-08 10:55:37 +00:00
|
|
|
|
2022-03-02 18:05:55 +00:00
|
|
|
int ff_dnn_fill_gettingoutput_task(TaskItem *task, DNNExecBaseParams *exec_params, void *backend_model, int input_height, int input_width, void *ctx)
|
2021-08-08 10:55:37 +00:00
|
|
|
{
|
|
|
|
AVFrame *in_frame = NULL;
|
|
|
|
AVFrame *out_frame = NULL;
|
|
|
|
|
|
|
|
in_frame = av_frame_alloc();
|
|
|
|
if (!in_frame) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for input frame\n");
|
2022-03-02 18:05:55 +00:00
|
|
|
return AVERROR(ENOMEM);
|
2021-08-08 10:55:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
out_frame = av_frame_alloc();
|
|
|
|
if (!out_frame) {
|
|
|
|
av_frame_free(&in_frame);
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for output frame\n");
|
2022-03-02 18:05:55 +00:00
|
|
|
return AVERROR(ENOMEM);
|
2021-08-08 10:55:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
in_frame->width = input_width;
|
|
|
|
in_frame->height = input_height;
|
|
|
|
exec_params->in_frame = in_frame;
|
|
|
|
exec_params->out_frame = out_frame;
|
|
|
|
|
|
|
|
return ff_dnn_fill_task(task, exec_params, backend_model, 0, 0);
|
|
|
|
}
|