[FFmpeg-devel] [PATCH 1/4] avformat/aviobuf: Move code specific to URLContexts to avio.c

Andreas Rheinhardt andreas.rheinhardt at outlook.com
Sat Mar 2 21:50:54 EET 2024


This separates the URL-layer adjacent parts of the code
from the parts that are also usable with custom IO.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>
---
 libavformat/avio.c    | 191 +++++++++++++++++++++++++++++++++++++++++-
 libavformat/aviobuf.c | 186 ----------------------------------------
 libavformat/url.h     |   2 -
 3 files changed, 189 insertions(+), 190 deletions(-)

diff --git a/libavformat/avio.c b/libavformat/avio.c
index b793a7546c..794ebd4bd8 100644
--- a/libavformat/avio.c
+++ b/libavformat/avio.c
@@ -24,6 +24,7 @@
 #include "libavutil/opt.h"
 #include "libavutil/time.h"
 #include "libavutil/avassert.h"
+#include "avio_internal.h"
 #include "os_support.h"
 #include "internal.h"
 #if CONFIG_NETWORK
@@ -31,6 +32,8 @@
 #endif
 #include "url.h"
 
+#define IO_BUFFER_SIZE 32768
+
 /** @name Logging context. */
 /*@{*/
 static const char *urlcontext_to_name(void *ptr)
@@ -60,7 +63,7 @@ static const AVOption options[] = {
     { NULL }
 };
 
-const AVClass ffurl_context_class = {
+static const AVClass url_context_class = {
     .class_name       = "URLContext",
     .item_name        = urlcontext_to_name,
     .option           = options,
@@ -70,6 +73,47 @@ const AVClass ffurl_context_class = {
 };
 /*@}*/
 
+static void *avio_child_next(void *obj, void *prev)
+{
+    AVIOContext *s = obj;
+    return prev ? NULL : s->opaque;
+}
+
+static const AVClass *child_class_iterate(void **iter)
+{
+    const AVClass *c = *iter ? NULL : &url_context_class;
+    *iter = (void*)(uintptr_t)c;
+    return c;
+}
+
+#define AVIOOFFSET(x) offsetof(AVIOContext,x)
+#define E AV_OPT_FLAG_ENCODING_PARAM
+#define D AV_OPT_FLAG_DECODING_PARAM
+static const AVOption avio_options[] = {
+    {"protocol_whitelist", "List of protocols that are allowed to be used", AVIOOFFSET(protocol_whitelist), AV_OPT_TYPE_STRING, { .str = NULL },  0, 0, D },
+    { NULL },
+};
+
+const AVClass ff_avio_class = {
+    .class_name = "AVIOContext",
+    .item_name  = av_default_item_name,
+    .version    = LIBAVUTIL_VERSION_INT,
+    .option     = avio_options,
+    .child_next = avio_child_next,
+    .child_class_iterate = child_class_iterate,
+};
+
+URLContext *ffio_geturlcontext(AVIOContext *s)
+{
+    if (!s)
+        return NULL;
+
+    if (s->opaque && s->read_packet == ffurl_read2)
+        return s->opaque;
+    else
+        return NULL;
+}
+
 static int url_alloc_for_protocol(URLContext **puc, const URLProtocol *up,
                                   const char *filename, int flags,
                                   const AVIOInterruptCB *int_cb)
@@ -96,7 +140,7 @@ static int url_alloc_for_protocol(URLContext **puc, const URLProtocol *up,
         err = AVERROR(ENOMEM);
         goto fail;
     }
-    uc->av_class = &ffurl_context_class;
+    uc->av_class = &url_context_class;
     uc->filename = (char *)&uc[1];
     strcpy(uc->filename, filename);
     uc->prot            = up;
@@ -225,6 +269,17 @@ int ffurl_accept(URLContext *s, URLContext **c)
     return AVERROR(EBADF);
 }
 
+int avio_accept(AVIOContext *s, AVIOContext **c)
+{
+    int ret;
+    URLContext *sc = s->opaque;
+    URLContext *cc = NULL;
+    ret = ffurl_accept(sc, &cc);
+    if (ret < 0)
+        return ret;
+    return ffio_fdopen(c, cc);
+}
+
 int ffurl_handshake(URLContext *c)
 {
     int ret;
@@ -237,6 +292,12 @@ int ffurl_handshake(URLContext *c)
     return 0;
 }
 
+int avio_handshake(AVIOContext *c)
+{
+    URLContext *cc = c->opaque;
+    return ffurl_handshake(cc);
+}
+
 #define URL_SCHEME_CHARS                        \
     "abcdefghijklmnopqrstuvwxyz"                \
     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"                \
@@ -347,6 +408,92 @@ fail:
     return ret;
 }
 
