ie_infer_request_t *infer_request;
/* for async execution */
- FFSafeQueue *request_queue; // holds RequestItem
- FFQueue *task_queue; // holds TaskItem
+ SafeQueue *request_queue; // holds RequestItem
+ Queue *task_queue; // holds TaskItem
} OVModel;
typedef struct TaskItem {
{
case FP32:
return DNN_FLOAT;
+ case U8:
+ return DNN_UINT8;
default:
av_assert0(!"not supported yet.");
return DNN_FLOAT;
{
case DNN_FLOAT:
return sizeof(float);
+ case DNN_UINT8:
+ return sizeof(uint8_t);
default:
av_assert0(!"not supported yet.");
return 1;
status |= ie_blob_get_dims(input_blob, &dims);
status |= ie_blob_get_precision(input_blob, &precision);
if (status != OK) {
+ ie_blob_free(&input_blob);
av_log(ctx, AV_LOG_ERROR, "Failed to get input blob dims/precision\n");
return DNN_ERROR;
}
status = ie_blob_get_buffer(input_blob, &blob_buffer);
if (status != OK) {
+ ie_blob_free(&input_blob);
av_log(ctx, AV_LOG_ERROR, "Failed to get input blob buffer\n");
return DNN_ERROR;
}
input.channels = dims.dims[1];
input.data = blob_buffer.buffer;
input.dt = precision_to_datatype(precision);
+ // all models in openvino open model zoo use BGR as input,
+ // change to be an option when necessary.
+ input.order = DCO_BGR;
av_assert0(request->task_count <= dims.dims[0]);
for (int i = 0; i < request->task_count; ++i) {
task = request->tasks[i];
if (task->do_ioproc) {
- if (ov_model->model->pre_proc != NULL) {
- ov_model->model->pre_proc(task->in_frame, &input, ov_model->model->filter_ctx);
+ if (ov_model->model->frame_pre_proc != NULL) {
+ ov_model->model->frame_pre_proc(task->in_frame, &input, ov_model->model->filter_ctx);
} else {
- ff_proc_from_frame_to_dnn(task->in_frame, &input, ctx);
+ ff_proc_from_frame_to_dnn(task->in_frame, &input, ov_model->model->func_type, ctx);
}
}
input.data = (uint8_t *)input.data
IEStatusCode status;
RequestItem *request = args;
TaskItem *task = request->tasks[0];
+ SafeQueue *requestq = task->ov_model->request_queue;
ie_blob_t *output_blob = NULL;
ie_blob_buffer_t blob_buffer;
DNNData output;
status = ie_blob_get_buffer(output_blob, &blob_buffer);
if (status != OK) {
+ ie_blob_free(&output_blob);
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) {
+ ie_blob_free(&output_blob);
av_log(ctx, AV_LOG_ERROR, "Failed to get dims or precision of output\n");
return;
}
av_assert0(request->task_count >= 1);
for (int i = 0; i < request->task_count; ++i) {
task = request->tasks[i];
- if (task->do_ioproc) {
- if (task->ov_model->model->post_proc != NULL) {
- task->ov_model->model->post_proc(task->out_frame, &output, task->ov_model->model->filter_ctx);
+
+ switch (task->ov_model->model->func_type) {
+ case DFT_PROCESS_FRAME:
+ if (task->do_ioproc) {
+ if (task->ov_model->model->frame_post_proc != NULL) {
+ task->ov_model->model->frame_post_proc(task->out_frame, &output, task->ov_model->model->filter_ctx);
+ } else {
+ ff_proc_from_dnn_to_frame(task->out_frame, &output, ctx);
+ }
} else {
- ff_proc_from_dnn_to_frame(task->out_frame, &output, ctx);
+ task->out_frame->width = output.width;
+ task->out_frame->height = output.height;
}
- } else {
- task->out_frame->width = output.width;
- task->out_frame->height = output.height;
+ break;
+ case DFT_ANALYTICS_DETECT:
+ if (!task->ov_model->model->detect_post_proc) {
+ av_log(ctx, AV_LOG_ERROR, "detect filter needs to provide post proc\n");
+ return;
+ }
+ task->ov_model->model->detect_post_proc(task->out_frame, &output, 1, task->ov_model->model->filter_ctx);
+ break;
+ default:
+ av_assert0(!"should not reach here");
+ break;
}
+
task->done = 1;
output.data = (uint8_t *)output.data
+ output.width * output.height * output.channels * get_datatype_size(output.dt);
request->task_count = 0;
if (task->async) {
- if (ff_safe_queue_push_back(task->ov_model->request_queue, request) < 0) {
+ if (ff_safe_queue_push_back(requestq, request) < 0) {
av_log(ctx, AV_LOG_ERROR, "Failed to push back request_queue.\n");
return;
}
}
}
-static DNNReturnType init_model_ov(OVModel *ov_model)
+static DNNReturnType init_model_ov(OVModel *ov_model, const char *input_name, const char *output_name)
{
OVContext *ctx = &ov_model->ctx;
IEStatusCode status;
goto err;
}
+ // 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) {
+ av_log(ctx, AV_LOG_ERROR, "Failed to set layout as NHWC for input %s\n", input_name);
+ goto err;
+ }
+ status = ie_network_set_output_layout(ov_model->network, output_name, NHWC);
+ if (status != OK) {
+ av_log(ctx, AV_LOG_ERROR, "Failed to set layout as NHWC for output %s\n", output_name);
+ goto err;
+ }
+
+ // all models in openvino open model zoo use BGR with range [0.0f, 255.0f] as input,
+ // we don't have a AVPixelFormat to describe it, so we'll use AV_PIX_FMT_BGR24 and
+ // 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);
+ goto err;
+ }
+ }
+
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");
goto err;
}
+ 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);
+ goto err;
+ }
+
status = ie_exec_network_create_infer_request(ov_model->exe_network, &item->infer_request);
if (status != OK) {
- av_freep(&item);
goto err;
}
item->tasks = av_malloc_array(ctx->options.batch_size, sizeof(*item->tasks));
if (!item->tasks) {
- av_freep(&item);
goto err;
}
item->task_count = 0;
-
- 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);
- goto err;
- }
}
ov_model->task_queue = ff_queue_create();
static DNNReturnType get_input_ov(void *model, DNNData *input, const char *input_name)
{
- OVModel *ov_model = (OVModel *)model;
+ OVModel *ov_model = model;
OVContext *ctx = &ov_model->ctx;
char *model_input_name = NULL;
char *all_input_names = NULL;
const char *output_name, int *output_width, int *output_height)
{
DNNReturnType ret;
- OVModel *ov_model = (OVModel *)model;
+ OVModel *ov_model = model;
OVContext *ctx = &ov_model->ctx;
TaskItem task;
RequestItem request;
- AVFrame *in_frame = av_frame_alloc();
+ AVFrame *in_frame = NULL;
AVFrame *out_frame = NULL;
TaskItem *ptask = &task;
IEStatusCode status;
input_shapes_t input_shapes;
- if (!in_frame) {
- av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for input frame\n");
- return DNN_ERROR;
- }
- out_frame = av_frame_alloc();
- if (!out_frame) {
- av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for output frame\n");
- av_frame_free(&in_frame);
+ if (ov_model->model->func_type != DFT_PROCESS_FRAME) {
+ av_log(ctx, AV_LOG_ERROR, "Get output dim only when processing frame.\n");
return DNN_ERROR;
}
- in_frame->width = input_width;
- in_frame->height = input_height;
if (ctx->options.input_resizable) {
status = ie_network_get_input_shapes(ov_model->network, &input_shapes);
}
if (!ov_model->exe_network) {
- if (init_model_ov(ov_model) != DNN_SUCCESS) {
+ if (init_model_ov(ov_model, input_name, output_name) != DNN_SUCCESS) {
av_log(ctx, AV_LOG_ERROR, "Failed init OpenVINO exectuable network or inference request\n");
return DNN_ERROR;
- };
+ }
+ }
+
+ in_frame = av_frame_alloc();
+ if (!in_frame) {
+ av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for input frame\n");
+ return DNN_ERROR;
+ }
+ in_frame->width = input_width;
+ in_frame->height = input_height;
+
+ out_frame = av_frame_alloc();
+ if (!out_frame) {
+ av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for output frame\n");
+ av_frame_free(&in_frame);
+ return DNN_ERROR;
}
task.done = 0;
return ret;
}
-DNNModel *ff_dnn_load_model_ov(const char *model_filename, const char *options, AVFilterContext *filter_ctx)
+DNNModel *ff_dnn_load_model_ov(const char *model_filename, DNNFunctionType func_type, const char *options, AVFilterContext *filter_ctx)
{
DNNModel *model = NULL;
OVModel *ov_model = NULL;
av_freep(&model);
return NULL;
}
- model->model = (void *)ov_model;
+ model->model = ov_model;
ov_model->model = model;
ov_model->ctx.class = &dnn_openvino_class;
ctx = &ov_model->ctx;
model->get_output = &get_output_ov;
model->options = options;
model->filter_ctx = filter_ctx;
+ model->func_type = func_type;
return model;
DNNReturnType ff_dnn_execute_model_ov(const DNNModel *model, const char *input_name, AVFrame *in_frame,
const char **output_names, uint32_t nb_output, AVFrame *out_frame)
{
- OVModel *ov_model = (OVModel *)model->model;
+ OVModel *ov_model = model->model;
OVContext *ctx = &ov_model->ctx;
TaskItem task;
RequestItem request;
return DNN_ERROR;
}
- if (!out_frame) {
+ if (!out_frame && model->func_type == DFT_PROCESS_FRAME) {
av_log(ctx, AV_LOG_ERROR, "out frame is NULL when execute model.\n");
return DNN_ERROR;
}
}
if (!ov_model->exe_network) {
- if (init_model_ov(ov_model) != DNN_SUCCESS) {
+ if (init_model_ov(ov_model, input_name, output_names[0]) != DNN_SUCCESS) {
av_log(ctx, AV_LOG_ERROR, "Failed init OpenVINO exectuable network or inference request\n");
return DNN_ERROR;
- };
+ }
}
task.done = 0;
DNNReturnType ff_dnn_execute_model_async_ov(const DNNModel *model, const char *input_name, AVFrame *in_frame,
const char **output_names, uint32_t nb_output, AVFrame *out_frame)
{
- OVModel *ov_model = (OVModel *)model->model;
+ OVModel *ov_model = model->model;
OVContext *ctx = &ov_model->ctx;
RequestItem *request;
TaskItem *task;
return DNN_ERROR;
}
- if (!out_frame) {
+ if (!out_frame && model->func_type == DFT_PROCESS_FRAME) {
av_log(ctx, AV_LOG_ERROR, "out frame is NULL when async execute model.\n");
return DNN_ERROR;
}
+ if (!ov_model->exe_network) {
+ if (init_model_ov(ov_model, input_name, output_names[0]) != DNN_SUCCESS) {
+ av_log(ctx, AV_LOG_ERROR, "Failed init OpenVINO exectuable network or inference request\n");
+ return DNN_ERROR;
+ }
+ }
+
task = av_malloc(sizeof(*task));
if (!task) {
av_log(ctx, AV_LOG_ERROR, "unable to alloc memory for task item.\n");
return DNN_ERROR;
}
- if (!ov_model->exe_network) {
- if (init_model_ov(ov_model) != DNN_SUCCESS) {
- av_log(ctx, AV_LOG_ERROR, "Failed init OpenVINO exectuable network or inference request\n");
- return DNN_ERROR;
- };
- }
-
task->done = 0;
task->do_ioproc = 1;
task->async = 1;
DNNAsyncStatusType ff_dnn_get_async_result_ov(const DNNModel *model, AVFrame **in, AVFrame **out)
{
- OVModel *ov_model = (OVModel *)model->model;
+ OVModel *ov_model = model->model;
TaskItem *task = ff_queue_peek_front(ov_model->task_queue);
if (!task) {
DNNReturnType ff_dnn_flush_ov(const DNNModel *model)
{
- OVModel *ov_model = (OVModel *)model->model;
+ OVModel *ov_model = model->model;
OVContext *ctx = &ov_model->ctx;
RequestItem *request;
IEStatusCode status;
void ff_dnn_free_model_ov(DNNModel **model)
{
if (*model){
- OVModel *ov_model = (OVModel *)(*model)->model;
+ OVModel *ov_model = (*model)->model;
while (ff_safe_queue_size(ov_model->request_queue) != 0) {
RequestItem *item = ff_safe_queue_pop_front(ov_model->request_queue);
if (item && item->infer_request) {