[FFmpeg-devel] [PATCH 3/4] libavfilter/dnn: separate depth_to_space layer from dnn_backend_native.c to a new file

Guo, Yejun yejun.guo at intel.com
Thu Sep 5 09:00:46 EEST 2019


the logic is that one layer in one separated source file to make
the source files simple for maintaining.

Signed-off-by: Guo, Yejun <yejun.guo at intel.com>
---
 libavfilter/dnn/Makefile                           |  1 +
 libavfilter/dnn/dnn_backend_native.c               | 44 +-------------
 libavfilter/dnn/dnn_backend_native.h               |  4 --
 .../dnn/dnn_backend_native_layer_depth2space.c     | 71 ++++++++++++++++++++++
 .../dnn/dnn_backend_native_layer_depth2space.h     | 39 ++++++++++++
 libavfilter/dnn/dnn_backend_tf.c                   |  1 +
 6 files changed, 113 insertions(+), 47 deletions(-)
 create mode 100644 libavfilter/dnn/dnn_backend_native_layer_depth2space.c
 create mode 100644 libavfilter/dnn/dnn_backend_native_layer_depth2space.h

diff --git a/libavfilter/dnn/Makefile b/libavfilter/dnn/Makefile
index 40b848b..63a35e7 100644
--- a/libavfilter/dnn/Makefile
+++ b/libavfilter/dnn/Makefile
@@ -2,6 +2,7 @@ OBJS-$(CONFIG_DNN)                           += dnn/dnn_interface.o
 OBJS-$(CONFIG_DNN)                           += dnn/dnn_backend_native.o
 OBJS-$(CONFIG_DNN)                           += dnn/dnn_backend_native_layer_pad.o
 OBJS-$(CONFIG_DNN)                           += dnn/dnn_backend_native_layer_conv2d.o
+OBJS-$(CONFIG_DNN)                           += dnn/dnn_backend_native_layer_depth2space.o
 
 DNN-OBJS-$(CONFIG_LIBTENSORFLOW)             += dnn/dnn_backend_tf.o
 
diff --git a/libavfilter/dnn/dnn_backend_native.c b/libavfilter/dnn/dnn_backend_native.c
index 5dabd15..be548c6 100644
--- a/libavfilter/dnn/dnn_backend_native.c
+++ b/libavfilter/dnn/dnn_backend_native.c
@@ -27,6 +27,7 @@
 #include "libavutil/avassert.h"
 #include "dnn_backend_native_layer_pad.h"
 #include "dnn_backend_native_layer_conv2d.h"
+#include "dnn_backend_native_layer_depth2space.h"
 
 static DNNReturnType set_input_output_native(void *model, DNNInputData *input, const char *input_name, const char **output_names, uint32_t nb_output)
 {
@@ -282,49 +283,6 @@ DNNModel *ff_dnn_load_model_native(const char *model_filename)
     return model;
 }
 