+int ffio_fdopen(AVIOContext **s, URLContext *h)
+{
+    uint8_t *buffer = NULL;
+    int buffer_size, max_packet_size;
+
+    max_packet_size = h->max_packet_size;
+    if (max_packet_size) {
+        buffer_size = max_packet_size; /* no need to bufferize more than one packet */
+    } else {
+        buffer_size = IO_BUFFER_SIZE;
+    }
+    if (!(h->flags & AVIO_FLAG_WRITE) && h->is_streamed) {
+        if (buffer_size > INT_MAX/2)
+            return AVERROR(EINVAL);
+        buffer_size *= 2;
+    }
+    buffer = av_malloc(buffer_size);
+    if (!buffer)
+        return AVERROR(ENOMEM);
+
+    *s = avio_alloc_context(buffer, buffer_size, h->flags & AVIO_FLAG_WRITE, h,
+                            ffurl_read2, ffurl_write2, ffurl_seek2);
+    if (!*s) {
+        av_freep(&buffer);
+        return AVERROR(ENOMEM);
+    }
+    (*s)->protocol_whitelist = av_strdup(h->protocol_whitelist);
+    if (!(*s)->protocol_whitelist && h->protocol_whitelist) {
+        avio_closep(s);
+        return AVERROR(ENOMEM);
+    }
+    (*s)->protocol_blacklist = av_strdup(h->protocol_blacklist);
+    if (!(*s)->protocol_blacklist && h->protocol_blacklist) {
+        avio_closep(s);
+        return AVERROR(ENOMEM);
+    }
+    (*s)->direct = h->flags & AVIO_FLAG_DIRECT;
+
+    (*s)->seekable = h->is_streamed ? 0 : AVIO_SEEKABLE_NORMAL;
+    (*s)->max_packet_size = max_packet_size;
+    (*s)->min_packet_size = h->min_packet_size;
+    if(h->prot) {
+        (*s)->read_pause = (int (*)(void *, int))h->prot->url_read_pause;
+        (*s)->read_seek  =
+            (int64_t (*)(void *, int, int64_t, int))h->prot->url_read_seek;
+
+        if (h->prot->url_read_seek)
+            (*s)->seekable |= AVIO_SEEKABLE_TIME;
+    }
+    ((FFIOContext*)(*s))->short_seek_get = ffurl_get_short_seek;
+    (*s)->av_class = &ff_avio_class;
+    return 0;
+}
+
+int ffio_open_whitelist(AVIOContext **s, const char *filename, int flags,
+                        const AVIOInterruptCB *int_cb, AVDictionary **options,
+                        const char *whitelist, const char *blacklist)
+{
+    URLContext *h;
+    int err;
+
+    *s = NULL;
+
+    err = ffurl_open_whitelist(&h, filename, flags, int_cb, options, whitelist, blacklist, NULL);
+    if (err < 0)
+        return err;
+    err = ffio_fdopen(s, h);
+    if (err < 0) {
+        ffurl_close(h);
+        return err;
+    }
+    return 0;
+}
+
+int avio_open2(AVIOContext **s, const char *filename, int flags,
+               const AVIOInterruptCB *int_cb, AVDictionary **options)
+{
+    return ffio_open_whitelist(s, filename, flags, int_cb, options, NULL, NULL);
+}
+
+int avio_open(AVIOContext **s, const char *filename, int flags)
+{
+    return avio_open2(s, filename, flags, NULL, NULL);
+}
+
+
 static inline int retry_transfer_wrapper(URLContext *h, uint8_t *buf,
                                          const uint8_t *cbuf,
                                          int size, int size_min,
@@ -464,6 +611,46 @@ int ffurl_close(URLContext *h)
     return ffurl_closep(&h);
 }
 
