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 "libavutil/cpu.h"
35 #include "queue.h"
36 #include "safe_queue.h"
37 }
38 
39 typedef struct THModel {
42  torch::jit::Module *jit_model;
46 } THModel;
47 
48 typedef struct THInferRequest {
49  torch::Tensor *output;
50  torch::Tensor *input_tensor;
52 
53 typedef struct THRequestItem {
56  uint32_t lltask_count;
59 
60 
61 #define OFFSET(x) offsetof(THOptions, x)
62 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM
63 static const AVOption dnn_th_options[] = {
64  { "optimize", "turn on graph executor optimization", OFFSET(optimize), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS},
65  { NULL }
66 };
67 
68 static int extract_lltask_from_task(TaskItem *task, Queue *lltask_queue)
69 {
70  THModel *th_model = (THModel *)task->model;
71  DnnContext *ctx = th_model->ctx;
72  LastLevelTaskItem *lltask = (LastLevelTaskItem *)av_malloc(sizeof(*lltask));
73  if (!lltask) {
74  av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for LastLevelTaskItem\n");
75  return AVERROR(ENOMEM);
76  }
77  task->inference_todo = 1;
78  task->inference_done = 0;
79  lltask->task = task;
80  if (ff_queue_push_back(lltask_queue, lltask) < 0) {
81  av_log(ctx, AV_LOG_ERROR, "Failed to push back lltask_queue.\n");
82  av_freep(&lltask);
83  return AVERROR(ENOMEM);
84  }
85  return 0;
86 }
87 
88 static void th_free_request(THInferRequest *request)
89 {
90  if (!request)
91  return;
92  if (request->output) {
93  delete(request->output);
94  request->output = NULL;
95  }
96  if (request->input_tensor) {
97  delete(request->input_tensor);
98  request->input_tensor = NULL;
99  }
100  return;
101 }
102 
104 {
105  THRequestItem *item;
106  if (!arg || !*arg) {
107  return;
108  }
109  item = *arg;
111  av_freep(&item->infer_request);
112  av_freep(&item->lltasks);
114  av_freep(arg);
115 }
116 
117 static void dnn_free_model_th(DNNModel **model)
118 {
119  THModel *th_model;
120  if (!model || !*model)
121  return;
122 
123  th_model = (THModel *)(*model);
124 
125  if (th_model->request_queue) {
126  while (ff_safe_queue_size(th_model->request_queue) != 0) {
128  destroy_request_item(&item);
129  }
131  }
132 
133  if (th_model->lltask_queue)
134  ff_queue_destroy(th_model->lltask_queue);
135  if (th_model->task_queue)
136  ff_queue_destroy(th_model->task_queue);
137 
138  if (th_model->jit_model)
139  delete th_model->jit_model;
140 
141  av_freep(&th_model);
142  *model = NULL;
143 }
144 
145 static int get_input_th(DNNModel *model, DNNData *input, const char *input_name)
146 {
147  input->dt = DNN_FLOAT;
148  input->order = DCO_RGB;
149  input->layout = DL_NCHW;
150  input->dims[0] = 1;
151  input->dims[1] = 3;
152  input->dims[2] = -1;
153  input->dims[3] = -1;
154  return 0;
155 }
156 
157 static void deleter(void *arg)
158 {
159  av_freep(&arg);
160 }
161 
162 static int fill_model_input_th(THModel *th_model, THRequestItem *request)
163 {
164  LastLevelTaskItem *lltask = NULL;
165  TaskItem *task = NULL;
166  THInferRequest *infer_request = NULL;
167  DNNData input = { 0 };
168  DnnContext *ctx = th_model->ctx;
169  int ret, width_idx, height_idx, channel_idx;
170  int batch_size = ctx->batch_size;
171  float *batch_data = NULL;
172  int frame_size = 0;
173 
174  infer_request = request->infer_request;
175 
176  ret = get_input_th(&th_model->model, &input, NULL);
177  if (ret != 0) {
178  goto err;
179  }
180  width_idx = dnn_get_width_idx_by_layout(input.layout);
181  height_idx = dnn_get_height_idx_by_layout(input.layout);
182  channel_idx = dnn_get_channel_idx_by_layout(input.layout);
183 
184  lltask = (LastLevelTaskItem *)ff_queue_peek_front(th_model->lltask_queue);
185  if (!lltask) {
186  ret = AVERROR(EINVAL);
187  goto err;
188  }
189  task = lltask->task;
190  input.dims[height_idx] = task->in_frame->height;
191  input.dims[width_idx] = task->in_frame->width;
192 
193  frame_size = input.dims[height_idx] * input.dims[width_idx] * input.dims[channel_idx];
194  batch_data = (float *)av_malloc(batch_size * frame_size * sizeof(float));
195  if (!batch_data) {
196  ret = AVERROR(ENOMEM);
197  goto err;
198  }
199 
200  for (int i = 0; i < batch_size; i++) {
201  lltask = (LastLevelTaskItem *)ff_queue_pop_front(th_model->lltask_queue);
202  if (!lltask)
203  break;
204 
205  request->lltasks[i] = lltask;
206  request->lltask_count = i + 1;
207  task = lltask->task;
208 
209  input.data = batch_data + i * frame_size;
210 
211  switch (th_model->model.func_type) {
212  case DFT_PROCESS_FRAME:
213  input.scale = 255;
214  if (task->do_ioproc) {
215  if (th_model->model.frame_pre_proc != NULL) {
216  th_model->model.frame_pre_proc(task->in_frame, &input, th_model->model.filter_ctx);
217  } else {
219  }
220  }
221  break;
222  default:
223  avpriv_report_missing_feature(NULL, "model function type %d", th_model->model.func_type);
224  break;
225  }
226  }
227 
228  infer_request->input_tensor = new torch::Tensor();
229  infer_request->output = new torch::Tensor();
230  *infer_request->input_tensor = torch::from_blob(batch_data,
231  {request->lltask_count, input.dims[channel_idx], input.dims[height_idx], input.dims[width_idx]},
232  deleter, torch::kFloat32);
233 
234  return 0;
235 
236 err:
237  if (batch_data)
238  av_freep(&batch_data);
239  th_free_request(infer_request);
240  return ret;
241 }
242 
243 static int th_start_inference(void *args)
244 {
245  THRequestItem *request = (THRequestItem *)args;
246  THInferRequest *infer_request = NULL;
247  LastLevelTaskItem *lltask = NULL;
248  TaskItem *task = NULL;
249  THModel *th_model = NULL;
250  DnnContext *ctx = NULL;
251  std::vector<torch::jit::IValue> inputs;
252  torch::NoGradGuard no_grad;
253 
254  if (!request) {
255  av_log(NULL, AV_LOG_ERROR, "THRequestItem is NULL\n");
256  return AVERROR(EINVAL);
257  }
258  infer_request = request->infer_request;
259  lltask = request->lltasks[0];
260  task = lltask->task;
261  th_model = (THModel *)task->model;
262  ctx = th_model->ctx;
263 
264  if (ctx->torch_option.optimize)
265  torch::jit::setGraphExecutorOptimize(true);
266  else
267  torch::jit::setGraphExecutorOptimize(false);
268 
269  if (!infer_request->input_tensor || !infer_request->output) {
270  av_log(ctx, AV_LOG_ERROR, "input or output tensor is NULL\n");
271  return DNN_GENERIC_ERROR;
272  }
273  // Transfer tensor to the same device as model
274  c10::Device device = (*th_model->jit_model->parameters().begin()).device();
275  if (infer_request->input_tensor->device() != device)
276  *infer_request->input_tensor = infer_request->input_tensor->to(device);
277  inputs.push_back(*infer_request->input_tensor);
278 
279  *infer_request->output = th_model->jit_model->forward(inputs).toTensor();
280 
281  return 0;
282 }
283 
284 static void infer_completion_callback(void *args) {
285  THRequestItem *request = (THRequestItem*)args;
286  THInferRequest *infer_request = request->infer_request;
287  LastLevelTaskItem *lltask = request->lltasks[0];
288  THModel *th_model = (THModel *)lltask->task->model;
289  torch::Tensor *output = infer_request->output;
290  DNNData outputs = { 0 };
291 
292  auto slices = torch::split(*output, /*split_size=*/1, /*dim=*/0);
293  for (uint32_t i = 0; i < request->lltask_count; i++) {
294  lltask = request->lltasks[i];
295  TaskItem *task = lltask->task;
296  torch::Tensor out_slice = slices[i];
297  c10::IntArrayRef sizes = out_slice.sizes();
298 
299  outputs.order = DCO_RGB;
300  outputs.layout = DL_NCHW;
301  outputs.dt = DNN_FLOAT;
302 
303  if (sizes.size() == 4) {
304  // 4 dimensions: [batch_size, channel, height, width]
305  // this format of data is normally used for video frame SR
306  outputs.dims[0] = sizes.at(0); // N
307  outputs.dims[1] = sizes.at(1); // C
308  outputs.dims[2] = sizes.at(2); // H
309  outputs.dims[3] = sizes.at(3); // W
310  } else {
311  avpriv_report_missing_feature(th_model->ctx, "Support of this kind of model");
312  goto err;
313  }
314 
315  switch (th_model->model.func_type) {
316  case DFT_PROCESS_FRAME:
317  if (task->do_ioproc) {
318  // Post process can only deal with CPU memory.
319  if (out_slice.device() != torch::kCPU)
320  out_slice = out_slice.to(torch::kCPU);
321  outputs.scale = 255;
322  outputs.data = out_slice.data_ptr();
323  if (th_model->model.frame_post_proc != NULL) {
324  th_model->model.frame_post_proc(task->out_frame, &outputs, th_model->model.filter_ctx);
325  } else {
326  ff_proc_from_dnn_to_frame(task->out_frame, &outputs, th_model->ctx);
327  }
328  } else {
331  }
332  break;
333  default:
334  avpriv_report_missing_feature(th_model->ctx, "model function type %d", th_model->model.func_type);
335  goto err;
336  }
337  task->inference_done++;
338  }
339 
340 err:
341  for (uint32_t i = 0; i < request->lltask_count; i++) {
342  av_freep(&request->lltasks[i]);
343  }
344  request->lltask_count = 0;
345 
346  th_free_request(infer_request);
347 
348  if (ff_safe_queue_push_back(th_model->request_queue, request) < 0) {
349  destroy_request_item(&request);
350  av_log(th_model->ctx, AV_LOG_ERROR, "Unable to push back request_queue when failed to start inference.\n");
351  }
352 }
353 
354 static int execute_model_th(THRequestItem *request, Queue *lltask_queue)
355 {
356  THModel *th_model = NULL;
357  LastLevelTaskItem *lltask;
358  TaskItem *task = NULL;
359  int ret = 0;
360 
361  if (ff_queue_size(lltask_queue) == 0) {
362  destroy_request_item(&request);
363  return 0;
364  }
365 
366  lltask = (LastLevelTaskItem *)ff_queue_peek_front(lltask_queue);
367  if (lltask == NULL) {
368  av_log(NULL, AV_LOG_ERROR, "Failed to get LastLevelTaskItem\n");
369  ret = AVERROR(EINVAL);
370  goto err;
371  }
372  task = lltask->task;
373  th_model = (THModel *)task->model;
374 
375  ret = fill_model_input_th(th_model, request);
376  if (ret != 0) {
377  goto err;
378  }
379 
380  if (task->async) {
381  ret = ff_dnn_start_inference_async(th_model->ctx, &request->exec_module);
382  if (ret != 0) {
383  goto err;
384  }
385  return 0;
386  } else {
387  // Synchronous execution path
388  ret = th_start_inference((void *)(request));
389  if (ret != 0) {
390  goto err;
391  }
392  infer_completion_callback(request);
393  return (task->inference_done == task->inference_todo) ? 0 : DNN_GENERIC_ERROR;
394  }
395 
396 err:
397  th_free_request(request->infer_request);
398  if (ff_safe_queue_push_back(th_model->request_queue, request) < 0) {
399  destroy_request_item(&request);
400  }
401  return ret;
402 }
403 
404 static int get_output_th(DNNModel *model, const char *input_name, int input_width, int input_height,
405  const char *output_name, int *output_width, int *output_height)
406 {
407  int ret = 0;
408  THModel *th_model = (THModel*) model;
409  DnnContext *ctx = th_model->ctx;
410  TaskItem task = { 0 };
411  THRequestItem *request = NULL;
412  DNNExecBaseParams exec_params = {
413  .input_name = input_name,
414  .output_names = &output_name,
415  .nb_output = 1,
416  .in_frame = NULL,
417  .out_frame = NULL,
418  };
419  ret = ff_dnn_fill_gettingoutput_task(&task, &exec_params, th_model, input_height, input_width, ctx);
420  if ( ret != 0) {
421  goto err;
422  }
423 
424  ret = extract_lltask_from_task(&task, th_model->lltask_queue);
425  if ( ret != 0) {
426  av_log(ctx, AV_LOG_ERROR, "unable to extract last level task from task.\n");
427  goto err;
428  }
429 
430  request = (THRequestItem*) ff_safe_queue_pop_front(th_model->request_queue);
431  if (!request) {
432  av_log(ctx, AV_LOG_ERROR, "unable to get infer request.\n");
433  ret = AVERROR(EINVAL);
434  goto err;
435  }
436 
437  ret = execute_model_th(request, th_model->lltask_queue);
438  *output_width = task.out_frame->width;
439  *output_height = task.out_frame->height;
440 
441 err:
442  av_frame_free(&task.out_frame);
443  av_frame_free(&task.in_frame);
444  return ret;
445 }
446 
448 {
449  THInferRequest *request = (THInferRequest *)av_malloc(sizeof(THInferRequest));
450  if (!request) {
451  return NULL;
452  }
453  request->input_tensor = NULL;
454  request->output = NULL;
455  return request;
456 }
457 
459 {
460  DNNModel *model = NULL;
461  THModel *th_model = NULL;
462  THRequestItem *item = NULL;
463  const char *device_name = ctx->device ? ctx->device : "cpu";
464 
465  th_model = (THModel *)av_mallocz(sizeof(THModel));
466  if (!th_model)
467  return NULL;
468  model = &th_model->model;
469  th_model->ctx = ctx;
470 
471  c10::Device device = c10::Device(device_name);
472  if (device.is_xpu()) {
473  if (!at::hasXPU()) {
474  av_log(ctx, AV_LOG_ERROR, "No XPU device found\n");
475  goto fail;
476  }
477 #if TORCH_VERSION_MAJOR > 2 || (TORCH_VERSION_MAJOR == 2 && TORCH_VERSION_MINOR >= 6)
478  at::detail::getXPUHooks().init();
479 #else
480  at::detail::getXPUHooks().initXPU();
481 #endif
482  } else if (device.is_cuda()) {
483  // CUDA device - works for both NVIDIA CUDA and AMD ROCm (which uses CUDA-compatible API)
484  if (!torch::cuda::is_available()) {
485  av_log(ctx, AV_LOG_ERROR, "CUDA/ROCm is not available\n");
486  goto fail;
487  }
488  av_log(ctx, AV_LOG_INFO, "Using CUDA/ROCm device: %s\n", device_name);
489  } else if (!device.is_cpu()) {
490  av_log(ctx, AV_LOG_ERROR, "Not supported device:\"%s\"\n", device_name);
491  goto fail;
492  }
493 
494  try {
495  th_model->jit_model = new torch::jit::Module;
496  (*th_model->jit_model) = torch::jit::load(ctx->model_filename);
497  th_model->jit_model->to(device);
498  } catch (const c10::Error& e) {
499  av_log(ctx, AV_LOG_ERROR, "Failed to load torch model\n");
500  goto fail;
501  }
502 
503  if (ctx->nireq <= 0) {
504  ctx->nireq = av_cpu_count() / 2 + 1;
505  }
506 
507  th_model->request_queue = ff_safe_queue_create();
508  if (!th_model->request_queue) {
509  goto fail;
510  }
511 
512  for (int i = 0; i < ctx->nireq; i++) {
513  item = (THRequestItem *)av_mallocz(sizeof(THRequestItem));
514  if (!item) {
515  goto fail;
516  }
518  if (!item->infer_request) {
519  goto fail;
520  }
521  item->lltasks = (LastLevelTaskItem **)av_malloc_array(ctx->batch_size, sizeof(*item->lltasks));
522  if (!item->lltasks) {
523  goto fail;
524  }
525  item->lltask_count = 0;
526 
529  item->exec_module.args = item;
530 
531  if (ff_safe_queue_push_back(th_model->request_queue, item) < 0) {
532  goto fail;
533  }
534  item = NULL;
535  }
536 
537  th_model->task_queue = ff_queue_create();
538  th_model->lltask_queue = ff_queue_create();
539 
540  model->get_input = &get_input_th;
541  model->get_output = &get_output_th;
542  model->filter_ctx = filter_ctx;
543  model->func_type = func_type;
544  return model;
545 
546 fail:
547  if (item) {
548  destroy_request_item(&item);
549  }
550  dnn_free_model_th(&model);
551  return NULL;
552 }
553 
554 static int dnn_execute_model_th(const DNNModel *model, DNNExecBaseParams *exec_params)
555 {
556  THModel *th_model = (THModel *)model;
557  DnnContext *ctx = th_model->ctx;
558  TaskItem *task;
559  THRequestItem *request;
560  int ret = 0;
561 
562  ret = ff_check_exec_params(ctx, DNN_TH, model->func_type, exec_params);
563  if (ret != 0) {
564  av_log(ctx, AV_LOG_ERROR, "exec parameter checking fail.\n");
565  return ret;
566  }
567 
568  task = (TaskItem *)av_malloc(sizeof(TaskItem));
569  if (!task) {
570  av_log(ctx, AV_LOG_ERROR, "unable to alloc memory for task item.\n");
571  return AVERROR(ENOMEM);
572  }
573 
574  ret = ff_dnn_fill_task(task, exec_params, th_model, ctx->async, 1);
575  if (ret != 0) {
576  av_freep(&task);
577  av_log(ctx, AV_LOG_ERROR, "unable to fill task.\n");
578  return ret;
579  }
580 
581  ret = ff_queue_push_back(th_model->task_queue, task);
582  if (ret < 0) {
583  av_freep(&task);
584  av_log(ctx, AV_LOG_ERROR, "unable to push back task_queue.\n");
585  return ret;
586  }
587 
588  ret = extract_lltask_from_task(task, th_model->lltask_queue);
589  if (ret != 0) {
590  av_log(ctx, AV_LOG_ERROR, "unable to extract last level task from task.\n");
591  return ret;
592  }
593 
594  while (ff_queue_size(th_model->lltask_queue) >= ctx->batch_size) {
595  request = (THRequestItem *)ff_safe_queue_pop_front(th_model->request_queue);
596  if (!request) {
597  av_log(ctx, AV_LOG_ERROR, "unable to get infer request.\n");
598  return AVERROR(EINVAL);
599  }
600 
601  ret = execute_model_th(request, th_model->lltask_queue);
602  if (ret != 0) {
603  return ret;
604  }
605  }
606 
607  return 0;
608 }
609 
611 {
612  THModel *th_model = (THModel *)model;
613  return ff_dnn_get_result_common(th_model->task_queue, in, out);
614 }
615 
616 static int dnn_flush_th(const DNNModel *model)
617 {
618  THModel *th_model = (THModel *)model;
619  THRequestItem *request;
620 
621  if (ff_queue_size(th_model->lltask_queue) == 0)
622  // no pending task need to flush
623  return 0;
624 
625  request = (THRequestItem *)ff_safe_queue_pop_front(th_model->request_queue);
626  if (!request) {
627  av_log(th_model->ctx, AV_LOG_ERROR, "unable to get infer request.\n");
628  return AVERROR(EINVAL);
629  }
630 
631  return execute_model_th(request, th_model->lltask_queue);
632 }
633 
634 extern const DNNModule ff_dnn_backend_torch = {
635  .clazz = DNN_DEFINE_CLASS(dnn_th),
636  .type = DNN_TH,
637  .load_model = dnn_load_model_th,
638  .execute_model = dnn_execute_model_th,
639  .get_result = dnn_get_result_th,
640  .flush = dnn_flush_th,
641  .free_model = dnn_free_model_th,
642 };
THModel::lltask_queue
Queue * lltask_queue
Definition: dnn_backend_torch.cpp:45
THRequestItem::infer_request
THInferRequest * infer_request
Definition: dnn_backend_torch.cpp:54
THModel::ctx
DnnContext * ctx
Definition: dnn_backend_torch.cpp:41
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:157
FLAGS
#define FLAGS
Definition: dnn_backend_torch.cpp:62
THModel
Definition: dnn_backend_torch.cpp:39
DNNAsyncExecModule
Common Async Execution Mechanism for the DNN Backends.
Definition: dnn_backend_common.h:65
DNNFunctionType
DNNFunctionType
Definition: dnn_interface.h:57
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:472
AVFrame::width
int width
Definition: frame.h:544
is_available
static int is_available(const VVCFrameContext *fc, const int x0, const int y0)
Definition: mvs.c:552
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:554
AVOption
AVOption.
Definition: opt.h:428
DNNModel::frame_pre_proc
FramePrePostProc frame_pre_proc
Definition: dnn_interface.h:111
av_cpu_count
int av_cpu_count(void)
Definition: cpu.c:228
DNNExecBaseParams::input_name
const char * input_name
Definition: dnn_interface.h:82
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
cpu.h
DNNModel::filter_ctx
AVFilterContext * filter_ctx
Definition: dnn_interface.h:100
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:210
TaskItem::model
void * model
Definition: dnn_backend_common.h:44
DnnContext
Definition: dnn_interface.h:151
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:42
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:103
frame_size
int frame_size
Definition: mxfenc.c:2489
th_create_inference_request
static THInferRequest * th_create_inference_request(void)
Definition: dnn_backend_torch.cpp:447
ff_queue_destroy
void ff_queue_destroy(Queue *q)
Destroy the Queue instance.
Definition: queue.c:72
DNNData
Definition: dnn_interface.h:70
DNNModule::clazz
const AVClass clazz
Definition: dnn_interface.h:189
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:107
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:66
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:458
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
fail
#define fail
Definition: test.h:478
THRequestItem::exec_module
DNNAsyncExecModule exec_module
Definition: dnn_backend_torch.cpp:57
NULL
#define NULL
Definition: coverity.c:32
sizes
static const int sizes[][2]
Definition: img2dec.c:62
get_input_th
static int get_input_th(DNNModel *model, DNNData *input, const char *input_name)
Definition: dnn_backend_torch.cpp:145
ff_safe_queue_create
SafeQueue * ff_safe_queue_create(void)
Create and initialize a SafeQueue instance.
Definition: safe_queue.c:52
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:404
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:284
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:68
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:49
TaskItem::async
uint8_t async
Definition: dnn_backend_common.h:49
TaskItem::inference_done
uint32_t inference_done
Definition: dnn_backend_common.h:53
i
#define i(width, name, range_min, range_max)
Definition: cbs_h264.c:63
queue.h
DNNModel::func_type
DNNFunctionType func_type
Definition: dnn_interface.h:102
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.
av_malloc
#define av_malloc(s)
Definition: ops_static.c:44
ff_safe_queue_destroy
void ff_safe_queue_destroy(SafeQueue *sq)
Destroy the SafeQueue instance.
Definition: safe_queue.c:69
split
static char * split(char *message, char delim)
Definition: af_channelmap.c:89
DNN_FLOAT
@ DNN_FLOAT
Definition: dnn_interface.h:42
dnn_get_result_th
static DNNAsyncStatusType dnn_get_result_th(const DNNModel *model, AVFrame **in, AVFrame **out)
Definition: dnn_backend_torch.cpp:610
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
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:221
DNN_DEFINE_CLASS
#define DNN_DEFINE_CLASS(fname)
Definition: dnn_backend_common.h:39
THRequestItem
Definition: dnn_backend_torch.cpp:53
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:243
THInferRequest::input_tensor
torch::Tensor * input_tensor
Definition: dnn_backend_torch.cpp:50
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:32
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:48
outputs
static const AVFilterPad outputs[]
Definition: af_aap.c:310
ret
ret
Definition: filter_design.txt:187
TaskItem::out_frame
AVFrame * out_frame
Definition: dnn_backend_common.h:46
AVFrame::height
int height
Definition: frame.h:544
dnn_backend_common.h
THModel::model
DNNModel model
Definition: dnn_backend_torch.cpp:40
dnn_th_options
static const AVOption dnn_th_options[]
Definition: dnn_backend_torch.cpp:63
execute_model_th
static int execute_model_th(THRequestItem *request, Queue *lltask_queue)
Definition: dnn_backend_torch.cpp:354
OFFSET
#define OFFSET(x)
Definition: dnn_backend_torch.cpp:61
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:258
THRequestItem::lltasks
LastLevelTaskItem ** lltasks
Definition: dnn_backend_torch.cpp:55
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:47
AVFilterContext
An instance of a filter.
Definition: avfilter.h:273
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:98
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:215
dnn_flush_th
static int dnn_flush_th(const DNNModel *model)
Definition: dnn_backend_torch.cpp:616
THModel::task_queue
Queue * task_queue
Definition: dnn_backend_torch.cpp:44
dnn_get_channel_idx_by_layout
static int dnn_get_channel_idx_by_layout(DNNLayout layout)
Definition: dnn_interface.h:220
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
DNNExecBaseParams
Definition: dnn_interface.h:81
DNNModel::get_input
int(* get_input)(struct DNNModel *model, DNNData *input, const char *input_name)
Definition: dnn_interface.h:105
dnn_free_model_th
static void dnn_free_model_th(DNNModel **model)
Definition: dnn_backend_torch.cpp:117
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:50
DFT_PROCESS_FRAME
@ DFT_PROCESS_FRAME
Definition: dnn_interface.h:59
DNNModule
Definition: dnn_interface.h:188
fill_model_input_th
static int fill_model_input_th(THModel *th_model, THRequestItem *request)
Definition: dnn_backend_torch.cpp:162
THModel::request_queue
SafeQueue * request_queue
Definition: dnn_backend_torch.cpp:43
THRequestItem::lltask_count
uint32_t lltask_count
Definition: dnn_backend_torch.cpp:56
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:88