FFmpeg
Loading...
Searching...
No Matches
dnn_backend_common.c
Go to the documentation of this file.
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
30int 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
50int 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 */
74static 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
87{
88 void *status = 0;
89 if (!async_module) {
90 return AVERROR(EINVAL);
91 }
92 if (async_module->thread_started) {
93 pthread_join(async_module->thread_id, &status);
94 async_module->thread_started = 0;
95 if (status == DNN_ASYNC_FAIL) {
96 av_log(NULL, AV_LOG_ERROR, "Last Inference Failed.\n");
97 return DNN_GENERIC_ERROR;
98 }
99 }
100 async_module->start_inference = NULL;
101 async_module->callback = NULL;
102 async_module->args = NULL;
103 return 0;
104}
105
106void ff_dnn_wait_requests(SafeQueue *request_queue, int nireq)
107{
108 if (!request_queue)
109 return;
110
111 ff_safe_queue_wait_for_size(request_queue, nireq);
112}
113
115{
116 int ret;
117 void *status = 0;
118
119 if (!async_module) {
120 av_log(ctx, AV_LOG_ERROR, "async_module is null when starting async inference.\n");
121 return AVERROR(EINVAL);
122 }
123
124 if (async_module->thread_started) {
125 pthread_join(async_module->thread_id, &status);
126 async_module->thread_started = 0;
127 if (status == DNN_ASYNC_FAIL) {
128 av_log(ctx, AV_LOG_ERROR, "Unable to start inference as previous inference failed.\n");
129 return DNN_GENERIC_ERROR;
130 }
131 }
132 ret = pthread_create(&async_module->thread_id, NULL, async_thread_routine, async_module);
133 if (ret != 0) {
134 av_log(ctx, AV_LOG_ERROR, "Unable to start async inference.\n");
135 return ret;
136 }
137 async_module->thread_started = 1;
138
139 return 0;
140}
141
143{
144 TaskItem *task = ff_queue_peek_front(task_queue);
145
146 if (!task) {
147 return DAST_EMPTY_QUEUE;
148 }
149
150 if (task->inference_done != task->inference_todo) {
151 return DAST_NOT_READY;
152 }
153
154 *in = task->in_frame;
155 *out = task->out_frame;
156 ff_queue_pop_front(task_queue);
157 av_freep(&task);
158
159 return DAST_SUCCESS;
160}
161
162int ff_dnn_fill_gettingoutput_task(TaskItem *task, DNNExecBaseParams *exec_params, void *backend_model, int input_height, int input_width, void *ctx)
163{
164 AVFrame *in_frame = NULL;
165 AVFrame *out_frame = NULL;
166
167 in_frame = av_frame_alloc();
168 if (!in_frame) {
169 av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for input frame\n");
170 return AVERROR(ENOMEM);
171 }
172
173 out_frame = av_frame_alloc();
174 if (!out_frame) {
175 av_frame_free(&in_frame);
176 av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for output frame\n");
177 return AVERROR(ENOMEM);
178 }
179
180 in_frame->width = input_width;
181 in_frame->height = input_height;
182 exec_params->in_frame = in_frame;
183 exec_params->out_frame = out_frame;
184
185 return ff_dnn_fill_task(task, exec_params, backend_model, 0, 0);
186}
static FILE * out
static AVFormatContext * ctx
#define NULL
Definition coverity.c:32
int ff_check_exec_params(void *ctx, DNNBackendType backend, DNNFunctionType func_type, DNNExecBaseParams *exec_params)
void ff_dnn_wait_requests(SafeQueue *request_queue, int nireq)
Wait for all inference requests to complete before teardown.
#define DNN_ASYNC_SUCCESS
static void * async_thread_routine(void *args)
Thread routine for async execution.
DNNAsyncStatusType ff_dnn_get_result_common(Queue *task_queue, AVFrame **in, AVFrame **out)
Extract input and output frame from the Task Queue after asynchronous inference.
int ff_dnn_async_module_cleanup(DNNAsyncExecModule *async_module)
Join the Async Execution thread and set module pointers to NULL.
#define DNN_ASYNC_FAIL
int ff_dnn_fill_task(TaskItem *task, DNNExecBaseParams *exec_params, void *backend_model, int async, int do_ioproc)
Fill the Task for Backend Execution.
int ff_dnn_start_inference_async(void *ctx, DNNAsyncExecModule *async_module)
Start asynchronous inference routine for the TensorFlow model on a detached thread.
int ff_dnn_fill_gettingoutput_task(TaskItem *task, DNNExecBaseParams *exec_params, void *backend_model, int input_height, int input_width, void *ctx)
Allocate input and output frames and fill the Task with execution parameters.
DNN common functions different backends.
DNNAsyncStatusType
@ DAST_NOT_READY
@ DAST_EMPTY_QUEUE
@ DAST_SUCCESS
DNNBackendType
DNNFunctionType
@ DFT_PROCESS_FRAME
#define DNN_GENERIC_ERROR
#define AVERROR(e)
Definition error.h:45
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:52
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
Memory handling functions.
static av_always_inline int pthread_join(pthread_t thread, void **value_ptr)
Definition os2threads.h:94
static av_always_inline int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
Definition os2threads.h:80
void * ff_queue_pop_front(Queue *q)
Remove and free first element from the Queue.
Definition queue.c:151
void * ff_queue_peek_front(Queue *q)
Return a pointer to the data at the head of the queue.
Definition queue.c:93
void ff_safe_queue_wait_for_size(SafeQueue *sq, size_t min_size)
Wait until queue length reaches at least min_size.
Definition safe_queue.c:66
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
int width
Definition frame.h:551
int height
Definition frame.h:551
Common Async Execution Mechanism for the DNN Backends.
void * args
Argument for the execution functions.
int(* start_inference)(void *request)
Synchronous inference function for the backend with corresponding request item as the argument.
void(* callback)(void *args)
Completion Callback for the backend.
const char ** output_names
const char * input_name
Linear double-ended data structure.
Definition executor.c:51
Double-ended queue with mutex locks ensuring data consistency while multithreading.
Definition safe_queue.c:27
uint32_t inference_done
AVFrame * in_frame
const char ** output_names
uint8_t do_ioproc
uint32_t inference_todo
const char * input_name
AVFrame * out_frame
uint32_t nb_output
#define av_freep(p)
#define av_log(a,...)