]> git.sesse.net Git - ffmpeg/blob - libavutil/dict.c
vc2enc: don't require interlacing for 1080p50/60 base video formats
[ffmpeg] / libavutil / dict.c
1 /*
2  * copyright (c) 2009 Michael Niedermayer
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include <string.h>
22
23 #include "avstring.h"
24 #include "dict.h"
25 #include "internal.h"
26 #include "mem.h"
27 #include "bprint.h"
28
29 struct AVDictionary {
30     int count;
31     AVDictionaryEntry *elems;
32 };
33
34 int av_dict_count(const AVDictionary *m)
35 {
36     return m ? m->count : 0;
37 }
38
39 AVDictionaryEntry *av_dict_get(const AVDictionary *m, const char *key,
40                                const AVDictionaryEntry *prev, int flags)
41 {
42     unsigned int i, j;
43
44     if (!m)
45         return NULL;
46
47     if (prev)
48         i = prev - m->elems + 1;
49     else
50         i = 0;
51
52     for (; i < m->count; i++) {
53         const char *s = m->elems[i].key;
54         if (flags & AV_DICT_MATCH_CASE)
55             for (j = 0; s[j] == key[j] && key[j]; j++)
56                 ;
57         else
58             for (j = 0; av_toupper(s[j]) == av_toupper(key[j]) && key[j]; j++)
59                 ;
60         if (key[j])
61             continue;
62         if (s[j] && !(flags & AV_DICT_IGNORE_SUFFIX))
63             continue;
64         return &m->elems[i];
65     }
66     return NULL;
67 }
68
69 int av_dict_set(AVDictionary **pm, const char *key, const char *value,
70                 int flags)
71 {
72     AVDictionary *m = *pm;
73     AVDictionaryEntry *tag = NULL;
74     char *oldval = NULL, *copy_key = NULL, *copy_value = NULL;
75
76     if (!(flags & AV_DICT_MULTIKEY)) {
77         tag = av_dict_get(m, key, NULL, flags);
78     }
79     if (flags & AV_DICT_DONT_STRDUP_KEY)
80         copy_key = (void *)key;
81     else
82         copy_key = av_strdup(key);
83     if (flags & AV_DICT_DONT_STRDUP_VAL)
84         copy_value = (void *)value;
85     else if (copy_key)
86         copy_value = av_strdup(value);
87     if (!m)
88         m = *pm = av_mallocz(sizeof(*m));
89     if (!m || (key && !copy_key) || (value && !copy_value))
90         goto err_out;
91
92     if (tag) {
93         if (flags & AV_DICT_DONT_OVERWRITE) {
94             av_free(copy_key);
95             av_free(copy_value);
96             return 0;
97         }
98         if (flags & AV_DICT_APPEND)
99             oldval = tag->value;
100         else
101             av_free(tag->value);
102         av_free(tag->key);
103         *tag = m->elems[--m->count];
104     } else if (copy_value) {
105         AVDictionaryEntry *tmp = av_realloc(m->elems,
106                                             (m->count + 1) * sizeof(*m->elems));
107         if (!tmp)
108             goto err_out;
109         m->elems = tmp;
110     }
111     if (copy_value) {
112         m->elems[m->count].key = copy_key;
113         m->elems[m->count].value = copy_value;
114         if (oldval && flags & AV_DICT_APPEND) {
115             size_t len = strlen(oldval) + strlen(copy_value) + 1;
116             char *newval = av_mallocz(len);
117             if (!newval)
118                 goto err_out;
119             av_strlcat(newval, oldval, len);
120             av_freep(&oldval);
121             av_strlcat(newval, copy_value, len);
122             m->elems[m->count].value = newval;
123             av_freep(&copy_value);
124         }
125         m->count++;
126     } else {
127         av_freep(&copy_key);
128     }
129     if (!m->count) {
130         av_freep(&m->elems);
131         av_freep(pm);
132     }
133
134     return 0;
135
136 err_out:
137     if (m && !m->count) {
138         av_freep(&m->elems);
139         av_freep(pm);
140     }
141     av_free(copy_key);
142     av_free(copy_value);
143     return AVERROR(ENOMEM);
144 }
145
146 int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value,
147                 int flags)
148 {
149     char valuestr[22];
150     snprintf(valuestr, sizeof(valuestr), "%"PRId64, value);
151     flags &= ~AV_DICT_DONT_STRDUP_VAL;
152     return av_dict_set(pm, key, valuestr, flags);
153 }
154
155 static int parse_key_value_pair(AVDictionary **pm, const char **buf,
156                                 const char *key_val_sep, const char *pairs_sep,
157                                 int flags)
158 {
159     char *key = av_get_token(buf, key_val_sep);
160     char *val = NULL;
161     int ret;
162
163     if (key && *key && strspn(*buf, key_val_sep)) {
164         (*buf)++;
165         val = av_get_token(buf, pairs_sep);
166     }
167
168     if (key && *key && val && *val)
169         ret = av_dict_set(pm, key, val, flags);
170     else
171         ret = AVERROR(EINVAL);
172
173     av_freep(&key);
174     av_freep(&val);
175
176     return ret;
177 }
178
179 int av_dict_parse_string(AVDictionary **pm, const char *str,
180                          const char *key_val_sep, const char *pairs_sep,
181                          int flags)
182 {
183     int ret;
184
185     if (!str)
186         return 0;
187
188     /* ignore STRDUP flags */
189     flags &= ~(AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
190
191     while (*str) {
192         if ((ret = parse_key_value_pair(pm, &str, key_val_sep, pairs_sep, flags)) < 0)
193             return ret;
194
195         if (*str)
196             str++;
197     }
198
199     return 0;
200 }
201
202 void av_dict_free(AVDictionary **pm)
203 {
204     AVDictionary *m = *pm;
205
206     if (m) {
207         while (m->count--) {
208             av_freep(&m->elems[m->count].key);
209             av_freep(&m->elems[m->count].value);
210         }
211         av_freep(&m->elems);
212     }
213     av_freep(pm);
214 }
215
216 int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
217 {
218     AVDictionaryEntry *t = NULL;
219
220     while ((t = av_dict_get(src, "", t, AV_DICT_IGNORE_SUFFIX))) {
221         int ret = av_dict_set(dst, t->key, t->value, flags);
222         if (ret < 0)
223             return ret;
224     }
225
226     return 0;
227 }
228
229 int av_dict_get_string(const AVDictionary *m, char **buffer,
230                        const char key_val_sep, const char pairs_sep)
231 {
232     AVDictionaryEntry *t = NULL;
233     AVBPrint bprint;
234     int cnt = 0;
235     char special_chars[] = {pairs_sep, key_val_sep, '\0'};
236
237     if (!buffer || pairs_sep == '\0' || key_val_sep == '\0' || pairs_sep == key_val_sep ||
238         pairs_sep == '\\' || key_val_sep == '\\')
239         return AVERROR(EINVAL);
240
241     if (!av_dict_count(m)) {
242         *buffer = av_strdup("");
243         return *buffer ? 0 : AVERROR(ENOMEM);
244     }
245
246     av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED);
247     while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX))) {
248         if (cnt++)
249             av_bprint_append_data(&bprint, &pairs_sep, 1);
250         av_bprint_escape(&bprint, t->key, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
251         av_bprint_append_data(&bprint, &key_val_sep, 1);
252         av_bprint_escape(&bprint, t->value, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
253     }
254     return av_bprint_finalize(&bprint, buffer);
255 }
256
257 #ifdef TEST
258 static void print_dict(const AVDictionary *m)
259 {
260     AVDictionaryEntry *t = NULL;
261     while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX)))
262         printf("%s %s   ", t->key, t->value);
263     printf("\n");
264 }
265
266 static void test_separators(const AVDictionary *m, const char pair, const char val)
267 {
268     AVDictionary *dict = NULL;
269     char pairs[] = {pair , '\0'};
270     char vals[]  = {val, '\0'};
271
272     char *buffer = NULL;
273     av_dict_copy(&dict, m, 0);
274     print_dict(dict);
275     av_dict_get_string(dict, &buffer, val, pair);
276     printf("%s\n", buffer);
277     av_dict_free(&dict);
278     av_dict_parse_string(&dict, buffer, vals, pairs, 0);
279     av_freep(&buffer);
280     print_dict(dict);
281     av_dict_free(&dict);
282 }
283
284 int main(void)
285 {
286     AVDictionary *dict = NULL;
287     AVDictionaryEntry *e;
288     char *buffer = NULL;
289
290     printf("Testing av_dict_get_string() and av_dict_parse_string()\n");
291     av_dict_get_string(dict, &buffer, '=', ',');
292     printf("%s\n", buffer);
293     av_freep(&buffer);
294     av_dict_set(&dict, "aaa", "aaa", 0);
295     av_dict_set(&dict, "b,b", "bbb", 0);
296     av_dict_set(&dict, "c=c", "ccc", 0);
297     av_dict_set(&dict, "ddd", "d,d", 0);
298     av_dict_set(&dict, "eee", "e=e", 0);
299     av_dict_set(&dict, "f,f", "f=f", 0);
300     av_dict_set(&dict, "g=g", "g,g", 0);
301     test_separators(dict, ',', '=');
302     av_dict_free(&dict);
303     av_dict_set(&dict, "aaa", "aaa", 0);
304     av_dict_set(&dict, "bbb", "bbb", 0);
305     av_dict_set(&dict, "ccc", "ccc", 0);
306     av_dict_set(&dict, "\\,=\'\"", "\\,=\'\"", 0);
307     test_separators(dict, '"',  '=');
308     test_separators(dict, '\'', '=');
309     test_separators(dict, ',', '"');
310     test_separators(dict, ',', '\'');
311     test_separators(dict, '\'', '"');
312     test_separators(dict, '"', '\'');
313     av_dict_free(&dict);
314
315     printf("\nTesting av_dict_set()\n");
316     av_dict_set(&dict, "a", "a", 0);
317     av_dict_set(&dict, "b", av_strdup("b"), AV_DICT_DONT_STRDUP_VAL);
318     av_dict_set(&dict, av_strdup("c"), "c", AV_DICT_DONT_STRDUP_KEY);
319     av_dict_set(&dict, av_strdup("d"), av_strdup("d"), AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
320     av_dict_set(&dict, "e", "e", AV_DICT_DONT_OVERWRITE);
321     av_dict_set(&dict, "e", "f", AV_DICT_DONT_OVERWRITE);
322     av_dict_set(&dict, "f", "f", 0);
323     av_dict_set(&dict, "f", NULL, 0);
324     av_dict_set(&dict, "ff", "f", 0);
325     av_dict_set(&dict, "ff", "f", AV_DICT_APPEND);
326     e = NULL;
327     while ((e = av_dict_get(dict, "", e, AV_DICT_IGNORE_SUFFIX)))
328         printf("%s %s\n", e->key, e->value);
329     av_dict_free(&dict);
330
331     av_dict_set(&dict, NULL, "a", 0);
332     av_dict_set(&dict, NULL, "b", 0);
333     av_dict_get(dict, NULL, NULL, 0);
334     e = NULL;
335     while ((e = av_dict_get(dict, "", e, AV_DICT_IGNORE_SUFFIX)))
336         printf("'%s' '%s'\n", e->key, e->value);
337     av_dict_free(&dict);
338
339
340     //valgrind sensible test
341     printf("\nTesting av_dict_set_int()\n");
342     av_dict_set_int(&dict, "1", 1, AV_DICT_DONT_STRDUP_VAL);
343     av_dict_set_int(&dict, av_strdup("2"), 2, AV_DICT_DONT_STRDUP_KEY);
344     av_dict_set_int(&dict, av_strdup("3"), 3, AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
345     av_dict_set_int(&dict, "4", 4, 0);
346     av_dict_set_int(&dict, "5", 5, AV_DICT_DONT_OVERWRITE);
347     av_dict_set_int(&dict, "5", 6, AV_DICT_DONT_OVERWRITE);
348     av_dict_set_int(&dict, "12", 1, 0);
349     av_dict_set_int(&dict, "12", 2, AV_DICT_APPEND);
350     e = NULL;
351     while ((e = av_dict_get(dict, "", e, AV_DICT_IGNORE_SUFFIX)))
352         printf("%s %s\n", e->key, e->value);
353     av_dict_free(&dict);
354
355     //valgrind sensible test
356     printf("\nTesting av_dict_set() with existing AVDictionaryEntry.key as key\n");
357     av_dict_set(&dict, "key", "old", 0);
358     e = av_dict_get(dict, "key", NULL, 0);
359     av_dict_set(&dict, e->key, "new val OK", 0);
360     e = av_dict_get(dict, "key", NULL, 0);
361     printf("%s\n", e->value);
362     av_dict_set(&dict, e->key, e->value, 0);
363     e = av_dict_get(dict, "key", NULL, 0);
364     printf("%s\n", e->value);
365     av_dict_free(&dict);
366
367     return 0;
368 }
369 #endif