diff mbox series

[FFmpeg-devel,1/3] lavu/dict: add AV_DICT_DEDUP

Message ID 20220824235200.22312-1-rcombs@rcombs.me
State New
Headers show
Series [FFmpeg-devel,1/3] lavu/dict: add AV_DICT_DEDUP | expand

Commit Message

rcombs Aug. 24, 2022, 11:51 p.m. UTC
This is useful when multiple metadata inputs may set the same value
(e.g. both a container-specific header and an ID3 tag).
---
 libavutil/dict.c | 11 +++++++++++
 libavutil/dict.h |  1 +
 2 files changed, 12 insertions(+)
diff mbox series

Patch

diff --git a/libavutil/dict.c b/libavutil/dict.c
index 9d3d96c58b..d8b38cc71f 100644
--- a/libavutil/dict.c
+++ b/libavutil/dict.c
@@ -76,6 +76,17 @@  int av_dict_set(AVDictionary **pm, const char *key, const char *value,
 
     if (!(flags & AV_DICT_MULTIKEY)) {
         tag = av_dict_get(m, key, NULL, flags);
+    } else if (flags & AV_DICT_DEDUP) {
+        while ((tag = av_dict_get(m, key, NULL, flags))) {
+            if ((!value && !tag->value) ||
+                (value && tag->value && !strcmp(value, tag->value))) {
+                if (flags & AV_DICT_DONT_STRDUP_KEY)
+                    av_free(key);
+                if (flags & AV_DICT_DONT_STRDUP_VAL)
+                    av_free(value);
+                return 0;
+            }
+        }
     }
     if (flags & AV_DICT_DONT_STRDUP_KEY)
         copy_key = (void *)key;
diff --git a/libavutil/dict.h b/libavutil/dict.h
index 0d1afc6c64..a957b48bf3 100644
--- a/libavutil/dict.h
+++ b/libavutil/dict.h
@@ -75,6 +75,7 @@ 
 #define AV_DICT_APPEND         32   /**< If the entry already exists, append to it.  Note that no
                                       delimiter is added, the strings are simply concatenated. */
 #define AV_DICT_MULTIKEY       64   /**< Allow to store several equal keys in the dictionary */
+#define AV_DICT_DEDUP         128   /**< If inserting a value that already exists for a key, do nothing. Only relevant with AV_DICT_MULTIKEY. */
 
 typedef struct AVDictionaryEntry {
     char *key;