Line |
Branch |
Exec |
Source |
1 |
|
|
/* |
2 |
|
|
* This file is part of FFmpeg. |
3 |
|
|
* |
4 |
|
|
* FFmpeg is free software; you can redistribute it and/or |
5 |
|
|
* modify it under the terms of the GNU Lesser General Public |
6 |
|
|
* License as published by the Free Software Foundation; either |
7 |
|
|
* version 2.1 of the License, or (at your option) any later version. |
8 |
|
|
* |
9 |
|
|
* FFmpeg is distributed in the hope that it will be useful, |
10 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 |
|
|
* Lesser General Public License for more details. |
13 |
|
|
* |
14 |
|
|
* You should have received a copy of the GNU Lesser General Public |
15 |
|
|
* License along with FFmpeg; if not, write to the Free Software |
16 |
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
17 |
|
|
*/ |
18 |
|
|
|
19 |
|
|
/** |
20 |
|
|
* @file |
21 |
|
|
* DNN common functions different backends. |
22 |
|
|
*/ |
23 |
|
|
|
24 |
|
|
#include "libavutil/mem.h" |
25 |
|
|
#include "dnn_backend_common.h" |
26 |
|
|
|
27 |
|
|
#define DNN_ASYNC_SUCCESS (void *)0 |
28 |
|
|
#define DNN_ASYNC_FAIL (void *)-1 |
29 |
|
|
|
30 |
|
✗ |
int ff_check_exec_params(void *ctx, DNNBackendType backend, DNNFunctionType func_type, DNNExecBaseParams *exec_params) |
31 |
|
|
{ |
32 |
|
✗ |
if (!exec_params) { |
33 |
|
✗ |
av_log(ctx, AV_LOG_ERROR, "exec_params is null when execute model.\n"); |
34 |
|
✗ |
return AVERROR(EINVAL); |
35 |
|
|
} |
36 |
|
|
|
37 |
|
✗ |
if (!exec_params->in_frame) { |
38 |
|
✗ |
av_log(ctx, AV_LOG_ERROR, "in frame is NULL when execute model.\n"); |
39 |
|
✗ |
return AVERROR(EINVAL); |
40 |
|
|
} |
41 |
|
|
|
42 |
|
✗ |
if (!exec_params->out_frame && func_type == DFT_PROCESS_FRAME) { |
43 |
|
✗ |
av_log(ctx, AV_LOG_ERROR, "out frame is NULL when execute model.\n"); |
44 |
|
✗ |
return AVERROR(EINVAL); |
45 |
|
|
} |
46 |
|
|
|
47 |
|
✗ |
return 0; |
48 |
|
|
} |
49 |
|
|
|
50 |
|
✗ |
int ff_dnn_fill_task(TaskItem *task, DNNExecBaseParams *exec_params, void *backend_model, int async, int do_ioproc) { |
51 |
|
✗ |
if (task == NULL || exec_params == NULL || backend_model == NULL) |
52 |
|
✗ |
return AVERROR(EINVAL); |
53 |
|
✗ |
if (do_ioproc != 0 && do_ioproc != 1) |
54 |
|
✗ |
return AVERROR(EINVAL); |
55 |
|
✗ |
if (async != 0 && async != 1) |
56 |
|
✗ |
return AVERROR(EINVAL); |
57 |
|
|
|
58 |
|
✗ |
task->do_ioproc = do_ioproc; |
59 |
|
✗ |
task->async = async; |
60 |
|
✗ |
task->input_name = exec_params->input_name; |
61 |
|
✗ |
task->in_frame = exec_params->in_frame; |
62 |
|
✗ |
task->out_frame = exec_params->out_frame; |
63 |
|
✗ |
task->model = backend_model; |
64 |
|
✗ |
task->nb_output = exec_params->nb_output; |
65 |
|
✗ |
task->output_names = exec_params->output_names; |
66 |
|
|
|
67 |
|
✗ |
return 0; |
68 |
|
|
} |
69 |
|
|
|
70 |
|
|
/** |
71 |
|
|
* Thread routine for async execution. |
72 |
|
|
* @param args pointer to DNNAsyncExecModule module |
73 |
|
|
*/ |
74 |
|
✗ |
static void *async_thread_routine(void *args) |
75 |
|
|
{ |
76 |
|
✗ |
DNNAsyncExecModule *async_module = args; |
77 |
|
✗ |
void *request = async_module->args; |
78 |
|
|
|
79 |
|
✗ |
if (async_module->start_inference(request) != 0) { |
80 |
|
✗ |
return DNN_ASYNC_FAIL; |
81 |
|
|
} |
82 |
|
✗ |
async_module->callback(request); |
83 |
|
✗ |
return DNN_ASYNC_SUCCESS; |
84 |
|
|
} |
85 |
|
|
|
86 |
|
✗ |
int ff_dnn_async_module_cleanup(DNNAsyncExecModule *async_module) |
87 |
|
|
{ |
88 |
|
✗ |
void *status = 0; |
89 |
|
✗ |
if (!async_module) { |
90 |
|
✗ |
return AVERROR(EINVAL); |
91 |
|
|
} |
92 |
|
|
#if HAVE_PTHREAD_CANCEL |
93 |
|
✗ |
pthread_join(async_module->thread_id, &status); |
94 |
|
✗ |
if (status == DNN_ASYNC_FAIL) { |
95 |
|
✗ |
av_log(NULL, AV_LOG_ERROR, "Last Inference Failed.\n"); |
96 |
|
✗ |
return DNN_GENERIC_ERROR; |
97 |
|
|
} |
98 |
|
|
#endif |
99 |
|
✗ |
async_module->start_inference = NULL; |
100 |
|
✗ |
async_module->callback = NULL; |
101 |
|
✗ |
async_module->args = NULL; |
102 |
|
✗ |
return 0; |
103 |
|
|
} |
104 |
|
|
|
105 |
|
✗ |
int ff_dnn_start_inference_async(void *ctx, DNNAsyncExecModule *async_module) |
106 |
|
|
{ |
107 |
|
|
int ret; |
108 |
|
✗ |
void *status = 0; |
109 |
|
|
|
110 |
|
✗ |
if (!async_module) { |
111 |
|
✗ |
av_log(ctx, AV_LOG_ERROR, "async_module is null when starting async inference.\n"); |
112 |
|
✗ |
return AVERROR(EINVAL); |
113 |
|
|
} |
114 |
|
|
|
115 |
|
|
#if HAVE_PTHREAD_CANCEL |
116 |
|
✗ |
pthread_join(async_module->thread_id, &status); |
117 |
|
✗ |
if (status == DNN_ASYNC_FAIL) { |
118 |
|
✗ |
av_log(ctx, AV_LOG_ERROR, "Unable to start inference as previous inference failed.\n"); |
119 |
|
✗ |
return DNN_GENERIC_ERROR; |
120 |
|
|
} |
121 |
|
✗ |
ret = pthread_create(&async_module->thread_id, NULL, async_thread_routine, async_module); |
122 |
|
✗ |
if (ret != 0) { |
123 |
|
✗ |
av_log(ctx, AV_LOG_ERROR, "Unable to start async inference.\n"); |
124 |
|
✗ |
return ret; |
125 |
|
|
} |
126 |
|
|
#else |
127 |
|
|
ret = async_module->start_inference(async_module->args); |
128 |
|
|
if (ret != 0) { |
129 |
|
|
return ret; |
130 |
|
|
} |
131 |
|
|
async_module->callback(async_module->args); |
132 |
|
|
#endif |
133 |
|
✗ |
return 0; |
134 |
|
|
} |
135 |
|
|
|
136 |
|
✗ |
DNNAsyncStatusType ff_dnn_get_result_common(Queue *task_queue, AVFrame **in, AVFrame **out) |
137 |
|
|
{ |
138 |
|
✗ |
TaskItem *task = ff_queue_peek_front(task_queue); |
139 |
|
|
|
140 |
|
✗ |
if (!task) { |
141 |
|
✗ |
return DAST_EMPTY_QUEUE; |
142 |
|
|
} |
143 |
|
|
|
144 |
|
✗ |
if (task->inference_done != task->inference_todo) { |
145 |
|
✗ |
return DAST_NOT_READY; |
146 |
|
|
} |
147 |
|
|
|
148 |
|
✗ |
*in = task->in_frame; |
149 |
|
✗ |
*out = task->out_frame; |
150 |
|
✗ |
ff_queue_pop_front(task_queue); |
151 |
|
✗ |
av_freep(&task); |
152 |
|
|
|
153 |
|
✗ |
return DAST_SUCCESS; |
154 |
|
|
} |
155 |
|
|
|
156 |
|
✗ |
int ff_dnn_fill_gettingoutput_task(TaskItem *task, DNNExecBaseParams *exec_params, void *backend_model, int input_height, int input_width, void *ctx) |
157 |
|
|
{ |
158 |
|
✗ |
AVFrame *in_frame = NULL; |
159 |
|
✗ |
AVFrame *out_frame = NULL; |
160 |
|
|
|
161 |
|
✗ |
in_frame = av_frame_alloc(); |
162 |
|
✗ |
if (!in_frame) { |
163 |
|
✗ |
av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for input frame\n"); |
164 |
|
✗ |
return AVERROR(ENOMEM); |
165 |
|
|
} |
166 |
|
|
|
167 |
|
✗ |
out_frame = av_frame_alloc(); |
168 |
|
✗ |
if (!out_frame) { |
169 |
|
✗ |
av_frame_free(&in_frame); |
170 |
|
✗ |
av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for output frame\n"); |
171 |
|
✗ |
return AVERROR(ENOMEM); |
172 |
|
|
} |
173 |
|
|
|
174 |
|
✗ |
in_frame->width = input_width; |
175 |
|
✗ |
in_frame->height = input_height; |
176 |
|
✗ |
exec_params->in_frame = in_frame; |
177 |
|
✗ |
exec_params->out_frame = out_frame; |
178 |
|
|
|
179 |
|
✗ |
return ff_dnn_fill_task(task, exec_params, backend_model, 0, 0); |
180 |
|
|
} |
181 |
|
|
|