+int avio_close(AVIOContext *s)
+{
+    FFIOContext *const ctx = ffiocontext(s);
+    URLContext *h;
+    int ret, error;
+
+    if (!s)
+        return 0;
+
+    avio_flush(s);
+    h         = s->opaque;
+    s->opaque = NULL;
+
+    av_freep(&s->buffer);
+    if (s->write_flag)
+        av_log(s, AV_LOG_VERBOSE,
+               "Statistics: %"PRId64" bytes written, %d seeks, %d writeouts\n",
+               ctx->bytes_written, ctx->seek_count, ctx->writeout_count);
+    else
+        av_log(s, AV_LOG_VERBOSE, "Statistics: %"PRId64" bytes read, %d seeks\n",
+               ctx->bytes_read, ctx->seek_count);
+    av_opt_free(s);
+
+    error = s->error;
+    avio_context_free(&s);
+
+    ret = ffurl_close(h);
+    if (ret < 0)
+        return ret;
+
+    return error;
+}
+
+int avio_closep(AVIOContext **s)
+{
+    int ret = avio_close(*s);
+    *s = NULL;
+    return ret;
+}
+
 
 const char *avio_find_protocol_name(const char *url)
 {
diff --git a/libavformat/aviobuf.c b/libavformat/aviobuf.c
index d80b8527bb..8dfed22622 100644
--- a/libavformat/aviobuf.c
+++ b/libavformat/aviobuf.c
@@ -22,7 +22,6 @@
 #include "libavutil/bprint.h"
 #include "libavutil/crc.h"
 #include "libavutil/dict.h"
-#include "libavutil/internal.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/log.h"
 #include "libavutil/opt.h"
@@ -31,7 +30,6 @@
 #include "avio.h"
 #include "avio_internal.h"
 #include "internal.h"
-#include "url.h"
 #include <stdarg.h>
 
 #define IO_BUFFER_SIZE 32768
@@ -43,36 +41,6 @@
  */
 #define SHORT_SEEK_THRESHOLD 32768
 
-static void *ff_avio_child_next(void *obj, void *prev)
-{
-    AVIOContext *s = obj;
-    return prev ? NULL : s->opaque;
-}
-
-static const AVClass *child_class_iterate(void **iter)
-{
-    const AVClass *c = *iter ? NULL : &ffurl_context_class;
-    *iter = (void*)(uintptr_t)c;
-    return c;
-}
-
-#define OFFSET(x) offsetof(AVIOContext,x)
-#define E AV_OPT_FLAG_ENCODING_PARAM
-#define D AV_OPT_FLAG_DECODING_PARAM
-static const AVOption ff_avio_options[] = {
-    {"protocol_whitelist", "List of protocols that are allowed to be used", OFFSET(protocol_whitelist), AV_OPT_TYPE_STRING, { .str = NULL },  0, 0, D },
-    { NULL },
-};
-
-const AVClass ff_avio_class = {
-    .class_name = "AVIOContext",
-    .item_name  = av_default_item_name,
-    .version    = LIBAVUTIL_VERSION_INT,
-    .option     = ff_avio_options,
-    .child_next = ff_avio_child_next,
-    .child_class_iterate = child_class_iterate,
-};
-
 static void fill_buffer(AVIOContext *s);
 static int url_resetbuf(AVIOContext *s, int flags);
 /** @warning must be called before any I/O */
@@ -1035,71 +1003,6 @@ void ffio_write_lines(AVIOContext *s, const unsigned char *buf, int size,
     }
 }
 
