FFmpeg
dnn_backend_torch.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2024
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 Torch backend implementation.
24  */
25 
26 #include <torch/torch.h>
27 #include <torch/script.h>
28 
29 extern "C" {
30 #include "dnn_io_proc.h"
31 #include "dnn_backend_common.h"
32 #include "libavutil/opt.h"
33 #include "libavutil/mem.h"
34 #include "queue.h"
35 #include "safe_queue.h"
36 }
37 
38 typedef struct THModel {
41  torch::jit::Module *jit_model;
45 } THModel;
46 
47 typedef struct THInferRequest {
48  torch::Tensor *output;
49  torch::Tensor *input_tensor;
51 
52 typedef struct THRequestItem {
57 
58 
59 #define OFFSET(x) offsetof(THOptions, x)
60 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM
61 static const AVOption dnn_th_options[] = {
62  { "optimize", "turn on graph executor optimization", OFFSET(optimize), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS},
63  { NULL }
64 };
65 
66 static int extract_lltask_from_task(TaskItem *task, Queue *lltask_queue)
67 {
68  THModel *th_model = (THModel *)task->model;
69  DnnContext *ctx = th_model->ctx;
70  LastLevelTaskItem *lltask = (LastLevelTaskItem *)av_malloc(sizeof(*lltask));
71  if (!lltask) {
72  av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for LastLevelTaskItem\n");
73  return AVERROR(ENOMEM);
74  }
75  task->inference_todo = 1;
76  task->inference_done = 0;
77  lltask->task = task;
78  if (ff_queue_push_back(lltask_queue, lltask) < 0) {
79  av_log(ctx, AV_LOG_ERROR, "Failed to push back lltask_queue.\n");
80  av_freep(&lltask);
81  return AVERROR(ENOMEM);
82  }
83  return 0;
84 }
85 
86 static void th_free_request(THInferRequest *request)
87 {
88  if (!request)
89  return;
90  if (request->output) {
91  delete(request->output);
92  request->output = NULL;
93  }
94  if (request->input_tensor) {
95  delete(request->input_tensor);
96  request->input_tensor = NULL;
97  }
98  return;
99 }
100 
102 {
103  THRequestItem *item;
104  if (!arg || !*arg) {
105  return;
106  }
107  item = *arg;
109  av_freep(&item->infer_request);
110  av_freep(&item->lltask);
112  av_freep(arg);
113 }
114 
115 static void dnn_free_model_th(DNNModel **model)
116 {
117  THModel *th_model;
118  if (!model || !*model)
119  return;
120 
121  th_model = (THModel *)(*model);
122 
123  if (th_model->request_queue) {
124  while (ff_safe_queue_size(th_model->request_queue) != 0) {
126  destroy_request_item(&item);
127  }
129  }
130 
131  if (th_model->lltask_queue)
132  ff_queue_destroy(th_model->lltask_queue);
133  if (th_model->task_queue)
134  ff_queue_destroy(th_model->task_queue);
135 
136  if (th_model->jit_model)
137  delete th_model->jit_model;
138 
139  av_freep(&th_model);
140  *model = NULL;
141 }
142 
143 static int get_input_th(DNNModel *model, DNNData *input, const char *input_name)
144 {
145  input->dt = DNN_FLOAT;
146  input->order = DCO_RGB;
147  input->layout = DL_NCHW;
148  input->dims[0] = 1;
149  input->dims[1] = 3;
150  input->dims[2] = -1;
151  input->dims[3] = -1;
152  return 0;
153 }
154 
155 static void deleter(void *arg)
156 {
157  av_freep(&arg);
158 }
159 
160 static int fill_model_input_th(THModel *th_model, THRequestItem *request)
161 {
162  LastLevelTaskItem *lltask = NULL;
163  TaskItem *task = NULL;
164  THInferRequest *infer_request = NULL;
165  DNNData input = { 0 };
166  DnnContext *ctx = th_model->ctx;
167  int ret, width_idx, height_idx, channel_idx;
168 
169  lltask = (LastLevelTaskItem *)ff_queue_pop_front(th_model->lltask_queue);
170  if (!lltask) {
171  ret = AVERROR(EINVAL);
172  goto err;
173  }
174  request->lltask = lltask;
175  task = lltask->task;
176  infer_request = request->infer_request;
177 
178  ret = get_input_th(&th_model->model, &input, NULL);
179  if ( ret != 0) {
180  goto err;
181  }
182  width_idx = dnn_get_width_idx_by_layout(input.layout);
183  height_idx = dnn_get_height_idx_by_layout(input.layout);
184  channel_idx = dnn_get_channel_idx_by_layout(input.layout);
185  input.dims[height_idx] = task->in_frame->height;
186  input.dims[width_idx] = task->in_frame->width;
187  input.data = av_malloc(input.dims[height_idx] * input.dims[width_idx] *
188  input.dims[channel_idx] * sizeof(float));
189  if (!input.data)
190  return AVERROR(ENOMEM);
191  infer_request->input_tensor = new torch::Tensor();
192  infer_request->output = new torch::Tensor();
193 
194  switch (th_model->model.func_type) {
195  case DFT_PROCESS_FRAME:
196  input.scale = 255;
197  if (task->do_ioproc) {
198  if (th_model->model.frame_pre_proc != NULL) {
199  th_model->model.frame_pre_proc(task->in_frame, &input, th_model->model.filter_ctx);
200  } else {
202  }
203  }
204  break;
205  default:
206  avpriv_report_missing_feature(NULL, "model function type %d", th_model->model.func_type);
207  break;
208  }
209  *infer_request->input_tensor = torch::from_blob(input.data,
210  {1, input.dims[channel_idx], input.dims[height_idx], input.dims[width_idx]},
211  deleter, torch::kFloat32);
212  return 0;
213 
214 err:
215  th_free_request(infer_request);
216  return ret;
217 }
218 
219 static int th_start_inference(void *args)
220 {
221  THRequestItem *request = (THRequestItem *)args;
222  THInferRequest *infer_request = NULL;
223  LastLevelTaskItem *lltask = NULL;
224  TaskItem *task = NULL;
225  THModel *th_model = NULL;
226  DnnContext *ctx = NULL;
227  std::vector<torch::jit::IValue> inputs;
228  torch::NoGradGuard no_grad;
229 
230  if (!request) {
231  av_log(NULL, AV_LOG_ERROR, "THRequestItem is NULL\n");
232  return AVERROR(EINVAL);
233  }
234  infer_request = request->infer_request;
235  lltask = request->lltask;
236  task = lltask->task;
237  th_model = (THModel *)task->model;
238  ctx = th_model->ctx;
239 
240  if (ctx->torch_option.optimize)
241  torch::jit::setGraphExecutorOptimize(true);
242  else
243  torch::jit::setGraphExecutorOptimize(false);
244 
245  if (!infer_request->input_tensor || !infer_request->output) {
246  av_log(ctx, AV_LOG_ERROR, "input or output tensor is NULL\n");
247  return DNN_GENERIC_ERROR;
248  }
249  // Transfer tensor to the same device as model
250  c10::Device device = (*th_model->jit_model->parameters().begin()).device();
251  if (infer_request->input_tensor->device() != device)
252  *infer_request->input_tensor = infer_request->input_tensor->to(device);
253  inputs.push_back(*infer_request->input_tensor);
254 
255  *infer_request->output = th_model->jit_model->forward(inputs).toTensor();
256 
257  return 0;
258 }
259 
260 static void infer_completion_callback(void *args) {
261  THRequestItem *request = (THRequestItem*)args;
262  LastLevelTaskItem *lltask = request->lltask;
263  TaskItem *task = lltask->task;
264  DNNData outputs = { 0 };
265  THInferRequest *infer_request = request->infer_request;
266  THModel *th_model = (THModel *)task->model;
267  torch::Tensor *output = infer_request->output;
268 
269  c10::IntArrayRef sizes = output->sizes();
270  outputs.order = DCO_RGB;
271  outputs.layout = DL_NCHW;
272  outputs.dt = DNN_FLOAT;
273  if (sizes.size() == 4) {
274  // 4 dimensions: [batch_size, channel, height, width]
275  // this format of data is normally used for video frame SR
276  outputs.dims[0] = sizes.at(0); // N
277  outputs.dims[1] = sizes.at(1); // C
278  outputs.dims[2] = sizes.at(2); // H
279  outputs.dims[3] = sizes.at(3); // W
280  } else {
281  avpriv_report_missing_feature(th_model->ctx, "Support of this kind of model");
282  goto err;
283  }
284 
285  switch (th_model->model.func_type) {
286  case DFT_PROCESS_FRAME:
287  if (task->do_ioproc) {
288  // Post process can only deal with CPU memory.
289  if (output->device() != torch::kCPU)
290  *output = output->to(torch::kCPU);
291  outputs.scale = 255;
292  outputs.data = output->data_ptr();
293  if (th_model->model.frame_post_proc != NULL) {
294  th_model->model.frame_post_proc(task->out_frame, &outputs, th_model->model.filter_ctx);
295  } else {
296  ff_proc_from_dnn_to_frame(task->out_frame, &outputs, th_model->ctx);
297  }
298  } else {
301  }
302  break;
303  default:
304  avpriv_report_missing_feature(th_model->ctx, "model function type %d", th_model->model.func_type);
305  goto err;
306  }
307  task->inference_done++;
308  av_freep(&request->lltask);
309 err:
310  th_free_request(infer_request);
311 
312  if (ff_safe_queue_push_back(th_model->request_queue, request) < 0) {
313  destroy_request_item(&request);
314  av_log(th_model->ctx, AV_LOG_ERROR, "Unable to push back request_queue when failed to start inference.\n");
315  }
316 }
317 
318 static int execute_model_th(THRequestItem *request, Queue *lltask_queue)
319 {
320  THModel *th_model = NULL;
321  LastLevelTaskItem *lltask;
322  TaskItem *task = NULL;
323  int ret = 0;
324 
325  if (ff_queue_size(lltask_queue) == 0) {
326  destroy_request_item(&request);
327  return 0;
328  }
329 
330  lltask = (LastLevelTaskItem *)ff_queue_peek_front(lltask_queue);
331  if (lltask == NULL) {
332  av_log(NULL, AV_LOG_ERROR, "Failed to get LastLevelTaskItem\n");
333  ret = AVERROR(EINVAL);
334  goto err;
335  }
336  task = lltask->task;
337  th_model = (THModel *)task->model;
338 
339  ret = fill_model_input_th(th_model, request);
340  if (ret != 0) {
341  goto err;
342  }
343 
344  if (task->async) {
345  return ff_dnn_start_inference_async(th_model->ctx, &request->exec_module);
346  } else {
347  // Synchronous execution path
348  ret = th_start_inference((void *)(request));
349  if (ret != 0) {
350  goto err;
351  }
352  infer_completion_callback(request);
353  return (task->inference_done == task->inference_todo) ? 0 : DNN_GENERIC_ERROR;
354  }
355 
356 err:
357  th_free_request(request->infer_request);
358  if (ff_safe_queue_push_back(th_model->request_queue, request) < 0) {
359  destroy_request_item(&request);
360  }
361  return ret;
362 }
363 
364 static int get_output_th(DNNModel *model, const char *input_name, int input_width, int input_height,
365  const char *output_name, int *output_width, int *output_height)
366 {
367  int ret = 0;
368  THModel *th_model = (THModel*) model;
369  DnnContext *ctx = th_model->ctx;
370  TaskItem task = { 0 };
371  THRequestItem *request = NULL;
372  DNNExecBaseParams exec_params = {
373  .input_name = input_name,
374  .output_names = &output_name,
375  .nb_output = 1,
376  .in_frame = NULL,
377  .out_frame = NULL,
378  };
379  ret = ff_dnn_fill_gettingoutput_task(&task, &exec_params, th_model, input_height, input_width, ctx);
380  if ( ret != 0) {
381  goto err;
382  }
383 
384  ret = extract_lltask_from_task(&task, th_model->lltask_queue);
385  if ( ret != 0) {
386  av_log(ctx, AV_LOG_ERROR, "unable to extract last level task from task.\n");
387  goto err;
388  }
389 
390  request = (THRequestItem*) ff_safe_queue_pop_front(th_model->request_queue);
391  if (!request) {
392  av_log(ctx, AV_LOG_ERROR, "unable to get infer request.\n");
393  ret = AVERROR(EINVAL);
394  goto err;
395  }
396 
397  ret = execute_model_th(request, th_model->lltask_queue);
398  *output_width = task.out_frame->width;
399  *output_height = task.out_frame->height;
400 
401 err:
402  av_frame_free(&task.out_frame);
403  av_frame_free(&task.in_frame);
404  return ret;
405 }
406 
408 {
409  THInferRequest *request = (THInferRequest *)av_malloc(sizeof(THInferRequest));
410  if (!request) {
411  return NULL;
412  }
413  request->input_tensor = NULL;
414  request->output = NULL;
415  return request;
416 }
417 
419 {
420  DNNModel *model = NULL;
421  THModel *th_model = NULL;
422  THRequestItem *item = NULL;
423  const char *device_name = ctx->device ? ctx->device : "cpu";
424 
425  th_model = (THModel *)av_mallocz(sizeof(THModel));
426  if (!th_model)
427  return NULL;
428  model = &th_model->model;
429  th_model->ctx = ctx;
430 
431  c10::Device device = c10::Device(device_name);
432  if (device.is_xpu()) {
433  if (!at::hasXPU()) {
434  av_log(ctx, AV_LOG_ERROR, "No XPU device found\n");
435  goto fail;
436  }
437 #if TORCH_VERSION_MAJOR > 2 || (TORCH_VERSION_MAJOR == 2 && TORCH_VERSION_MINOR >= 6)
438  at::detail::getXPUHooks().init();
439 #else
440  at::detail::getXPUHooks().initXPU();
441 #endif
442  } else if (!device.is_cpu()) {
443  av_log(ctx, AV_LOG_ERROR, "Not supported device:\"%s\"\n", device_name);
444  goto fail;
445  }
446 
447  try {
448  th_model->jit_model = new torch::jit::Module;
449  (*th_model->jit_model) = torch::jit::load(ctx->model_filename);
450  th_model->jit_model->to(device);
451  } catch (const c10::Error& e) {
452  av_log(ctx, AV_LOG_ERROR, "Failed to load torch model\n");
453  goto fail;
454  }
455 
456  th_model->request_queue = ff_safe_queue_create();
457  if (!th_model->request_queue) {
458  goto fail;
459  }
460 
461  item = (THRequestItem *)av_mallocz(sizeof(THRequestItem));
462  if (!item) {
463  goto fail;
464  }
466  if (!item->infer_request) {
467  goto fail;
468  }
469 
472  item->exec_module.args = item;
473 
474  if (ff_safe_queue_push_back(th_model->request_queue, item) < 0) {
475  goto fail;
476  }
477  item = NULL;
478 
479  th_model->task_queue = ff_queue_create();
480  th_model->lltask_queue = ff_queue_create();
481 
482  model->get_input = &get_input_th;
483  model->get_output = &get_output_th;
484  model->filter_ctx = filter_ctx;
485  model->func_type = func_type;
486  return model;
487 
488 fail:
489  if (item) {
490  destroy_request_item(&item);
491  }
492  dnn_free_model_th(&model);
493  return NULL;
494 }
495 
496 static int dnn_execute_model_th(const DNNModel *model, DNNExecBaseParams *exec_params)
497 {
498  THModel *th_model = (THModel *)model;
499  DnnContext *ctx = th_model->ctx;
500  TaskItem *task;
501  THRequestItem *request;
502  int ret = 0;
503 
504  ret = ff_check_exec_params(ctx, DNN_TH, model->func_type, exec_params);
505  if (ret != 0) {
506  av_log(ctx, AV_LOG_ERROR, "exec parameter checking fail.\n");
507  return ret;
508  }
509 
510  task = (TaskItem *)av_malloc(sizeof(TaskItem));
511  if (!task) {
512  av_log(ctx, AV_LOG_ERROR, "unable to alloc memory for task item.\n");
513  return AVERROR(ENOMEM);
514  }
515 
516  ret = ff_dnn_fill_task(task, exec_params, th_model, ctx->async, 1);
517  if (ret != 0) {
518  av_freep(&task);
519  av_log(ctx, AV_LOG_ERROR, "unable to fill task.\n");
520  return ret;
521  }
522 
523  ret = ff_queue_push_back(th_model->task_queue, task);
524  if (ret < 0) {
525  av_freep(&task);
526  av_log(ctx, AV_LOG_ERROR, "unable to push back task_queue.\n");
527  return ret;
528  }
529 
530  ret = extract_lltask_from_task(task, th_model->lltask_queue);
531  if (ret != 0) {
532  av_log(ctx, AV_LOG_ERROR, "unable to extract last level task from task.\n");
533  return ret;
534  }
535 
536  request = (THRequestItem *)ff_safe_queue_pop_front(th_model->request_queue);
537  if (!request) {
538  av_log(ctx, AV_LOG_ERROR, "unable to get infer request.\n");
539  return AVERROR(EINVAL);
540  }
541 
542  return execute_model_th(request, th_model->lltask_queue);
543 }
544 
546 {
547  THModel *th_model = (THModel *)model;
548  return ff_dnn_get_result_common(th_model->task_queue, in, out);
549 }
550 
551 static int dnn_flush_th(const DNNModel *model)
552 {
553  THModel *th_model = (THModel *)model;
554  THRequestItem *request;
555 
556  if (ff_queue_size(th_model->lltask_queue) == 0)
557  // no pending task need to flush
558  return 0;
559 
560  request = (THRequestItem *)ff_safe_queue_pop_front(th_model->request_queue);
561  if (!request) {
562  av_log(th_model->ctx, AV_LOG_ERROR, "unable to get infer request.\n");
563  return AVERROR(EINVAL);
564  }
565 
566  return execute_model_th(request, th_model->lltask_queue);
567 }
568 
569 extern const DNNModule ff_dnn_backend_torch = {
570  .clazz = DNN_DEFINE_CLASS(dnn_th),
571  .type = DNN_TH,
572  .load_model = dnn_load_model_th,
573  .execute_model = dnn_execute_model_th,
574  .get_result = dnn_get_result_th,
575  .flush = dnn_flush_th,
576  .free_model = dnn_free_model_th,
577 };
THRequestItem::lltask
LastLevelTaskItem * lltask
Definition: dnn_backend_torch.cpp:54
THModel::lltask_queue
Queue * lltask_queue
Definition: dnn_backend_torch.cpp:44
THRequestItem::infer_request
THInferRequest * infer_request
Definition: dnn_backend_torch.cpp:53
THModel::ctx
DnnContext * ctx
Definition: dnn_backend_torch.cpp:40
AVERROR
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
opt.h
ff_safe_queue_pop_front
void * ff_safe_queue_pop_front(SafeQueue *sq)
Remove and free first element from the queue in SafeQueue.
Definition: safe_queue.c:105
out
static FILE * out
Definition: movenc.c:55
deleter
static void deleter(void *arg)
Definition: dnn_backend_torch.cpp:155
FLAGS
#define FLAGS
Definition: dnn_backend_torch.cpp:60
THModel
Definition: dnn_backend_torch.cpp:38
DNNAsyncExecModule
Common Async Execution Mechanism for the DNN Backends.
Definition: dnn_backend_common.h:65
DNNFunctionType
DNNFunctionType
Definition: dnn_interface.h:56
output
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce output
Definition: filter_design.txt:226
ff_queue_pop_front
void * ff_queue_pop_front(Queue *q)
Remove and free first element from the Queue.
Definition: queue.c:151
ff_check_exec_params
int ff_check_exec_params(void *ctx, DNNBackendType backend, DNNFunctionType func_type, DNNExecBaseParams *exec_params)
Definition: dnn_backend_common.c:30
ff_queue_size
size_t ff_queue_size(Queue *q)
Return the length of the Queue.
Definition: queue.c:88
DNN_GENERIC_ERROR
#define DNN_GENERIC_ERROR
Definition: dnn_interface.h:33
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:64
LastLevelTaskItem
Definition: dnn_backend_common.h:57
ff_dnn_backend_torch
const DNNModule ff_dnn_backend_torch
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:427
AVFrame::width
int width
Definition: frame.h:499
SafeQueue
Double-ended queue with mutex locks ensuring data consistency while multithreading.
Definition: safe_queue.c:46
dnn_execute_model_th
static int dnn_execute_model_th(const DNNModel *model, DNNExecBaseParams *exec_params)
Definition: dnn_backend_torch.cpp:496
AVOption
AVOption.
Definition: opt.h:429
DNNModel::frame_pre_proc
FramePrePostProc frame_pre_proc
Definition: dnn_interface.h:110
DNNExecBaseParams::input_name
const char * input_name
Definition: dnn_interface.h:81
dnn_io_proc.h
TaskItem
Definition: dnn_backend_common.h:43
DNNAsyncExecModule::callback
void(* callback)(void *args)
Completion Callback for the backend.
Definition: dnn_backend_common.h:77
DNNModel::filter_ctx
AVFilterContext * filter_ctx
Definition: dnn_interface.h:99
ff_queue_create
Queue * ff_queue_create(void)
Create a Queue instance.
Definition: queue.c:47
dnn_get_width_idx_by_layout
static int dnn_get_width_idx_by_layout(DNNLayout layout)
Definition: dnn_interface.h:197
TaskItem::model
void * model
Definition: dnn_backend_common.h:44
fail
#define fail()
Definition: checkasm.h:219
DnnContext
Definition: dnn_interface.h:143
filter_ctx
static FilteringContext * filter_ctx
Definition: transcode.c:52
Queue
Linear double-ended data structure.
Definition: executor.c:51
ff_queue_push_back
int ff_queue_push_back(Queue *q, void *v)
Add data to the tail of the queue.
Definition: queue.c:130
THModel::jit_model
torch::jit::Module * jit_model
Definition: dnn_backend_torch.cpp:41
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
LastLevelTaskItem::task
TaskItem * task
Definition: dnn_backend_common.h:58
destroy_request_item
static void destroy_request_item(THRequestItem **arg)
Definition: dnn_backend_torch.cpp:101
th_create_inference_request
static THInferRequest * th_create_inference_request(void)
Definition: dnn_backend_torch.cpp:407
ff_queue_destroy
void ff_queue_destroy(Queue *q)
Destroy the Queue instance.
Definition: queue.c:72
DNNData
Definition: dnn_interface.h:69
DNNModule::clazz
const AVClass clazz
Definition: dnn_interface.h:176
ff_dnn_fill_gettingoutput_task
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.
Definition: dnn_backend_common.c:156
DNNModel::get_output
int(* get_output)(struct DNNModel *model, const char *input_name, int input_width, int input_height, const char *output_name, int *output_width, int *output_height)
Definition: dnn_interface.h:106
ctx
static AVFormatContext * ctx
Definition: movenc.c:49
TaskItem::inference_todo
uint32_t inference_todo
Definition: dnn_backend_common.h:52
DL_NCHW
@ DL_NCHW
Definition: dnn_interface.h:65
av_mallocz
#define av_mallocz(s)
Definition: tableprint_vlc.h:31
dnn_load_model_th
static DNNModel * dnn_load_model_th(DnnContext *ctx, DNNFunctionType func_type, AVFilterContext *filter_ctx)
Definition: dnn_backend_torch.cpp:418
arg
const char * arg
Definition: jacosubdec.c:65
if
if(ret)
Definition: filter_design.txt:179
ff_safe_queue_size
size_t ff_safe_queue_size(SafeQueue *sq)
Return the length of the SafeQueue.
Definition: safe_queue.c:80
ff_proc_from_frame_to_dnn
int ff_proc_from_frame_to_dnn(AVFrame *frame, DNNData *input, void *log_ctx)
Definition: dnn_io_proc.c:182
THRequestItem::exec_module
DNNAsyncExecModule exec_module
Definition: dnn_backend_torch.cpp:55
NULL
#define NULL
Definition: coverity.c:32
sizes
static const int sizes[][2]
Definition: img2dec.c:61
get_input_th
static int get_input_th(DNNModel *model, DNNData *input, const char *input_name)
Definition: dnn_backend_torch.cpp:143
ff_safe_queue_create
SafeQueue * ff_safe_queue_create(void)
Create and initialize a SafeQueue instance.
Definition: safe_queue.c:52
DNNModel::frame_post_proc
FramePrePostProc frame_post_proc
Definition: dnn_interface.h:113
get_output_th
static int get_output_th(DNNModel *model, const char *input_name, int input_width, int input_height, const char *output_name, int *output_width, int *output_height)
Definition: dnn_backend_torch.cpp:364
ff_dnn_async_module_cleanup
int ff_dnn_async_module_cleanup(DNNAsyncExecModule *async_module)
Join the Async Execution thread and set module pointers to NULL.
Definition: dnn_backend_common.c:86
infer_completion_callback
static void infer_completion_callback(void *args)
Definition: dnn_backend_torch.cpp:260
TaskItem::in_frame
AVFrame * in_frame
Definition: dnn_backend_common.h:45
extract_lltask_from_task
static int extract_lltask_from_task(TaskItem *task, Queue *lltask_queue)
Definition: dnn_backend_torch.cpp:66
inputs
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs
Definition: filter_design.txt:244
THInferRequest::output
torch::Tensor * output
Definition: dnn_backend_torch.cpp:48
TaskItem::async
uint8_t async
Definition: dnn_backend_common.h:49
TaskItem::inference_done
uint32_t inference_done
Definition: dnn_backend_common.h:53
queue.h
DNNModel::func_type
DNNFunctionType func_type
Definition: dnn_interface.h:101
avpriv_report_missing_feature
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
ff_safe_queue_destroy
void ff_safe_queue_destroy(SafeQueue *sq)
Destroy the SafeQueue instance.
Definition: safe_queue.c:69
DNN_FLOAT
@ DNN_FLOAT
Definition: dnn_interface.h:41
dnn_get_result_th
static DNNAsyncStatusType dnn_get_result_th(const DNNModel *model, AVFrame **in, AVFrame **out)
Definition: dnn_backend_torch.cpp:545
ff_dnn_fill_task
int ff_dnn_fill_task(TaskItem *task, DNNExecBaseParams *exec_params, void *backend_model, int async, int do_ioproc)
Fill the Task for Backend Execution.
Definition: dnn_backend_common.c:50
input
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
Definition: filter_design.txt:172
DNN_DEFINE_CLASS
#define DNN_DEFINE_CLASS(fname)
Definition: dnn_backend_common.h:39
THRequestItem
Definition: dnn_backend_torch.cpp:52
ff_safe_queue_push_back
int ff_safe_queue_push_back(SafeQueue *sq, void *v)
Add data to the tail of queue in the SafeQueue after locking mutex.
Definition: safe_queue.c:95
th_start_inference
static int th_start_inference(void *args)
Definition: dnn_backend_torch.cpp:219
THInferRequest::input_tensor
torch::Tensor * input_tensor
Definition: dnn_backend_torch.cpp:49
DNNAsyncExecModule::start_inference
int(* start_inference)(void *request)
Synchronous inference function for the backend with corresponding request item as the argument.
Definition: dnn_backend_common.h:70
DNNAsyncExecModule::args
void * args
Argument for the execution functions.
Definition: dnn_backend_common.h:83
safe_queue.h
THInferRequest
Definition: dnn_backend_torch.cpp:47
outputs
static const AVFilterPad outputs[]
Definition: af_aap.c:310
ret
ret
Definition: filter_design.txt:187
av_malloc
void * av_malloc(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:98
TaskItem::out_frame
AVFrame * out_frame
Definition: dnn_backend_common.h:46
AVFrame::height
int height
Definition: frame.h:499
dnn_backend_common.h
THModel::model
DNNModel model
Definition: dnn_backend_torch.cpp:39
dnn_th_options
static const AVOption dnn_th_options[]
Definition: dnn_backend_torch.cpp:61
execute_model_th
static int execute_model_th(THRequestItem *request, Queue *lltask_queue)
Definition: dnn_backend_torch.cpp:318
OFFSET
#define OFFSET(x)
Definition: dnn_backend_torch.cpp:59
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
ff_dnn_get_result_common
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.
Definition: dnn_backend_common.c:136
ff_queue_peek_front
void * ff_queue_peek_front(Queue *q)
Return a pointer to the data at the head of the queue.
Definition: queue.c:93
DCO_RGB
@ DCO_RGB
Definition: dnn_interface.h:46
AVFilterContext
An instance of a filter.
Definition: avfilter.h:274
ff_dnn_start_inference_async
int ff_dnn_start_inference_async(void *ctx, DNNAsyncExecModule *async_module)
Start asynchronous inference routine for the TensorFlow model on a detached thread.
Definition: dnn_backend_common.c:105
DNNModel
Definition: dnn_interface.h:97
DNN_TH
@ DNN_TH
Definition: dnn_interface.h:38
mem.h
dnn_get_height_idx_by_layout
static int dnn_get_height_idx_by_layout(DNNLayout layout)
Definition: dnn_interface.h:202
dnn_flush_th
static int dnn_flush_th(const DNNModel *model)
Definition: dnn_backend_torch.cpp:551
THModel::task_queue
Queue * task_queue
Definition: dnn_backend_torch.cpp:43
dnn_get_channel_idx_by_layout
static int dnn_get_channel_idx_by_layout(DNNLayout layout)
Definition: dnn_interface.h:207
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
DNNExecBaseParams
Definition: dnn_interface.h:80
DNNModel::get_input
int(* get_input)(struct DNNModel *model, DNNData *input, const char *input_name)
Definition: dnn_interface.h:104
dnn_free_model_th
static void dnn_free_model_th(DNNModel **model)
Definition: dnn_backend_torch.cpp:115
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
TaskItem::do_ioproc
uint8_t do_ioproc
Definition: dnn_backend_common.h:50
DNNAsyncStatusType
DNNAsyncStatusType
Definition: dnn_interface.h:49
DFT_PROCESS_FRAME
@ DFT_PROCESS_FRAME
Definition: dnn_interface.h:58
DNNModule
Definition: dnn_interface.h:175
fill_model_input_th
static int fill_model_input_th(THModel *th_model, THRequestItem *request)
Definition: dnn_backend_torch.cpp:160
THModel::request_queue
SafeQueue * request_queue
Definition: dnn_backend_torch.cpp:42
ff_proc_from_dnn_to_frame
int ff_proc_from_dnn_to_frame(AVFrame *frame, DNNData *output, void *log_ctx)
Definition: dnn_io_proc.c:42
th_free_request
static void th_free_request(THInferRequest *request)
Definition: dnn_backend_torch.cpp:86