Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH 1/2] avfilter/dnn: do not manually parse anchors filter option
@ 2025-02-26 13:57 Leandro Santiago
  2025-02-27  2:38 ` Chen, Wenbin
  0 siblings, 1 reply; 2+ messages in thread
From: Leandro Santiago @ 2025-02-26 13:57 UTC (permalink / raw)
  To: ffmpeg-devel

Instead, rely on AV_OPT_TYPE_FLAG_ARRAY, which will automatically
perform the parsing.

Signed-off-by: Leandro Santiago <leandrosansilva@gmail.com>
---
 libavfilter/vf_dnn_detect.c | 43 ++++---------------------------------
 1 file changed, 4 insertions(+), 39 deletions(-)

diff --git a/libavfilter/vf_dnn_detect.c b/libavfilter/vf_dnn_detect.c
index 590e4e256d..cd70c64e98 100644
--- a/libavfilter/vf_dnn_detect.c
+++ b/libavfilter/vf_dnn_detect.c
@@ -31,6 +31,7 @@
 #include "libavutil/avstring.h"
 #include "libavutil/detection_bbox.h"
 #include "libavutil/fifo.h"
+#include <float.h>
 
 typedef enum {
     DDMT_SSD,
@@ -53,11 +54,12 @@ typedef struct DnnDetectContext {
     AVFifo *bboxes_fifo;
     int scale_width;
     int scale_height;
-    char *anchors_str;
     float *anchors;
     int nb_anchor;
 } DnnDetectContext;
 
+static const AVOptionArrayDef anchor_array_def = { .sep = '&' };
+
 #define OFFSET(x) offsetof(DnnDetectContext, dnnctx.x)
 #define OFFSET2(x) offsetof(DnnDetectContext, x)
 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
@@ -79,7 +81,7 @@ static const AVOption dnn_detect_options[] = {
     { "cell_w",      "cell width",                 OFFSET2(cell_w),          AV_OPT_TYPE_INT,       { .i64 = 0 },    0, INTMAX_MAX, FLAGS },
     { "cell_h",      "cell height",                OFFSET2(cell_h),          AV_OPT_TYPE_INT,       { .i64 = 0 },    0, INTMAX_MAX, FLAGS },
     { "nb_classes",  "The number of class",        OFFSET2(nb_classes),      AV_OPT_TYPE_INT,       { .i64 = 0 },    0, INTMAX_MAX, FLAGS },
-    { "anchors",     "anchors, splited by '&'",    OFFSET2(anchors_str),         AV_OPT_TYPE_STRING,    { .str = NULL }, 0, 0, FLAGS },
+    { "anchors",     "anchors, splited by '&'",    OFFSET2(anchors),         AV_OPT_TYPE_FLOAT | AV_OPT_TYPE_FLAG_ARRAY,    { .arr = &anchor_array_def }, FLT_MIN, FLT_MAX, FLAGS },
     { NULL }
 };
 
@@ -106,34 +108,6 @@ static int dnn_detect_get_label_id(int nb_classes, int cell_size, float *label_d
     return label_id;
 }
 
-static int dnn_detect_parse_anchors(char *anchors_str, float **anchors)
-{
-    char *saveptr = NULL, *token;
-    float *anchors_buf;
-    int nb_anchor = 0, i = 0;
-    while(anchors_str[i] != '\0') {
-        if(anchors_str[i] == '&')
-            nb_anchor++;
-        i++;
-    }
-    nb_anchor++;
-    anchors_buf = av_mallocz(nb_anchor * sizeof(**anchors));
-    if (!anchors_buf) {
-        return 0;
-    }
-    for (int i = 0; i < nb_anchor; i++) {
-        token = av_strtok(anchors_str, "&", &saveptr);
-        if (!token) {
-            av_freep(&anchors_buf);
-            return 0;
-        }
-        anchors_buf[i] = strtof(token, NULL);
-        anchors_str = NULL;
-    }
-    *anchors = anchors_buf;
-    return nb_anchor;
-}
-
 /* Calculate Intersection Over Union */
 static float dnn_detect_IOU(AVDetectionBBox *bbox1, AVDetectionBBox *bbox2)
 {
@@ -701,15 +675,6 @@ static av_cold int dnn_detect_init(AVFilterContext *context)
         }
     }
 
-    if (ctx->anchors_str) {
-        ret = dnn_detect_parse_anchors(ctx->anchors_str, &ctx->anchors);
-        if (!ctx->anchors) {
-            av_log(context, AV_LOG_ERROR, "failed to parse anchors_str\n");
-            return AVERROR(EINVAL);
-        }
-        ctx->nb_anchor = ret;
-    }
-
     return 0;
 }
 
-- 
2.48.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [FFmpeg-devel] [PATCH 1/2] avfilter/dnn: do not manually parse anchors filter option
  2025-02-26 13:57 [FFmpeg-devel] [PATCH 1/2] avfilter/dnn: do not manually parse anchors filter option Leandro Santiago
@ 2025-02-27  2:38 ` Chen, Wenbin
  0 siblings, 0 replies; 2+ messages in thread
