summaryrefslogtreecommitdiff
path: root/libavfilter/dnn/dnn_backend_common.c
blob: 91a4a3c4bf41096407249d8650ab52987c36fbd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
 * @file
 * DNN common functions different backends.
 */

#include "dnn_backend_common.h"

#define DNN_ASYNC_SUCCESS (void *)0
#define DNN_ASYNC_FAIL (void *)-1

int ff_check_exec_params(void *ctx, DNNBackendType backend, DNNFunctionType func_type, DNNExecBaseParams *exec_params)
{
    if (!exec_params) {
        av_log(ctx, AV_LOG_ERROR, "exec_params is null when execute model.\n");
        return AVERROR(EINVAL);
    }

    if (!exec_params->in_frame) {
        av_log(ctx, AV_LOG_ERROR, "in frame is NULL when execute model.\n");
        return AVERROR(EINVAL);
    }

    if (!exec_params->out_frame && func_type == DFT_PROCESS_FRAME) {
        av_log(ctx, AV_LOG_ERROR, "out frame is NULL when execute model.\n");
        return AVERROR(EINVAL);
    }

    if (exec_params->nb_output != 1 && backend != DNN_TF) {
        // currently, the filter does not need multiple outputs,
        // so we just pending the support until we really need it.
        avpriv_report_missing_feature(ctx, "multiple outputs");
        return AVERROR(ENOSYS);
    }

    return 0;
}

int ff_dnn_fill_task(TaskItem *task, DNNExecBaseParams *exec_params, void *backend_model, int async, int do_ioproc) {
    if (task == NULL || exec_params == NULL || backend_model == NULL)
        return AVERROR(EINVAL);
    if (do_ioproc != 0 && do_ioproc != 1)
        return AVERROR(EINVAL);
    if (async != 0 && async != 1)
        return AVERROR(EINVAL);

    task->do_ioproc = do_ioproc;
    task->async = async;
    task->input_name = exec_params->input_name;
    task->in_frame = exec_params->in_frame;
    task->out_frame = exec_params->out_frame;
    task->model = backend_model;
    task->nb_output = exec_params->nb_output;
    task->output_names = exec_params->output_names;

    return 0;
}

/**
 * Thread routine for async execution.
 * @param args pointer to DNNAsyncExecModule module
 */
static void *async_thread_routine(void *args)
{
    DNNAsyncExecModule *async_module = args;
    void *request = async_module->args;

    if (async_module->start_inference(request) != 0) {
        return DNN_ASYNC_FAIL;
    }
    async_module->callback(request);
    return DNN_ASYNC_SUCCESS;
}

int ff_dnn_async_module_cleanup(DNNAsyncExecModule *async_module)
{
    void *status = 0;
    if (!async_module) {
        return AVERROR(EINVAL);
    }
#if HAVE_PTHREAD_CANCEL
    pthread_join(async_module->thread_id, &status);
    if (status == DNN_ASYNC_FAIL) {
        av_log(NULL, AV_LOG_ERROR, "Last Inference Failed.\n");
        return DNN_GENERIC_ERROR;
    }
#endif
    async_module->start_inference = NULL;
    async_module->callback = NULL;
    async_module->args = NULL;
    return 0;
}

int ff_dnn_start_inference_async(void *ctx, DNNAsyncExecModule *async_module)
{
    int ret;
    void *status = 0;

    if (!async_module) {
        av_log(ctx, AV_LOG_ERROR, "async_module is null when starting async inference.\n");
        return AVERROR(EINVAL);
    }

#if HAVE_PTHREAD_CANCEL
    pthread_join(async_module->thread_id, &status);
    if (status == DNN_ASYNC_FAIL) {
        av_log(ctx, AV_LOG_ERROR, "Unable to start inference as previous inference failed.\n");
        return DNN_GENERIC_ERROR;
    }
    ret = pthread_create(&async_module->thread_id, NULL, async_thread_routine, async_module);
    if (ret != 0) {
        av_log(ctx, AV_LOG_ERROR, "Unable to start async inference.\n");
        return ret;
    }
#else
    ret = async_module->start_inference(async_module->args);
    if (ret != 0) {
        return ret;
    }
    async_module->callback(async_module->args);
#endif
    return 0;
}

DNNAsyncStatusType ff_dnn_get_result_common(Queue *task_queue, AVFrame **in, AVFrame **out)
{
    TaskItem *task = ff_queue_peek_front(task_queue);

    if (!task) {
        return DAST_EMPTY_QUEUE;
    }

    if (task->inference_done != task->inference_todo) {
        return DAST_NOT_READY;
    }

    *in = task->in_frame;
    *out = task->out_frame;
    ff_queue_pop_front(task_queue);
    av_freep(&task);

    return DAST_SUCCESS;
}

int ff_dnn_fill_gettingoutput_task(TaskItem *task, DNNExecBaseParams *exec_params, void *backend_model, int input_height, int input_width, void *ctx)
{
    AVFrame *in_frame = NULL;
    AVFrame *out_frame = NULL;

    in_frame = av_frame_alloc();
    if (!in_frame) {
        av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for input frame\n");
        return AVERROR(ENOMEM);
    }

    out_frame = av_frame_alloc();
    if (!out_frame) {
        av_frame_free(&in_frame);
        av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for output frame\n");
        return AVERROR(ENOMEM);
    }

    in_frame->width = input_width;
    in_frame->height = input_height;
    exec_params->in_frame = in_frame;
    exec_params->out_frame = out_frame;

    return ff_dnn_fill_task(task, exec_params, backend_model, 0, 0);
}