-static int depth_to_space(DnnOperand *operands, const int32_t *input_operand_indexes, int32_t output_operand_index, int block_size)
-{
-    float *output;
-    int32_t input_operand_index = input_operand_indexes[0];
-    int number = operands[input_operand_index].dims[0];
-    int height = operands[input_operand_index].dims[1];
-    int width = operands[input_operand_index].dims[2];
-    int channels = operands[input_operand_index].dims[3];
-    const float *input = operands[input_operand_index].data;
-
-    int y, x, by, bx, ch;
-    int new_channels = channels / (block_size * block_size);
-    int output_linesize = width * channels;
-    int by_linesize = output_linesize / block_size;
-    int x_linesize = new_channels * block_size;
-
-    DnnOperand *output_operand = &operands[output_operand_index];
-    output_operand->dims[0] = number;
-    output_operand->dims[1] = height * block_size;
-    output_operand->dims[2] = width * block_size;
-    output_operand->dims[3] = new_channels;
-    output_operand->length = calculate_operand_data_length(output_operand);
-    output_operand->data = av_realloc(output_operand->data, output_operand->length);
-    if (!output_operand->data)
-        return -1;
-    output = output_operand->data;
-
-    for (y = 0; y < height; ++y){
-        for (x = 0; x < width; ++x){
-            for (by = 0; by < block_size; ++by){
-                for (bx = 0; bx < block_size; ++bx){
-                    for (ch = 0; ch < new_channels; ++ch){
-                        output[by * by_linesize + x * x_linesize + bx * new_channels + ch] = input[ch];
-                    }
-                    input += new_channels;
-                }
-            }
-        }
-        output += output_linesize;
-    }
-    return 0;
-}
-
 DNNReturnType ff_dnn_execute_model_native(const DNNModel *model, DNNData *outputs, uint32_t nb_output)
 {
     ConvolutionalNetwork *network = (ConvolutionalNetwork *)model->model;
diff --git a/libavfilter/dnn/dnn_backend_native.h b/libavfilter/dnn/dnn_backend_native.h
index aa52222..a74d138 100644
--- a/libavfilter/dnn/dnn_backend_native.h
+++ b/libavfilter/dnn/dnn_backend_native.h
@@ -90,10 +90,6 @@ typedef struct InputParams{
     int height, width, channels;
 } InputParams;
 
-typedef struct DepthToSpaceParams{
-    int block_size;
-} DepthToSpaceParams;
-
 // Represents simple feed-forward convolutional network.
 typedef struct ConvolutionalNetwork{
     Layer *layers;
diff --git a/libavfilter/dnn/dnn_backend_native_layer_depth2space.c b/libavfilter/dnn/dnn_backend_native_layer_depth2space.c
new file mode 100644
index 0000000..a248764
--- /dev/null
+++ b/libavfilter/dnn/dnn_backend_native_layer_depth2space.c
@@ -0,0 +1,71 @@
+/*
+ * Copyright (c) 2018 Sergey Lavrushkin
+ *
+ * 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 native backend implementation.
+ */
+
+#include "dnn_backend_native.h"
+#include "libavutil/avassert.h"
+#include "dnn_backend_native_layer_depth2space.h"
+
+int depth_to_space(DnnOperand *operands, const int32_t *input_operand_indexes, int32_t output_operand_index, int block_size)
+{
+    float *output;
+    int32_t input_operand_index = input_operand_indexes[0];
+    int number = operands[input_operand_index].dims[0];
+    int height = operands[input_operand_index].dims[1];
+    int width = operands[input_operand_index].dims[2];
+    int channels = operands[input_operand_index].dims[3];
+    const float *input = operands[input_operand_index].data;
+
+    int y, x, by, bx, ch;
+    int new_channels = channels / (block_size * block_size);
+    int output_linesize = width * channels;
+    int by_linesize = output_linesize / block_size;
+    int x_linesize = new_channels * block_size;
+
+    DnnOperand *output_operand = &operands[output_operand_index];
+    output_operand->dims[0] = number;
+    output_operand->dims[1] = height * block_size;
+    output_operand->dims[2] = width * block_size;
+    output_operand->dims[3] = new_channels;
+    output_operand->length = calculate_operand_data_length(output_operand);
+    output_operand->data = av_realloc(output_operand->data, output_operand->length);
+    if (!output_operand->data)
+        return -1;
+    output = output_operand->data;
+
+    for (y = 0; y < height; ++y){
+        for (x = 0; x < width; ++x){
+            for (by = 0; by < block_size; ++by){
+                for (bx = 0; bx < block_size; ++bx){
+                    for (ch = 0; ch < new_channels; ++ch){
+                        output[by * by_linesize + x * x_linesize + bx * new_channels + ch] = input[ch];
+                    }
+                    input += new_channels;
+                }
+            }
+        }
+        output += output_linesize;
+    }
+    return 0;
+}
diff --git a/libavfilter/dnn/dnn_backend_native_layer_depth2space.h b/libavfilter/dnn/dnn_backend_native_layer_depth2space.h
new file mode 100644
index 0000000..7a051af
--- /dev/null
+++ b/libavfilter/dnn/dnn_backend_native_layer_depth2space.h
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2018 Sergey Lavrushkin
+ *
+ * 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 inference functions interface for native backend.
+ */
+
+
+#ifndef AVFILTER_DNN_DNN_BACKEND_NATIVE_DEPTH2SPACE_H
+#define AVFILTER_DNN_DNN_BACKEND_NATIVE_DEPTH2SPACE_H
+
+#include "../dnn_interface.h"
+#include "libavformat/avio.h"
+
+typedef struct DepthToSpaceParams{
+    int block_size;
+} DepthToSpaceParams;
+
+int depth_to_space(DnnOperand *operands, const int32_t *input_operand_indexes, int32_t output_operand_index, int block_size);
+
+#endif
diff --git a/libavfilter/dnn/dnn_backend_tf.c b/libavfilter/dnn/dnn_backend_tf.c
index 46dfa00..8a3e40a 100644
--- a/libavfilter/dnn/dnn_backend_tf.c
+++ b/libavfilter/dnn/dnn_backend_tf.c
@@ -26,6 +26,7 @@
 #include "dnn_backend_tf.h"
 #include "dnn_backend_native.h"
 #include "dnn_backend_native_layer_conv2d.h"
+#include "dnn_backend_native_layer_depth2space.h"
 #include "libavformat/avio.h"
 #include "libavutil/avassert.h"
 #include "dnn_backend_native_layer_pad.h"
-- 
2.7.4



More information about the ffmpeg-devel mailing list