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