diff mbox series

[FFmpeg-devel,v3,1/2] avfilter/dnn/dnn_backend_tf: fix cross library usage

Message ID 1620436656-18917-1-git-send-email-lance.lmwang@gmail.com
State Accepted
Headers show
Series [FFmpeg-devel,v3,1/2] avfilter/dnn/dnn_backend_tf: fix cross library usage | expand

Checks

Context Check Description
andriy/x86_make success Make finished
andriy/x86_make_fate success Make fate finished
andriy/PPC64_make success Make finished
andriy/PPC64_make_fate success Make fate finished

Commit Message

Lance Wang May 8, 2021, 1:17 a.m. UTC
From: Limin Wang <lance.lmwang@gmail.com>

duplicate ff_hex_to_data() function from avformat and rename it to
hex_to_data() as static function.

Signed-off-by: Limin Wang <lance.lmwang@gmail.com>
---
 libavfilter/dnn/dnn_backend_tf.c | 41 +++++++++++++++++++++++++++++++++++++---
 1 file changed, 38 insertions(+), 3 deletions(-)

Comments

Guo, Yejun May 8, 2021, 6:52 a.m. UTC | #1
> -----Original Message-----
> From: ffmpeg-devel <ffmpeg-devel-bounces@ffmpeg.org> On Behalf Of
> lance.lmwang@gmail.com
> Sent: 2021年5月8日 9:18
> To: ffmpeg-devel@ffmpeg.org
> Cc: Limin Wang <lance.lmwang@gmail.com>
> Subject: [FFmpeg-devel] [PATCH v3 1/2] avfilter/dnn/dnn_backend_tf: fix
> cross library usage
> 
> From: Limin Wang <lance.lmwang@gmail.com>
> 
> duplicate ff_hex_to_data() function from avformat and rename it to
> hex_to_data() as static function.
> 
> Signed-off-by: Limin Wang <lance.lmwang@gmail.com>
> ---
>  libavfilter/dnn/dnn_backend_tf.c | 41
> +++++++++++++++++++++++++++++++++++++---
>  1 file changed, 38 insertions(+), 3 deletions(-)
> 
> diff --git a/libavfilter/dnn/dnn_backend_tf.c
> b/libavfilter/dnn/dnn_backend_tf.c
> index 03fe310..5980919 100644
> --- a/libavfilter/dnn/dnn_backend_tf.c
> +++ b/libavfilter/dnn/dnn_backend_tf.c
> @@ -28,8 +28,8 @@
>  #include "dnn_backend_native_layer_conv2d.h"
>  #include "dnn_backend_native_layer_depth2space.h"
>  #include "libavformat/avio.h"
> -#include "libavformat/internal.h"
>  #include "libavutil/avassert.h"
> +#include "libavutil/avstring.h"
>  #include "../internal.h"
>  #include "dnn_backend_native_layer_pad.h"
>  #include "dnn_backend_native_layer_maximum.h"
> @@ -195,6 +195,38 @@ static DNNReturnType get_output_tf(void *model,
> const char *input_name, int inpu
>      return ret;
>  }
> 
> +#define SPACE_CHARS " \t\r\n"
> +static int hex_to_data(uint8_t *data, int data_size, const char *p)
> +{
> +    int c, len, v;
> +
> +    len = 0;
> +    v   = 1;
> +    for (;;) {
> +        p += strspn(p, SPACE_CHARS);
> +        if (*p == '\0')
> +            break;
> +        c = av_toupper((unsigned char) *p++);
> +        if (c >= '0' && c <= '9')
> +            c = c - '0';
> +        else if (c >= 'A' && c <= 'F')
> +            c = c - 'A' + 10;
> +        else
> +            break;
> +        v = (v << 4) | c;
> +        if (v & 0x100) {
> +            if (data) {
> +                if (len >= data_size)
> +                    return AVERROR(ERANGE);
> +                data[len] = v;
> +            }
> +            len++;
> +            v = 1;
> +        }
> +    }
> +    return len;
> +}
> +
>  static DNNReturnType load_tf_model(TFModel *tf_model, const char
> *model_filename)
>  {
>      TFContext *ctx = &tf_model->ctx;
> @@ -219,14 +251,17 @@ static DNNReturnType load_tf_model(TFModel
> *tf_model, const char *model_filename
>              return DNN_ERROR;
>          }
>          config = tf_model->ctx.options.sess_config + 2;
> -        sess_config_length = ff_hex_to_data(NULL, config);
> +        sess_config_length = hex_to_data(NULL, 0, config);
> 
>          sess_config = av_mallocz(sess_config_length +
> AV_INPUT_BUFFER_PADDING_SIZE);
>          if (!sess_config) {
>              av_log(ctx, AV_LOG_ERROR, "failed to allocate memory\n");
>              return DNN_ERROR;
>          }
> -        ff_hex_to_data(sess_config, config);
> +        if (hex_to_data(sess_config, sess_config_length, config) < 0) {
> +            av_log(ctx, AV_LOG_ERROR, "failed to convert hex to data\n");
> +            return DNN_ERROR;
> +        }
>      }
> 
>      graph_def = read_graph(model_filename);
> --
LGTM, thanks.
diff mbox series

