From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id 547F0409CA for ; Fri, 4 Mar 2022 15:12:54 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 7763368B194; Fri, 4 Mar 2022 17:12:42 +0200 (EET) Received: from haasn.dev (haasn.dev [78.46.187.166]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 7AB8468AB3B for ; Fri, 4 Mar 2022 17:12:34 +0200 (EET) Received: from haasn.dev (unknown [10.30.0.2]) by haasn.dev (Postfix) with ESMTP id 6E7E14360B; Fri, 4 Mar 2022 16:03:14 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=haasn.xyz; s=mail; t=1646406194; bh=bxibmkO/6JUsR8qAsG1/DKdeN6OQ2OfnSAb9vU3kHew=; h=From:To:Cc:Subject:Date:From; b=C2fPdrm/z3pqaR0zdlCRkxRT2k10pCyOkc6r7uAx3pqajm5uJjTn1YmcZ3yl5RAUM xTdtCbm0kvRNTeGoN/rMl2qXhJ4FS2s5BGH30LQs8aZwwrGhwV6SGHcpdL3Nqwk4gp NvS0Sv7GVgSF+Qdd3rFlCCB4zZ5ieUVF26v+8QJo= From: Niklas Haas To: ffmpeg-devel@ffmpeg.org Date: Fri, 4 Mar 2022 16:03:06 +0100 Message-Id: <20220304150307.61769-1-ffmpeg@haasn.xyz> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/2] lavu: add syntax for loading AV_OPT_TYPE_BINARY from files X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Niklas Haas Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: From: Niklas Haas I arbitrarily decided to use the syntax 'opt=@filename' to match e.g. `curl -Ffield=@filename`, and also because @ is not a valid hex character, nor does it conflict with any other common shell or ffmpeg syntax. This is arguably somewhat clunky because it does not round-trip with av_opt_get - you will get back a hex interpretation of the loaded file, rather than the filename it was loaded from. It also implies a (perhaps unnecessary) memcpy from mapped file memory into a allocated memory. This is unfortunately necessary because there's no way for us to know whether av_free or av_file_unmap is needed to clean up previous option values. The motivating use case was the introduction of several new binary options for vf_libplacebo, but other filters that currently rely on manual file-path loading could benefit from it as well. --- doc/APIchanges | 4 ++++ libavutil/opt.c | 53 ++++++++++++++++++++++++++++++++++++++++++++- libavutil/opt.h | 5 +++++ libavutil/version.h | 2 +- 4 files changed, 62 insertions(+), 2 deletions(-) diff --git a/doc/APIchanges b/doc/APIchanges index ea402f6118..0400b10721 100644 --- a/doc/APIchanges +++ b/doc/APIchanges @@ -14,6 +14,10 @@ libavutil: 2021-04-27 API changes, most recent first: +2022-03-04 - xxxxxxxxxx - lavu 57.23.100 - opt.h + Add av_opt_set_filepath() to set a binary option from a file. + Support '@filename' syntax in av_opt_set() for binary options. + 2022-02-07 - xxxxxxxxxx - lavu 57.21.100 - fifo.h Deprecate AVFifoBuffer and the API around it, namely av_fifo_alloc(), av_fifo_alloc_array(), av_fifo_free(), av_fifo_freep(), av_fifo_reset(), diff --git a/libavutil/opt.c b/libavutil/opt.c index d951edca9d..151536d229 100644 --- a/libavutil/opt.c +++ b/libavutil/opt.c @@ -32,6 +32,7 @@ #include "common.h" #include "dict.h" #include "eval.h" +#include "file.h" #include "log.h" #include "parseutils.h" #include "pixdesc.h" @@ -465,6 +466,33 @@ static int set_string_dict(void *obj, const AVOption *o, const char *val, uint8_ return 0; } +static int set_string_filepath(void *obj, const AVOption *o, const char *path, uint8_t **dst) +{ + int *lendst = (int *)(dst + 1); + uint8_t *bin; + size_t bin_len; + int err; + + av_freep(dst); + *lendst = 0; + + if (!path || !path[0]) + return 0; + + err = av_file_map(path, &bin, &bin_len, 0, obj); + if (err) + return err; + + *dst = av_memdup(bin, bin_len); + av_file_unmap(bin, bin_len); + + if (!*dst) + return AVERROR(ENOMEM); + + *lendst = bin_len; + return 0; +} + int av_opt_set(void *obj, const char *name, const char *val, int search_flags) { int ret = 0; @@ -492,7 +520,11 @@ int av_opt_set(void *obj, const char *name, const char *val, int search_flags) case AV_OPT_TYPE_STRING: return set_string(obj, o, val, dst); case AV_OPT_TYPE_BINARY: - return set_string_binary(obj, o, val, dst); + if (val && val[0] == '@') { + return set_string_filepath(obj, o, &val[1], dst); + } else { + return set_string_binary(obj, o, val, dst); + } case AV_OPT_TYPE_FLAGS: case AV_OPT_TYPE_INT: case AV_OPT_TYPE_INT64: @@ -631,6 +663,25 @@ int av_opt_set_bin(void *obj, const char *name, const uint8_t *val, int len, int return 0; } +int av_opt_set_filepath(void *obj, const char *name, const char *path, int search_flags) +{ + uint8_t *bin; + size_t bin_len; + int err; + + if (!path || !path[0]) + return av_opt_set_bin(obj, name, NULL, 0, search_flags); + + err = av_file_map(path, &bin, &bin_len, 0, obj); + if (err) + return err; + + err = av_opt_set_bin(obj, name, bin, bin_len, search_flags); + av_file_unmap(bin, bin_len); + + return err; +} + int av_opt_set_image_size(void *obj, const char *name, int w, int h, int search_flags) { void *target_obj; diff --git a/libavutil/opt.h b/libavutil/opt.h index 2820435eec..97d7c69482 100644 --- a/libavutil/opt.h +++ b/libavutil/opt.h @@ -699,6 +699,11 @@ int av_opt_set_channel_layout(void *obj, const char *name, int64_t ch_layout, in * caller still owns val is and responsible for freeing it. */ int av_opt_set_dict_val(void *obj, const char *name, const AVDictionary *val, int search_flags); +/** + * @note This behaves like av_opt_set_bin() but loads the binary data from a + * file. May also return errors related to file I/O. + */ +int av_opt_set_filepath(void *obj, const char *name, const char *val, int search_flags); /** * Set a binary option to an integer list. diff --git a/libavutil/version.h b/libavutil/version.h index c7004601c8..6c5d90507c 100644 --- a/libavutil/version.h +++ b/libavutil/version.h @@ -79,7 +79,7 @@ */ #define LIBAVUTIL_VERSION_MAJOR 57 -#define LIBAVUTIL_VERSION_MINOR 22 +#define LIBAVUTIL_VERSION_MINOR 23 #define LIBAVUTIL_VERSION_MICRO 100 #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \ -- 2.35.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".