Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: James Almer <jamrial@gmail.com>
To: ffmpeg-devel@ffmpeg.org
Subject: [FFmpeg-devel] [PATCH 2/4] fftools/cmdutils: add OPT_TYPE_FILE
Date: Sun,  7 Jan 2024 18:30:58 -0300
Message-ID: <20240107213100.16205-2-jamrial@gmail.com> (raw)
In-Reply-To: <20240107213100.16205-1-jamrial@gmail.com>

Same as OPT_TYPE_STRING, except it takes a file argument whose contents
are read as a string.

Signed-off-by: James Almer <jamrial@gmail.com>
---
 fftools/cmdutils.c   | 34 ++++++++++++++++++++++++++++++++++
 fftools/cmdutils.h   |  3 +++
 fftools/ffmpeg.h     |  1 -
 fftools/ffmpeg_opt.c | 32 +++-----------------------------
 4 files changed, 40 insertions(+), 30 deletions(-)

diff --git a/fftools/cmdutils.c b/fftools/cmdutils.c
index 44228ea637..40623a9ace 100644
--- a/fftools/cmdutils.c
+++ b/fftools/cmdutils.c
@@ -37,6 +37,7 @@
 #include "libswresample/swresample.h"
 #include "libavutil/avassert.h"
 #include "libavutil/avstring.h"
+#include "libavutil/bprint.h"
 #include "libavutil/channel_layout.h"
 #include "libavutil/display.h"
 #include "libavutil/getenv_utf8.h"
@@ -266,6 +267,13 @@ static int write_option(void *optctx, const OptionDef *po, const char *opt,
         if (!str)
             return AVERROR(ENOMEM);
         *(char **)dst = str;
+    } else if (po->type == OPT_TYPE_FILE) {
+        char *str;
+        str = file_read(arg);
+        av_freep(dst);
+        if (!str)
+            return AVERROR(ENOMEM);
+        *(char **)dst = str;
     } else if (po->type == OPT_TYPE_BOOL || po->type == OPT_TYPE_INT) {
         ret = parse_number(opt, arg, OPT_TYPE_INT64, INT_MIN, INT_MAX, &num);
         if (ret < 0)
@@ -1088,3 +1096,29 @@ double get_rotation(const int32_t *displaymatrix)
 
     return theta;
 }
