diff mbox series

[FFmpeg-devel,3/6] lavf: add public functions converting dispositions to/from strings

Message ID 20211112163255.20522-3-anton@khirnov.net
State Accepted
Commit b731fb5104d06ddbfa6e61ea9451ba77c1c22bce
Headers show
Series [FFmpeg-devel,1/6] lavf: improve AV_DISPOSITION_* doxy
Related show

Checks

Context Check Description
andriy/configurex86 warning Failed to apply patch
andriy/configureppc warning Failed to apply patch

Commit Message

Anton Khirnov Nov. 12, 2021, 4:32 p.m. UTC
---
 doc/APIchanges         |  1 +
 libavformat/avformat.h | 14 +++++++++++++
 libavformat/utils.c    | 47 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 62 insertions(+)
diff mbox series

Patch

diff --git a/doc/APIchanges b/doc/APIchanges
index a86f4b936e..53f229d41a 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -17,6 +17,7 @@  API changes, most recent first:
 2021-11-xx - xxxxxxxxxx - lavf 59.9.100 - avformat.h
   Add av_stream_get_class(). Schedule adding AVStream.av_class at libavformat
   major version 60.
+  Add av_disposition_to_string() and av_disposition_from_string().
 
 2021-11-12 - xxxxxxxxxx - lavu 57.8.100 - hwcontext_vulkan.h
   Added AVFrame.sem_value, AVVulkanDeviceContext.queue_family_encode_index,
diff --git a/libavformat/avformat.h b/libavformat/avformat.h
index 4dabc90f24..ac3e1b6e60 100644
--- a/libavformat/avformat.h
+++ b/libavformat/avformat.h
@@ -894,6 +894,20 @@  typedef struct AVIndexEntry {
  */
 #define AV_DISPOSITION_STILL_IMAGE          (1 << 20)
 
+/**
+ * @return The AV_DISPOSITION_* flag corresponding to disp or a negative error
+ *         code if disp does not correspond to a known stream disposition.
+ */
+int av_disposition_from_string(const char *disp);
+
+/**
+ * @param disposition a combination of AV_DISPOSITION_* values
+ * @return The string description corresponding to the lowest set bit in
+ *         disposition. NULL when the lowest set bit does not correspond
+ *         to a known disposition or when disposition is 0.
+ */
+const char *av_disposition_to_string(int disposition);
+
 /**
  * Options for behavior on timestamp wrap detection.
  */
diff --git a/libavformat/utils.c b/libavformat/utils.c
index d889134a1a..9c38c60e81 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -28,6 +28,7 @@ 
 #include "libavutil/bprint.h"
 #include "libavutil/dict.h"
 #include "libavutil/internal.h"
+#include "libavutil/intmath.h"
 #include "libavutil/opt.h"
 #include "libavutil/parseutils.h"
 #include "libavutil/pixfmt.h"
@@ -1965,3 +1966,49 @@  void ff_format_set_url(AVFormatContext *s, char *url)
     av_freep(&s->url);
     s->url = url;
 }
+
+static const struct {
+    const char *str;
+    int disposition;
+} dispositions[] = {
+    { "default",            AV_DISPOSITION_DEFAULT            },
+    { "dub",                AV_DISPOSITION_DUB                },
+    { "original",           AV_DISPOSITION_ORIGINAL           },
+    { "comment",            AV_DISPOSITION_COMMENT            },
+    { "lyrics",             AV_DISPOSITION_LYRICS             },
+    { "karaoke",            AV_DISPOSITION_KARAOKE            },
+    { "forced",             AV_DISPOSITION_FORCED             },
+    { "hearing_impaired",   AV_DISPOSITION_HEARING_IMPAIRED   },
+    { "visual_impaired",    AV_DISPOSITION_VISUAL_IMPAIRED    },
+    { "clean_effects",      AV_DISPOSITION_CLEAN_EFFECTS      },
+    { "attached_pic",       AV_DISPOSITION_ATTACHED_PIC       },
+    { "timed_thumbnails",   AV_DISPOSITION_TIMED_THUMBNAILS   },
+    { "captions",           AV_DISPOSITION_CAPTIONS           },
+    { "descriptions",       AV_DISPOSITION_DESCRIPTIONS       },
+    { "metadata",           AV_DISPOSITION_METADATA           },
+    { "dependent",          AV_DISPOSITION_DEPENDENT          },
+    { "still_image",        AV_DISPOSITION_STILL_IMAGE        },
+};
+
+int av_disposition_from_string(const char *disp)
+{
+    for (int i = 0; i < FF_ARRAY_ELEMS(dispositions); i++)
+        if (!strcmp(disp, dispositions[i].str))
+            return dispositions[i].disposition;
+    return AVERROR(EINVAL);
+}
+
+const char *av_disposition_to_string(int disposition)
+{
+    int val;
+
+    if (disposition <= 0)
+        return NULL;
+
+    val = 1 << ff_ctz(disposition);
+    for (int i = 0; i < FF_ARRAY_ELEMS(dispositions); i++)
+        if (dispositions[i].disposition == val)
+            return dispositions[i].str;
+
+    return NULL;
+}