FFmpeg
Loading...
Searching...
No Matches
dnn_backend_common.h
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#ifndef AVFILTER_DNN_DNN_BACKEND_COMMON_H
25#define AVFILTER_DNN_DNN_BACKEND_COMMON_H
26
27#include "queue.h"
28#include "safe_queue.h"
29#include "../dnn_interface.h"
30#include "libavutil/thread.h"
31
32#define DNN_DEFINE_CLASS_EXT(name, desc, options) \
33 { \
34 .class_name = desc, \
35 .item_name = av_default_item_name, \
36 .option = options, \
37 .version = LIBAVUTIL_VERSION_INT, \
38 .category = AV_CLASS_CATEGORY_FILTER, \
39 }
40#define DNN_DEFINE_CLASS(fname) \
41 DNN_DEFINE_CLASS_EXT(fname, #fname, fname##_options)
42
43// one task for one function call from dnn interface
44typedef struct TaskItem {
45 void *model; // model for the backend
48 const char *input_name;
49 const char **output_names;
50 uint8_t async;
51 uint8_t do_ioproc;
52 uint32_t nb_output;
55} TaskItem;
56
57// one task might have multiple inferences
62
63/**
64 * Common Async Execution Mechanism for the DNN Backends.
65 */
66typedef struct DNNAsyncExecModule {
67 /**
68 * Synchronous inference function for the backend
69 * with corresponding request item as the argument.
70 */
71 int (*start_inference)(void *request);
72
73 /**
74 * Completion Callback for the backend.
75 * Expected argument type of callback must match that
76 * of the inference function.
77 */
78 void (*callback)(void *args);
79
80 /**
81 * Argument for the execution functions.
82 * i.e. Request item for the backend.
83 */
84 void *args;
85#if HAVE_PTHREAD_CANCEL
86 pthread_t thread_id;
87 pthread_attr_t thread_attr;
88#endif
90
91int ff_check_exec_params(void *ctx, DNNBackendType backend, DNNFunctionType func_type, DNNExecBaseParams *exec_params);
92
93/**
94 * Fill the Task for Backend Execution. It should be called after
95 * checking execution parameters using ff_check_exec_params.
96 *
97 * @param task pointer to the allocated task
98 * @param exec_param pointer to execution parameters
99 * @param backend_model void pointer to the backend model
100 * @param async flag for async execution. Must be 0 or 1
101 * @param do_ioproc flag for IO processing. Must be 0 or 1
102 *
103 * @returns 0 if successful or error code otherwise.
104 */
105int ff_dnn_fill_task(TaskItem *task, DNNExecBaseParams *exec_params, void *backend_model, int async, int do_ioproc);
106
107/**
108 * Join the Async Execution thread and set module pointers to NULL.
109 *
110 * @param async_module pointer to DNNAsyncExecModule module
111 *
112 * @returns 0 if successful or error code otherwise.
113 */
115
116/**
117 * Wait for all inference requests to complete before teardown.
118 * This blocks the calling thread until all request items have been
119 * returned to the request_queue by the async inference threads.
120 *
121 * @param request_queue pointer to the SafeQueue holding request items
122 * @param nireq total number of allocated request items
123 */
124void ff_dnn_wait_requests(SafeQueue *request_queue, int nireq);
125
126/**
127 * Start asynchronous inference routine for the TensorFlow
128 * model on a detached thread. It calls the completion callback
129 * after the inference completes. Completion callback and inference
130 * function must be set before calling this function.
131 *
132 * If POSIX threads aren't supported, the execution rolls back
133 * to synchronous mode, calling completion callback after inference.
134 *
135 * @param ctx pointer to the backend context
136 * @param async_module pointer to DNNAsyncExecModule module
137 *
138 * @returns 0 on the start of async inference or error code otherwise.
139 */
140int ff_dnn_start_inference_async(void *ctx, DNNAsyncExecModule *async_module);
141
142/**
143 * Extract input and output frame from the Task Queue after
144 * asynchronous inference.
145 *
146 * @param task_queue pointer to the task queue of the backend
147 * @param in double pointer to the input frame
148 * @param out double pointer to the output frame
149 *
150 * @retval DAST_EMPTY_QUEUE if task queue is empty
151 * @retval DAST_NOT_READY if inference not completed yet.
152 * @retval DAST_SUCCESS if result successfully extracted
153 */
155
156/**
157 * Allocate input and output frames and fill the Task
158 * with execution parameters.
159 *
160 * @param task pointer to the allocated task
161 * @param exec_params pointer to execution parameters
162 * @param backend_model void pointer to the backend model
163 * @param input_height height of input frame
164 * @param input_width width of input frame
165 * @param ctx pointer to the backend context
166 *
167 * @returns 0 if successful or error code otherwise.
168 */
169int ff_dnn_fill_gettingoutput_task(TaskItem *task, DNNExecBaseParams *exec_params, void *backend_model, int input_height, int input_width, void *ctx);
170
171#endif
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.
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.
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 inference engine interface.
DNNAsyncStatusType
DNNBackendType
DNNFunctionType
void pthread_attr_t
Definition os2threads.h:51
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
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.
Linear double-ended data structure.
Definition executor.c:51
Double-ended queue with mutex locks ensuring data consistency while multithreading.
Definition safe_queue.c:46
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
static FILE * out
Definition movenc.c:55
static AVFormatContext * ctx
Definition movenc.c:49