diff mbox series

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

Message ID 20200904124604.179561-1-xujunzz@sjtu.edu.cn
State Superseded
Headers show
Series [FFmpeg-devel,v2,1/2] dnn_backend_native.c: parse options in native backend | expand

Checks

Context Check Description
andriy/default pending
andriy/make_warn warning New warnings during build
andriy/make success Make finished
andriy/make_fate success Make fate finished

Commit Message

Xu Jun Sept. 4, 2020, 12:46 p.m. UTC
From: Xu Jun <xujunzz@sjtu.edu.cn>

v2: use av_opt_set_from_string instead of function dnn_parse_option().

Signed-off-by: Xu Jun <xujunzz@sjtu.edu.cn>
---
 libavfilter/dnn/dnn_backend_native.c | 19 ++++++++++---------
 libavfilter/dnn/dnn_backend_native.h | 21 +++++++++++++++++++++
 2 files changed, 31 insertions(+), 9 deletions(-)

Comments

Guo, Yejun Sept. 4, 2020, 1:32 p.m. UTC | #1
> -----Original Message-----
> From: ffmpeg-devel <ffmpeg-devel-bounces@ffmpeg.org> On Behalf Of
> xujunzz@sjtu.edu.cn
> Sent: 2020年9月4日 20:46
> To: ffmpeg-devel@ffmpeg.org
> Cc: xujunzz@sjtu.edu.cn
> Subject: [FFmpeg-devel] [PATCH v2 1/2] dnn_backend_native.c: parse options in
> native backend
> 
> From: Xu Jun <xujunzz@sjtu.edu.cn>
> 
> v2: use av_opt_set_from_string instead of function dnn_parse_option().
> 
> Signed-off-by: Xu Jun <xujunzz@sjtu.edu.cn>
> ---

please add your v2/v3 comments here, so it will not be shown in commit log.

>  libavfilter/dnn/dnn_backend_native.c | 19 ++++++++++---------
> libavfilter/dnn/dnn_backend_native.h | 21 +++++++++++++++++++++
>  2 files changed, 31 insertions(+), 9 deletions(-)
> 
> diff --git a/libavfilter/dnn/dnn_backend_native.c
> b/libavfilter/dnn/dnn_backend_native.c
> index a8fe6b94eb..b1ee6fb399 100644
> --- a/libavfilter/dnn/dnn_backend_native.c
> +++ b/libavfilter/dnn/dnn_backend_native.c
> @@ -28,14 +28,6 @@
>  #include "dnn_backend_native_layer_conv2d.h"
>  #include "dnn_backend_native_layers.h"
> 
> -static const AVClass dnn_native_class = {
> -    .class_name = "dnn_native",
> -    .item_name  = av_default_item_name,
> -    .option     = NULL,
> -    .version    = LIBAVUTIL_VERSION_INT,
> -    .category   = AV_CLASS_CATEGORY_FILTER,
> -};
> -
>  static DNNReturnType get_input_native(void *model, DNNData *input, const
> char *input_name)  {
>      NativeModel *native_model = (NativeModel *)model; @@ -174,8
> +166,18 @@ DNNModel *ff_dnn_load_model_native(const char
> *model_filename, const char *optio
>      }
> 
>      native_model->ctx.class = &dnn_native_class;
> +    model->options = options;
> +    if (av_opt_set_from_string(&native_model->ctx, model->options,
> "conv2d_threads", "=", "&") < 0)

should be  if (av_opt_set_from_string(&native_model->ctx, model->options, NULL, "=", "&") < 0) , 
so all the options are supported, not just conv2d_threads, and it also fixed the build warning.
diff mbox series

Patch

diff --git a/libavfilter/dnn/dnn_backend_native.c b/libavfilter/dnn/dnn_backend_native.c
index a8fe6b94eb..b1ee6fb399 100644
--- a/libavfilter/dnn/dnn_backend_native.c
+++ b/libavfilter/dnn/dnn_backend_native.c
@@ -28,14 +28,6 @@ 
 #include "dnn_backend_native_layer_conv2d.h"
 #include "dnn_backend_native_layers.h"
 
-static const AVClass dnn_native_class = {
-    .class_name = "dnn_native",
-    .item_name  = av_default_item_name,
-    .option     = NULL,
-    .version    = LIBAVUTIL_VERSION_INT,
-    .category   = AV_CLASS_CATEGORY_FILTER,
-};
-
 static DNNReturnType get_input_native(void *model, DNNData *input, const char *input_name)
 {
     NativeModel *native_model = (NativeModel *)model;
@@ -174,8 +166,18 @@  DNNModel *ff_dnn_load_model_native(const char *model_filename, const char *optio
     }
 
     native_model->ctx.class = &dnn_native_class;
+    model->options = options;
+    if (av_opt_set_from_string(&native_model->ctx, model->options, "conv2d_threads", "=", "&") < 0)
+        goto fail;
     model->model = (void *)native_model;
 
+#if !HAVE_PTHREAD_CANCEL
+    if (native_model->ctx.options.conv2d_threads > 1){
+        av_log(&native_model->ctx, AV_LOG_WARNING, "'conv2d_threads' option was set but it is not supported "
+                       "on this build (pthread support is required)\n");
+    }
+#endif
+
     avio_seek(model_file_context, file_size - 8, SEEK_SET);
     native_model->layers_num = (int32_t)avio_rl32(model_file_context);
     native_model->operands_num = (int32_t)avio_rl32(model_file_context);
@@ -248,7 +250,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..3954fee199 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,30 @@  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 },
+};
+
+static const AVClass dnn_native_class = {
+    .class_name = "dnn_native",
+    .item_name  = av_default_item_name,
+    .option     = dnn_native_options,
+    .version    = LIBAVUTIL_VERSION_INT,
+    .category   = AV_CLASS_CATEGORY_FILTER,
+};
+
 // Represents simple feed-forward convolutional network.
 typedef struct NativeModel{
     NativeContext ctx;