Browse code

Move metadata API from lavf to lavu.

Rename it to AVDictionary, since it will be used as such. Tags
documentation and metadata conversion API is lavf-specific, so remains
there.

Anton Khirnov authored on 2011/05/22 19:45:00
Showing 9 changed files
... ...
@@ -13,6 +13,11 @@ libavutil:   2011-04-18
13 13
 
14 14
 API changes, most recent first:
15 15
 
16
+2011-06-xx - xxxxxxx - lavu 51.5.0 - AVMetadata
17
+  Move AVMetadata from lavf to lavu and rename it to
18
+  AVDictionary -- new installed header dict.h.
19
+  All av_metadata_* functions renamed to av_dict_*.
20
+
16 21
 2011-06-07 - a6703fa - lavu 51.4.0 - av_get_bytes_per_sample()
17 22
   Add av_get_bytes_per_sample() in libavutil/samplefmt.h.
18 23
   Deprecate av_get_bits_per_sample_fmt().
... ...
@@ -40,6 +40,7 @@ const char *avformat_license(void);
40 40
 #include <time.h>
41 41
 #include <stdio.h>  /* FILE */
42 42
 #include "libavcodec/avcodec.h"
43
+#include "libavutil/dict.h"
43 44
 
44 45
 #include "avio.h"
45 46
 #include "libavformat/version.h"
... ...
@@ -106,21 +107,24 @@ struct AVFormatContext;
106 106
  * variant_bitrate -- the total bitrate of the bitrate variant that the current stream is part of
107 107
  */
108 108
 
109
-#define AV_METADATA_MATCH_CASE      1
110
-#define AV_METADATA_IGNORE_SUFFIX   2
111
-#define AV_METADATA_DONT_STRDUP_KEY 4
112
-#define AV_METADATA_DONT_STRDUP_VAL 8
113
-#define AV_METADATA_DONT_OVERWRITE 16   ///< Don't overwrite existing tags.
109
+#if FF_API_OLD_METADATA2
110
+/**
111
+ * @defgroup old_metadata Old metadata API
112
+ * The following functions are deprecated, use
113
+ * their equivalents from libavutil/dict.h instead.
114
+ * @{
115
+ */
114 116
 
115
-typedef struct {
116
-    char *key;
117
-    char *value;
118
-}AVMetadataTag;
117
+#define AV_METADATA_MATCH_CASE      AV_DICT_MATCH_CASE
118
+#define AV_METADATA_IGNORE_SUFFIX   AV_DICT_IGNORE_SUFFIX
119
+#define AV_METADATA_DONT_STRDUP_KEY AV_DICT_DONT_STRDUP_KEY
120
+#define AV_METADATA_DONT_STRDUP_VAL AV_DICT_DONT_STRDUP_VAL
121
+#define AV_METADATA_DONT_OVERWRITE  AV_DICT_DONT_OVERWRITE
122
+
123
+typedef attribute_deprecated AVDictionary AVMetadata;
124
+typedef attribute_deprecated AVDictionaryEntry  AVMetadataTag;
119 125
 
120
-typedef struct AVMetadata AVMetadata;
121
-#if FF_API_OLD_METADATA2
122 126
 typedef struct AVMetadataConv AVMetadataConv;
123
-#endif
124 127
 
125 128
 /**
126 129
  * Get a metadata element with matching key.
... ...
@@ -130,8 +134,8 @@ typedef struct AVMetadataConv AVMetadataConv;
130 130
  * @param flags Allows case as well as suffix-insensitive comparisons.
131 131
  * @return Found tag or NULL, changing key or value leads to undefined behavior.
132 132
  */
133
-AVMetadataTag *
134
-av_metadata_get(AVMetadata *m, const char *key, const AVMetadataTag *prev, int flags);
133
+attribute_deprecated AVDictionaryEntry *
134
+av_metadata_get(AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags);
135 135
 
136 136
 /**
137 137
  * Set the given tag in *pm, overwriting an existing tag.
... ...
@@ -143,30 +147,32 @@ av_metadata_get(AVMetadata *m, const char *key, const AVMetadataTag *prev, int f
143 143
  *        Passing a NULL value will cause an existing tag to be deleted.
144 144
  * @return >= 0 on success otherwise an error code <0
145 145
  */