Patch

diff --git a/libavfilter/dnn/dnn_backend_tf.c b/libavfilter/dnn/dnn_backend_tf.c
index 03fe310..5980919 100644
--- a/libavfilter/dnn/dnn_backend_tf.c
+++ b/libavfilter/dnn/dnn_backend_tf.c
@@ -28,8 +28,8 @@ 
 #include "dnn_backend_native_layer_conv2d.h"
 #include "dnn_backend_native_layer_depth2space.h"
 #include "libavformat/avio.h"
-#include "libavformat/internal.h"
 #include "libavutil/avassert.h"
+#include "libavutil/avstring.h"
 #include "../internal.h"
 #include "dnn_backend_native_layer_pad.h"
 #include "dnn_backend_native_layer_maximum.h"
@@ -195,6 +195,38 @@  static DNNReturnType get_output_tf(void *model, const char *input_name, int inpu
     return ret;
 }
 
+#define SPACE_CHARS " \t\r\n"
+static int hex_to_data(uint8_t *data, int data_size, const char *p)
+{
+    int c, len, v;
+
+    len = 0;
+    v   = 1;
+    for (;;) {
+        p += strspn(p, SPACE_CHARS);
+        if (*p == '\0')
+            break;
+        c = av_toupper((unsigned char) *p++);
+        if (c >= '0' && c <= '9')
+            c = c - '0';
+        else if (c >= 'A' && c <= 'F')
+            c = c - 'A' + 10;
+        else
+            break;
+        v = (v << 4) | c;
+        if (v & 0x100) {
+            if (data) {
+                if (len >= data_size)
+                    return AVERROR(ERANGE);
+                data[len] = v;
+            }
+            len++;
+            v = 1;
+        }
+    }
+    return len;
+}
+
 static DNNReturnType load_tf_model(TFModel *tf_model, const char *model_filename)
 {
     TFContext *ctx = &tf_model->ctx;
@@ -219,14 +251,17 @@  static DNNReturnType load_tf_model(TFModel *tf_model, const char *model_filename
             return DNN_ERROR;
         }
         config = tf_model->ctx.options.sess_config + 2;
-        sess_config_length = ff_hex_to_data(NULL, config);
+        sess_config_length = hex_to_data(NULL, 0, config);
 
         sess_config = av_mallocz(sess_config_length + AV_INPUT_BUFFER_PADDING_SIZE);
         if (!sess_config) {
             av_log(ctx, AV_LOG_ERROR, "failed to allocate memory\n");
             return DNN_ERROR;
         }
-        ff_hex_to_data(sess_config, config);
+        if (hex_to_data(sess_config, sess_config_length, config) < 0) {
+            av_log(ctx, AV_LOG_ERROR, "failed to convert hex to data\n");
+            return DNN_ERROR;
+        }
     }
 
     graph_def = read_graph(model_filename);