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