146
-int av_metadata_set2(AVMetadata **pm, const char *key, const char *value, int flags);
146
+attribute_deprecated int av_metadata_set2(AVDictionary **pm, const char *key, const char *value, int flags);
147 147
 
148
-#if FF_API_OLD_METADATA2
149 148
 /**
150 149
  * This function is provided for compatibility reason and currently does nothing.
151 150
  */
152 151
 attribute_deprecated void av_metadata_conv(struct AVFormatContext *ctx, const AVMetadataConv *d_conv,
153 152
                                                                         const AVMetadataConv *s_conv);
154
-#endif
155 153
 
156 154
 /**
157
- * Copy metadata from one AVMetadata struct into another.
158
- * @param dst pointer to a pointer to a AVMetadata struct. If *dst is NULL,
155
+ * Copy metadata from one AVDictionary struct into another.
156
+ * @param dst pointer to a pointer to a AVDictionary struct. If *dst is NULL,
159 157
  *            this function will allocate a struct for you and put it in *dst
160
- * @param src pointer to source AVMetadata struct
158
+ * @param src pointer to source AVDictionary struct
161 159
  * @param flags flags to use when setting metadata in *dst
162 160
  * @note metadata is read using the AV_METADATA_IGNORE_SUFFIX flag
163 161
  */
164
-void av_metadata_copy(AVMetadata **dst, AVMetadata *src, int flags);
162
+attribute_deprecated void av_metadata_copy(AVDictionary **dst, AVDictionary *src, int flags);
165 163
 
166 164
 /**
167
- * Free all the memory allocated for an AVMetadata struct.
165
+ * Free all the memory allocated for an AVDictionary struct.
168 166
  */
169
-void av_metadata_free(AVMetadata **m);
167
+attribute_deprecated void av_metadata_free(AVDictionary **m);
168
+/**
169
+ * @}
170
+ */
171
+#endif
170 172
 
171 173
 
172 174
 /* packet functions */
... ...
@@ -21,107 +21,51 @@
21 21
 #include <strings.h>
22 22
 #include "avformat.h"
23 23
 #include "metadata.h"
24
+#include "libavutil/dict.h"
24 25
 
