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 | 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 | 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 | 156 | #endif |
| @@ -640,10 +640,15 @@ static DNNReturnType get_output_ov(void *model, const char *input_name, int inpu · 639 unmodified lines | ||
| 640 | 640 | OVContext *ctx = &ov_model->ctx; |
| 641 | 641 | TaskItem task; |
| 642 | 642 | OVRequestItem *request; |
| 643 | − AVFrame *in_frame = NULL; | |
| 644 | − AVFrame *out_frame = NULL; | |
| 645 | 643 | IEStatusCode status; |
| 646 | 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 | 653 | if (ov_model->model->func_type != DFT_PROCESS_FRAME) { |
| 649 | 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(); | |
| 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) { | |
| 675 | 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 | 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 | 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 | 687 | request = ff_safe_queue_pop_front(ov_model->request_queue); |
| 704 | 688 | if (!request) { |
| 705 | − av_frame_free(&out_frame); | |
| 706 | − av_frame_free(&in_frame); | |
| 707 | 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 | 694 | 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); | |
| 717 | 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 | 310 | DNNReturnType ret; |
| 311 | 311 | TFModel *tf_model = model; |
| 312 | 312 | TFContext *ctx = &tf_model->ctx; |
| 313 | − AVFrame *in_frame = av_frame_alloc(); | |
| 314 | − AVFrame *out_frame = NULL; | |
| 315 | 313 | TaskItem task; |
| 316 | 314 | 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) { | |
| 328 | 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 | 327 | if (extract_inference_from_task(&task, tf_model->inference_queue) != DNN_SUCCESS) { |
| 344 | 328 | av_log(ctx, AV_LOG_ERROR, "unable to extract inference from task.\n"); |
| 345 | 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 | 340 | 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; | |
| 359 | 343 | |
| 360 | 344 | 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); | |
| 363 | 347 | return ret; |
| 364 | 348 | } |
| 365 | 349 | |