To avoid export multiple functions for each backend implementation.
Unified · Split
| @@ -23,7 +23,6 @@ · 22 unmodified lines | ||
| 23 | 23 | * DNN OpenVINO backend implementation. |
| 24 | 24 | */ |
| 25 | 25 | |
| 26 | −#include "dnn_backend_openvino.h" | |
| 27 | 26 | #include "dnn_io_proc.h" |
| 28 | 27 | #include "libavformat/avio.h" |
| 29 | 28 | #include "libavutil/avassert.h" |
| @@ -293,6 +292,46 @@ static void infer_completion_callback(void *args) · 263 unmodified lines | ||
| 293 | 292 | } |
| 294 | 293 | } |
| 295 | 294 | |
| 295 | +static void dnn_free_model_ov(DNNModel **model) | |
| 296 | +{ | |
| 297 | + if (*model){ | |
| 298 | + OVModel *ov_model = (*model)->model; | |
| 299 | + while (ff_safe_queue_size(ov_model->request_queue) != 0) { | |
| 300 | + OVRequestItem *item = ff_safe_queue_pop_front(ov_model->request_queue); | |
| 301 | + if (item && item->infer_request) { | |
| 302 | + ie_infer_request_free(&item->infer_request); | |
| 303 | + } | |
| 304 | + av_freep(&item->lltasks); | |
| 305 | + av_freep(&item); | |
| 306 | + } | |
| 307 | + ff_safe_queue_destroy(ov_model->request_queue); | |
| 308 | + | |
| 309 | + while (ff_queue_size(ov_model->lltask_queue) != 0) { | |
| 310 | + LastLevelTaskItem *item = ff_queue_pop_front(ov_model->lltask_queue); | |
| 311 | + av_freep(&item); | |
| 312 | + } | |
| 313 | + ff_queue_destroy(ov_model->lltask_queue); | |
| 314 | + | |
| 315 | + while (ff_queue_size(ov_model->task_queue) != 0) { | |
| 316 | + TaskItem *item = ff_queue_pop_front(ov_model->task_queue); | |
| 317 | + av_frame_free(&item->in_frame); | |
| 318 | + av_frame_free(&item->out_frame); | |
| 319 | + av_freep(&item); | |
| 320 | + } | |
| 321 | + ff_queue_destroy(ov_model->task_queue); | |
| 322 | + | |
| 323 | + if (ov_model->exe_network) | |
| 324 | + ie_exec_network_free(&ov_model->exe_network); | |
| 325 | + if (ov_model->network) | |
| 326 | + ie_network_free(&ov_model->network); | |
| 327 | + if (ov_model->core) | |
| 328 | + ie_core_free(&ov_model->core); | |
| 329 | + av_freep(&ov_model); | |
| 330 | + av_freep(model); | |
| 331 | + } | |
| 332 | +} | |
| 333 | + | |
| 334 | + | |
| 296 | 335 | static int init_model_ov(OVModel *ov_model, const char *input_name, const char *output_name) |
| 297 | 336 | { |
| 298 | 337 | int ret = 0; |
| @@ -438,7 +477,7 @@ static int init_model_ov(OVModel *ov_model, const char *input_name, const char * · 139 unmodified lines | ||
| 438 | 477 | return 0; |
| 439 | 478 | |
| 440 | 479 | err: |
| 441 | − ff_dnn_free_model_ov(&ov_model->model); | |
| 480 | + dnn_free_model_ov(&ov_model->model); | |
| 442 | 481 | return ret; |
| 443 | 482 | } |
| 444 | 483 | |
| @@ -721,7 +760,7 @@ err: · 276 unmodified lines | ||
| 721 | 760 | return ret; |
| 722 | 761 | } |
| 723 | 762 | |
| 724 | −DNNModel *ff_dnn_load_model_ov(const char *model_filename, DNNFunctionType func_type, const char *options, AVFilterContext *filter_ctx) | |
| 763 | +static DNNModel *dnn_load_model_ov(const char *model_filename, DNNFunctionType func_type, const char *options, AVFilterContext *filter_ctx) | |
| 725 | 764 | { |
| 726 | 765 | DNNModel *model = NULL; |
| 727 | 766 | OVModel *ov_model = NULL; |
| @@ -806,11 +845,11 @@ DNNModel *ff_dnn_load_model_ov(const char *model_filename, DNNFunctionType func_ · 78 unmodified lines | ||
| 806 | 845 | return model; |
| 807 | 846 | |
| 808 | 847 | err: |
| 809 | − ff_dnn_free_model_ov(&model); | |
| 848 | + dnn_free_model_ov(&model); | |
| 810 | 849 | return NULL; |
| 811 | 850 | } |
| 812 | 851 | |
| 813 | −int ff_dnn_execute_model_ov(const DNNModel *model, DNNExecBaseParams *exec_params) | |
| 852 | +static int dnn_execute_model_ov(const DNNModel *model, DNNExecBaseParams *exec_params) | |
| 814 | 853 | { |
| 815 | 854 | OVModel *ov_model = model->model; |
| 816 | 855 | OVContext *ctx = &ov_model->ctx; |
| @@ -893,13 +932,13 @@ int ff_dnn_execute_model_ov(const DNNModel *model, DNNExecBaseParams *exec_param · 76 unmodified lines | ||
| 893 | 932 | } |
| 894 | 933 | } |
| 895 | 934 | |
| 896 | −DNNAsyncStatusType ff_dnn_get_result_ov(const DNNModel *model, AVFrame **in, AVFrame **out) | |
| 935 | +static DNNAsyncStatusType dnn_get_result_ov(const DNNModel *model, AVFrame **in, AVFrame **out) | |
| 897 | 936 | { |
| 898 | 937 | OVModel *ov_model = model->model; |
| 899 | 938 | return ff_dnn_get_result_common(ov_model->task_queue, in, out); |
| 900 | 939 | } |
| 901 | 940 | |
| 902 | −int ff_dnn_flush_ov(const DNNModel *model) | |
| 941 | +static int dnn_flush_ov(const DNNModel *model) | |
| 903 | 942 | { |
| 904 | 943 | OVModel *ov_model = model->model; |
| 905 | 944 | OVContext *ctx = &ov_model->ctx; |
| @@ -937,41 +976,10 @@ int ff_dnn_flush_ov(const DNNModel *model) · 31 unmodified lines | ||
| 937 | 976 | return 0; |
| 938 | 977 | } |
| 939 | 978 | |
| 940 | −void ff_dnn_free_model_ov(DNNModel **model) | |
| 941 | −{ | |
| 942 | − if (*model){ | |
| 943 | − OVModel *ov_model = (*model)->model; | |
| 944 | − while (ff_safe_queue_size(ov_model->request_queue) != 0) { | |
| 945 | − OVRequestItem *item = ff_safe_queue_pop_front(ov_model->request_queue); | |
| 946 | − if (item && item->infer_request) { | |
| 947 | − ie_infer_request_free(&item->infer_request); | |
| 948 | − } | |
| 949 | − av_freep(&item->lltasks); | |
| 950 | − av_freep(&item); | |
| 951 | − } | |
| 952 | − ff_safe_queue_destroy(ov_model->request_queue); | |
| 953 | − | |
| 954 | − while (ff_queue_size(ov_model->lltask_queue) != 0) { | |
| 955 | − LastLevelTaskItem *item = ff_queue_pop_front(ov_model->lltask_queue); | |
| 956 | − av_freep(&item); | |
| 957 | − } | |
| 958 | − ff_queue_destroy(ov_model->lltask_queue); | |
| 959 | − | |
| 960 | − while (ff_queue_size(ov_model->task_queue) != 0) { | |
| 961 | − TaskItem *item = ff_queue_pop_front(ov_model->task_queue); | |
| 962 | − av_frame_free(&item->in_frame); | |
| 963 | − av_frame_free(&item->out_frame); | |
| 964 | − av_freep(&item); | |
| 965 | − } | |
| 966 | − ff_queue_destroy(ov_model->task_queue); | |
| 967 | − | |
| 968 | − if (ov_model->exe_network) | |
| 969 | − ie_exec_network_free(&ov_model->exe_network); | |
| 970 | − if (ov_model->network) | |
| 971 | − ie_network_free(&ov_model->network); | |
| 972 | − if (ov_model->core) | |
| 973 | − ie_core_free(&ov_model->core); | |
| 974 | − av_freep(&ov_model); | |
| 975 | − av_freep(model); | |
| 976 | − } | |
| 977 | −} | |
| 979 | +const DNNModule ff_dnn_backend_openvino = { | |
| 980 | + .load_model = dnn_load_model_ov, | |
| 981 | + .execute_model = dnn_execute_model_ov, | |
| 982 | + .get_result = dnn_get_result_ov, | |
| 983 | + .flush = dnn_flush_ov, | |
| 984 | + .free_model = dnn_free_model_ov, | |
| 985 | +}; | |
| @@ -1,40 +0,0 @@ | ||
| 1 | −/* | |
| 2 | − * Copyright (c) 2020 | |
| 3 | − * | |
| 4 | − * This file is part of FFmpeg. | |
| 5 | − * | |
| 6 | − * FFmpeg is free software; you can redistribute it and/or | |
| 7 | − * modify it under the terms of the GNU Lesser General Public | |
| 8 | − * License as published by the Free Software Foundation; either | |
| 9 | − * version 2.1 of the License, or (at your option) any later version. | |
| 10 | − * | |
| 11 | − * FFmpeg is distributed in the hope that it will be useful, | |
| 12 | − * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 13 | − * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 14 | − * Lesser General Public License for more details. | |
| 15 | − * | |
| 16 | − * You should have received a copy of the GNU Lesser General Public | |
| 17 | − * License along with FFmpeg; if not, write to the Free Software | |
| 18 | − * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
| 19 | − */ | |
| 20 | − | |
| 21 | −/** | |
| 22 | − * @file | |
| 23 | − * DNN inference functions interface for OpenVINO backend. | |
| 24 | − */ | |
| 25 | − | |
| 26 | − | |
| 27 | −#ifndef AVFILTER_DNN_DNN_BACKEND_OPENVINO_H | |
| 28 | −#define AVFILTER_DNN_DNN_BACKEND_OPENVINO_H | |
| 29 | − | |
| 30 | −#include "../dnn_interface.h" | |
| 31 | − | |
| 32 | −DNNModel *ff_dnn_load_model_ov(const char *model_filename, DNNFunctionType func_type, const char *options, AVFilterContext *filter_ctx); | |
| 33 | − | |
| 34 | −int ff_dnn_execute_model_ov(const DNNModel *model, DNNExecBaseParams *exec_params); | |
| 35 | −DNNAsyncStatusType ff_dnn_get_result_ov(const DNNModel *model, AVFrame **in, AVFrame **out); | |
| 36 | −int ff_dnn_flush_ov(const DNNModel *model); | |
| 37 | − | |
| 38 | −void ff_dnn_free_model_ov(DNNModel **model); | |
| 39 | − | |
| 40 | −#endif | |
| @@ -23,7 +23,6 @@ · 22 unmodified lines | ||
| 23 | 23 | * DNN tensorflow backend implementation. |
| 24 | 24 | */ |
| 25 | 25 | |
| 26 | −#include "dnn_backend_tf.h" | |
| 27 | 26 | #include "libavformat/avio.h" |
| 28 | 27 | #include "libavutil/avassert.h" |
| 29 | 28 | #include "libavutil/avstring.h" |
| @@ -477,7 +476,48 @@ static int load_tf_model(TFModel *tf_model, const char *model_filename) · 447 unmodified lines | ||
| 477 | 476 | |
| 478 | 477 | #define NAME_BUFFER_SIZE 256 |
| 479 | 478 | |
| 480 | −DNNModel *ff_dnn_load_model_tf(const char *model_filename, DNNFunctionType func_type, const char *options, AVFilterContext *filter_ctx) | |
| 479 | +static void dnn_free_model_tf(DNNModel **model) | |
| 480 | +{ | |
| 481 | + TFModel *tf_model; | |
| 482 | + | |
| 483 | + if (*model){ | |
| 484 | + tf_model = (*model)->model; | |
| 485 | + while (ff_safe_queue_size(tf_model->request_queue) != 0) { | |
| 486 | + TFRequestItem *item = ff_safe_queue_pop_front(tf_model->request_queue); | |
| 487 | + destroy_request_item(&item); | |
| 488 | + } | |
| 489 | + ff_safe_queue_destroy(tf_model->request_queue); | |
| 490 | + | |
| 491 | + while (ff_queue_size(tf_model->lltask_queue) != 0) { | |
| 492 | + LastLevelTaskItem *item = ff_queue_pop_front(tf_model->lltask_queue); | |
| 493 | + av_freep(&item); | |
| 494 | + } | |
| 495 | + ff_queue_destroy(tf_model->lltask_queue); | |
| 496 | + | |
| 497 | + while (ff_queue_size(tf_model->task_queue) != 0) { | |
| 498 | + TaskItem *item = ff_queue_pop_front(tf_model->task_queue); | |
| 499 | + av_frame_free(&item->in_frame); | |
| 500 | + av_frame_free(&item->out_frame); | |
| 501 | + av_freep(&item); | |
| 502 | + } | |
| 503 | + ff_queue_destroy(tf_model->task_queue); | |
| 504 | + | |
| 505 | + if (tf_model->graph){ | |
| 506 | + TF_DeleteGraph(tf_model->graph); | |
| 507 | + } | |
| 508 | + if (tf_model->session){ | |
| 509 | + TF_CloseSession(tf_model->session, tf_model->status); | |
| 510 | + TF_DeleteSession(tf_model->session, tf_model->status); | |
| 511 | + } | |
| 512 | + if (tf_model->status){ | |
| 513 | + TF_DeleteStatus(tf_model->status); | |
| 514 | + } | |
| 515 | + av_freep(&tf_model); | |
| 516 | + av_freep(model); | |
| 517 | + } | |
| 518 | +} | |
| 519 | + | |
| 520 | +static DNNModel *dnn_load_model_tf(const char *model_filename, DNNFunctionType func_type, const char *options, AVFilterContext *filter_ctx) | |
| 481 | 521 | { |
| 482 | 522 | DNNModel *model = NULL; |
| 483 | 523 | TFModel *tf_model = NULL; |
| @@ -567,7 +607,7 @@ DNNModel *ff_dnn_load_model_tf(const char *model_filename, DNNFunctionType func_ · 83 unmodified lines | ||
| 567 | 607 | |
| 568 | 608 | return model; |
| 569 | 609 | err: |
| 570 | − ff_dnn_free_model_tf(&model); | |
| 610 | + dnn_free_model_tf(&model); | |
| 571 | 611 | return NULL; |
| 572 | 612 | } |
| 573 | 613 | |
| @@ -765,11 +805,11 @@ err: · 191 unmodified lines | ||
| 765 | 805 | if (ff_safe_queue_push_back(tf_model->request_queue, request) < 0) { |
| 766 | 806 | destroy_request_item(&request); |
| 767 | 807 | } |
| 768 | − ff_dnn_free_model_tf(&tf_model->model); | |
| 808 | + dnn_free_model_tf(&tf_model->model); | |
| 769 | 809 | return ret; |
| 770 | 810 | } |
| 771 | 811 | |
| 772 | −int ff_dnn_execute_model_tf(const DNNModel *model, DNNExecBaseParams *exec_params) | |
| 812 | +static int dnn_execute_model_tf(const DNNModel *model, DNNExecBaseParams *exec_params) | |
| 773 | 813 | { |
| 774 | 814 | TFModel *tf_model = model->model; |
| 775 | 815 | TFContext *ctx = &tf_model->ctx; |
| @@ -817,13 +857,13 @@ int ff_dnn_execute_model_tf(const DNNModel *model, DNNExecBaseParams *exec_param · 41 unmodified lines | ||
| 817 | 857 | return execute_model_tf(request, tf_model->lltask_queue); |
| 818 | 858 | } |
| 819 | 859 | |
| 820 | −DNNAsyncStatusType ff_dnn_get_result_tf(const DNNModel *model, AVFrame **in, AVFrame **out) | |
| 860 | +static DNNAsyncStatusType dnn_get_result_tf(const DNNModel *model, AVFrame **in, AVFrame **out) | |
| 821 | 861 | { |
| 822 | 862 | TFModel *tf_model = model->model; |
| 823 | 863 | return ff_dnn_get_result_common(tf_model->task_queue, in, out); |
| 824 | 864 | } |
| 825 | 865 | |
| 826 | −int ff_dnn_flush_tf(const DNNModel *model) | |
| 866 | +static int dnn_flush_tf(const DNNModel *model) | |
| 827 | 867 | { |
| 828 | 868 | TFModel *tf_model = model->model; |
| 829 | 869 | TFContext *ctx = &tf_model->ctx; |
| @@ -853,43 +893,10 @@ int ff_dnn_flush_tf(const DNNModel *model) · 23 unmodified lines | ||
| 853 | 893 | return ff_dnn_start_inference_async(ctx, &request->exec_module); |
| 854 | 894 | } |
| 855 | 895 | |
| 856 | −void ff_dnn_free_model_tf(DNNModel **model) | |
| 857 | −{ | |
| 858 | − TFModel *tf_model; | |
| 859 | − | |
| 860 | − if (*model){ | |
| 861 | − tf_model = (*model)->model; | |
| 862 | − while (ff_safe_queue_size(tf_model->request_queue) != 0) { | |
| 863 | − TFRequestItem *item = ff_safe_queue_pop_front(tf_model->request_queue); | |
| 864 | − destroy_request_item(&item); | |
| 865 | − } | |
| 866 | − ff_safe_queue_destroy(tf_model->request_queue); | |
| 867 | − | |
| 868 | − while (ff_queue_size(tf_model->lltask_queue) != 0) { | |
| 869 | − LastLevelTaskItem *item = ff_queue_pop_front(tf_model->lltask_queue); | |
| 870 | − av_freep(&item); | |
| 871 | − } | |
| 872 | − ff_queue_destroy(tf_model->lltask_queue); | |
| 873 | − | |
| 874 | − while (ff_queue_size(tf_model->task_queue) != 0) { | |
| 875 | − TaskItem *item = ff_queue_pop_front(tf_model->task_queue); | |
| 876 | − av_frame_free(&item->in_frame); | |
| 877 | − av_frame_free(&item->out_frame); | |
| 878 | − av_freep(&item); | |
| 879 | − } | |
| 880 | − ff_queue_destroy(tf_model->task_queue); | |
| 881 | − | |
| 882 | − if (tf_model->graph){ | |
| 883 | − TF_DeleteGraph(tf_model->graph); | |
| 884 | − } | |
| 885 | − if (tf_model->session){ | |
| 886 | − TF_CloseSession(tf_model->session, tf_model->status); | |
| 887 | − TF_DeleteSession(tf_model->session, tf_model->status); | |
| 888 | − } | |
| 889 | − if (tf_model->status){ | |
| 890 | − TF_DeleteStatus(tf_model->status); | |
| 891 | − } | |
| 892 | − av_freep(&tf_model); | |
| 893 | − av_freep(model); | |
| 894 | − } | |
| 895 | −} | |
| 896 | +const DNNModule ff_dnn_backend_tf = { | |
| 897 | + .load_model = dnn_load_model_tf, | |
| 898 | + .execute_model = dnn_execute_model_tf, | |
| 899 | + .get_result = dnn_get_result_tf, | |
| 900 | + .flush = dnn_flush_tf, | |
| 901 | + .free_model = dnn_free_model_tf, | |
| 902 | +}; | |
| @@ -1,40 +0,0 @@ | ||
| 1 | −/* | |
| 2 | − * Copyright (c) 2018 Sergey Lavrushkin | |
| 3 | − * | |
| 4 | − * This file is part of FFmpeg. | |
| 5 | − * | |
| 6 | − * FFmpeg is free software; you can redistribute it and/or | |
| 7 | − * modify it under the terms of the GNU Lesser General Public | |
| 8 | − * License as published by the Free Software Foundation; either | |
| 9 | − * version 2.1 of the License, or (at your option) any later version. | |
| 10 | − * | |
| 11 | − * FFmpeg is distributed in the hope that it will be useful, | |
| 12 | − * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 13 | − * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 14 | − * Lesser General Public License for more details. | |
| 15 | − * | |
| 16 | − * You should have received a copy of the GNU Lesser General Public | |
| 17 | − * License along with FFmpeg; if not, write to the Free Software | |
| 18 | − * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
| 19 | − */ | |
| 20 | − | |
| 21 | −/** | |
| 22 | − * @file | |
| 23 | − * DNN inference functions interface for TensorFlow backend. | |
| 24 | − */ | |
| 25 | − | |
| 26 | − | |
| 27 | −#ifndef AVFILTER_DNN_DNN_BACKEND_TF_H | |
| 28 | −#define AVFILTER_DNN_DNN_BACKEND_TF_H | |
| 29 | − | |
| 30 | −#include "../dnn_interface.h" | |
| 31 | − | |
| 32 | −DNNModel *ff_dnn_load_model_tf(const char *model_filename, DNNFunctionType func_type, const char *options, AVFilterContext *filter_ctx); | |
| 33 | − | |
| 34 | −int ff_dnn_execute_model_tf(const DNNModel *model, DNNExecBaseParams *exec_params); | |
| 35 | −DNNAsyncStatusType ff_dnn_get_result_tf(const DNNModel *model, AVFrame **in, AVFrame **out); | |
| 36 | −int ff_dnn_flush_tf(const DNNModel *model); | |
| 37 | − | |
| 38 | −void ff_dnn_free_model_tf(DNNModel **model); | |
| 39 | − | |
| 40 | −#endif | |
| @@ -24,10 +24,11 @@ · 23 unmodified lines | ||
| 24 | 24 | */ |
| 25 | 25 | |
| 26 | 26 | #include "../dnn_interface.h" |
| 27 | −#include "dnn_backend_tf.h" | |
| 28 | −#include "dnn_backend_openvino.h" | |
| 29 | 27 | #include "libavutil/mem.h" |
| 30 | 28 | |
| 29 | +extern const DNNModule ff_dnn_backend_openvino; | |
| 30 | +extern const DNNModule ff_dnn_backend_tf; | |
| 31 | + | |
| 31 | 32 | DNNModule *ff_get_dnn_module(DNNBackendType backend_type) |
| 32 | 33 | { |
| 33 | 34 | DNNModule *dnn_module; |
| @@ -40,11 +41,7 @@ DNNModule *ff_get_dnn_module(DNNBackendType backend_type) · 6 unmodified lines | ||
| 40 | 41 | switch(backend_type){ |
| 41 | 42 | case DNN_TF: |
| 42 | 43 | #if (CONFIG_LIBTENSORFLOW == 1) |
| 43 | − dnn_module->load_model = &ff_dnn_load_model_tf; | |
| 44 | − dnn_module->execute_model = &ff_dnn_execute_model_tf; | |
| 45 | − dnn_module->get_result = &ff_dnn_get_result_tf; | |
| 46 | − dnn_module->flush = &ff_dnn_flush_tf; | |
| 47 | − dnn_module->free_model = &ff_dnn_free_model_tf; | |
| 44 | + *dnn_module = ff_dnn_backend_tf; | |
| 48 | 45 | #else |
| 49 | 46 | av_freep(&dnn_module); |
| 50 | 47 | return NULL; |
| @@ -52,11 +49,7 @@ DNNModule *ff_get_dnn_module(DNNBackendType backend_type) · 1 unmodified line | ||
| 52 | 49 | break; |
| 53 | 50 | case DNN_OV: |
| 54 | 51 | #if (CONFIG_LIBOPENVINO == 1) |
| 55 | − dnn_module->load_model = &ff_dnn_load_model_ov; | |
| 56 | − dnn_module->execute_model = &ff_dnn_execute_model_ov; | |
| 57 | − dnn_module->get_result = &ff_dnn_get_result_ov; | |
| 58 | − dnn_module->flush = &ff_dnn_flush_ov; | |
| 59 | − dnn_module->free_model = &ff_dnn_free_model_ov; | |
| 52 | + *dnn_module = ff_dnn_backend_openvino; | |
| 60 | 53 | #else |
| 61 | 54 | av_freep(&dnn_module); |
| 62 | 55 | return NULL; |