25
-AVMetadataTag *
26
-av_metadata_get(AVMetadata *m, const char *key, const AVMetadataTag *prev, int flags)
26
+#if FF_API_OLD_METADATA2
27
+AVDictionaryEntry *
28
+av_metadata_get(AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
27 29
 {
28
-    unsigned int i, j;
29
-
30
-    if(!m)
31
-        return NULL;
32
-
33
-    if(prev) i= prev - m->elems + 1;
34
-    else     i= 0;
35
-
36
-    for(; i<m->count; i++){
37
-        const char *s= m->elems[i].key;
38
-        if(flags & AV_METADATA_MATCH_CASE) for(j=0;         s[j]  ==         key[j]  && key[j]; j++);
39
-        else                               for(j=0; toupper(s[j]) == toupper(key[j]) && key[j]; j++);
40
-        if(key[j])
41
-            continue;
42
-        if(s[j] && !(flags & AV_METADATA_IGNORE_SUFFIX))
43
-            continue;
44
-        return &m->elems[i];
45
-    }
46
-    return NULL;
30
+    return av_dict_get(m, key, prev, flags);
47 31
 }
48 32
 
49
-int av_metadata_set2(AVMetadata **pm, const char *key, const char *value, int flags)
33
+int av_metadata_set2(AVDictionary **pm, const char *key, const char *value, int flags)
50 34
 {
51
-    AVMetadata *m= *pm;
52
-    AVMetadataTag *tag= av_metadata_get(m, key, NULL, flags);
53
-
54
-    if(!m)
55
-        m=*pm= av_mallocz(sizeof(*m));
56
-
57
-    if(tag){
58
-        if (flags & AV_METADATA_DONT_OVERWRITE)
59
-            return 0;
60
-        av_free(tag->value);
61
-        av_free(tag->key);
62
-        *tag= m->elems[--m->count];
63
-    }else{
64
-        AVMetadataTag *tmp= av_realloc(m->elems, (m->count+1) * sizeof(*m->elems));
65
-        if(tmp){
66
-            m->elems= tmp;
67
-        }else
68
-            return AVERROR(ENOMEM);
69
-    }
70
-    if(value){
71
-        if(flags & AV_METADATA_DONT_STRDUP_KEY){
72
-            m->elems[m->count].key  = key;
73
-        }else
74
-        m->elems[m->count].key  = av_strdup(key  );
75
-        if(flags & AV_METADATA_DONT_STRDUP_VAL){
76
-            m->elems[m->count].value= value;
77
-        }else
78
-        m->elems[m->count].value= av_strdup(value);
79
-        m->count++;
80
-    }
81
-    if(!m->count) {
82
-        av_free(m->elems);
83
-        av_freep(pm);
84
-    }
85
-
86
-    return 0;
35
+    return av_dict_set(pm, key, value, flags);
87 36
 }
88 37
 
89
-#if FF_API_OLD_METADATA2
90 38
 void av_metadata_conv(AVFormatContext *ctx, const AVMetadataConv *d_conv,
91 39
                                             const AVMetadataConv *s_conv)
92 40
 {
93 41
     return;
94 42
 }
95
-#endif
96 43
 
97
-void av_metadata_free(AVMetadata **pm)
44
+void av_metadata_free(AVDictionary **pm)
98 45
 {
99
-    AVMetadata *m= *pm;
46
+    av_dict_free(pm);
47
+}
100 48
 
101
-    if(m){
102
-        while(m->count--){
103
-            av_free(m->elems[m->count].key);
104
-            av_free(m->elems[m->count].value);
105
-        }
106
-        av_free(m->elems);
107
-    }
108
-    av_freep(pm);
49
+void av_metadata_copy(AVDictionary **dst, AVDictionary *src, int flags)
50
+{
51
+    av_dict_copy(dst, src, flags);
109 52
 }
53
+#endif
110 54
 
111
-void ff_metadata_conv(AVMetadata **pm, const AVMetadataConv *d_conv,
55
+void ff_metadata_conv(AVDictionary **pm, const AVMetadataConv *d_conv,
112 56
                                        const AVMetadataConv *s_conv)
113 57
 {
114 58
     /* TODO: use binary search to look up the two conversion tables
115 59
        if the tables are getting big enough that it would matter speed wise */
116 60
     const AVMetadataConv *sc, *dc;
117
-    AVMetadataTag *mtag = NULL;
118
-    AVMetadata *dst = NULL;
61
+    AVDictionaryEntry *mtag = NULL;
62
+    AVDictionary *dst = NULL;
119 63
     const char *key;
120 64
 
121 65
     if (d_conv == s_conv)
122 66
         return;
123 67
 
124
-    while((mtag=av_metadata_get(*pm, "", mtag, AV_METADATA_IGNORE_SUFFIX))) {
68
+    while ((mtag = av_dict_get(*pm, "", mtag, AV_DICT_IGNORE_SUFFIX))) {
125 69
         key = mtag->key;
126 70
         if (s_conv)
127 71
             for (sc=s_conv; sc->native; sc++)
... ...
@@ -135,9 +79,9 @@ void ff_metadata_conv(AVMetadata **pm, const AVMetadataConv *d_conv,
135 135
                     key = dc->native;
136 136
                     break;
137 137
                 }
138
-        av_metadata_set2(&dst, key, mtag->value, 0);
138
+        av_dict_set(&dst, key, mtag->value, 0);
139 139
     }
140
-    av_metadata_free(pm);
140
+    av_dict_free(pm);
141 141
     *pm = dst;
142 142
 }
143 143
 
... ...
@@ -154,10 +98,3 @@ void ff_metadata_conv_ctx(AVFormatContext *ctx, const AVMetadataConv *d_conv,
154 154
         ff_metadata_conv(&ctx->programs[i]->metadata, d_conv, s_conv);
155 155
 }
156 156
 
157
-void av_metadata_copy(AVMetadata **dst, AVMetadata *src, int flags)
158
-{
159
-    AVMetadataTag *t = NULL;
160
-
161
-    while ((t = av_metadata_get(src, "", t, AV_METADATA_IGNORE_SUFFIX)))
162
-        av_metadata_set2(dst, t->key, t->value, flags);
163
-}
... ...
@@ -29,11 +29,7 @@
29 29
 
30 30
 
31 31
 #include "avformat.h"
32
-
33
-struct AVMetadata{
34
-    int count;
35
-    AVMetadataTag *elems;
36
-};
32
+#include "libavutil/dict.h"
37 33
 
38 34
 struct AVMetadataConv{
39 35
     const char *native;
... ...
@@ -27,6 +27,7 @@ HEADERS = adler32.h                                                     \
27 27
           mathematics.h                                                 \
28 28
           md5.h                                                         \
29 29
           mem.h                                                         \
30
+          dict.h                                                        \
30 31
           opt.h                                                         \
31 32
           parseutils.h                                                  \
32 33
           pixdesc.h                                                     \
... ...
@@ -60,6 +61,7 @@ OBJS = adler32.o                                                        \
60 60
        mathematics.o                                                    \
61 61
        md5.o                                                            \
62 62
        mem.o                                                            \
63
+       dict.o                                                           \
63 64
        opt.o                                                            \
64 65
        parseutils.o                                                     \
65 66
        pixdesc.o                                                        \
... ...
@@ -40,7 +40,7 @@
40 40
 #define AV_VERSION(a, b, c) AV_VERSION_DOT(a, b, c)
41 41
 
42 42
 #define LIBAVUTIL_VERSION_MAJOR 51
43
-#define LIBAVUTIL_VERSION_MINOR  4
43
+#define LIBAVUTIL_VERSION_MINOR  5
44 44
 #define LIBAVUTIL_VERSION_MICRO  0
45 45
 
46 46
 #define LIBAVUTIL_VERSION_INT   AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
47 47
new file mode 100644
... ...
@@ -0,0 +1,110 @@
0
+/*
1
+ * copyright (c) 2009 Michael Niedermayer
2
+ *
3
+ * This file is part of Libav.
4
+ *
5
+ * Libav is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * Libav is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with Libav; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+#include <strings.h>
21
+#include "dict.h"
22
+#include "internal.h"
23
+#include "mem.h"
24
+
25
+AVDictionaryEntry *
26
+av_dict_get(AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
27
+{
28
+    unsigned int i, j;
29
+
30
+    if(!m)
31
+        return NULL;
32
+
33
+    if(prev) i= prev - m->elems + 1;
34
+    else     i= 0;
35
+
36
+    for(; i<m->count; i++){
37
+        const char *s= m->elems[i].key;
38
+        if(flags & AV_DICT_MATCH_CASE) for(j=0;         s[j]  ==         key[j]  && key[j]; j++);
39
+        else                               for(j=0; toupper(s[j]) == toupper(key[j]) && key[j]; j++);
40
+        if(key[j])
41
+            continue;
42
+        if(s[j] && !(flags & AV_DICT_IGNORE_SUFFIX))
43
+            continue;
44
+        return &m->elems[i];
45
+    }
46
+    return NULL;
47
+}
48
+
49
+int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
50
+{
51
+    AVDictionary      *m = *pm;
52
+    AVDictionaryEntry *tag = av_dict_get(m, key, NULL, flags);
53
+
54
+    if(!m)
55
+        m = *pm = av_mallocz(sizeof(*m));
56
+
57
+    if(tag) {
58
+        if (flags & AV_DICT_DONT_OVERWRITE)
59
+            return 0;
60
+        av_free(tag->value);
61
+        av_free(tag->key);
62
+        *tag = m->elems[--m->count];
63
+    } else {
64
+        AVDictionaryEntry *tmp = av_realloc(m->elems, (m->count+1) * sizeof(*m->elems));
65
+        if(tmp) {
66
+            m->elems = tmp;
67
+        } else
68
+            return AVERROR(ENOMEM);
69
+    }
70
+    if (value) {
71
+        if (flags & AV_DICT_DONT_STRDUP_KEY) {
72
+            m->elems[m->count].key  = key;
73
+        } else
74
+        m->elems[m->count].key  = av_strdup(key  );
75
+        if (flags & AV_DICT_DONT_STRDUP_VAL) {
76
+            m->elems[m->count].value = value;
77
+        } else
78
+            m->elems[m->count].value = av_strdup(value);
79
+        m->count++;
80
+    }
81
+    if (!m->count) {
82
+        av_free(m->elems);
83
+        av_freep(pm);
84
+    }
85
+
86
+    return 0;
87
+}
88
+
89
+void av_dict_free(AVDictionary **pm)
90
+{
91
+    AVDictionary *m = *pm;
92
+
93
+    if (m) {
94
+        while(m->count--) {
95
+            av_free(m->elems[m->count].key);
96
+            av_free(m->elems[m->count].value);
97
+        }
98
+        av_free(m->elems);
99
+    }
100
+    av_freep(pm);
101
+}
102
+
103
+void av_dict_copy(AVDictionary **dst, AVDictionary *src, int flags)
104
+{
105
+    AVDictionaryEntry *t = NULL;
106
+
107
+    while ((t = av_dict_get(src, "", t, AV_DICT_IGNORE_SUFFIX)))
108
+        av_dict_set(dst, t->key, t->value, flags);
109
+}
0 110
new file mode 100644
... ...
@@ -0,0 +1,78 @@
0
+/*
1
+ *
2
+ * This file is part of Libav.
3
+ *
4
+ * Libav is free software; you can redistribute it and/or
5
+ * modify it under the terms of the GNU Lesser General Public
6
+ * License as published by the Free Software Foundation; either
7
+ * version 2.1 of the License, or (at your option) any later version.
8
+ *
9
+ * Libav is distributed in the hope that it will be useful,
10
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
+ * Lesser General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU Lesser General Public
15
+ * License along with Libav; if not, write to the Free Software
16
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
+ */
18
+
19
+/**
20
+ * @file Public dictionary API.
21
+ */
22
+
23
+#ifndef AVUTIL_DICT_H
24
+#define AVUTIL_DICT_H
25
+
26
+#define AV_DICT_MATCH_CASE      1
27
+#define AV_DICT_IGNORE_SUFFIX   2
28
+#define AV_DICT_DONT_STRDUP_KEY 4
29
+#define AV_DICT_DONT_STRDUP_VAL 8
30
+#define AV_DICT_DONT_OVERWRITE 16   ///< Don't overwrite existing entries.
31
+
32
+typedef struct {
33
+    char *key;
34
+    char *value;
35
+} AVDictionaryEntry;
36
+
37
+typedef struct AVDictionary AVDictionary;
38
+
39
+/**
40
+ * Get a dictionary entry with matching key.
41
+ *
42
+ * @param prev Set to the previous matching element to find the next.
43
+ *             If set to NULL the first matching element is returned.
44
+ * @param flags Allows case as well as suffix-insensitive comparisons.
45
+ * @return Found entry or NULL, changing key or value leads to undefined behavior.
46
+ */
47
+AVDictionaryEntry *
48
+av_dict_get(AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags);
49
+
50
+/**
51
+ * Set the given entry in *pm, overwriting an existing entry.
52
+ *
53
+ * @param pm pointer to a pointer to a dictionary struct. If *pm is NULL
54
+ * a dictionary struct is allocated and put in *pm.
55
+ * @param key entry key to add to *pm (will be av_strduped depending on flags)
56
+ * @param value entry value to add to *pm (will be av_strduped depending on flags).
57
+ *        Passing a NULL value will cause an existing tag to be deleted.
58
+ * @return >= 0 on success otherwise an error code <0
59
+ */
60
+int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags);
61
+
62
+/**
63
+ * Copy entries from one AVDictionary struct into another.
64
+ * @param dst pointer to a pointer to a AVDictionary struct. If *dst is NULL,
65
+ *            this function will allocate a struct for you and put it in *dst
66
+ * @param src pointer to source AVDictionary struct
67
+ * @param flags flags to use when setting entries in *dst
68
+ * @note metadata is read using the AV_DICT_IGNORE_SUFFIX flag
69
+ */
70
+void av_dict_copy(AVDictionary **dst, AVDictionary *src, int flags);
71
+
72
+/**
73
+ * Free all the memory allocated for an AVDictionary struct.
74
+ */
75
+void av_dict_free(AVDictionary **m);
76
+
77
+#endif // AVUTIL_DICT_H
... ...
@@ -37,6 +37,12 @@
37 37
 #include "config.h"
38 38
 #include "attributes.h"
39 39
 #include "timer.h"
40
+#include "dict.h"
41
+
42
+struct AVDictionary {
43
+    int count;
44
+    AVDictionaryEntry *elems;
45
+};
40 46
 
41 47
 #ifndef attribute_align_arg
42 48
 #if ARCH_X86_32 && AV_GCC_VERSION_AT_LEAST(4,2)