diff mbox series

[FFmpeg-devel,7/8] lavf/options: add av_disposition_write()

Message ID 20230428095508.221826-7-george@nsup.org
State New
Headers show
Series [FFmpeg-devel,1/8] lavu: add macros to help making future-proof structures | expand

Commit Message

Nicolas George April 28, 2023, 9:55 a.m. UTC
TODO APIchanges entry

Signed-off-by: Nicolas George <george@nsup.org>
---
 libavformat/avformat.h | 14 ++++++++++++++
 libavformat/options.c  | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+)


This and the next patch show the kind of fruits become low-hanging
thanks to the platform of AVWriter.

Eventually, I want that every public structure and enumeration to have a
_write() function with a standardized API, and later we will be able to
use a custom %something format string in printf directly.

Another project of mine (that has AVWriter as a prerequisite) would
invert the logic of this function: instead of having the to-string and
from-string function explore the options table, we would have the
options system quety the to-string and from-string functions.
diff mbox series

Patch

diff --git a/libavformat/avformat.h b/libavformat/avformat.h
index 5302a34c0d..51fd78b71c 100644
--- a/libavformat/avformat.h
+++ b/libavformat/avformat.h
@@ -824,6 +824,20 @@  int av_disposition_from_string(const char *disp);
  */
 const char *av_disposition_to_string(int disposition);
 
+/**
+ * Write a disposition as a string.
+ * The before and after string are written around each element of the
+ * disposition and the between string is written between them, if they are
+ * not NULL.
+ * Unknown disposition bits are written as an extra hexadecimal value.
+ * No flags are defined.
+ */
+void av_disposition_write(AVWriter wr, unsigned disposition,
+                          const char *before,
+                          const char *after,
+                          const char *between,
+                          unsigned flags);
+
 /**
  * Options for behavior on timestamp wrap detection.
  */
diff --git a/libavformat/options.c b/libavformat/options.c
index e4a3aceed0..a813e11060 100644
--- a/libavformat/options.c
+++ b/libavformat/options.c
@@ -29,6 +29,7 @@ 
 #include "libavutil/internal.h"
 #include "libavutil/intmath.h"
 #include "libavutil/opt.h"
+#include "libavutil/writer.h"
 
 /**
  * @file
@@ -348,3 +349,36 @@  const char *av_disposition_to_string(int disposition)
 
     return NULL;
 }
+
+void av_disposition_write(AVWriter wr, unsigned disposition,
+                          const char *before,
+                          const char *after,
+                          const char *between,
+                          unsigned flags)
+{
+    int sep = 0;
+
+    for (const AVOption *opt = stream_options; opt->name; opt++) {
+        if (option_is_disposition(opt) && (disposition & opt->default_val.i64)) {
+            if (sep && between)
+                av_writer_print(wr, between);
+            if (before)
+                av_writer_print(wr, before);
+            av_writer_print(wr, opt->name);
+            if (after)
+                av_writer_print(wr, after);
+            disposition &= ~opt->default_val.i64;
+            sep = 1;
+        }
+    }
+    if (disposition) {
+        if (sep && between)
+            av_writer_print(wr, between);
+        if (before)
+            av_writer_print(wr, before);
+        av_writer_printf(wr, "%x?", disposition);
+        if (after)
+            av_writer_print(wr, after);
+        sep = 1;
+    }
+}