+
+/* read file contents into a string */
+char *file_read(const char *filename)
+{
+    AVIOContext *pb      = NULL;
+    int ret = avio_open(&pb, filename, AVIO_FLAG_READ);
+    AVBPrint bprint;
+    char *str;
+
+    if (ret < 0) {
+        av_log(NULL, AV_LOG_ERROR, "Error opening file %s.\n", filename);
+        return NULL;
+    }
+
+    av_bprint_init(&bprint, 0, AV_BPRINT_SIZE_UNLIMITED);
+    ret = avio_read_to_bprint(pb, &bprint, SIZE_MAX);
+    avio_closep(&pb);
+    if (ret < 0) {
+        av_bprint_finalize(&bprint, NULL);
+        return NULL;
+    }
+    ret = av_bprint_finalize(&bprint, &str);
+    if (ret < 0)
+        return NULL;
+    return str;
+}
diff --git a/fftools/cmdutils.h b/fftools/cmdutils.h
index 53227abb47..016ee325b9 100644
--- a/fftools/cmdutils.h
+++ b/fftools/cmdutils.h
@@ -81,6 +81,7 @@ enum OptionType {
     OPT_TYPE_FUNC,
     OPT_TYPE_BOOL,
     OPT_TYPE_STRING,
+    OPT_TYPE_FILE,
     OPT_TYPE_INT,
     OPT_TYPE_INT64,
     OPT_TYPE_FLOAT,
@@ -470,4 +471,6 @@ void *allocate_array_elem(void *array, size_t elem_size, int *nb_elems);
 
 double get_rotation(const int32_t *displaymatrix);
 
+char *file_read(const char *filename);
+
 #endif /* FFTOOLS_CMDUTILS_H */
diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index 6137ac991e..cdde3c2c03 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -652,7 +652,6 @@ void remove_avoptions(AVDictionary **a, AVDictionary *b);
 int check_avoptions(AVDictionary *m);
 
 int assert_file_overwrite(const char *filename);
-char *file_read(const char *filename);
 AVDictionary *strip_specifiers(const AVDictionary *dict);
 int find_codec(void *logctx, const char *name,
                enum AVMediaType type, int encoder, const AVCodec **codec);
diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
index c189cf373b..7ae1b55cf0 100644
--- a/fftools/ffmpeg_opt.c
+++ b/fftools/ffmpeg_opt.c
@@ -43,7 +43,6 @@
 #include "libavutil/avassert.h"
 #include "libavutil/avstring.h"
 #include "libavutil/avutil.h"
-#include "libavutil/bprint.h"
 #include "libavutil/channel_layout.h"
 #include "libavutil/display.h"
 #include "libavutil/intreadwrite.h"
@@ -108,12 +107,13 @@ static void uninit_options(OptionsContext *o)
             SpecifierOptList *so = dst;
             for (int i = 0; i < so->nb_opt; i++) {
                 av_freep(&so->opt[i].specifier);
-                if (po->type == OPT_TYPE_STRING)
+                if (po->type == OPT_TYPE_STRING || po->type == OPT_TYPE_FILE)
                     av_freep(&so->opt[i].u.str);
             }
             av_freep(&so->opt);
             so->nb_opt = 0;
-        } else if (po->flags & OPT_FLAG_OFFSET && po->type == OPT_TYPE_STRING)
+        } else if (po->flags & OPT_FLAG_OFFSET && (   po->type == OPT_TYPE_STRING
+                                                   || po->type == OPT_TYPE_FILE))
             av_freep(dst);
         po++;
     }
@@ -759,32 +759,6 @@ int assert_file_overwrite(const char *filename)
     return 0;
 }
 
-/* read file contents into a string */
-char *file_read(const char *filename)
-{
-    AVIOContext *pb      = NULL;
-    int ret = avio_open(&pb, filename, AVIO_FLAG_READ);
-    AVBPrint bprint;
-    char *str;
-
-    if (ret < 0) {
-        av_log(NULL, AV_LOG_ERROR, "Error opening file %s.\n", filename);
-        return NULL;
-    }
-
-    av_bprint_init(&bprint, 0, AV_BPRINT_SIZE_UNLIMITED);
-    ret = avio_read_to_bprint(pb, &bprint, SIZE_MAX);
-    avio_closep(&pb);
-    if (ret < 0) {
-        av_bprint_finalize(&bprint, NULL);
-        return NULL;
-    }
-    ret = av_bprint_finalize(&bprint, &str);
-    if (ret < 0)
-        return NULL;
-    return str;
-}
-
 /* arg format is "output-stream-index:streamid-value". */
 static int opt_streamid(void *optctx, const char *opt, const char *arg)
 {
-- 
2.43.0

_______________________________________________
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".

  reply	other threads:[~2024-01-07 21:31 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-07 21:30 [FFmpeg-devel] [PATCH 1/4] avformat/iamfenc: don't write empty packets James Almer
2024-01-07 21:30 ` James Almer [this message]
2024-01-07 21:30 ` [FFmpeg-devel] [PATCH 3/4] fftools/ffmpeg_opt: add a stream_group_script option James Almer
2024-01-08  1:43   ` Steven Liu
2024-01-08  1:44     ` James Almer
2024-01-07 21:31 ` [FFmpeg-devel] [PATCH 4/4] fate: add raw IAMF tests James Almer
2024-01-10  3:05   ` Michael Niedermayer
2024-01-10 12:52     ` James Almer
2024-01-11  3:17       ` Michael Niedermayer
2024-01-11 12:02         ` James Almer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240107213100.16205-2-jamrial@gmail.com \
    --to=jamrial@gmail.com \
    --cc=ffmpeg-devel@ffmpeg.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

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