From: Chen, Wenbin @ 2025-02-27  2:38 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

> Instead, rely on AV_OPT_TYPE_FLAG_ARRAY, which will automatically
> perform the parsing.
> 
> Signed-off-by: Leandro Santiago <leandrosansilva@gmail.com>
> ---
>  libavfilter/vf_dnn_detect.c | 43 ++++---------------------------------
>  1 file changed, 4 insertions(+), 39 deletions(-)
> 
> diff --git a/libavfilter/vf_dnn_detect.c b/libavfilter/vf_dnn_detect.c
> index 590e4e256d..cd70c64e98 100644
> --- a/libavfilter/vf_dnn_detect.c
> +++ b/libavfilter/vf_dnn_detect.c
> @@ -31,6 +31,7 @@
>  #include "libavutil/avstring.h"
>  #include "libavutil/detection_bbox.h"
>  #include "libavutil/fifo.h"
> +#include <float.h>
> 
>  typedef enum {
>      DDMT_SSD,
> @@ -53,11 +54,12 @@ typedef struct DnnDetectContext {
>      AVFifo *bboxes_fifo;
>      int scale_width;
>      int scale_height;
> -    char *anchors_str;
>      float *anchors;
>      int nb_anchor;
>  } DnnDetectContext;
> 
> +static const AVOptionArrayDef anchor_array_def = { .sep = '&' };
> +
>  #define OFFSET(x) offsetof(DnnDetectContext, dnnctx.x)
>  #define OFFSET2(x) offsetof(DnnDetectContext, x)
>  #define FLAGS AV_OPT_FLAG_FILTERING_PARAM |
> AV_OPT_FLAG_VIDEO_PARAM
> @@ -79,7 +81,7 @@ static const AVOption dnn_detect_options[] = {
>      { "cell_w",      "cell width",                 OFFSET2(cell_w),
> AV_OPT_TYPE_INT,       { .i64 = 0 },    0, INTMAX_MAX, FLAGS },
>      { "cell_h",      "cell height",                OFFSET2(cell_h),
> AV_OPT_TYPE_INT,       { .i64 = 0 },    0, INTMAX_MAX, FLAGS },
>      { "nb_classes",  "The number of class",        OFFSET2(nb_classes),
> AV_OPT_TYPE_INT,       { .i64 = 0 },    0, INTMAX_MAX, FLAGS },
> -    { "anchors",     "anchors, splited by '&'",    OFFSET2(anchors_str),
> AV_OPT_TYPE_STRING,    { .str = NULL }, 0, 0, FLAGS },
> +    { "anchors",     "anchors, splited by '&'",    OFFSET2(anchors),
> AV_OPT_TYPE_FLOAT | AV_OPT_TYPE_FLAG_ARRAY,    { .arr =
> &anchor_array_def }, FLT_MIN, FLT_MAX, FLAGS },
>      { NULL }
>  };
> 
> @@ -106,34 +108,6 @@ static int dnn_detect_get_label_id(int nb_classes, int
> cell_size, float *label_d
>      return label_id;
>  }
> 
> -static int dnn_detect_parse_anchors(char *anchors_str, float **anchors)
> -{
> -    char *saveptr = NULL, *token;
> -    float *anchors_buf;
> -    int nb_anchor = 0, i = 0;
> -    while(anchors_str[i] != '\0') {
> -        if(anchors_str[i] == '&')
> -            nb_anchor++;
> -        i++;
> -    }
> -    nb_anchor++;
> -    anchors_buf = av_mallocz(nb_anchor * sizeof(**anchors));
> -    if (!anchors_buf) {
> -        return 0;
> -    }
> -    for (int i = 0; i < nb_anchor; i++) {
> -        token = av_strtok(anchors_str, "&", &saveptr);
> -        if (!token) {
> -            av_freep(&anchors_buf);
> -            return 0;
> -        }
> -        anchors_buf[i] = strtof(token, NULL);
> -        anchors_str = NULL;
> -    }
> -    *anchors = anchors_buf;
> -    return nb_anchor;
> -}
> -
>  /* Calculate Intersection Over Union */
>  static float dnn_detect_IOU(AVDetectionBBox *bbox1, AVDetectionBBox
> *bbox2)
>  {
> @@ -701,15 +675,6 @@ static av_cold int dnn_detect_init(AVFilterContext
> *context)
>          }
>      }
> 
> -    if (ctx->anchors_str) {
> -        ret = dnn_detect_parse_anchors(ctx->anchors_str, &ctx->anchors);
> -        if (!ctx->anchors) {
> -            av_log(context, AV_LOG_ERROR, "failed to parse anchors_str\n");
> -            return AVERROR(EINVAL);
> -        }
> -        ctx->nb_anchor = ret;
> -    }
> -
>      return 0;
>  }
> 
> --
> 2.48.1
>

LGTM. Thanks

Wenbin
 
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> 
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-02-27  2:39 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-02-26 13:57 [FFmpeg-devel] [PATCH 1/2] avfilter/dnn: do not manually parse anchors filter option Leandro Santiago
2025-02-27  2:38 ` Chen, Wenbin

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git