[FFmpeg-devel] [PATCH 1/2] dnn_backend_native.c: parse options in native backend

xujunzz at sjtu.edu.cn xujunzz at sjtu.edu.cn
Thu Sep 3 18:57:23 EEST 2020


From: Xu Jun <xujunzz at sjtu.edu.cn>

Signed-off-by: Xu Jun <xujunzz at sjtu.edu.cn>
---
 libavfilter/dnn/dnn_backend_native.c | 22 ++++++++++++++++++++--
 libavfilter/dnn/dnn_backend_native.h | 13 +++++++++++++
 2 files changed, 33 insertions(+), 2 deletions(-)

diff --git a/libavfilter/dnn/dnn_backend_native.c b/libavfilter/dnn/dnn_backend_native.c
index a8fe6b94eb..83205aac72 100644
--- a/libavfilter/dnn/dnn_backend_native.c
+++ b/libavfilter/dnn/dnn_backend_native.c
@@ -31,7 +31,7 @@
 static const AVClass dnn_native_class = {
     .class_name = "dnn_native",
     .item_name  = av_default_item_name,
-    .option     = NULL,
+    .option     = dnn_native_options,
     .version    = LIBAVUTIL_VERSION_INT,
     .category   = AV_CLASS_CATEGORY_FILTER,
 };
@@ -112,6 +112,22 @@ static DNNReturnType set_input_native(void *model, DNNData *input, const char *i
     return DNN_SUCCESS;
 }
 
+static int dnn_parse_options(void *ctx, const char *options)
+{
+    AVDictionary *dict = NULL;
+    int err = av_dict_parse_string(&dict, options, "=", "&", 0);
+    if (err < 0) {
+        av_dict_free(&dict);
+        return err;
+    }
+
+    av_opt_set_defaults(ctx);
+    err = av_opt_set_dict(ctx, &dict);
+
+    av_dict_free(&dict);
+    return err;
+}
+
 // Loads model and its parameters that are stored in a binary file with following structure:
 // layers_num,layer_type,layer_parameterss,layer_type,layer_parameters...
 // For CONV layer: activation_function, input_num, output_num, kernel_size, kernel, biases
@@ -174,6 +190,9 @@ DNNModel *ff_dnn_load_model_native(const char *model_filename, const char *optio
     }
 
     native_model->ctx.class = &dnn_native_class;
+    model->options = options;
+    if (dnn_parse_options(&native_model->ctx, model->options) < 0)
+        goto fail;
     model->model = (void *)native_model;
 
     avio_seek(model_file_context, file_size - 8, SEEK_SET);
@@ -248,7 +267,6 @@ DNNModel *ff_dnn_load_model_native(const char *model_filename, const char *optio
 
     model->set_input = &set_input_native;
     model->get_input = &get_input_native;
-    model->options = options;
 
     return model;
 
diff --git a/libavfilter/dnn/dnn_backend_native.h b/libavfilter/dnn/dnn_backend_native.h
index 197f557dee..a19b9a4233 100644
--- a/libavfilter/dnn/dnn_backend_native.h
+++ b/libavfilter/dnn/dnn_backend_native.h
@@ -29,6 +29,7 @@
 
 #include "../dnn_interface.h"
 #include "libavformat/avio.h"
+#include "libavutil/opt.h"
 
 /**
  * the enum value of DNNLayerType should not be changed,
@@ -106,10 +107,22 @@ typedef struct InputParams{
     int height, width, channels;
 } InputParams;
 
+typedef struct NativeOptions{
+    uint32_t conv2d_threads;
+} NativeOptions;
+
 typedef struct NativeContext {
     const AVClass *class;
+    NativeOptions options;
 } NativeContext;
 
+#define OFFSET(x) offsetof(NativeContext, x)
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM
+static const AVOption dnn_native_options[] = {
+    { "conv2d_threads", "threads num for conv2d layer", OFFSET(options.conv2d_threads), AV_OPT_TYPE_INT,  { .i64 = 0 }, INT_MIN, INT_MAX, FLAGS },
+    { NULL },
+};
+
 // Represents simple feed-forward convolutional network.
 typedef struct NativeModel{
     NativeContext ctx;
-- 
2.28.0



More information about the ffmpeg-devel mailing list