[FFmpeg-devel] [PATCH 10/23] dnn/dnn_backend_native: Avoid allocation for checking file magic

Guo, Yejun yejun.guo at intel.com
Thu Mar 11 03:46:25 EET 2021



> -----Original Message-----
> From: ffmpeg-devel <ffmpeg-devel-bounces at ffmpeg.org> On Behalf Of
> Andreas Rheinhardt
> Sent: 2021年3月11日 5:55
> To: ffmpeg-devel at ffmpeg.org
> Cc: Andreas Rheinhardt <andreas.rheinhardt at gmail.com>
> Subject: [FFmpeg-devel] [PATCH 10/23] dnn/dnn_backend_native: Avoid
> allocation for checking file magic
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at gmail.com>
> ---
>  libavfilter/dnn/dnn_backend_native.c | 21 +++++----------------
>  1 file changed, 5 insertions(+), 16 deletions(-)
> 
> diff --git a/libavfilter/dnn/dnn_backend_native.c
> b/libavfilter/dnn/dnn_backend_native.c
> index 3bc253c1ad..5336cd765a 100644
> --- a/libavfilter/dnn/dnn_backend_native.c
> +++ b/libavfilter/dnn/dnn_backend_native.c
> @@ -114,10 +114,9 @@ static DNNReturnType get_output_native(void
> *model, const char *input_name, int  // For DEPTH_TO_SPACE layer:
> block_size  DNNModel *ff_dnn_load_model_native(const char
> *model_filename, DNNFunctionType func_type, const char *options,
> AVFilterContext *filter_ctx)  {
> +#define DNN_NATIVE_MAGIC "FFMPEGDNNNATIVE"
>      DNNModel *model = NULL;
> -    char header_expected[] = "FFMPEGDNNNATIVE";
> -    char *buf;
> -    size_t size;
> +    char buf[sizeof(DNN_NATIVE_MAGIC) - 1];
>      int version, header_size, major_version_expected = 1;
>      NativeModel *native_model = NULL;
>      AVIOContext *model_file_context;
> @@ -138,20 +137,10 @@ DNNModel *ff_dnn_load_model_native(const char
> *model_filename, DNNFunctionType f
>      /**
>       * check file header with string and version
>       */
> -    size = sizeof(header_expected);
> -    buf = av_malloc(size);
> -    if (!buf) {
> +    if (avio_read(model_file_context, buf, sizeof(buf)) != sizeof(buf) ||
> +        memcmp(buf, DNN_NATIVE_MAGIC, sizeof(buf)))
>          goto fail;
> -    }
> -
> -    // size - 1 to skip the ending '\0' which is not saved in file

LGTM, and nice to add similar comment for '-1' in
'char buf[sizeof(DNN_NATIVE_MAGIC) - 1];'


More information about the ffmpeg-devel mailing list