[FFmpeg-devel] [PATCH v5 11/21] avdevice: Add internal helpers for querying device capabilities
Diederick Niehorster
dcnieho at gmail.com
Wed Mar 30 15:17:56 EEST 2022
Signed-off-by: Diederick Niehorster <dcnieho at gmail.com>
---
libavdevice/internal.h | 31 +++++++++++++++++++++++++++
libavdevice/utils.c | 48 ++++++++++++++++++++++++++++++++++++++++++
2 files changed, 79 insertions(+)
diff --git a/libavdevice/internal.h b/libavdevice/internal.h
index bef3a4bd2d..eee493a4c7 100644
--- a/libavdevice/internal.h
+++ b/libavdevice/internal.h
@@ -58,4 +58,35 @@ struct AVDeviceCapabilitiesQuery {
*/
extern const AVOption ff_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..de9023f215 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 AVDeviceCapabilitiesQueryType 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
--
2.28.0.windows.1
More information about the ffmpeg-devel
mailing list