2 * Copyright (c) 2018 Sergey Lavrushkin
4 * This file is part of FFmpeg.
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.
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.
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
23 * DNN native backend implementation.
26 #include "dnn_backend_native.h"
27 #include "libavutil/avassert.h"
28 #include "dnn_backend_native_layer_conv2d.h"
29 #include "dnn_backend_native_layers.h"
31 static DNNReturnType get_input_native(void *model, DNNData *input, const char *input_name)
33 NativeModel *native_model = (NativeModel *)model;
35 for (int i = 0; i < native_model->operands_num; ++i) {
36 DnnOperand *oprd = &native_model->operands[i];
37 if (strcmp(oprd->name, input_name) == 0) {
38 if (oprd->type != DOT_INPUT)
40 input->dt = oprd->data_type;
41 av_assert0(oprd->dims[0] == 1);
42 input->height = oprd->dims[1];
43 input->width = oprd->dims[2];
44 input->channels = oprd->dims[3];
49 // do not find the input operand
53 static DNNReturnType set_input_output_native(void *model, DNNData *input, const char *input_name, const char **output_names, uint32_t nb_output)
55 NativeModel *native_model = (NativeModel *)model;
56 DnnOperand *oprd = NULL;
58 if (native_model->layers_num <= 0 || native_model->operands_num <= 0)
62 for (int i = 0; i < native_model->operands_num; ++i) {
63 oprd = &native_model->operands[i];
64 if (strcmp(oprd->name, input_name) == 0) {
65 if (oprd->type != DOT_INPUT)
76 oprd->dims[1] = input->height;
77 oprd->dims[2] = input->width;
78 oprd->dims[3] = input->channels;
80 av_freep(&oprd->data);
81 oprd->length = calculate_operand_data_length(oprd);
82 if (oprd->length <= 0)
84 oprd->data = av_malloc(oprd->length);
88 input->data = oprd->data;
91 native_model->nb_output = 0;
92 av_freep(&native_model->output_indexes);
93 native_model->output_indexes = av_mallocz_array(nb_output, sizeof(*native_model->output_indexes));
94 if (!native_model->output_indexes)
97 for (uint32_t i = 0; i < nb_output; ++i) {
98 const char *output_name = output_names[i];
99 for (int j = 0; j < native_model->operands_num; ++j) {
100 oprd = &native_model->operands[j];
101 if (strcmp(oprd->name, output_name) == 0) {
102 native_model->output_indexes[native_model->nb_output++] = j;
108 if (native_model->nb_output != nb_output)
114 // Loads model and its parameters that are stored in a binary file with following structure:
115 // layers_num,layer_type,layer_parameterss,layer_type,layer_parameters...
116 // For CONV layer: activation_function, input_num, output_num, kernel_size, kernel, biases
117 // For DEPTH_TO_SPACE layer: block_size
118 DNNModel *ff_dnn_load_model_native(const char *model_filename, const char *options)
120 DNNModel *model = NULL;
121 char header_expected[] = "FFMPEGDNNNATIVE";
124 int version, header_size, major_version_expected = 1;
125 NativeModel *native_model = NULL;
126 AVIOContext *model_file_context;
127 int file_size, dnn_size, parsed_size;
129 DNNLayerType layer_type;
131 if (avio_open(&model_file_context, model_filename, AVIO_FLAG_READ) < 0){
134 file_size = avio_size(model_file_context);
136 model = av_mallocz(sizeof(DNNModel));
142 * check file header with string and version
144 size = sizeof(header_expected);
145 buf = av_malloc(size);
150 // size - 1 to skip the ending '\0' which is not saved in file
151 avio_get_str(model_file_context, size - 1, buf, size);
153 if (strncmp(buf, header_expected, size) != 0) {
159 version = (int32_t)avio_rl32(model_file_context);
161 if (version != major_version_expected) {
165 // currently no need to check minor version
166 version = (int32_t)avio_rl32(model_file_context);
168 header_size = dnn_size;
170 native_model = av_mallocz(sizeof(NativeModel));
174 model->model = (void *)native_model;
176 avio_seek(model_file_context, file_size - 8, SEEK_SET);
177 native_model->layers_num = (int32_t)avio_rl32(model_file_context);
178 native_model->operands_num = (int32_t)avio_rl32(model_file_context);
180 avio_seek(model_file_context, header_size, SEEK_SET);
182 native_model->layers = av_mallocz(native_model->layers_num * sizeof(Layer));
183 if (!native_model->layers){
187 native_model->operands = av_mallocz(native_model->operands_num * sizeof(DnnOperand));
188 if (!native_model->operands){
192 for (layer = 0; layer < native_model->layers_num; ++layer){
193 layer_type = (int32_t)avio_rl32(model_file_context);
196 if (layer_type >= DLT_COUNT) {
200 native_model->layers[layer].type = layer_type;
201 parsed_size = layer_funcs[layer_type].pf_load(&native_model->layers[layer], model_file_context, file_size, native_model->operands_num);
205 dnn_size += parsed_size;
208 for (int32_t i = 0; i < native_model->operands_num; ++i){
211 int32_t operand_index = (int32_t)avio_rl32(model_file_context);
214 if (operand_index >= native_model->operands_num) {
218 oprd = &native_model->operands[operand_index];
219 name_len = (int32_t)avio_rl32(model_file_context);
222 avio_get_str(model_file_context, name_len, oprd->name, sizeof(oprd->name));
223 dnn_size += name_len;
225 oprd->type = (int32_t)avio_rl32(model_file_context);
228 oprd->data_type = (int32_t)avio_rl32(model_file_context);
231 for (int32_t dim = 0; dim < 4; ++dim) {
232 oprd->dims[dim] = (int32_t)avio_rl32(model_file_context);
239 avio_closep(&model_file_context);
241 if (dnn_size != file_size){
242 ff_dnn_free_model_native(&model);
246 model->set_input_output = &set_input_output_native;
247 model->get_input = &get_input_native;
248 model->options = options;
253 ff_dnn_free_model_native(&model);
254 avio_closep(&model_file_context);
258 DNNReturnType ff_dnn_execute_model_native(const DNNModel *model, DNNData *outputs, uint32_t nb_output)
260 NativeModel *native_model = (NativeModel *)model->model;
262 uint32_t nb = FFMIN(nb_output, native_model->nb_output);
264 if (native_model->layers_num <= 0 || native_model->operands_num <= 0)
266 if (!native_model->operands[0].data)
269 for (layer = 0; layer < native_model->layers_num; ++layer){
270 DNNLayerType layer_type = native_model->layers[layer].type;
271 layer_funcs[layer_type].pf_exec(native_model->operands,
272 native_model->layers[layer].input_operand_indexes,
273 native_model->layers[layer].output_operand_index,
274 native_model->layers[layer].params);
277 for (uint32_t i = 0; i < nb; ++i) {
278 DnnOperand *oprd = &native_model->operands[native_model->output_indexes[i]];
279 outputs[i].data = oprd->data;
280 outputs[i].height = oprd->dims[1];
281 outputs[i].width = oprd->dims[2];
282 outputs[i].channels = oprd->dims[3];
283 outputs[i].dt = oprd->data_type;
289 int32_t calculate_operand_dims_count(const DnnOperand *oprd)
292 for (int i = 0; i < 4; ++i)
293 result *= oprd->dims[i];
298 int32_t calculate_operand_data_length(const DnnOperand* oprd)
300 // currently, we just support DNN_FLOAT
301 uint64_t len = sizeof(float);
302 for (int i = 0; i < 4; i++) {
303 len *= oprd->dims[i];
310 void ff_dnn_free_model_native(DNNModel **model)
312 NativeModel *native_model;
313 ConvolutionalParams *conv_params;
318 if ((*model)->model) {
319 native_model = (NativeModel *)(*model)->model;
320 if (native_model->layers) {
321 for (layer = 0; layer < native_model->layers_num; ++layer){
322 if (native_model->layers[layer].type == DLT_CONV2D){
323 conv_params = (ConvolutionalParams *)native_model->layers[layer].params;
324 av_freep(&conv_params->kernel);
325 av_freep(&conv_params->biases);
327 av_freep(&native_model->layers[layer].params);
329 av_freep(&native_model->layers);
332 if (native_model->operands) {
333 for (uint32_t operand = 0; operand < native_model->operands_num; ++operand)
334 av_freep(&native_model->operands[operand].data);
335 av_freep(&native_model->operands);
338 av_freep(&native_model->output_indexes);
339 av_freep(&native_model);