[FFmpeg-devel] [PATCH v1 2/2] libavformat/protocols.c: avio_enum_protocols(): Refactor

Michael Witten mfwitten at gmail.com
Fri Aug 20 20:45:02 EEST 2021


Date: Wed, 11 Aug 2021 19:00:00 -0000
This commit is the result of squashing a series of very tiny
transformations; it refactors 'avio_enum_protocols()' into
2 functions:

  * avio_enum_protocols_for_input()
  * avio_enum_protocols_for_output()

Those functions are in turn mostly implemented by this macro:

  * AVIO_ENUM_PROTOCOLS()

The goals of this refactoring were the following:

  * To make the code more immediately understandable.
  * To reduce the potential for redundant computation.
---
 libavformat/avio.h      |  2 +-
 libavformat/protocols.c | 38 +++++++++++++++++++++++++++-----------
 2 files changed, 28 insertions(+), 12 deletions(-)

diff --git a/libavformat/avio.h b/libavformat/avio.h
index 0b35409787..3b92cf742a 100644
--- a/libavformat/avio.h
+++ b/libavformat/avio.h
@@ -786,7 +786,7 @@ int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer);
  *
  * @return A static string containing the name of current protocol or NULL
  */
-const char *avio_enum_protocols(void **opaque, int output);
+const char *avio_enum_protocols(void **const opaque, const int output);
 
 /**
  * Get AVClass by names of available protocols.
diff --git a/libavformat/protocols.c b/libavformat/protocols.c
index e0b3405ab8..4cb8ae0b63 100644
--- a/libavformat/protocols.c
+++ b/libavformat/protocols.c
@@ -91,19 +91,35 @@ const AVClass *ff_urlcontext_child_class_iterate(void **iter)
     return ret;
 }
 
-const char *avio_enum_protocols(void **opaque, int output)
+#define AVIO_ENUM_PROTOCOLS(METHOD) \
+    typedef const URLProtocol *const *Iterator; \
+    for (Iterator p = *opaque ? (Iterator)(*opaque) + 1 : url_protocols; *p; ++p) { \
+        if ((*p)->METHOD) { \
+            *opaque = (void *)p; \
+            return (*p)->name; \
+        } \
+    } \
+    *opaque = NULL; \
+    return NULL;
+
+static inline
+const char *avio_enum_protocols_for_output(void **const opaque)
 {
-    const URLProtocol *const *p = *opaque;
+    AVIO_ENUM_PROTOCOLS(url_write);
+}
 
-    p = p ? p + 1 : url_protocols;
-    *opaque = (void *)p;
-    if (!*p) {
-        *opaque = NULL;
-        return NULL;
-    }
-    if ((output && (*p)->url_write) || (!output && (*p)->url_read))
-        return (*p)->name;
-    return avio_enum_protocols(opaque, output);
+static inline
+const char *avio_enum_protocols_for_input(void **const opaque)
+{
+    AVIO_ENUM_PROTOCOLS(url_read);
+}
+
+const char *avio_enum_protocols(void **const opaque, const int output)
+{
+    if (output)
+        return avio_enum_protocols_for_output(opaque);
+    else
+        return avio_enum_protocols_for_input(opaque);
 }
 
 const AVClass *avio_protocol_get_class(const char *name)
-- 
2.22.0



More information about the ffmpeg-devel mailing list