History

lavfi/dnn: Extract Common Parts from get_output functions

The frame allocation and filling the TaskItem with execution parameters is common in the three backends. This commit shifts this logic to dnn_backend_common.

Shubhanshu Saxena committed 5 years ago

Commit 009b2e51 parent 4d627ac4 files · +72 −63Browse files

Unified · Split

4 files
  1. libavfilter/dnn/dnn_backend_common.c +26 −0
  2. libavfilter/dnn/dnn_backend_common.h +16 −0
  3. libavfilter/dnn/dnn_backend_openvino.c +17 −34
  4. libavfilter/dnn/dnn_backend_tf.c +13 −29
libavfilter/dnn/dnn_backend_common.c+26−0
@@ -142,3 +142,29 @@ DNNAsyncStatusType ff_dnn_get_async_result_common(Queue *task_queue, AVFrame **i · 141 unmodified lines
142142
143143 return DAST_SUCCESS;
144144 }
145+
146+DNNReturnType ff_dnn_fill_gettingoutput_task(TaskItem *task, DNNExecBaseParams *exec_params, void *backend_model, int input_height, int input_width, void *ctx)
147+{
148+ AVFrame *in_frame = NULL;
149+ AVFrame *out_frame = NULL;
150+
151+ in_frame = av_frame_alloc();
152+ if (!in_frame) {
153+ av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for input frame\n");
154+ return DNN_ERROR;
155+ }
156+
157+ out_frame = av_frame_alloc();
158+ if (!out_frame) {
159+ av_frame_free(&in_frame);
160+ av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for output frame\n");
161+ return DNN_ERROR;
162+ }
163+
164+ in_frame->width = input_width;
165+ in_frame->height = input_height;
166+ exec_params->in_frame = in_frame;
167+ exec_params->out_frame = out_frame;
168+
169+ return ff_dnn_fill_task(task, exec_params, backend_model, 0, 0);
170+}
libavfilter/dnn/dnn_backend_common.h+16−0
@@ -137,4 +137,20 @@ DNNReturnType ff_dnn_start_inference_async(void *ctx, DNNAsyncExecModule *async_ · 136 unmodified lines
137137 */
138138 DNNAsyncStatusType ff_dnn_get_async_result_common(Queue *task_queue, AVFrame **in, AVFrame **out);
139139
140+/**
141+ * Allocate input and output frames and fill the Task
142+ * with execution parameters.
143+ *
144+ * @param task pointer to the allocated task
145+ * @param exec_params pointer to execution parameters
146+ * @param backend_model void pointer to the backend model
147+ * @param input_height height of input frame
148+ * @param input_width width of input frame
149+ * @param ctx pointer to the backend context
150+ *
151+ * @retval DNN_SUCCESS if successful
152+ * @retval DNN_ERROR if allocation fails
153+ */
154+DNNReturnType ff_dnn_fill_gettingoutput_task(TaskItem *task, DNNExecBaseParams *exec_params, void *backend_model, int input_height, int input_width, void *ctx);
155+
140156 #endif
libavfilter/dnn/dnn_backend_openvino.c+17−34
@@ -640,10 +640,15 @@ static DNNReturnType get_output_ov(void *model, const char *input_name, int inpu · 639 unmodified lines
640640 OVContext *ctx = &ov_model->ctx;
641641 TaskItem task;
642642 OVRequestItem *request;
643− AVFrame *in_frame = NULL;
644− AVFrame *out_frame = NULL;
645643 IEStatusCode status;
646644 input_shapes_t input_shapes;
645+ DNNExecBaseParams exec_params = {
646+ .input_name = input_name,
647+ .output_names = &output_name,
648+ .nb_output = 1,
649+ .in_frame = NULL,
650+ .out_frame = NULL,
651+ };
647652
648653 if (ov_model->model->func_type != DFT_PROCESS_FRAME) {
649654 av_log(ctx, AV_LOG_ERROR, "Get output dim only when processing frame.\n");
@@ -669,51 +674,29 @@ static DNNReturnType get_output_ov(void *model, const char *input_name, int inpu · 19 unmodified lines
669674 }
670675 }
671676
672− in_frame = av_frame_alloc();
673− if (!in_frame) {
674− av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for input frame\n");
677+ if (ff_dnn_fill_gettingoutput_task(&task, &exec_params, ov_model, input_height, input_width, ctx) != DNN_SUCCESS) {
675678 return DNN_ERROR;
676679 }
677− in_frame->width = input_width;
678− in_frame->height = input_height;
679−
680− out_frame = av_frame_alloc();
681− if (!out_frame) {
682− av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for output frame\n");
683− av_frame_free(&in_frame);
684− return DNN_ERROR;
685− }
686−
687− task.do_ioproc = 0;
688− task.async = 0;
689− task.input_name = input_name;
690− task.in_frame = in_frame;
691− task.output_names = &output_name;
692− task.out_frame = out_frame;
693− task.nb_output = 1;
694− task.model = ov_model;
695680
696681 if (extract_inference_from_task(ov_model->model->func_type, &task, ov_model->inference_queue, NULL) != DNN_SUCCESS) {
697− av_frame_free(&out_frame);
698− av_frame_free(&in_frame);
699682 av_log(ctx, AV_LOG_ERROR, "unable to extract inference from task.\n");
700− return DNN_ERROR;
683+ ret = DNN_ERROR;
684+ goto err;
701685 }
702686
703687 request = ff_safe_queue_pop_front(ov_model->request_queue);
704688 if (!request) {
705− av_frame_free(&out_frame);
706− av_frame_free(&in_frame);
707689 av_log(ctx, AV_LOG_ERROR, "unable to get infer request.\n");
708− return DNN_ERROR;
690+ ret = DNN_ERROR;
691+ goto err;
709692 }
710693
711694 ret = execute_model_ov(request, ov_model->inference_queue);
712− *output_width = out_frame->width;
713− *output_height = out_frame->height;
714−
715− av_frame_free(&out_frame);
716− av_frame_free(&in_frame);
695+ *output_width = task.out_frame->width;
696+ *output_height = task.out_frame->height;
697+err:
698+ av_frame_free(&task.out_frame);
699+ av_frame_free(&task.in_frame);
717700 return ret;
718701 }
719702
libavfilter/dnn/dnn_backend_tf.c+13−29
@@ -310,36 +310,20 @@ static DNNReturnType get_output_tf(void *model, const char *input_name, int inpu · 309 unmodified lines
310310 DNNReturnType ret;
311311 TFModel *tf_model = model;
312312 TFContext *ctx = &tf_model->ctx;
313− AVFrame *in_frame = av_frame_alloc();
314− AVFrame *out_frame = NULL;
315313 TaskItem task;
316314 TFRequestItem *request;
317−
318− if (!in_frame) {
319− av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for input frame\n");
320− ret = DNN_ERROR;
321− goto err;
322− }
323−
324− out_frame = av_frame_alloc();
325− if (!out_frame) {
326− av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for output frame\n");
327− ret = DNN_ERROR;
315+ DNNExecBaseParams exec_params = {
316+ .input_name = input_name,
317+ .output_names = &output_name,
318+ .nb_output = 1,
319+ .in_frame = NULL,
320+ .out_frame = NULL,
321+ };
322+
323+ if (ff_dnn_fill_gettingoutput_task(&task, &exec_params, tf_model, input_height, input_width, ctx) != DNN_SUCCESS) {
328324 goto err;
329325 }
330326
331− in_frame->width = input_width;
332− in_frame->height = input_height;
333−
334− task.do_ioproc = 0;
335− task.async = 0;
336− task.input_name = input_name;
337− task.in_frame = in_frame;
338− task.output_names = &output_name;
339− task.out_frame = out_frame;
340− task.model = tf_model;
341− task.nb_output = 1;
342−
343327 if (extract_inference_from_task(&task, tf_model->inference_queue) != DNN_SUCCESS) {
344328 av_log(ctx, AV_LOG_ERROR, "unable to extract inference from task.\n");
345329 ret = DNN_ERROR;
@@ -354,12 +338,12 @@ static DNNReturnType get_output_tf(void *model, const char *input_name, int inpu · 8 unmodified lines
354338 }
355339
356340 ret = execute_model_tf(request, tf_model->inference_queue);
357− *output_width = out_frame->width;
358− *output_height = out_frame->height;
341+ *output_width = task.out_frame->width;
342+ *output_height = task.out_frame->height;
359343
360344 err:
361− av_frame_free(&out_frame);
362− av_frame_free(&in_frame);
345+ av_frame_free(&task.out_frame);
346+ av_frame_free(&task.in_frame);
363347 return ret;
364348 }
365349

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t