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 const AVClass dnn_native_class = {
32 .class_name = "dnn_native",
33 .item_name = av_default_item_name,
35 .version = LIBAVUTIL_VERSION_INT,
36 .category = AV_CLASS_CATEGORY_FILTER,
39 static DNNReturnType get_input_native(void *model, DNNData *input, const char *input_name)
41 NativeModel *native_model = (NativeModel *)model;
42 NativeContext *ctx = &native_model->ctx;
44 for (int i = 0; i < native_model->operands_num; ++i) {
45 DnnOperand *oprd = &native_model->operands[i];
46 if (strcmp(oprd->name, input_name) == 0) {
47 if (oprd->type != DOT_INPUT) {
48 av_log(ctx, AV_LOG_ERROR, "Found \"%s\" in model, but it is not input node\n", input_name);
51 input->dt = oprd->data_type;
52 av_assert0(oprd->dims[0] == 1);
53 input->height = oprd->dims[1];
54 input->width = oprd->dims[2];
55 input->channels = oprd->dims[3];
60 // do not find the input operand
61 av_log(ctx, AV_LOG_ERROR, "Could not find \"%s\" in model\n", input_name);
65 static DNNReturnType set_input_native(void *model, DNNData *input, const char *input_name)
67 NativeModel *native_model = (NativeModel *)model;
68 NativeContext *ctx = &native_model->ctx;
69 DnnOperand *oprd = NULL;
71 if (native_model->layers_num <= 0 || native_model->operands_num <= 0) {
72 av_log(ctx, AV_LOG_ERROR, "No operands or layers in model\n");
77 for (int i = 0; i < native_model->operands_num; ++i) {
78 oprd = &native_model->operands[i];
79 if (strcmp(oprd->name, input_name) == 0) {
80 if (oprd->type != DOT_INPUT) {
81 av_log(ctx, AV_LOG_ERROR, "Found \"%s\" in model, but it is not input node\n", input_name);
89 av_log(ctx, AV_LOG_ERROR, "Could not find \"%s\" in model\n", input_name);
94 oprd->dims[1] = input->height;
95 oprd->dims[2] = input->width;
96 oprd->dims[3] = input->channels;
98 av_freep(&oprd->data);
99 oprd->length = calculate_operand_data_length(oprd);
100 if (oprd->length <= 0) {
101 av_log(ctx, AV_LOG_ERROR, "The input data length overflow\n");
104 oprd->data = av_malloc(oprd->length);
106 av_log(ctx, AV_LOG_ERROR, "Failed to malloc memory for input data\n");
110 input->data = oprd->data;
115 // Loads model and its parameters that are stored in a binary file with following structure:
116 // layers_num,layer_type,layer_parameterss,layer_type,layer_parameters...
117 // For CONV layer: activation_function, input_num, output_num, kernel_size, kernel, biases
118 // For DEPTH_TO_SPACE layer: block_size
119 DNNModel *ff_dnn_load_model_native(const char *model_filename, const char *options)
121 DNNModel *model = NULL;
122 char header_expected[] = "FFMPEGDNNNATIVE";
125 int version, header_size, major_version_expected = 1;
126 NativeModel *native_model = NULL;
127 AVIOContext *model_file_context;
128 int file_size, dnn_size, parsed_size;
130 DNNLayerType layer_type;
132 if (avio_open(&model_file_context, model_filename, AVIO_FLAG_READ) < 0){
135 file_size = avio_size(model_file_context);
137 model = av_mallocz(sizeof(DNNModel));
143 * check file header with string and version
145 size = sizeof(header_expected);
146 buf = av_malloc(size);
151 // size - 1 to skip the ending '\0' which is not saved in file
152 avio_get_str(model_file_context, size - 1, buf, size);
154 if (strncmp(buf, header_expected, size) != 0) {
160 version = (int32_t)avio_rl32(model_file_context);
162 if (version != major_version_expected) {
166 // currently no need to check minor version
167 version = (int32_t)avio_rl32(model_file_context);
169 header_size = dnn_size;
171 native_model = av_mallocz(sizeof(NativeModel));
176 native_model->ctx.class = &dnn_native_class;
177 model->model = (void *)native_model;
179 avio_seek(model_file_context, file_size - 8, SEEK_SET);
180 native_model->layers_num = (int32_t)avio_rl32(model_file_context);
181 native_model->operands_num = (int32_t)avio_rl32(model_file_context);
183 avio_seek(model_file_context, header_size, SEEK_SET);
185 native_model->layers = av_mallocz(native_model->layers_num * sizeof(Layer));
186 if (!native_model->layers){
190 native_model->operands = av_mallocz(native_model->operands_num * sizeof(DnnOperand));
191 if (!native_model->operands){
195 for (layer = 0; layer < native_model->layers_num; ++layer){
196 layer_type = (int32_t)avio_rl32(model_file_context);
199 if (layer_type >= DLT_COUNT) {
203 native_model->layers[layer].type = layer_type;
204 parsed_size = layer_funcs[layer_type].pf_load(&native_model->layers[layer], model_file_context, file_size, native_model->operands_num);
208 dnn_size += parsed_size;
211 for (int32_t i = 0; i < native_model->operands_num; ++i){
214 int32_t operand_index = (int32_t)avio_rl32(model_file_context);
217 if (operand_index >= native_model->operands_num) {
221 oprd = &native_model->operands[operand_index];
222 name_len = (int32_t)avio_rl32(model_file_context);
225 avio_get_str(model_file_context, name_len, oprd->name, sizeof(oprd->name));
226 dnn_size += name_len;
228 oprd->type = (int32_t)avio_rl32(model_file_context);
231 oprd->data_type = (int32_t)avio_rl32(model_file_context);
234 for (int32_t dim = 0; dim < 4; ++dim) {
235 oprd->dims[dim] = (int32_t)avio_rl32(model_file_context);
242 avio_closep(&model_file_context);
244 if (dnn_size != file_size){
245 ff_dnn_free_model_native(&model);
249 model->set_input = &set_input_native;
250 model->get_input = &get_input_native;
251 model->options = options;
256 ff_dnn_free_model_native(&model);
257 avio_closep(&model_file_context);
261 DNNReturnType ff_dnn_execute_model_native(const DNNModel *model, DNNData *outputs, const char **output_names, uint32_t nb_output)
263 NativeModel *native_model = (NativeModel *)model->model;
264 NativeContext *ctx = &native_model->ctx;
267 if (native_model->layers_num <= 0 || native_model->operands_num <= 0) {
268 av_log(ctx, AV_LOG_ERROR, "No operands or layers in model\n");
271 if (!native_model->operands[0].data) {
272 av_log(ctx, AV_LOG_ERROR, "Empty model input data\n");
276 for (layer = 0; layer < native_model->layers_num; ++layer){
277 DNNLayerType layer_type = native_model->layers[layer].type;
278 if (layer_funcs[layer_type].pf_exec(native_model->operands,
279 native_model->layers[layer].input_operand_indexes,
280 native_model->layers[layer].output_operand_index,
281 native_model->layers[layer].params,
282 &native_model->ctx) == DNN_ERROR) {
283 av_log(ctx, AV_LOG_ERROR, "Failed to execuet model\n");
288 for (uint32_t i = 0; i < nb_output; ++i) {
289 DnnOperand *oprd = NULL;
290 const char *output_name = output_names[i];
291 for (int j = 0; j < native_model->operands_num; ++j) {
292 if (strcmp(native_model->operands[j].name, output_name) == 0) {
293 oprd = &native_model->operands[j];
299 av_log(ctx, AV_LOG_ERROR, "Could not find output in model\n");
303 outputs[i].data = oprd->data;
304 outputs[i].height = oprd->dims[1];
305 outputs[i].width = oprd->dims[2];
306 outputs[i].channels = oprd->dims[3];
307 outputs[i].dt = oprd->data_type;
313 int32_t calculate_operand_dims_count(const DnnOperand *oprd)
316 for (int i = 0; i < 4; ++i)
317 result *= oprd->dims[i];
322 int32_t calculate_operand_data_length(const DnnOperand* oprd)
324 // currently, we just support DNN_FLOAT
325 uint64_t len = sizeof(float);
326 for (int i = 0; i < 4; i++) {
327 len *= oprd->dims[i];
334 void ff_dnn_free_model_native(DNNModel **model)
336 NativeModel *native_model;
337 ConvolutionalParams *conv_params;
342 if ((*model)->model) {
343 native_model = (NativeModel *)(*model)->model;
344 if (native_model->layers) {
345 for (layer = 0; layer < native_model->layers_num; ++layer){
346 if (native_model->layers[layer].type == DLT_CONV2D){
347 conv_params = (ConvolutionalParams *)native_model->layers[layer].params;
348 av_freep(&conv_params->kernel);
349 av_freep(&conv_params->biases);
351 av_freep(&native_model->layers[layer].params);
353 av_freep(&native_model->layers);
356 if (native_model->operands) {
357 for (uint32_t operand = 0; operand < native_model->operands_num; ++operand)
358 av_freep(&native_model->operands[operand].data);
359 av_freep(&native_model->operands);
362 av_freep(&native_model);