[FFmpeg-devel] [PATCH 26/35] avdevice: Add internal helpers for querying device capabilities

Diederick Niehorster dcnieho at gmail.com
Tue Jun 8 02:04:05 EEST 2021


Signed-off-by: Diederick Niehorster <dcnieho at gmail.com>
---
 libavdevice/internal.h | 31 +++++++++++++++++++++++++++
 libavdevice/utils.c    | 48 ++++++++++++++++++++++++++++++++++++++++++
 libavdevice/version.h  |  2 +-
 3 files changed, 80 insertions(+), 1 deletion(-)

diff --git a/libavdevice/internal.h b/libavdevice/internal.h
index fe4be64ee7..10e1cdb80c 100644
--- a/libavdevice/internal.h
+++ b/libavdevice/internal.h
@@ -58,4 +58,35 @@ typedef struct AVDeviceCapabilitiesQuery {
  */
 extern const AVOption av_device_capabilities[];
 
+/**
+ * Enumeration indicating which device capability is being queried.
+ */
+enum AVDeviceCapabilitiesQueryType {
+    AV_DEV_CAP_QUERY_NONE = 0,
+    // both audio and video
+    AV_DEV_CAP_QUERY_CODEC,
+    // audio
+    AV_DEV_CAP_QUERY_SAMPLE_FORMAT,
+    AV_DEV_CAP_QUERY_SAMPLE_RATE,
+    AV_DEV_CAP_QUERY_CHANNELS,
+    AV_DEV_CAP_QUERY_CHANNEL_LAYOUT,
+    // video
+    AV_DEV_CAP_QUERY_PIXEL_FORMAT,
+    AV_DEV_CAP_QUERY_WINDOW_SIZE,
+    AV_DEV_CAP_QUERY_FRAME_SIZE,
+    AV_DEV_CAP_QUERY_FPS
+};
+
+/**
+ * Find AVDeviceCapabilitiesQueryType enumeration by means of options name.
+ * Returns AV_DEV_CAP_QUERY_NONE if not found.
+ */
+enum AVDeviceCapabilitiesQueryType ff_device_get_query_type(const char* option_name);
+
+/**
+ * Get component name from AVDeviceCapabilitiesQueryType enumeration and component index.
+ * (Some options have multiple components, e.g. AV_DEV_CAP_QUERY_FRAME_SIZE).
+ */
+const char* ff_device_get_query_component_name(enum AVDeviceCapabilitiesQueryType query_type, int component);
+
 #endif
diff --git a/libavdevice/utils.c b/libavdevice/utils.c
index d9a52c53ab..73fdc5766e 100644
--- a/libavdevice/utils.c
+++ b/libavdevice/utils.c
@@ -19,6 +19,7 @@
 #include "internal.h"
 #include "libavutil/opt.h"
 #include "libavformat/avformat.h"
+#include "libavutil/avassert.h"
 
 int ff_alloc_input_device_context(AVFormatContext **avctx, const AVInputFormat *iformat, const char *format)
 {
@@ -57,3 +58,50 @@ int ff_alloc_input_device_context(AVFormatContext **avctx, const AVInputFormat *
     avformat_free_context(s);
     return ret;
 }
+
+typedef struct AVDeviceCapabilitiesQueryTypeEntry {
+    const char* name;
+    enum DshowCapQueryType  query_type;
+} AVDeviceCapabilitiesQueryTypeEntry;
+
+static const AVDeviceCapabilitiesQueryTypeEntry query_table[] = {
+    // both audio and video
+    { "codec",          AV_DEV_CAP_QUERY_CODEC },
+    // audio
+    { "sample_format",  AV_DEV_CAP_QUERY_SAMPLE_FORMAT },
+    { "sample_rate",    AV_DEV_CAP_QUERY_SAMPLE_RATE },
+    { "channels",       AV_DEV_CAP_QUERY_CHANNELS },
+    { "channel_layout", AV_DEV_CAP_QUERY_CHANNEL_LAYOUT },
+    // video
+    { "pixel_format",   AV_DEV_CAP_QUERY_PIXEL_FORMAT },
+    { "frame_size",     AV_DEV_CAP_QUERY_FRAME_SIZE },
+    { "window_size",    AV_DEV_CAP_QUERY_WINDOW_SIZE },
+    { "fps",            AV_DEV_CAP_QUERY_FPS },
+};
+
+enum AVDeviceCapabilitiesQueryType ff_device_get_query_type(const char* option_name)
+{
+    for (int i = 0; i < FF_ARRAY_ELEMS(query_table); ++i) {
+        if (!strcmp(query_table[i].name, option_name))
+            return query_table[i].query_type;
+    }
+    // not found
+    return AV_DEV_CAP_QUERY_NONE;
+}
+
+const char* ff_device_get_query_component_name(enum AVDeviceCapabilitiesQueryType query_type, int component)
+{
+    if (query_type == AV_DEV_CAP_QUERY_WINDOW_SIZE || query_type == AV_DEV_CAP_QUERY_FRAME_SIZE) {
+        // special case: different name for each component
+        return component == 0 ? "pixel_count" : (component == 1 ? "width" : (component == 2 ? "height" : ""));
+    }
+    else {
+        av_assert0(component == 0);
+        for (int i = 0; i < FF_ARRAY_ELEMS(query_table); ++i) {
+            if (query_table[i].query_type == query_type)
+                return query_table[i].name;
+        }
+    }
+    // not found
+    return NULL;
+}
\ No newline at end of file
diff --git a/libavdevice/version.h b/libavdevice/version.h
index 53af6fa0d0..e0361aebc1 100644
--- a/libavdevice/version.h
+++ b/libavdevice/version.h
@@ -29,7 +29,7 @@
 
 #define LIBAVDEVICE_VERSION_MAJOR  59
 #define LIBAVDEVICE_VERSION_MINOR   3
-#define LIBAVDEVICE_VERSION_MICRO 100
+#define LIBAVDEVICE_VERSION_MICRO 101
 
 #define LIBAVDEVICE_VERSION_INT AV_VERSION_INT(LIBAVDEVICE_VERSION_MAJOR, \
                                                LIBAVDEVICE_VERSION_MINOR, \
-- 
2.28.0.windows.1



More information about the ffmpeg-devel mailing list