Go to the documentation of this file.
32 #include "../internal.h"
36 #include <tensorflow/c/c_api.h>
78 #define OFFSET(x) offsetof(TFContext, x)
79 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM
116 for (uint32_t
i = 0;
i < nb_output; ++
i) {
135 if (!infer_request) {
142 return infer_request;
171 if (TF_GetCode(request->
status) != TF_OK) {
192 TF_DeleteStatus(request->
status);
219 TF_Buffer *graph_buf;
220 unsigned char *graph_data =
NULL;
222 long size, bytes_read;
235 bytes_read =
avio_read(model_file_context, graph_data,
size);
237 if (bytes_read !=
size){
242 graph_buf = TF_NewBuffer();
243 graph_buf->data = graph_data;
244 graph_buf->length =
size;
273 return TF_AllocateTensor(dt, input_dims, 4,
274 input_dims[1] * input_dims[2] * input_dims[3] *
size);
286 tf_output.oper = TF_GraphOperationByName(tf_model->
graph, input_name);
287 if (!tf_output.oper) {
293 dt = TF_OperationOutputType(tf_output);
308 TF_GraphGetTensorShape(tf_model->
graph, tf_output, dims, 4,
status);
309 if (TF_GetCode(
status) != TF_OK){
318 for (
int i = 0;
i < 4;
i++)
325 static int get_output_tf(
void *model,
const char *input_name,
int input_width,
int input_height,
326 const char *output_name,
int *output_width,
int *output_height)
335 .output_names = &output_name,
369 #define SPACE_CHARS " \t\r\n"
381 if (
c >=
'0' &&
c <=
'9')
383 else if (
c >=
'A' &&
c <=
'F')
402 TF_Buffer *graph_def;
403 TF_ImportGraphDefOptions *graph_opts;
404 TF_SessionOptions *sess_opts;
405 const TF_Operation *init_op;
406 uint8_t *sess_config =
NULL;
407 int sess_config_length = 0;
442 tf_model->
graph = TF_NewGraph();
443 tf_model->
status = TF_NewStatus();
444 graph_opts = TF_NewImportGraphDefOptions();
445 TF_GraphImportGraphDef(tf_model->
graph, graph_def, graph_opts, tf_model->
status);
446 TF_DeleteImportGraphDefOptions(graph_opts);
447 TF_DeleteBuffer(graph_def);
448 if (TF_GetCode(tf_model->
status) != TF_OK){
454 init_op = TF_GraphOperationByName(tf_model->
graph,
"init");
455 sess_opts = TF_NewSessionOptions();
458 TF_SetConfig(sess_opts, sess_config, sess_config_length,tf_model->
status);
460 if (TF_GetCode(tf_model->
status) != TF_OK) {
461 TF_DeleteSessionOptions(sess_opts);
469 TF_DeleteSessionOptions(sess_opts);
470 if (TF_GetCode(tf_model->
status) != TF_OK)
483 if (TF_GetCode(tf_model->
status) != TF_OK)
499 tf_model = (*model)->
model;
520 if (tf_model->
graph){
521 TF_DeleteGraph(tf_model->
graph);
528 TF_DeleteStatus(tf_model->
status);
551 model->
model = tf_model;
552 tf_model->
model = model;
554 ctx->class = &dnn_tensorflow_class;
568 if (
ctx->options.nireq <= 0) {
572 #if !HAVE_PTHREAD_CANCEL
573 if (
ctx->options.async) {
574 ctx->options.async = 0;
584 for (
int i = 0;
i <
ctx->options.nireq;
i++) {
596 item->
status = TF_NewStatus();
659 if (!infer_request->
tf_input->oper){
801 tf_model = task->
model;
AVFILTER_DEFINE_CLASS(dnn_tensorflow)
#define AV_LOG_WARNING
Something somehow does not look correct.
Stores execution parameters for single call to the TensorFlow C API.
static int execute_model_tf(TFRequestItem *request, Queue *lltask_queue)
void av_opt_set_defaults(void *s)
Set the values of all AVOption fields to their default values.
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
void * ff_safe_queue_pop_front(SafeQueue *sq)
Remove and free first element from the queue in SafeQueue.
Common Async Execution Mechanism for the DNN Backends.
static int extract_lltask_from_task(TaskItem *task, Queue *lltask_queue)
void * ff_queue_pop_front(Queue *q)
Remove and free first element from the Queue.
int ff_check_exec_params(void *ctx, DNNBackendType backend, DNNFunctionType func_type, DNNExecBaseParams *exec_params)
size_t ff_queue_size(Queue *q)
Return the length of the Queue.
#define DNN_GENERIC_ERROR
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
This structure describes decoded (raw) audio or video data.
static DNNModel * dnn_load_model_tf(const char *model_filename, DNNFunctionType func_type, const char *options, AVFilterContext *filter_ctx)
Double-ended queue with mutex locks ensuring data consistency while multithreading.
int av_opt_set_from_string(void *ctx, const char *opts, const char *const *shorthand, const char *key_val_sep, const char *pairs_sep)
Parse the key-value pairs list in opts.
DNNModel *(* load_model)(const char *model_filename, DNNFunctionType func_type, const char *options, AVFilterContext *filter_ctx)
FramePrePostProc frame_pre_proc
int avio_open(AVIOContext **s, const char *filename, int flags)
Create and initialize a AVIOContext for accessing the resource indicated by url.
static int load_tf_model(TFModel *tf_model, const char *model_filename)
SafeQueue * request_queue
void(* callback)(void *args)
Completion Callback for the backend.
int64_t avio_size(AVIOContext *s)
Get the filesize.
static void destroy_request_item(TFRequestItem **arg)
Free the TFRequestItem completely.
AVFilterContext * filter_ctx
Queue * ff_queue_create(void)
Create a Queue instance.
static int dnn_get_width_idx_by_layout(DNNLayout layout)
static FilteringContext * filter_ctx
static int get_input_tf(void *model, DNNData *input, const char *input_name)
Linear double-ended data structure.
int ff_queue_push_back(Queue *q, void *v)
Add data to the tail of the queue.
#define DNN_BACKEND_COMMON_OPTIONS
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static int fill_model_input_tf(TFModel *tf_model, TFRequestItem *request)
DNNAsyncExecModule exec_module
static TF_Buffer * read_graph(const char *model_filename)
void ff_queue_destroy(Queue *q)
Destroy the Queue instance.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
static const AVOption dnn_tensorflow_options[]
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.
size_t ff_safe_queue_size(SafeQueue *sq)
Return the length of the SafeQueue.
int ff_proc_from_frame_to_dnn(AVFrame *frame, DNNData *input, void *log_ctx)
Describe the class of an AVClass context structure.
int ff_frame_to_dnn_detect(AVFrame *frame, DNNData *input, void *log_ctx)
SafeQueue * ff_safe_queue_create(void)
Create and initialize a SafeQueue instance.
FramePrePostProc frame_post_proc
static TFInferRequest * tf_create_inference_request(void)
Create a TensorFlow inference request.
int ff_dnn_async_module_cleanup(DNNAsyncExecModule *async_module)
Join the Async Execution thread and set module pointers to NULL.
static void infer_completion_callback(void *args)
static void tf_free_request(TFInferRequest *request)
Free the contents of TensorFlow inference request.
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
const OptionDef options[]
DetectPostProc detect_post_proc
DNNFunctionType func_type
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
static int dnn_flush_tf(const DNNModel *model)
void ff_safe_queue_destroy(SafeQueue *sq)
Destroy the SafeQueue instance.
static int hex_to_data(uint8_t *data, const char *p)
static int tf_start_inference(void *args)
Start synchronous inference for the TensorFlow model.
int ff_dnn_fill_task(TaskItem *task, DNNExecBaseParams *exec_params, void *backend_model, int async, int do_ioproc)
Fill the Task for Backend Execution.
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some input
int ff_safe_queue_push_back(SafeQueue *sq, void *v)
Add data to the tail of queue in the SafeQueue after locking mutex.
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel layout
const DNNModule ff_dnn_backend_tf
static int dnn_execute_model_tf(const DNNModel *model, DNNExecBaseParams *exec_params)
#define i(width, name, range_min, range_max)
TF_Tensor ** output_tensors
TFInferRequest * infer_request
#define av_malloc_array(a, b)
int(* start_inference)(void *request)
Synchronous inference function for the backend with corresponding request item as the argument.
void * args
Argument for the execution functions.
static av_const int av_toupper(int c)
Locale-independent conversion of ASCII characters to uppercase.
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
const char ** output_names
void * av_calloc(size_t nmemb, size_t size)
static const AVFilterPad outputs[]
int(* get_input)(void *model, DNNData *input, const char *input_name)
#define AV_INPUT_BUFFER_PADDING_SIZE
static DNNAsyncStatusType dnn_get_result_tf(const DNNModel *model, AVFrame **in, AVFrame **out)
static TF_Tensor * allocate_input_tensor(const DNNData *input)
LastLevelTaskItem * lltask
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
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.
void * ff_queue_peek_front(Queue *q)
Return a pointer to the data at the head of the queue.
int ff_dnn_start_inference_async(void *ctx, DNNAsyncExecModule *async_module)
Start asynchronous inference routine for the TensorFlow model on a detached thread.
#define AVIO_FLAG_READ
read-only
static int dnn_get_height_idx_by_layout(DNNLayout layout)
static void dnn_free_model_tf(DNNModel **model)
static int dnn_get_channel_idx_by_layout(DNNLayout layout)
int avio_closep(AVIOContext **s)
Close the resource accessed by the AVIOContext *s, free it and set the pointer pointing to it to NULL...
static void free_buffer(void *data, size_t length)
static int get_output_tf(void *model, const char *input_name, int input_width, int input_height, const char *output_name, int *output_width, int *output_height)
int(* get_output)(void *model, const char *input_name, int input_width, int input_height, const char *output_name, int *output_width, int *output_height)
int ff_proc_from_dnn_to_frame(AVFrame *frame, DNNData *output, void *log_ctx)