summaryrefslogtreecommitdiff
path: root/libavfilter/dnn/dnn_backend_openvino.c
blob: d343bf25599b56336ef7fb09dc46561fad1a7e15 (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
 * Copyright (c) 2020
 *
 * 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 OpenVINO backend implementation.
 */

#include "dnn_backend_openvino.h"
#include "libavformat/avio.h"
#include "libavutil/avassert.h"
#include <c_api/ie_c_api.h>

typedef struct OVModel{
    ie_core_t *core;
    ie_network_t *network;
    ie_executable_network_t *exe_network;
    ie_infer_request_t *infer_request;
    ie_blob_t *input_blob;
    ie_blob_t **output_blobs;
    uint32_t nb_output;
} OVModel;

static DNNDataType precision_to_datatype(precision_e precision)
{
    switch (precision)
    {
    case FP32:
        return DNN_FLOAT;
    default:
        av_assert0(!"not supported yet.");
        return DNN_FLOAT;
    }
}

static DNNReturnType get_input_ov(void *model, DNNData *input, const char *input_name)
{
    OVModel *ov_model = (OVModel *)model;
    char *model_input_name = NULL;
    IEStatusCode status;
    size_t model_input_count = 0;
    dimensions_t dims;
    precision_e precision;

    status = ie_network_get_inputs_number(ov_model->network, &model_input_count);
    if (status != OK)
        return DNN_ERROR;

    for (size_t i = 0; i < model_input_count; i++) {
        status = ie_network_get_input_name(ov_model->network, i, &model_input_name);
        if (status != OK)
            return DNN_ERROR;
        if (strcmp(model_input_name, input_name) == 0) {
            ie_network_name_free(&model_input_name);
            status |= ie_network_get_input_dims(ov_model->network, input_name, &dims);
            status |= ie_network_get_input_precision(ov_model->network, input_name, &precision);
            if (status != OK)
                return DNN_ERROR;

            // The order of dims in the openvino is fixed and it is always NCHW for 4-D data.
            // while we pass NHWC data from FFmpeg to openvino
            status = ie_network_set_input_layout(ov_model->network, input_name, NHWC);
            if (status != OK)
                return DNN_ERROR;

            input->channels = dims.dims[1];
            input->height   = dims.dims[2];
            input->width    = dims.dims[3];
            input->dt       = precision_to_datatype(precision);
            return DNN_SUCCESS;
        }

        ie_network_name_free(&model_input_name);
    }

    return DNN_ERROR;
}

static DNNReturnType set_input_output_ov(void *model, DNNData *input, const char *input_name, const char **output_names, uint32_t nb_output)
{
    OVModel *ov_model = (OVModel *)model;
    IEStatusCode status;
    dimensions_t dims;
    precision_e precision;
    ie_blob_buffer_t blob_buffer;

    status = ie_exec_network_create_infer_request(ov_model->exe_network, &ov_model->infer_request);
    if (status != OK)
        goto err;

    status = ie_infer_request_get_blob(ov_model->infer_request, input_name, &ov_model->input_blob);
    if (status != OK)
        goto err;

    status |= ie_blob_get_dims(ov_model->input_blob, &dims);
    status |= ie_blob_get_precision(ov_model->input_blob, &precision);
    if (status != OK)
        goto err;

    av_assert0(input->channels == dims.dims[1]);
    av_assert0(input->height   == dims.dims[2]);
    av_assert0(input->width    == dims.dims[3]);
    av_assert0(input->dt       == precision_to_datatype(precision));

    status = ie_blob_get_buffer(ov_model->input_blob, &blob_buffer);
    if (status != OK)
        goto err;
    input->data = blob_buffer.buffer;

    // outputs
    ov_model->nb_output = 0;
    av_freep(&ov_model->output_blobs);
    ov_model->output_blobs = av_mallocz_array(nb_output, sizeof(*ov_model->output_blobs));
    if (!ov_model->output_blobs)
        goto err;

    for (int i = 0; i < nb_output; i++) {
        const char *output_name = output_names[i];
        status = ie_infer_request_get_blob(ov_model->infer_request, output_name, &(ov_model->output_blobs[i]));
        if (status != OK)
            goto err;
        ov_model->nb_output++;
    }

    return DNN_SUCCESS;

err:
    if (ov_model->output_blobs) {
        for (uint32_t i = 0; i < ov_model->nb_output; i++) {
            ie_blob_free(&(ov_model->output_blobs[i]));
        }
        av_freep(&ov_model->output_blobs);
    }
    if (ov_model->input_blob)
        ie_blob_free(&ov_model->input_blob);
    if (ov_model->infer_request)
        ie_infer_request_free(&ov_model->infer_request);
    return DNN_ERROR;
}

DNNModel *ff_dnn_load_model_ov(const char *model_filename, const char *options)
{
    DNNModel *model = NULL;
    OVModel *ov_model = NULL;
    IEStatusCode status;
    ie_config_t config = {NULL, NULL, NULL};

    model = av_malloc(sizeof(DNNModel));
    if (!model){
        return NULL;
    }

    ov_model = av_mallocz(sizeof(OVModel));
    if (!ov_model)
        goto err;

    status = ie_core_create("", &ov_model->core);
    if (status != OK)
        goto err;

    status = ie_core_read_network(ov_model->core, model_filename, NULL, &ov_model->network);
    if (status != OK)
        goto err;

    status = ie_core_load_network(ov_model->core, ov_model->network, "CPU", &config, &ov_model->exe_network);
    if (status != OK)
        goto err;

    model->model = (void *)ov_model;
    model->set_input_output = &set_input_output_ov;
    model->get_input = &get_input_ov;
    model->options = options;

    return model;

err:
    if (model)
        av_freep(&model);
    if (ov_model) {
        if (ov_model->exe_network)
            ie_exec_network_free(&ov_model->exe_network);
        if (ov_model->network)
            ie_network_free(&ov_model->network);
        if (ov_model->core)
            ie_core_free(&ov_model->core);
        av_freep(&ov_model);
    }
    return NULL;
}

DNNReturnType ff_dnn_execute_model_ov(const DNNModel *model, DNNData *outputs, uint32_t nb_output)
{
    dimensions_t dims;
    precision_e precision;
    ie_blob_buffer_t blob_buffer;
    OVModel *ov_model = (OVModel *)model->model;
    uint32_t nb = FFMIN(nb_output, ov_model->nb_output);
    IEStatusCode status = ie_infer_request_infer(ov_model->infer_request);
    if (status != OK)
        return DNN_ERROR;

    for (uint32_t i = 0; i < nb; ++i) {
        status = ie_blob_get_buffer(ov_model->output_blobs[i], &blob_buffer);
        if (status != OK)
            return DNN_ERROR;

        status |= ie_blob_get_dims(ov_model->output_blobs[i], &dims);
        status |= ie_blob_get_precision(ov_model->output_blobs[i], &precision);
        if (status != OK)
            return DNN_ERROR;

        outputs[i].channels = dims.dims[1];
        outputs[i].height   = dims.dims[2];
        outputs[i].width    = dims.dims[3];
        outputs[i].dt       = precision_to_datatype(precision);
        outputs[i].data     = blob_buffer.buffer;
    }

    return DNN_SUCCESS;
}

void ff_dnn_free_model_ov(DNNModel **model)
{
    if (*model){
        OVModel *ov_model = (OVModel *)(*model)->model;
        if (ov_model->output_blobs) {
            for (uint32_t i = 0; i < ov_model->nb_output; i++) {
                ie_blob_free(&(ov_model->output_blobs[i]));
            }
            av_freep(&ov_model->output_blobs);
        }
        if (ov_model->input_blob)
            ie_blob_free(&ov_model->input_blob);
        if (ov_model->infer_request)
            ie_infer_request_free(&ov_model->infer_request);
        if (ov_model->exe_network)
            ie_exec_network_free(&ov_model->exe_network);
        if (ov_model->network)
            ie_network_free(&ov_model->network);
        if (ov_model->core)
            ie_core_free(&ov_model->core);
        av_freep(&ov_model);
        av_freep(model);
    }
}