-int ffio_fdopen(AVIOContext **s, URLContext *h)
-{
-    uint8_t *buffer = NULL;
-    int buffer_size, max_packet_size;
-
-    max_packet_size = h->max_packet_size;
-    if (max_packet_size) {
-        buffer_size = max_packet_size; /* no need to bufferize more than one packet */
-    } else {
-        buffer_size = IO_BUFFER_SIZE;
-    }
-    if (!(h->flags & AVIO_FLAG_WRITE) && h->is_streamed) {
-        if (buffer_size > INT_MAX/2)
-            return AVERROR(EINVAL);
-        buffer_size *= 2;
-    }
-    buffer = av_malloc(buffer_size);
-    if (!buffer)
-        return AVERROR(ENOMEM);
-
-    *s = avio_alloc_context(buffer, buffer_size, h->flags & AVIO_FLAG_WRITE, h,
-                            ffurl_read2, ffurl_write2, ffurl_seek2);
-    if (!*s) {
-        av_freep(&buffer);
-        return AVERROR(ENOMEM);
-    }
-    (*s)->protocol_whitelist = av_strdup(h->protocol_whitelist);
-    if (!(*s)->protocol_whitelist && h->protocol_whitelist) {
-        avio_closep(s);
-        return AVERROR(ENOMEM);
-    }
-    (*s)->protocol_blacklist = av_strdup(h->protocol_blacklist);
-    if (!(*s)->protocol_blacklist && h->protocol_blacklist) {
-        avio_closep(s);
-        return AVERROR(ENOMEM);
-    }
-    (*s)->direct = h->flags & AVIO_FLAG_DIRECT;
-
-    (*s)->seekable = h->is_streamed ? 0 : AVIO_SEEKABLE_NORMAL;
-    (*s)->max_packet_size = max_packet_size;
-    (*s)->min_packet_size = h->min_packet_size;
-    if(h->prot) {
-        (*s)->read_pause = (int (*)(void *, int))h->prot->url_read_pause;
-        (*s)->read_seek  =
-            (int64_t (*)(void *, int, int64_t, int))h->prot->url_read_seek;
-
-        if (h->prot->url_read_seek)
-            (*s)->seekable |= AVIO_SEEKABLE_TIME;
-    }
-    ((FFIOContext*)(*s))->short_seek_get = ffurl_get_short_seek;
-    (*s)->av_class = &ff_avio_class;
-    return 0;
-}
-
-URLContext* ffio_geturlcontext(AVIOContext *s)
-{
-    if (!s)
-        return NULL;
-
-    if (s->opaque && s->read_packet == ffurl_read2)
-        return s->opaque;
-    else
-        return NULL;
-}
-
 int ffio_copy_url_options(AVIOContext* pb, AVDictionary** avio_opts)
 {
     const char *opts[] = {
@@ -1300,78 +1203,6 @@ int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char **bufp, int buf_si
     return 0;
 }
 
-int avio_open(AVIOContext **s, const char *filename, int flags)
-{
-    return avio_open2(s, filename, flags, NULL, NULL);
-}
-
-int ffio_open_whitelist(AVIOContext **s, const char *filename, int flags,
-                         const AVIOInterruptCB *int_cb, AVDictionary **options,
-                         const char *whitelist, const char *blacklist
-                        )
-{
-    URLContext *h;
-    int err;
-
-    *s = NULL;
-
-    err = ffurl_open_whitelist(&h, filename, flags, int_cb, options, whitelist, blacklist, NULL);
-    if (err < 0)
-        return err;
-    err = ffio_fdopen(s, h);
-    if (err < 0) {
-        ffurl_close(h);
-        return err;
-    }
-    return 0;
-}
-
-int avio_open2(AVIOContext **s, const char *filename, int flags,
-               const AVIOInterruptCB *int_cb, AVDictionary **options)
-{
-    return ffio_open_whitelist(s, filename, flags, int_cb, options, NULL, NULL);
-}
-
-int avio_close(AVIOContext *s)
-{
-    FFIOContext *const ctx = ffiocontext(s);
-    URLContext *h;
-    int ret, error;
-
-    if (!s)
-        return 0;
-
-    avio_flush(s);
-    h         = s->opaque;
-    s->opaque = NULL;
-
-    av_freep(&s->buffer);
-    if (s->write_flag)
-        av_log(s, AV_LOG_VERBOSE,
-               "Statistics: %"PRId64" bytes written, %d seeks, %d writeouts\n",
-               ctx->bytes_written, ctx->seek_count, ctx->writeout_count);
-    else
-        av_log(s, AV_LOG_VERBOSE, "Statistics: %"PRId64" bytes read, %d seeks\n",
-               ctx->bytes_read, ctx->seek_count);
-    av_opt_free(s);
-
-    error = s->error;
-    avio_context_free(&s);
-
-    ret = ffurl_close(h);
-    if (ret < 0)
-        return ret;
-
-    return error;
-}
-
-int avio_closep(AVIOContext **s)
-{
-    int ret = avio_close(*s);
-    *s = NULL;
-    return ret;
-}
-
 int avio_vprintf(AVIOContext *s, const char *fmt, va_list ap)
 {
     AVBPrint bp;
@@ -1450,23 +1281,6 @@ int avio_read_to_bprint(AVIOContext *h, AVBPrint *pb, size_t max_size)
     return 0;
 }
 
-int avio_accept(AVIOContext *s, AVIOContext **c)
-{
-    int ret;
-    URLContext *sc = s->opaque;
-    URLContext *cc = NULL;
-    ret = ffurl_accept(sc, &cc);
-    if (ret < 0)
-        return ret;
-    return ffio_fdopen(c, cc);
-}
-
-int avio_handshake(AVIOContext *c)
-{
-    URLContext *cc = c->opaque;
-    return ffurl_handshake(cc);
-}
-
 /* output in a dynamic buffer */
 
 typedef struct DynBuffer {
diff --git a/libavformat/url.h b/libavformat/url.h
index 59d9f1b870..4f3bfb6d57 100644
--- a/libavformat/url.h
+++ b/libavformat/url.h
@@ -32,8 +32,6 @@
 #define URL_PROTOCOL_FLAG_NESTED_SCHEME 1 /*< The protocol name can be the first part of a nested protocol scheme */
 #define URL_PROTOCOL_FLAG_NETWORK       2 /*< The protocol uses network */
 
-extern const AVClass ffurl_context_class;
-
 typedef struct URLContext {
     const AVClass *av_class;    /**< information for av_log(). Set by url_open(). */
     const struct URLProtocol *prot;
-- 
2.40.1



More information about the ffmpeg-devel mailing list