2020-05-25 07:38:09 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020
|
|
|
|
*
|
|
|
|
* 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 OpenVINO backend implementation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "dnn_backend_openvino.h"
|
2020-08-28 04:51:44 +00:00
|
|
|
#include "dnn_io_proc.h"
|
2020-05-25 07:38:09 +00:00
|
|
|
#include "libavformat/avio.h"
|
|
|
|
#include "libavutil/avassert.h"
|
2021-06-12 20:10:11 +00:00
|
|
|
#include "libavutil/cpu.h"
|
2020-09-09 01:52:18 +00:00
|
|
|
#include "libavutil/opt.h"
|
|
|
|
#include "libavutil/avstring.h"
|
2021-03-16 05:02:56 +00:00
|
|
|
#include "libavutil/detection_bbox.h"
|
2020-09-09 01:52:18 +00:00
|
|
|
#include "../internal.h"
|
2020-11-18 06:30:11 +00:00
|
|
|
#include "safe_queue.h"
|
2020-05-25 07:38:09 +00:00
|
|
|
#include <c_api/ie_c_api.h>
|
2021-04-01 02:06:06 +00:00
|
|
|
#include "dnn_backend_common.h"
|
2020-05-25 07:38:09 +00:00
|
|
|
|
2020-09-09 01:52:18 +00:00
|
|
|
typedef struct OVOptions{
|
|
|
|
char *device_type;
|
2020-11-18 06:30:11 +00:00
|
|
|
int nireq;
|
2021-08-25 21:10:45 +00:00
|
|
|
uint8_t async;
|
2021-01-07 03:14:10 +00:00
|
|
|
int batch_size;
|
2021-01-18 03:42:14 +00:00
|
|
|
int input_resizable;
|
2020-09-09 01:52:18 +00:00
|
|
|
} OVOptions;
|
|
|
|
|
2020-08-27 04:17:21 +00:00
|
|
|
typedef struct OVContext {
|
|
|
|
const AVClass *class;
|
2020-09-09 01:52:18 +00:00
|
|
|
OVOptions options;
|
2020-08-27 04:17:21 +00:00
|
|
|
} OVContext;
|
|
|
|
|
2020-05-25 07:38:09 +00:00
|
|
|
typedef struct OVModel{
|
2020-08-27 04:17:21 +00:00
|
|
|
OVContext ctx;
|
2020-08-28 04:51:44 +00:00
|
|
|
DNNModel *model;
|
2020-05-25 07:38:09 +00:00
|
|
|
ie_core_t *core;
|
|
|
|
ie_network_t *network;
|
|
|
|
ie_executable_network_t *exe_network;
|
2021-07-11 16:15:02 +00:00
|
|
|
SafeQueue *request_queue; // holds OVRequestItem
|
2021-01-18 08:09:56 +00:00
|
|
|
Queue *task_queue; // holds TaskItem
|
2021-08-25 21:10:48 +00:00
|
|
|
Queue *lltask_queue; // holds LastLevelTaskItem
|
2022-07-21 09:41:03 +00:00
|
|
|
const char *all_input_names;
|
|
|
|
const char *all_output_names;
|
2020-05-25 07:38:09 +00:00
|
|
|
} OVModel;
|
|
|
|
|
2021-03-15 02:13:14 +00:00
|
|
|
// one request for one call to openvino
|
2021-07-11 16:15:02 +00:00
|
|
|
typedef struct OVRequestItem {
|
2020-11-17 12:55:13 +00:00
|
|
|
ie_infer_request_t *infer_request;
|
2021-08-25 21:10:48 +00:00
|
|
|
LastLevelTaskItem **lltasks;
|
|
|
|
uint32_t lltask_count;
|
2020-11-18 06:30:11 +00:00
|
|
|
ie_complete_call_back_t callback;
|
2021-07-11 16:15:02 +00:00
|
|
|
} OVRequestItem;
|
2020-11-17 12:55:13 +00:00
|
|
|
|
2020-09-09 01:52:18 +00:00
|
|
|
#define APPEND_STRING(generated_string, iterate_string) \
|
|
|
|
generated_string = generated_string ? av_asprintf("%s %s", generated_string, iterate_string) : \
|
|
|
|
av_asprintf("%s", iterate_string);
|
|
|
|
|
|
|
|
#define OFFSET(x) offsetof(OVContext, x)
|
|
|
|
#define FLAGS AV_OPT_FLAG_FILTERING_PARAM
|
|
|
|
static const AVOption dnn_openvino_options[] = {
|
|
|
|
{ "device", "device to run model", OFFSET(options.device_type), AV_OPT_TYPE_STRING, { .str = "CPU" }, 0, 0, FLAGS },
|
2021-07-05 10:30:55 +00:00
|
|
|
DNN_BACKEND_COMMON_OPTIONS
|
2021-01-07 03:14:10 +00:00
|
|
|
{ "batch_size", "batch size per request", OFFSET(options.batch_size), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, 1000, FLAGS},
|
2021-01-18 03:42:14 +00:00
|
|
|
{ "input_resizable", "can input be resizable or not", OFFSET(options.input_resizable), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
|
2020-09-09 01:52:18 +00:00
|
|
|
{ NULL }
|
2020-08-27 04:17:21 +00:00
|
|
|
};
|
|
|
|
|
2020-09-09 01:52:18 +00:00
|
|
|
AVFILTER_DEFINE_CLASS(dnn_openvino);
|
|
|
|
|
2020-05-25 07:38:09 +00:00
|
|
|
static DNNDataType precision_to_datatype(precision_e precision)
|
|
|
|
{
|
|
|
|
switch (precision)
|
|
|
|
{
|
|
|
|
case FP32:
|
|
|
|
return DNN_FLOAT;
|
2021-02-07 07:03:43 +00:00
|
|
|
case U8:
|
|
|
|
return DNN_UINT8;
|
2020-05-25 07:38:09 +00:00
|
|
|
default:
|
|
|
|
av_assert0(!"not supported yet.");
|
|
|
|
return DNN_FLOAT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-07 03:14:10 +00:00
|
|
|
static int get_datatype_size(DNNDataType dt)
|
|
|
|
{
|
|
|
|
switch (dt)
|
|
|
|
{
|
|
|
|
case DNN_FLOAT:
|
|
|
|
return sizeof(float);
|
2021-02-07 07:03:43 +00:00
|
|
|
case DNN_UINT8:
|
|
|
|
return sizeof(uint8_t);
|
2021-01-07 03:14:10 +00:00
|
|
|
default:
|
|
|
|
av_assert0(!"not supported yet.");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-02 18:05:52 +00:00
|
|
|
static int fill_model_input_ov(OVModel *ov_model, OVRequestItem *request)
|
2020-11-17 12:55:13 +00:00
|
|
|
{
|
|
|
|
dimensions_t dims;
|
|
|
|
precision_e precision;
|
|
|
|
ie_blob_buffer_t blob_buffer;
|
|
|
|
OVContext *ctx = &ov_model->ctx;
|
|
|
|
IEStatusCode status;
|
|
|
|
DNNData input;
|
|
|
|
ie_blob_t *input_blob = NULL;
|
2021-08-25 21:10:48 +00:00
|
|
|
LastLevelTaskItem *lltask;
|
2021-03-15 02:13:14 +00:00
|
|
|
TaskItem *task;
|
|
|
|
|
2021-08-25 21:10:48 +00:00
|
|
|
lltask = ff_queue_peek_front(ov_model->lltask_queue);
|
|
|
|
av_assert0(lltask);
|
|
|
|
task = lltask->task;
|
2020-11-17 12:55:13 +00:00
|
|
|
|
|
|
|
status = ie_infer_request_get_blob(request->infer_request, task->input_name, &input_blob);
|
|
|
|
if (status != OK) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Failed to get input blob with name %s\n", task->input_name);
|
2022-03-02 18:05:52 +00:00
|
|
|
return DNN_GENERIC_ERROR;
|
2020-11-17 12:55:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
status |= ie_blob_get_dims(input_blob, &dims);
|
|
|
|
status |= ie_blob_get_precision(input_blob, &precision);
|
|
|
|
if (status != OK) {
|
2021-03-13 05:52:21 +00:00
|
|
|
ie_blob_free(&input_blob);
|
2020-11-17 12:55:13 +00:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "Failed to get input blob dims/precision\n");
|
2022-03-02 18:05:52 +00:00
|
|
|
return DNN_GENERIC_ERROR;
|
2020-11-17 12:55:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
status = ie_blob_get_buffer(input_blob, &blob_buffer);
|
|
|
|
if (status != OK) {
|
2021-03-13 05:52:21 +00:00
|
|
|
ie_blob_free(&input_blob);
|
2020-11-17 12:55:13 +00:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "Failed to get input blob buffer\n");
|
2022-03-02 18:05:52 +00:00
|
|
|
return DNN_GENERIC_ERROR;
|
2020-11-17 12:55:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
input.height = dims.dims[2];
|
|
|
|
input.width = dims.dims[3];
|
|
|
|
input.channels = dims.dims[1];
|
|
|
|
input.data = blob_buffer.buffer;
|
|
|
|
input.dt = precision_to_datatype(precision);
|
2021-02-07 07:03:43 +00:00
|
|
|
// all models in openvino open model zoo use BGR as input,
|
|
|
|
// change to be an option when necessary.
|
|
|
|
input.order = DCO_BGR;
|
2021-01-07 03:14:10 +00:00
|
|
|
|
2021-03-15 02:13:14 +00:00
|
|
|
for (int i = 0; i < ctx->options.batch_size; ++i) {
|
2021-08-25 21:10:48 +00:00
|
|
|
lltask = ff_queue_pop_front(ov_model->lltask_queue);
|
|
|
|
if (!lltask) {
|
2021-03-15 02:13:14 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-08-25 21:10:48 +00:00
|
|
|
request->lltasks[i] = lltask;
|
|
|
|
request->lltask_count = i + 1;
|
|
|
|
task = lltask->task;
|
2021-06-05 18:08:02 +00:00
|
|
|
switch (ov_model->model->func_type) {
|
2021-03-16 05:02:56 +00:00
|
|
|
case DFT_PROCESS_FRAME:
|
|
|
|
if (task->do_ioproc) {
|
|
|
|
if (ov_model->model->frame_pre_proc != NULL) {
|
|
|
|
ov_model->model->frame_pre_proc(task->in_frame, &input, ov_model->model->filter_ctx);
|
|
|
|
} else {
|
2021-05-17 02:31:16 +00:00
|
|
|
ff_proc_from_frame_to_dnn(task->in_frame, &input, ctx);
|
2021-03-16 05:02:56 +00:00
|
|
|
}
|
2021-01-07 03:14:10 +00:00
|
|
|
}
|
2021-03-16 05:02:56 +00:00
|
|
|
break;
|
2021-05-17 02:31:16 +00:00
|
|
|
case DFT_ANALYTICS_DETECT:
|
|
|
|
ff_frame_to_dnn_detect(task->in_frame, &input, ctx);
|
|
|
|
break;
|
2021-03-16 05:02:56 +00:00
|
|
|
case DFT_ANALYTICS_CLASSIFY:
|
2021-08-25 21:10:48 +00:00
|
|
|
ff_frame_to_dnn_classify(task->in_frame, &input, lltask->bbox_index, ctx);
|
2021-03-16 05:02:56 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
av_assert0(!"should not reach here");
|
|
|
|
break;
|
2020-11-17 12:55:13 +00:00
|
|
|
}
|
2021-01-07 03:14:10 +00:00
|
|
|
input.data = (uint8_t *)input.data
|
|
|
|
+ input.width * input.height * input.channels * get_datatype_size(input.dt);
|
2020-11-17 12:55:13 +00:00
|
|
|
}
|
|
|
|
ie_blob_free(&input_blob);
|
|
|
|
|
2022-03-02 18:05:56 +00:00
|
|
|
return 0;
|
2020-11-17 12:55:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void infer_completion_callback(void *args)
|
|
|
|
{
|
|
|
|
dimensions_t dims;
|
|
|
|
precision_e precision;
|
|
|
|
IEStatusCode status;
|
2021-07-11 16:15:02 +00:00
|
|
|
OVRequestItem *request = args;
|
2021-08-25 21:10:48 +00:00
|
|
|
LastLevelTaskItem *lltask = request->lltasks[0];
|
|
|
|
TaskItem *task = lltask->task;
|
2021-06-05 18:08:02 +00:00
|
|
|
OVModel *ov_model = task->model;
|
|
|
|
SafeQueue *requestq = ov_model->request_queue;
|
2020-11-17 12:55:13 +00:00
|
|
|
ie_blob_t *output_blob = NULL;
|
|
|
|
ie_blob_buffer_t blob_buffer;
|
|
|
|
DNNData output;
|
2021-06-05 18:08:02 +00:00
|
|
|
OVContext *ctx = &ov_model->ctx;
|
2020-11-17 12:55:13 +00:00
|
|
|
|
2021-06-05 18:08:03 +00:00
|
|
|
status = ie_infer_request_get_blob(request->infer_request, task->output_names[0], &output_blob);
|
2020-11-17 12:55:13 +00:00
|
|
|
if (status != OK) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR,
|
|
|
|
"output \"%s\" may not correct, all output(s) are: \"%s\"\n",
|
2022-07-21 09:41:03 +00:00
|
|
|
task->output_names[0], ov_model->all_output_names);
|
2020-11-17 12:55:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = ie_blob_get_buffer(output_blob, &blob_buffer);
|
|
|
|
if (status != OK) {
|
2021-03-13 05:52:21 +00:00
|
|
|
ie_blob_free(&output_blob);
|
2020-11-17 12:55:13 +00:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "Failed to access output memory\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
status |= ie_blob_get_dims(output_blob, &dims);
|
|
|
|
status |= ie_blob_get_precision(output_blob, &precision);
|
|
|
|
if (status != OK) {
|
2021-03-13 05:52:21 +00:00
|
|
|
ie_blob_free(&output_blob);
|
2020-11-17 12:55:13 +00:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "Failed to get dims or precision of output\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
output.channels = dims.dims[1];
|
|
|
|
output.height = dims.dims[2];
|
|
|
|
output.width = dims.dims[3];
|
|
|
|
output.dt = precision_to_datatype(precision);
|
|
|
|
output.data = blob_buffer.buffer;
|
2021-01-07 03:14:10 +00:00
|
|
|
|
2021-08-25 21:10:48 +00:00
|
|
|
av_assert0(request->lltask_count <= dims.dims[0]);
|
|
|
|
av_assert0(request->lltask_count >= 1);
|
|
|
|
for (int i = 0; i < request->lltask_count; ++i) {
|
|
|
|
task = request->lltasks[i]->task;
|
2021-03-09 06:51:42 +00:00
|
|
|
|
2021-06-05 18:08:02 +00:00
|
|
|
switch (ov_model->model->func_type) {
|
2021-03-09 06:51:42 +00:00
|
|
|
case DFT_PROCESS_FRAME:
|
|
|
|
if (task->do_ioproc) {
|
2021-06-05 18:08:02 +00:00
|
|
|
if (ov_model->model->frame_post_proc != NULL) {
|
|
|
|
ov_model->model->frame_post_proc(task->out_frame, &output, ov_model->model->filter_ctx);
|
2021-03-09 06:51:42 +00:00
|
|
|
} else {
|
|
|
|
ff_proc_from_dnn_to_frame(task->out_frame, &output, ctx);
|
|
|
|
}
|
2021-01-07 03:14:10 +00:00
|
|
|
} else {
|
2021-03-09 06:51:42 +00:00
|
|
|
task->out_frame->width = output.width;
|
|
|
|
task->out_frame->height = output.height;
|
2021-01-07 03:14:10 +00:00
|
|
|
}
|
2021-03-09 06:51:42 +00:00
|
|
|
break;
|
|
|
|
case DFT_ANALYTICS_DETECT:
|
2021-06-05 18:08:02 +00:00
|
|
|
if (!ov_model->model->detect_post_proc) {
|
2021-03-09 06:51:42 +00:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "detect filter needs to provide post proc\n");
|
|
|
|
return;
|
|
|
|
}
|
2021-08-25 21:10:45 +00:00
|
|
|
ov_model->model->detect_post_proc(task->in_frame, &output, 1, ov_model->model->filter_ctx);
|
2021-03-09 06:51:42 +00:00
|
|
|
break;
|
2021-03-16 05:02:56 +00:00
|
|
|
case DFT_ANALYTICS_CLASSIFY:
|
2021-06-05 18:08:02 +00:00
|
|
|
if (!ov_model->model->classify_post_proc) {
|
2021-03-16 05:02:56 +00:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "classify filter needs to provide post proc\n");
|
|
|
|
return;
|
|
|
|
}
|
2021-08-25 21:10:48 +00:00
|
|
|
ov_model->model->classify_post_proc(task->in_frame, &output, request->lltasks[i]->bbox_index, ov_model->model->filter_ctx);
|
2021-03-16 05:02:56 +00:00
|
|
|
break;
|
2021-03-09 06:51:42 +00:00
|
|
|
default:
|
|
|
|
av_assert0(!"should not reach here");
|
|
|
|
break;
|
2020-11-17 12:55:13 +00:00
|
|
|
}
|
2021-03-09 06:51:42 +00:00
|
|
|
|
2022-12-16 09:37:27 +00:00
|
|
|
task->inference_done++;
|
2021-08-25 21:10:48 +00:00
|
|
|
av_freep(&request->lltasks[i]);
|
2021-01-07 03:14:10 +00:00
|
|
|
output.data = (uint8_t *)output.data
|
|
|
|
+ output.width * output.height * output.channels * get_datatype_size(output.dt);
|
2020-11-17 12:55:13 +00:00
|
|
|
}
|
|
|
|
ie_blob_free(&output_blob);
|
2020-11-18 06:30:11 +00:00
|
|
|
|
2021-08-25 21:10:48 +00:00
|
|
|
request->lltask_count = 0;
|
2021-03-10 08:27:41 +00:00
|
|
|
if (ff_safe_queue_push_back(requestq, request) < 0) {
|
2021-06-14 17:56:16 +00:00
|
|
|
ie_infer_request_free(&request->infer_request);
|
|
|
|
av_freep(&request);
|
2021-03-10 08:27:41 +00:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "Failed to push back request_queue.\n");
|
|
|
|
return;
|
2020-11-18 06:30:11 +00:00
|
|
|
}
|
2020-11-17 12:55:13 +00:00
|
|
|
}
|
|
|
|
|
2022-03-02 18:05:52 +00:00
|
|
|
static int init_model_ov(OVModel *ov_model, const char *input_name, const char *output_name)
|
2021-01-18 03:42:13 +00:00
|
|
|
{
|
2022-03-02 18:05:56 +00:00
|
|
|
int ret = 0;
|
2021-01-18 03:42:13 +00:00
|
|
|
OVContext *ctx = &ov_model->ctx;
|
|
|
|
IEStatusCode status;
|
|
|
|
ie_available_devices_t a_dev;
|
|
|
|
ie_config_t config = {NULL, NULL, NULL};
|
|
|
|
char *all_dev_names = NULL;
|
|
|
|
|
|
|
|
// batch size
|
|
|
|
if (ctx->options.batch_size <= 0) {
|
|
|
|
ctx->options.batch_size = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx->options.batch_size > 1) {
|
|
|
|
input_shapes_t input_shapes;
|
|
|
|
status = ie_network_get_input_shapes(ov_model->network, &input_shapes);
|
2022-03-02 18:05:52 +00:00
|
|
|
if (status != OK) {
|
|
|
|
ret = DNN_GENERIC_ERROR;
|
2021-01-18 03:42:13 +00:00
|
|
|
goto err;
|
2022-03-02 18:05:52 +00:00
|
|
|
}
|
2021-01-18 03:42:13 +00:00
|
|
|
for (int i = 0; i < input_shapes.shape_num; i++)
|
|
|
|
input_shapes.shapes[i].shape.dims[0] = ctx->options.batch_size;
|
|
|
|
status = ie_network_reshape(ov_model->network, input_shapes);
|
|
|
|
ie_network_input_shapes_free(&input_shapes);
|
2022-03-02 18:05:52 +00:00
|
|
|
if (status != OK) {
|
|
|
|
ret = DNN_GENERIC_ERROR;
|
2021-01-18 03:42:13 +00:00
|
|
|
goto err;
|
2022-03-02 18:05:52 +00:00
|
|
|
}
|
2021-01-18 03:42:13 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 01:22:23 +00:00
|
|
|
// The order of dims in the openvino is fixed and it is always NCHW for 4-D data.
|
|
|
|
// while we pass NHWC data from FFmpeg to openvino
|
|
|
|
status = ie_network_set_input_layout(ov_model->network, input_name, NHWC);
|
|
|
|
if (status != OK) {
|
2022-07-21 09:41:03 +00:00
|
|
|
if (status == NOT_FOUND) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Could not find \"%s\" in model, failed to set input layout as NHWC, "\
|
|
|
|
"all input(s) are: \"%s\"\n", input_name, ov_model->all_input_names);
|
|
|
|
} else{
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Failed to set layout as NHWC for input %s\n", input_name);
|
|
|
|
}
|
2022-03-02 18:05:52 +00:00
|
|
|
ret = DNN_GENERIC_ERROR;
|
2021-02-08 01:22:23 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
status = ie_network_set_output_layout(ov_model->network, output_name, NHWC);
|
|
|
|
if (status != OK) {
|
2022-07-21 09:41:03 +00:00
|
|
|
if (status == NOT_FOUND) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Could not find \"%s\" in model, failed to set output layout as NHWC, "\
|
|
|
|
"all output(s) are: \"%s\"\n", input_name, ov_model->all_output_names);
|
|
|
|
} else{
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Failed to set layout as NHWC for output %s\n", output_name);
|
|
|
|
}
|
2022-03-02 18:05:52 +00:00
|
|
|
ret = DNN_GENERIC_ERROR;
|
2021-02-08 01:22:23 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2021-02-07 07:03:43 +00:00
|
|
|
// all models in openvino open model zoo use BGR with range [0.0f, 255.0f] as input,
|
2021-04-23 10:56:51 +00:00
|
|
|
// we don't have a AVPixelFormat to describe it, so we'll use AV_PIX_FMT_BGR24 and
|
2021-02-07 07:03:43 +00:00
|
|
|
// ask openvino to do the conversion internally.
|
|
|
|
// the current supported SR model (frame processing) is generated from tensorflow model,
|
|
|
|
// and its input is Y channel as float with range [0.0f, 1.0f], so do not set for this case.
|
|
|
|
// TODO: we need to get a final clear&general solution with all backends/formats considered.
|
|
|
|
if (ov_model->model->func_type != DFT_PROCESS_FRAME) {
|
|
|
|
status = ie_network_set_input_precision(ov_model->network, input_name, U8);
|
|
|
|
if (status != OK) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Failed to set input precision as U8 for %s\n", input_name);
|
2022-03-02 18:05:52 +00:00
|
|
|
ret = DNN_GENERIC_ERROR;
|
2021-03-13 05:56:40 +00:00
|
|
|
goto err;
|
2021-02-07 07:03:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-18 03:42:13 +00:00
|
|
|
status = ie_core_load_network(ov_model->core, ov_model->network, ctx->options.device_type, &config, &ov_model->exe_network);
|
|
|
|
if (status != OK) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Failed to load OpenVINO model network\n");
|
|
|
|
status = ie_core_get_available_devices(ov_model->core, &a_dev);
|
|
|
|
if (status != OK) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Failed to get available devices\n");
|
2022-03-02 18:05:52 +00:00
|
|
|
ret = DNN_GENERIC_ERROR;
|
2021-01-18 03:42:13 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < a_dev.num_devices; i++) {
|
|
|
|
APPEND_STRING(all_dev_names, a_dev.devices[i])
|
|
|
|
}
|
|
|
|
av_log(ctx, AV_LOG_ERROR,"device %s may not be supported, all available devices are: \"%s\"\n",
|
|
|
|
ctx->options.device_type, all_dev_names);
|
2022-03-02 18:05:52 +00:00
|
|
|
ret = AVERROR(ENODEV);
|
2021-01-18 03:42:13 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
// create infer_requests for async execution
|
|
|
|
if (ctx->options.nireq <= 0) {
|
|
|
|
// the default value is a rough estimation
|
|
|
|
ctx->options.nireq = av_cpu_count() / 2 + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ov_model->request_queue = ff_safe_queue_create();
|
|
|
|
if (!ov_model->request_queue) {
|
2022-03-02 18:05:52 +00:00
|
|
|
ret = AVERROR(ENOMEM);
|
2021-01-18 03:42:13 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < ctx->options.nireq; i++) {
|
2021-07-11 16:15:02 +00:00
|
|
|
OVRequestItem *item = av_mallocz(sizeof(*item));
|
2021-01-18 03:42:13 +00:00
|
|
|
if (!item) {
|
2022-03-02 18:05:52 +00:00
|
|
|
ret = AVERROR(ENOMEM);
|
2021-01-18 03:42:13 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2021-03-13 06:09:33 +00:00
|
|
|
item->callback.completeCallBackFunc = infer_completion_callback;
|
|
|
|
item->callback.args = item;
|
|
|
|
if (ff_safe_queue_push_back(ov_model->request_queue, item) < 0) {
|
|
|
|
av_freep(&item);
|
2022-03-02 18:05:52 +00:00
|
|
|
ret = AVERROR(ENOMEM);
|
2021-03-13 06:09:33 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2021-01-18 03:42:13 +00:00
|
|
|
status = ie_exec_network_create_infer_request(ov_model->exe_network, &item->infer_request);
|
|
|
|
if (status != OK) {
|
2022-03-02 18:05:52 +00:00
|
|
|
ret = DNN_GENERIC_ERROR;
|
2021-01-18 03:42:13 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2021-08-25 21:10:48 +00:00
|
|
|
item->lltasks = av_malloc_array(ctx->options.batch_size, sizeof(*item->lltasks));
|
|
|
|
if (!item->lltasks) {
|
2022-03-02 18:05:52 +00:00
|
|
|
ret = AVERROR(ENOMEM);
|
2021-01-18 03:42:13 +00:00
|
|
|
goto err;
|
|
|
|
}
|
2021-08-25 21:10:48 +00:00
|
|
|
item->lltask_count = 0;
|
2021-01-18 03:42:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ov_model->task_queue = ff_queue_create();
|
|
|
|
if (!ov_model->task_queue) {
|
2022-03-02 18:05:52 +00:00
|
|
|
ret = AVERROR(ENOMEM);
|
2021-01-18 03:42:13 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2021-08-25 21:10:48 +00:00
|
|
|
ov_model->lltask_queue = ff_queue_create();
|
|
|
|
if (!ov_model->lltask_queue) {
|
2022-03-02 18:05:52 +00:00
|
|
|
ret = AVERROR(ENOMEM);
|
2021-03-15 02:13:14 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2022-03-02 18:05:56 +00:00
|
|
|
return 0;
|
2021-01-18 03:42:13 +00:00
|
|
|
|
|
|
|
err:
|
|
|
|
ff_dnn_free_model_ov(&ov_model->model);
|
2022-03-02 18:05:52 +00:00
|
|
|
return ret;
|
2021-01-18 03:42:13 +00:00
|
|
|
}
|
|
|
|
|
2022-03-02 18:05:52 +00:00
|
|
|
static int execute_model_ov(OVRequestItem *request, Queue *inferenceq)
|
2020-11-17 12:55:13 +00:00
|
|
|
{
|
|
|
|
IEStatusCode status;
|
2021-08-25 21:10:48 +00:00
|
|
|
LastLevelTaskItem *lltask;
|
2022-03-02 18:05:56 +00:00
|
|
|
int ret = 0;
|
2021-03-15 02:13:14 +00:00
|
|
|
TaskItem *task;
|
|
|
|
OVContext *ctx;
|
2021-06-05 18:08:02 +00:00
|
|
|
OVModel *ov_model;
|
2021-03-15 02:13:14 +00:00
|
|
|
|
|
|
|
if (ff_queue_size(inferenceq) == 0) {
|
2021-07-11 16:15:01 +00:00
|
|
|
ie_infer_request_free(&request->infer_request);
|
|
|
|
av_freep(&request);
|
2022-03-02 18:05:56 +00:00
|
|
|
return 0;
|
2021-03-15 02:13:14 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 21:10:48 +00:00
|
|
|
lltask = ff_queue_peek_front(inferenceq);
|
|
|
|
task = lltask->task;
|
2021-06-05 18:08:02 +00:00
|
|
|
ov_model = task->model;
|
|
|
|
ctx = &ov_model->ctx;
|
2020-11-17 12:55:13 +00:00
|
|
|
|
2020-11-18 06:30:11 +00:00
|
|
|
if (task->async) {
|
2021-06-05 18:08:02 +00:00
|
|
|
ret = fill_model_input_ov(ov_model, request);
|
2022-03-02 18:05:56 +00:00
|
|
|
if (ret != 0) {
|
2021-07-11 16:15:01 +00:00
|
|
|
goto err;
|
2021-01-07 03:14:10 +00:00
|
|
|
}
|
2020-11-18 06:30:11 +00:00
|
|
|
status = ie_infer_set_completion_callback(request->infer_request, &request->callback);
|
|
|
|
if (status != OK) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Failed to set completion callback for inference\n");
|
2022-03-02 18:05:52 +00:00
|
|
|
ret = DNN_GENERIC_ERROR;
|
2021-06-18 16:23:08 +00:00
|
|
|
goto err;
|
2020-11-18 06:30:11 +00:00
|
|
|
}
|
|
|
|
status = ie_infer_request_infer_async(request->infer_request);
|
|
|
|
if (status != OK) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Failed to start async inference\n");
|
2022-03-02 18:05:52 +00:00
|
|
|
ret = DNN_GENERIC_ERROR;
|
2021-06-18 16:23:08 +00:00
|
|
|
goto err;
|
2020-11-18 06:30:11 +00:00
|
|
|
}
|
2022-03-02 18:05:56 +00:00
|
|
|
return 0;
|
2020-11-18 06:30:11 +00:00
|
|
|
} else {
|
2021-06-05 18:08:02 +00:00
|
|
|
ret = fill_model_input_ov(ov_model, request);
|
2022-03-02 18:05:56 +00:00
|
|
|
if (ret != 0) {
|
2021-07-11 16:15:01 +00:00
|
|
|
goto err;
|
2021-01-07 03:14:10 +00:00
|
|
|
}
|
2020-11-18 06:30:11 +00:00
|
|
|
status = ie_infer_request_infer(request->infer_request);
|
|
|
|
if (status != OK) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Failed to start synchronous model inference\n");
|
2022-03-02 18:05:52 +00:00
|
|
|
ret = DNN_GENERIC_ERROR;
|
2021-06-18 16:23:08 +00:00
|
|
|
goto err;
|
2020-11-18 06:30:11 +00:00
|
|
|
}
|
|
|
|
infer_completion_callback(request);
|
2022-03-02 18:05:56 +00:00
|
|
|
return (task->inference_done == task->inference_todo) ? 0 : DNN_GENERIC_ERROR;
|
2020-11-17 12:55:13 +00:00
|
|
|
}
|
2021-06-18 16:23:08 +00:00
|
|
|
err:
|
|
|
|
if (ff_safe_queue_push_back(ov_model->request_queue, request) < 0) {
|
|
|
|
ie_infer_request_free(&request->infer_request);
|
|
|
|
av_freep(&request);
|
|
|
|
}
|
2022-03-02 18:05:52 +00:00
|
|
|
return ret;
|
2020-11-17 12:55:13 +00:00
|
|
|
}
|
|
|
|
|
2022-03-02 18:05:52 +00:00
|
|
|
static int get_input_ov(void *model, DNNData *input, const char *input_name)
|
2020-05-25 07:38:09 +00:00
|
|
|
{
|
2021-01-22 14:20:15 +00:00
|
|
|
OVModel *ov_model = model;
|
2020-08-27 04:17:21 +00:00
|
|
|
OVContext *ctx = &ov_model->ctx;
|
2020-05-25 07:38:09 +00:00
|
|
|
char *model_input_name = NULL;
|
|
|
|
IEStatusCode status;
|
|
|
|
size_t model_input_count = 0;
|
|
|
|
dimensions_t dims;
|
|
|
|
precision_e precision;
|
2021-01-18 03:42:14 +00:00
|
|
|
int input_resizable = ctx->options.input_resizable;
|
2020-05-25 07:38:09 +00:00
|
|
|
|
|
|
|
status = ie_network_get_inputs_number(ov_model->network, &model_input_count);
|
2020-08-27 04:17:21 +00:00
|
|
|
if (status != OK) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Failed to get input count\n");
|
2022-03-02 18:05:52 +00:00
|
|
|
return DNN_GENERIC_ERROR;
|
2020-08-27 04:17:21 +00:00
|
|
|
}
|
2020-05-25 07:38:09 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < model_input_count; i++) {
|
|
|
|
status = ie_network_get_input_name(ov_model->network, i, &model_input_name);
|
2020-08-27 04:17:21 +00:00
|
|
|
if (status != OK) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Failed to get No.%d input's name\n", (int)i);
|
2022-03-02 18:05:52 +00:00
|
|
|
return DNN_GENERIC_ERROR;
|
2020-08-27 04:17:21 +00:00
|
|
|
}
|
2020-05-25 07:38:09 +00:00
|
|
|
if (strcmp(model_input_name, input_name) == 0) {
|
|
|
|
ie_network_name_free(&model_input_name);
|
|
|
|
status |= ie_network_get_input_dims(ov_model->network, input_name, &dims);
|
|
|
|
status |= ie_network_get_input_precision(ov_model->network, input_name, &precision);
|
2020-08-27 04:17:21 +00:00
|
|
|
if (status != OK) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Failed to get No.%d input's dims or precision\n", (int)i);
|
2022-03-02 18:05:52 +00:00
|
|
|
return DNN_GENERIC_ERROR;
|
2020-08-27 04:17:21 +00:00
|
|
|
}
|
2020-05-25 07:38:09 +00:00
|
|
|
|
|
|
|
input->channels = dims.dims[1];
|
2021-01-18 03:42:14 +00:00
|
|
|
input->height = input_resizable ? -1 : dims.dims[2];
|
|
|
|
input->width = input_resizable ? -1 : dims.dims[3];
|
2020-05-25 07:38:09 +00:00
|
|
|
input->dt = precision_to_datatype(precision);
|
2022-03-02 18:05:56 +00:00
|
|
|
return 0;
|
2020-05-25 07:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ie_network_name_free(&model_input_name);
|
|
|
|
}
|
|
|
|
|
2022-07-21 09:41:03 +00:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "Could not find \"%s\" in model, all input(s) are: \"%s\"\n", input_name, ov_model->all_input_names);
|
2022-03-02 18:05:52 +00:00
|
|
|
return AVERROR(EINVAL);
|
2020-05-25 07:38:09 +00:00
|
|
|
}
|
|
|
|
|
2021-03-16 05:02:56 +00:00
|
|
|
static int contain_valid_detection_bbox(AVFrame *frame)
|
|
|
|
{
|
|
|
|
AVFrameSideData *sd;
|
|
|
|
const AVDetectionBBoxHeader *header;
|
|
|
|
const AVDetectionBBox *bbox;
|
|
|
|
|
|
|
|
sd = av_frame_get_side_data(frame, AV_FRAME_DATA_DETECTION_BBOXES);
|
|
|
|
if (!sd) { // this frame has nothing detected
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sd->size) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
header = (const AVDetectionBBoxHeader *)sd->data;
|
|
|
|
if (!header->nb_bboxes) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < header->nb_bboxes; i++) {
|
|
|
|
bbox = av_get_detection_bbox(header, i);
|
|
|
|
if (bbox->x < 0 || bbox->w < 0 || bbox->x + bbox->w >= frame->width) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (bbox->y < 0 || bbox->h < 0 || bbox->y + bbox->h >= frame->width) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bbox->classify_count == AV_NUM_DETECTION_BBOX_CLASSIFY) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-03-02 18:05:52 +00:00
|
|
|
static int extract_lltask_from_task(DNNFunctionType func_type, TaskItem *task, Queue *lltask_queue, DNNExecBaseParams *exec_params)
|
2021-03-15 02:13:14 +00:00
|
|
|
{
|
|
|
|
switch (func_type) {
|
|
|
|
case DFT_PROCESS_FRAME:
|
|
|
|
case DFT_ANALYTICS_DETECT:
|
|
|
|
{
|
2021-08-25 21:10:48 +00:00
|
|
|
LastLevelTaskItem *lltask = av_malloc(sizeof(*lltask));
|
|
|
|
if (!lltask) {
|
2022-03-02 18:05:52 +00:00
|
|
|
return AVERROR(ENOMEM);
|
2021-03-15 02:13:14 +00:00
|
|
|
}
|
|
|
|
task->inference_todo = 1;
|
|
|
|
task->inference_done = 0;
|
2021-08-25 21:10:48 +00:00
|
|
|
lltask->task = task;
|
|
|
|
if (ff_queue_push_back(lltask_queue, lltask) < 0) {
|
|
|
|
av_freep(&lltask);
|
2022-03-02 18:05:52 +00:00
|
|
|
return AVERROR(ENOMEM);
|
2021-03-15 02:13:14 +00:00
|
|
|
}
|
2022-03-02 18:05:56 +00:00
|
|
|
return 0;
|
2021-03-15 02:13:14 +00:00
|
|
|
}
|
2021-03-16 05:02:56 +00:00
|
|
|
case DFT_ANALYTICS_CLASSIFY:
|
|
|
|
{
|
|
|
|
const AVDetectionBBoxHeader *header;
|
|
|
|
AVFrame *frame = task->in_frame;
|
|
|
|
AVFrameSideData *sd;
|
|
|
|
DNNExecClassificationParams *params = (DNNExecClassificationParams *)exec_params;
|
|
|
|
|
|
|
|
task->inference_todo = 0;
|
|
|
|
task->inference_done = 0;
|
|
|
|
|
|
|
|
if (!contain_valid_detection_bbox(frame)) {
|
2022-03-02 18:05:56 +00:00
|
|
|
return 0;
|
2021-03-16 05:02:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sd = av_frame_get_side_data(frame, AV_FRAME_DATA_DETECTION_BBOXES);
|
|
|
|
header = (const AVDetectionBBoxHeader *)sd->data;
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < header->nb_bboxes; i++) {
|
2021-08-25 21:10:48 +00:00
|
|
|
LastLevelTaskItem *lltask;
|
2021-03-16 05:02:56 +00:00
|
|
|
const AVDetectionBBox *bbox = av_get_detection_bbox(header, i);
|
|
|
|
|
2021-06-13 13:27:13 +00:00
|
|
|
if (params->target) {
|
|
|
|
if (av_strncasecmp(bbox->detect_label, params->target, sizeof(bbox->detect_label)) != 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-03-16 05:02:56 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 21:10:48 +00:00
|
|
|
lltask = av_malloc(sizeof(*lltask));
|
|
|
|
if (!lltask) {
|
2022-03-02 18:05:52 +00:00
|
|
|
return AVERROR(ENOMEM);
|
2021-03-16 05:02:56 +00:00
|
|
|
}
|
|
|
|
task->inference_todo++;
|
2021-08-25 21:10:48 +00:00
|
|
|
lltask->task = task;
|
|
|
|
lltask->bbox_index = i;
|
|
|
|
if (ff_queue_push_back(lltask_queue, lltask) < 0) {
|
|
|
|
av_freep(&lltask);
|
2022-03-02 18:05:52 +00:00
|
|
|
return AVERROR(ENOMEM);
|
2021-03-16 05:02:56 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-02 18:05:56 +00:00
|
|
|
return 0;
|
2021-03-16 05:02:56 +00:00
|
|
|
}
|
2021-03-15 02:13:14 +00:00
|
|
|
default:
|
|
|
|
av_assert0(!"should not reach here");
|
2022-03-02 18:05:52 +00:00
|
|
|
return AVERROR(EINVAL);
|
2021-03-15 02:13:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-02 18:05:52 +00:00
|
|
|
static int get_output_ov(void *model, const char *input_name, int input_width, int input_height,
|
2020-09-11 14:15:04 +00:00
|
|
|
const char *output_name, int *output_width, int *output_height)
|
|
|
|
{
|
2022-03-02 18:05:52 +00:00
|
|
|
int ret;
|
2021-01-22 14:20:15 +00:00
|
|
|
OVModel *ov_model = model;
|
2020-10-14 00:59:44 +00:00
|
|
|
OVContext *ctx = &ov_model->ctx;
|
2020-11-17 12:55:13 +00:00
|
|
|
TaskItem task;
|
2021-07-11 16:15:02 +00:00
|
|
|
OVRequestItem *request;
|
2021-01-18 03:42:14 +00:00
|
|
|
IEStatusCode status;
|
|
|
|
input_shapes_t input_shapes;
|
2021-08-08 10:55:37 +00:00
|
|
|
DNNExecBaseParams exec_params = {
|
|
|
|
.input_name = input_name,
|
|
|
|
.output_names = &output_name,
|
|
|
|
.nb_output = 1,
|
|
|
|
.in_frame = NULL,
|
|
|
|
.out_frame = NULL,
|
|
|
|
};
|
2020-10-14 00:59:44 +00:00
|
|
|
|
2021-03-15 08:42:27 +00:00
|
|
|
if (ov_model->model->func_type != DFT_PROCESS_FRAME) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Get output dim only when processing frame.\n");
|
2022-03-02 18:05:52 +00:00
|
|
|
return AVERROR(EINVAL);
|
2021-03-15 08:42:27 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 03:42:14 +00:00
|
|
|
if (ctx->options.input_resizable) {
|
|
|
|
status = ie_network_get_input_shapes(ov_model->network, &input_shapes);
|
|
|
|
input_shapes.shapes->shape.dims[2] = input_height;
|
|
|
|
input_shapes.shapes->shape.dims[3] = input_width;
|
|
|
|
status |= ie_network_reshape(ov_model->network, input_shapes);
|
|
|
|
ie_network_input_shapes_free(&input_shapes);
|
|
|
|
if (status != OK) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Failed to reshape input size for %s\n", input_name);
|
2022-03-02 18:05:52 +00:00
|
|
|
return DNN_GENERIC_ERROR;
|
2021-01-18 03:42:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-18 03:42:13 +00:00
|
|
|
if (!ov_model->exe_network) {
|
2022-03-02 18:05:52 +00:00
|
|
|
ret = init_model_ov(ov_model, input_name, output_name);
|
2022-03-02 18:05:56 +00:00
|
|
|
if (ret != 0) {
|
2021-01-18 03:42:13 +00:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "Failed init OpenVINO exectuable network or inference request\n");
|
2022-03-02 18:05:52 +00:00
|
|
|
return ret;
|
2021-01-26 00:22:27 +00:00
|
|
|
}
|
2021-01-18 03:42:13 +00:00
|
|
|
}
|
|
|
|
|
2022-03-02 18:05:52 +00:00
|
|
|
ret = ff_dnn_fill_gettingoutput_task(&task, &exec_params, ov_model, input_height, input_width, ctx);
|
2022-03-02 18:05:56 +00:00
|
|
|
if (ret != 0) {
|
2022-03-02 18:05:52 +00:00
|
|
|
goto err;
|
2021-03-13 05:35:29 +00:00
|
|
|
}
|
2020-11-17 12:55:13 +00:00
|
|
|
|
2022-03-02 18:05:52 +00:00
|
|
|
ret = extract_lltask_from_task(ov_model->model->func_type, &task, ov_model->lltask_queue, NULL);
|
2022-03-02 18:05:56 +00:00
|
|
|
if (ret != 0) {
|
2022-03-02 18:05:52 +00:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "unable to extract inference from task.\n");
|
2021-08-08 10:55:37 +00:00
|
|
|
goto err;
|
2021-03-15 02:13:14 +00:00
|
|
|
}
|
|
|
|
|
2021-03-10 08:27:41 +00:00
|
|
|
request = ff_safe_queue_pop_front(ov_model->request_queue);
|
|
|
|
if (!request) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "unable to get infer request.\n");
|
2022-03-02 18:05:52 +00:00
|
|
|
ret = AVERROR(EINVAL);
|
2021-08-08 10:55:37 +00:00
|
|
|
goto err;
|
2021-03-10 08:27:41 +00:00
|
|
|
}
|
2020-11-17 12:55:13 +00:00
|
|
|
|
2021-08-25 21:10:48 +00:00
|
|
|
ret = execute_model_ov(request, ov_model->lltask_queue);
|
2021-08-08 10:55:37 +00:00
|
|
|
*output_width = task.out_frame->width;
|
|
|
|
*output_height = task.out_frame->height;
|
|
|
|
err:
|
|
|
|
av_frame_free(&task.out_frame);
|
|
|
|
av_frame_free(&task.in_frame);
|
2020-09-11 14:15:04 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-02-07 06:35:22 +00:00
|
|
|
DNNModel *ff_dnn_load_model_ov(const char *model_filename, DNNFunctionType func_type, const char *options, AVFilterContext *filter_ctx)
|
2020-05-25 07:38:09 +00:00
|
|
|
{
|
|
|
|
DNNModel *model = NULL;
|
|
|
|
OVModel *ov_model = NULL;
|
2020-09-09 01:52:18 +00:00
|
|
|
OVContext *ctx = NULL;
|
2020-05-25 07:38:09 +00:00
|
|
|
IEStatusCode status;
|
2022-07-21 09:41:03 +00:00
|
|
|
size_t node_count = 0;
|
|
|
|
char *node_name = NULL;
|
2020-05-25 07:38:09 +00:00
|
|
|
|
2020-08-28 04:51:44 +00:00
|
|
|
model = av_mallocz(sizeof(DNNModel));
|
2020-05-25 07:38:09 +00:00
|
|
|
if (!model){
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ov_model = av_mallocz(sizeof(OVModel));
|
2020-11-18 06:16:24 +00:00
|
|
|
if (!ov_model) {
|
|
|
|
av_freep(&model);
|
|
|
|
return NULL;
|
|
|
|
}
|
2021-01-22 14:20:15 +00:00
|
|
|
model->model = ov_model;
|
2020-08-28 04:51:44 +00:00
|
|
|
ov_model->model = model;
|
2020-08-27 04:17:21 +00:00
|
|
|
ov_model->ctx.class = &dnn_openvino_class;
|
2020-09-09 01:52:18 +00:00
|
|
|
ctx = &ov_model->ctx;
|
2022-07-21 09:41:03 +00:00
|
|
|
ov_model->all_input_names = NULL;
|
|
|
|
ov_model->all_output_names = NULL;
|
2020-09-09 01:52:18 +00:00
|
|
|
|
|
|
|
//parse options
|
|
|
|
av_opt_set_defaults(ctx);
|
|
|
|
if (av_opt_set_from_string(ctx, options, NULL, "=", "&") < 0) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Failed to parse options \"%s\"\n", options);
|
|
|
|
goto err;
|
|
|
|
}
|
2020-05-25 07:38:09 +00:00
|
|
|
|
|
|
|
status = ie_core_create("", &ov_model->core);
|
|
|
|
if (status != OK)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
status = ie_core_read_network(ov_model->core, model_filename, NULL, &ov_model->network);
|
2021-01-18 07:27:23 +00:00
|
|
|
if (status != OK) {
|
|
|
|
ie_version_t ver;
|
|
|
|
ver = ie_c_api_version();
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Failed to read the network from model file %s,\n"
|
|
|
|
"Please check if the model version matches the runtime OpenVINO %s\n",
|
|
|
|
model_filename, ver.api_version);
|
|
|
|
ie_version_free(&ver);
|
2020-05-25 07:38:09 +00:00
|
|
|
goto err;
|
2021-01-18 07:27:23 +00:00
|
|
|
}
|
2020-05-25 07:38:09 +00:00
|
|
|
|
2022-07-21 09:41:03 +00:00
|
|
|
//get all the input and output names
|
|
|
|
status = ie_network_get_inputs_number(ov_model->network, &node_count);
|
|
|
|
if (status != OK) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Failed to get input count\n");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
for (size_t i = 0; i < node_count; i++) {
|
|
|
|
status = ie_network_get_input_name(ov_model->network, i, &node_name);
|
|
|
|
if (status != OK) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Failed to get No.%d input's name\n", (int)i);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
APPEND_STRING(ov_model->all_input_names, node_name)
|
|
|
|
}
|
|
|
|
status = ie_network_get_outputs_number(ov_model->network, &node_count);
|
|
|
|
if (status != OK) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Failed to get output count\n");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
for (size_t i = 0; i < node_count; i++) {
|
|
|
|
status = ie_network_get_output_name(ov_model->network, i, &node_name);
|
|
|
|
if (status != OK) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Failed to get No.%d output's name\n", (int)i);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
APPEND_STRING(ov_model->all_output_names, node_name)
|
|
|
|
}
|
|
|
|
|
2020-05-25 07:38:09 +00:00
|
|
|
model->get_input = &get_input_ov;
|
2020-09-11 14:15:04 +00:00
|
|
|
model->get_output = &get_output_ov;
|
2020-08-07 06:32:55 +00:00
|
|
|
model->options = options;
|
2020-11-18 06:54:10 +00:00
|
|
|
model->filter_ctx = filter_ctx;
|
2021-02-07 06:35:22 +00:00
|
|
|
model->func_type = func_type;
|
2020-05-25 07:38:09 +00:00
|
|
|
|
|
|
|
return model;
|
|
|
|
|
|
|
|
err:
|
2020-11-18 06:16:24 +00:00
|
|
|
ff_dnn_free_model_ov(&model);
|
2020-05-25 07:38:09 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-03-02 18:05:52 +00:00
|
|
|
int ff_dnn_execute_model_ov(const DNNModel *model, DNNExecBaseParams *exec_params)
|
2020-11-18 06:30:11 +00:00
|
|
|
{
|
2021-01-22 14:20:15 +00:00
|
|
|
OVModel *ov_model = model->model;
|
2020-11-18 06:30:11 +00:00
|
|
|
OVContext *ctx = &ov_model->ctx;
|
2021-07-11 16:15:02 +00:00
|
|
|
OVRequestItem *request;
|
2020-11-18 06:30:11 +00:00
|
|
|
TaskItem *task;
|
2022-03-02 18:05:52 +00:00
|
|
|
int ret;
|
2020-11-18 06:30:11 +00:00
|
|
|
|
2022-03-02 18:05:52 +00:00
|
|
|
ret = ff_check_exec_params(ctx, DNN_OV, model->func_type, exec_params);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
2020-11-18 06:30:11 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 03:42:13 +00:00
|
|
|
if (!ov_model->exe_network) {
|
2022-03-02 18:05:52 +00:00
|
|
|
ret = init_model_ov(ov_model, exec_params->input_name, exec_params->output_names[0]);
|
2022-03-02 18:05:56 +00:00
|
|
|
if (ret != 0) {
|
2021-01-18 03:42:13 +00:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "Failed init OpenVINO exectuable network or inference request\n");
|
2022-03-02 18:05:52 +00:00
|
|
|
return ret;
|
2021-01-26 00:22:27 +00:00
|
|
|
}
|
2021-01-18 03:42:13 +00:00
|
|
|
}
|
|
|
|
|
2021-03-13 06:14:51 +00:00
|
|
|
task = av_malloc(sizeof(*task));
|
|
|
|
if (!task) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "unable to alloc memory for task item.\n");
|
2022-03-02 18:05:52 +00:00
|
|
|
return AVERROR(ENOMEM);
|
2021-03-13 06:14:51 +00:00
|
|
|
}
|
|
|
|
|
2022-03-02 18:05:52 +00:00
|
|
|
ret = ff_dnn_fill_task(task, exec_params, ov_model, ctx->options.async, 1);
|
2022-03-02 18:05:56 +00:00
|
|
|
if (ret != 0) {
|
2021-08-25 21:10:45 +00:00
|
|
|
av_freep(&task);
|
2022-03-02 18:05:52 +00:00
|
|
|
return ret;
|
2021-06-05 18:08:06 +00:00
|
|
|
}
|
|
|
|
|
2020-12-30 05:11:27 +00:00
|
|
|
if (ff_queue_push_back(ov_model->task_queue, task) < 0) {
|
|
|
|
av_freep(&task);
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "unable to push back task_queue.\n");
|
2022-03-02 18:05:52 +00:00
|
|
|
return AVERROR(ENOMEM);
|
2020-12-30 05:11:27 +00:00
|
|
|
}
|
2020-11-18 06:30:11 +00:00
|
|
|
|
2022-03-02 18:05:52 +00:00
|
|
|
ret = extract_lltask_from_task(model->func_type, task, ov_model->lltask_queue, exec_params);
|
2022-03-02 18:05:56 +00:00
|
|
|
if (ret != 0) {
|
2021-03-15 02:13:14 +00:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "unable to extract inference from task.\n");
|
2022-03-02 18:05:52 +00:00
|
|
|
return ret;
|
2021-03-15 02:13:14 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 21:10:45 +00:00
|
|
|
if (ctx->options.async) {
|
2021-08-25 21:10:48 +00:00
|
|
|
while (ff_queue_size(ov_model->lltask_queue) >= ctx->options.batch_size) {
|
2021-08-25 21:10:45 +00:00
|
|
|
request = ff_safe_queue_pop_front(ov_model->request_queue);
|
|
|
|
if (!request) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "unable to get infer request.\n");
|
2022-03-02 18:05:52 +00:00
|
|
|
return AVERROR(EINVAL);
|
2021-08-25 21:10:45 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 21:10:48 +00:00
|
|
|
ret = execute_model_ov(request, ov_model->lltask_queue);
|
2022-03-02 18:05:56 +00:00
|
|
|
if (ret != 0) {
|
2021-08-25 21:10:45 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-02 18:05:56 +00:00
|
|
|
return 0;
|
2021-08-25 21:10:45 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (model->func_type == DFT_ANALYTICS_CLASSIFY) {
|
|
|
|
// Classification filter has not been completely
|
|
|
|
// tested with the sync mode. So, do not support now.
|
|
|
|
avpriv_report_missing_feature(ctx, "classify for sync execution");
|
2022-03-02 18:05:52 +00:00
|
|
|
return AVERROR(ENOSYS);
|
2021-08-25 21:10:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx->options.batch_size > 1) {
|
|
|
|
avpriv_report_missing_feature(ctx, "batch mode for sync execution");
|
2022-03-02 18:05:52 +00:00
|
|
|
return AVERROR(ENOSYS);
|
2021-08-25 21:10:45 +00:00
|
|
|
}
|
|
|
|
|
2021-03-16 05:02:56 +00:00
|
|
|
request = ff_safe_queue_pop_front(ov_model->request_queue);
|
|
|
|
if (!request) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "unable to get infer request.\n");
|
2022-03-02 18:05:52 +00:00
|
|
|
return AVERROR(EINVAL);
|
2021-03-16 05:02:56 +00:00
|
|
|
}
|
2021-08-25 21:10:48 +00:00
|
|
|
return execute_model_ov(request, ov_model->lltask_queue);
|
2020-11-18 06:30:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-25 21:10:45 +00:00
|
|
|
DNNAsyncStatusType ff_dnn_get_result_ov(const DNNModel *model, AVFrame **in, AVFrame **out)
|
2020-11-18 06:30:11 +00:00
|
|
|
{
|
2021-01-22 14:20:15 +00:00
|
|
|
OVModel *ov_model = model->model;
|
2021-08-25 21:10:45 +00:00
|
|
|
return ff_dnn_get_result_common(ov_model->task_queue, in, out);
|
2020-11-18 06:30:11 +00:00
|
|
|
}
|
|
|
|
|
2022-03-02 18:05:52 +00:00
|
|
|
int ff_dnn_flush_ov(const DNNModel *model)
|
2021-01-07 03:14:10 +00:00
|
|
|
{
|
2021-01-22 14:20:15 +00:00
|
|
|
OVModel *ov_model = model->model;
|
2021-01-07 03:14:10 +00:00
|
|
|
OVContext *ctx = &ov_model->ctx;
|
2021-07-11 16:15:02 +00:00
|
|
|
OVRequestItem *request;
|
2021-01-07 03:14:10 +00:00
|
|
|
IEStatusCode status;
|
2022-03-02 18:05:52 +00:00
|
|
|
int ret;
|
2021-01-07 03:14:10 +00:00
|
|
|
|
2021-08-25 21:10:48 +00:00
|
|
|
if (ff_queue_size(ov_model->lltask_queue) == 0) {
|
2021-03-15 02:13:14 +00:00
|
|
|
// no pending task need to flush
|
2022-03-02 18:05:56 +00:00
|
|
|
return 0;
|
2021-03-15 02:13:14 +00:00
|
|
|
}
|
|
|
|
|
2021-01-07 03:14:10 +00:00
|
|
|
request = ff_safe_queue_pop_front(ov_model->request_queue);
|
|
|
|
if (!request) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "unable to get infer request.\n");
|
2022-03-02 18:05:52 +00:00
|
|
|
return AVERROR(EINVAL);
|
2021-01-07 03:14:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = fill_model_input_ov(ov_model, request);
|
2022-03-02 18:05:56 +00:00
|
|
|
if (ret != 0) {
|
2021-01-07 03:14:10 +00:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "Failed to fill model input.\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
status = ie_infer_set_completion_callback(request->infer_request, &request->callback);
|
|
|
|
if (status != OK) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Failed to set completion callback for inference\n");
|
2022-03-02 18:05:52 +00:00
|
|
|
return DNN_GENERIC_ERROR;
|
2021-01-07 03:14:10 +00:00
|
|
|
}
|
|
|
|
status = ie_infer_request_infer_async(request->infer_request);
|
|
|
|
if (status != OK) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Failed to start async inference\n");
|
2022-03-02 18:05:52 +00:00
|
|
|
return DNN_GENERIC_ERROR;
|
2021-01-07 03:14:10 +00:00
|
|
|
}
|
|
|
|
|
2022-03-02 18:05:56 +00:00
|
|
|
return 0;
|
2021-01-07 03:14:10 +00:00
|
|
|
}
|
|
|
|
|
2020-05-25 07:38:09 +00:00
|
|
|
void ff_dnn_free_model_ov(DNNModel **model)
|
|
|
|
{
|
|
|
|
if (*model){
|
2021-01-22 14:20:15 +00:00
|
|
|
OVModel *ov_model = (*model)->model;
|
2020-12-30 01:37:14 +00:00
|
|
|
while (ff_safe_queue_size(ov_model->request_queue) != 0) {
|
2021-07-11 16:15:02 +00:00
|
|
|
OVRequestItem *item = ff_safe_queue_pop_front(ov_model->request_queue);
|
2020-11-18 06:30:11 +00:00
|
|
|
if (item && item->infer_request) {
|
|
|
|
ie_infer_request_free(&item->infer_request);
|
|
|
|
}
|
2021-08-25 21:10:48 +00:00
|
|
|
av_freep(&item->lltasks);
|
2020-11-18 06:30:11 +00:00
|
|
|
av_freep(&item);
|
|
|
|
}
|
2020-12-30 01:37:14 +00:00
|
|
|
ff_safe_queue_destroy(ov_model->request_queue);
|
2020-11-18 06:30:11 +00:00
|
|
|
|
2021-08-25 21:10:48 +00:00
|
|
|
while (ff_queue_size(ov_model->lltask_queue) != 0) {
|
|
|
|
LastLevelTaskItem *item = ff_queue_pop_front(ov_model->lltask_queue);
|
2021-03-15 02:13:14 +00:00
|
|
|
av_freep(&item);
|
|
|
|
}
|
2021-08-25 21:10:48 +00:00
|
|
|
ff_queue_destroy(ov_model->lltask_queue);
|
2021-03-15 02:13:14 +00:00
|
|
|
|
2020-12-30 01:37:14 +00:00
|
|
|
while (ff_queue_size(ov_model->task_queue) != 0) {
|
|
|
|
TaskItem *item = ff_queue_pop_front(ov_model->task_queue);
|
2021-01-07 03:14:10 +00:00
|
|
|
av_frame_free(&item->in_frame);
|
|
|
|
av_frame_free(&item->out_frame);
|
2020-11-18 06:30:11 +00:00
|
|
|
av_freep(&item);
|
|
|
|
}
|
2020-12-30 01:37:14 +00:00
|
|
|
ff_queue_destroy(ov_model->task_queue);
|
2020-11-18 06:30:11 +00:00
|
|
|
|
2020-05-25 07:38:09 +00:00
|
|
|
if (ov_model->exe_network)
|
|
|
|
ie_exec_network_free(&ov_model->exe_network);
|
|
|
|
if (ov_model->network)
|
|
|
|
ie_network_free(&ov_model->network);
|
|
|
|
if (ov_model->core)
|
|
|
|
ie_core_free(&ov_model->core);
|
|
|
|
av_freep(&ov_model);
|
|
|
|
av_freep(model);
|
|
|
|
}
|
|
|
|
}
|