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.
Showing the changes to libavfilter (4 of 4 files). Show all
Unified · Split
| @@ -142,3 +142,29 @@ DNNAsyncStatusType ff_dnn_get_async_result_common(Queue *task_queue, AVFrame **i · 141 unmodified lines | |||
| 142 | 142 | ||
| 143 | return DAST_SUCCESS; | 143 | return DAST_SUCCESS; |
| 144 | } | 144 | } |
| 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 | } | ||
| @@ -137,4 +137,20 @@ DNNReturnType ff_dnn_start_inference_async(void *ctx, DNNAsyncExecModule *async_ · 136 unmodified lines | |||
| 137 | */ | 137 | */ |
| 138 | DNNAsyncStatusType ff_dnn_get_async_result_common(Queue *task_queue, AVFrame **in, AVFrame **out); | 138 | DNNAsyncStatusType ff_dnn_get_async_result_common(Queue *task_queue, AVFrame **in, AVFrame **out); |
| 139 | 139 | ||
| 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 | |||
| 140 | #endif | 156 | #endif |
| @@ -640,10 +640,15 @@ static DNNReturnType get_output_ov(void *model, const char *input_name, int inpu · 639 unmodified lines | |||
| 640 | OVContext *ctx = &ov_model->ctx; | 640 | OVContext *ctx = &ov_model->ctx; |
| 641 | TaskItem task; | 641 | TaskItem task; |
| 642 | OVRequestItem *request; | 642 | OVRequestItem *request; |
| 643 | AVFrame *in_frame = NULL; | ||
| 644 | AVFrame *out_frame = NULL; | ||
| 645 | IEStatusCode status; | 643 | IEStatusCode status; |
| 646 | input_shapes_t input_shapes; | 644 | 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 | }; | ||
| 647 | 652 | ||
| 648 | if (ov_model->model->func_type != DFT_PROCESS_FRAME) { | 653 | if (ov_model->model->func_type != DFT_PROCESS_FRAME) { |
| 649 | av_log(ctx, AV_LOG_ERROR, "Get output dim only when processing frame.\n"); | 654 | 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 | |||
| 669 | } | 674 | } |
| 670 | } | 675 | } |
| 671 | 676 | ||
| 672 | in_frame = av_frame_alloc(); | 677 | if (ff_dnn_fill_gettingoutput_task(&task, &exec_params, ov_model, input_height, input_width, ctx) != DNN_SUCCESS) { |
| 673 | if (!in_frame) { | ||
| 674 | av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for input frame\n"); | ||
| 675 | return DNN_ERROR; | 678 | return DNN_ERROR; |
| 676 | } | 679 | } |
| 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; | ||
| 695 | 680 | ||
| 696 | if (extract_inference_from_task(ov_model->model->func_type, &task, ov_model->inference_queue, NULL) != DNN_SUCCESS) { | 681 | 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); | ||
| 699 | av_log(ctx, AV_LOG_ERROR, "unable to extract inference from task.\n"); | 682 | av_log(ctx, AV_LOG_ERROR, "unable to extract inference from task.\n"); |
| 700 | return DNN_ERROR; | 683 | ret = DNN_ERROR; |
| 684 | goto err; | ||
| 701 | } | 685 | } |
| 702 | 686 | ||
| 703 | request = ff_safe_queue_pop_front(ov_model->request_queue); | 687 | request = ff_safe_queue_pop_front(ov_model->request_queue); |
| 704 | if (!request) { | 688 | if (!request) { |
| 705 | av_frame_free(&out_frame); | ||
| 706 | av_frame_free(&in_frame); | ||
| 707 | av_log(ctx, AV_LOG_ERROR, "unable to get infer request.\n"); | 689 | av_log(ctx, AV_LOG_ERROR, "unable to get infer request.\n"); |
| 708 | return DNN_ERROR; | 690 | ret = DNN_ERROR; |
| 691 | goto err; | ||
| 709 | } | 692 | } |
| 710 | 693 | ||
| 711 | ret = execute_model_ov(request, ov_model->inference_queue); | 694 | ret = execute_model_ov(request, ov_model->inference_queue); |
| 712 | *output_width = out_frame->width; | 695 | *output_width = task.out_frame->width; |
| 713 | *output_height = out_frame->height; | 696 | *output_height = task.out_frame->height; |
| 714 | 697 | err: | |
| 715 | av_frame_free(&out_frame); | 698 | av_frame_free(&task.out_frame); |
| 716 | av_frame_free(&in_frame); | 699 | av_frame_free(&task.in_frame); |
| 717 | return ret; | 700 | return ret; |
| 718 | } | 701 | } |
| 719 | 702 | ||
| @@ -310,36 +310,20 @@ static DNNReturnType get_output_tf(void *model, const char *input_name, int inpu · 309 unmodified lines | |||
| 310 | DNNReturnType ret; | 310 | DNNReturnType ret; |
| 311 | TFModel *tf_model = model; | 311 | TFModel *tf_model = model; |
| 312 | TFContext *ctx = &tf_model->ctx; | 312 | TFContext *ctx = &tf_model->ctx; |
| 313 | AVFrame *in_frame = av_frame_alloc(); | ||
| 314 | AVFrame *out_frame = NULL; | ||
| 315 | TaskItem task; | 313 | TaskItem task; |
| 316 | TFRequestItem *request; | 314 | TFRequestItem *request; |
| 317 | 315 | DNNExecBaseParams exec_params = { | |
| 318 | if (!in_frame) { | 316 | .input_name = input_name, |
| 319 | av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for input frame\n"); | 317 | .output_names = &output_name, |
| 320 | ret = DNN_ERROR; | 318 | .nb_output = 1, |
| 321 | goto err; | 319 | .in_frame = NULL, |
| 322 | } | 320 | .out_frame = NULL, |
| 323 | 321 | }; | |
| 324 | out_frame = av_frame_alloc(); | 322 | |
| 325 | if (!out_frame) { | 323 | if (ff_dnn_fill_gettingoutput_task(&task, &exec_params, tf_model, input_height, input_width, ctx) != DNN_SUCCESS) { |
| 326 | av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for output frame\n"); | ||
| 327 | ret = DNN_ERROR; | ||
| 328 | goto err; | 324 | goto err; |
| 329 | } | 325 | } |
| 330 | 326 | ||
| 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 | |||
| 343 | if (extract_inference_from_task(&task, tf_model->inference_queue) != DNN_SUCCESS) { | 327 | if (extract_inference_from_task(&task, tf_model->inference_queue) != DNN_SUCCESS) { |
| 344 | av_log(ctx, AV_LOG_ERROR, "unable to extract inference from task.\n"); | 328 | av_log(ctx, AV_LOG_ERROR, "unable to extract inference from task.\n"); |
| 345 | ret = DNN_ERROR; | 329 | ret = DNN_ERROR; |
| @@ -354,12 +338,12 @@ static DNNReturnType get_output_tf(void *model, const char *input_name, int inpu · 8 unmodified lines | |||
| 354 | } | 338 | } |
| 355 | 339 | ||
| 356 | ret = execute_model_tf(request, tf_model->inference_queue); | 340 | ret = execute_model_tf(request, tf_model->inference_queue); |
| 357 | *output_width = out_frame->width; | 341 | *output_width = task.out_frame->width; |
| 358 | *output_height = out_frame->height; | 342 | *output_height = task.out_frame->height; |
| 359 | 343 | ||
| 360 | err: | 344 | err: |
| 361 | av_frame_free(&out_frame); | 345 | av_frame_free(&task.out_frame); |
| 362 | av_frame_free(&in_frame); | 346 | av_frame_free(&task.in_frame); |
| 363 | return ret; | 347 | return ret; |
| 364 | } | 348 | } |
| 365 | 349 | ||