diff options
author | Anton Khirnov <anton@khirnov.net> | 2011-05-22 14:45:00 +0400 |
---|---|---|
committer | Anton Khirnov <anton@khirnov.net> | 2011-06-08 09:43:45 +0400 |
commit | d9f80ea2a7325f9c84307568843512811a99baff (patch) | |
tree | 4cfb0b73956c53a83b6bebded7345eaa3f69d181 /libavutil | |
parent | ac4a8548110bc180cb67bea6eaf8b8e1081370cf (diff) |
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.
Diffstat (limited to 'libavutil')
-rw-r--r-- | libavutil/Makefile | 2 | ||||
-rw-r--r-- | libavutil/avutil.h | 2 | ||||
-rw-r--r-- | libavutil/dict.c | 110 | ||||
-rw-r--r-- | libavutil/dict.h | 78 | ||||
-rw-r--r-- | libavutil/internal.h | 6 |
5 files changed, 197 insertions, 1 deletions
diff --git a/libavutil/Makefile b/libavutil/Makefile index 01231bd52d..0d8f01dd8d 100644 --- a/libavutil/Makefile +++ b/libavutil/Makefile @@ -27,6 +27,7 @@ HEADERS = adler32.h \ mathematics.h \ md5.h \ mem.h \ + dict.h \ opt.h \ parseutils.h \ pixdesc.h \ @@ -60,6 +61,7 @@ OBJS = adler32.o \ mathematics.o \ md5.o \ mem.o \ + dict.o \ opt.o \ parseutils.o \ pixdesc.o \ diff --git a/libavutil/avutil.h b/libavutil/avutil.h index 8536790109..9c660f3a99 100644 --- a/libavutil/avutil.h +++ b/libavutil/avutil.h @@ -40,7 +40,7 @@ #define AV_VERSION(a, b, c) AV_VERSION_DOT(a, b, c) #define LIBAVUTIL_VERSION_MAJOR 51 -#define LIBAVUTIL_VERSION_MINOR 4 +#define LIBAVUTIL_VERSION_MINOR 5 #define LIBAVUTIL_VERSION_MICRO 0 #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \ diff --git a/libavutil/dict.c b/libavutil/dict.c new file mode 100644 index 0000000000..56f1513d32 --- /dev/null +++ b/libavutil/dict.c @@ -0,0 +1,110 @@ +/* + * copyright (c) 2009 Michael Niedermayer + * + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <strings.h> +#include "dict.h" +#include "internal.h" +#include "mem.h" + +AVDictionaryEntry * +av_dict_get(AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags) +{ + unsigned int i, j; + + if(!m) + return NULL; + + if(prev) i= prev - m->elems + 1; + else i= 0; + + for(; i<m->count; i++){ + const char *s= m->elems[i].key; + if(flags & AV_DICT_MATCH_CASE) for(j=0; s[j] == key[j] && key[j]; j++); + else for(j=0; toupper(s[j]) == toupper(key[j]) && key[j]; j++); + if(key[j]) + continue; + if(s[j] && !(flags & AV_DICT_IGNORE_SUFFIX)) + continue; + return &m->elems[i]; + } + return NULL; +} + +int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags) +{ + AVDictionary *m = *pm; + AVDictionaryEntry *tag = av_dict_get(m, key, NULL, flags); + + if(!m) + m = *pm = av_mallocz(sizeof(*m)); + + if(tag) { + if (flags & AV_DICT_DONT_OVERWRITE) + return 0; + av_free(tag->value); + av_free(tag->key); + *tag = m->elems[--m->count]; + } else { + AVDictionaryEntry *tmp = av_realloc(m->elems, (m->count+1) * sizeof(*m->elems)); + if(tmp) { + m->elems = tmp; + } else + return AVERROR(ENOMEM); + } + if (value) { + if (flags & AV_DICT_DONT_STRDUP_KEY) { + m->elems[m->count].key = key; + } else + m->elems[m->count].key = av_strdup(key ); + if (flags & AV_DICT_DONT_STRDUP_VAL) { + m->elems[m->count].value = value; + } else + m->elems[m->count].value = av_strdup(value); + m->count++; + } + if (!m->count) { + av_free(m->elems); + av_freep(pm); + } + + return 0; +} + +void av_dict_free(AVDictionary **pm) +{ + AVDictionary *m = *pm; + + if (m) { + while(m->count--) { + av_free(m->elems[m->count].key); + av_free(m->elems[m->count].value); + } + av_free(m->elems); + } + av_freep(pm); +} + +void av_dict_copy(AVDictionary **dst, AVDictionary *src, int flags) +{ + AVDictionaryEntry *t = NULL; + + while ((t = av_dict_get(src, "", t, AV_DICT_IGNORE_SUFFIX))) + av_dict_set(dst, t->key, t->value, flags); +} diff --git a/libavutil/dict.h b/libavutil/dict.h new file mode 100644 index 0000000000..bfd7f2682c --- /dev/null +++ b/libavutil/dict.h @@ -0,0 +1,78 @@ +/* + * + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file Public dictionary API. + */ + +#ifndef AVUTIL_DICT_H +#define AVUTIL_DICT_H + +#define AV_DICT_MATCH_CASE 1 +#define AV_DICT_IGNORE_SUFFIX 2 +#define AV_DICT_DONT_STRDUP_KEY 4 +#define AV_DICT_DONT_STRDUP_VAL 8 +#define AV_DICT_DONT_OVERWRITE 16 ///< Don't overwrite existing entries. + +typedef struct { + char *key; + char *value; +} AVDictionaryEntry; + +typedef struct AVDictionary AVDictionary; + +/** + * Get a dictionary entry with matching key. + * + * @param prev Set to the previous matching element to find the next. + * If set to NULL the first matching element is returned. + * @param flags Allows case as well as suffix-insensitive comparisons. + * @return Found entry or NULL, changing key or value leads to undefined behavior. + */ +AVDictionaryEntry * +av_dict_get(AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags); + +/** + * Set the given entry in *pm, overwriting an existing entry. + * + * @param pm pointer to a pointer to a dictionary struct. If *pm is NULL + * a dictionary struct is allocated and put in *pm. + * @param key entry key to add to *pm (will be av_strduped depending on flags) + * @param value entry value to add to *pm (will be av_strduped depending on flags). + * Passing a NULL value will cause an existing tag to be deleted. + * @return >= 0 on success otherwise an error code <0 + */ +int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags); + +/** + * Copy entries from one AVDictionary struct into another. + * @param dst pointer to a pointer to a AVDictionary struct. If *dst is NULL, + * this function will allocate a struct for you and put it in *dst + * @param src pointer to source AVDictionary struct + * @param flags flags to use when setting entries in *dst + * @note metadata is read using the AV_DICT_IGNORE_SUFFIX flag + */ +void av_dict_copy(AVDictionary **dst, AVDictionary *src, int flags); + +/** + * Free all the memory allocated for an AVDictionary struct. + */ +void av_dict_free(AVDictionary **m); + +#endif // AVUTIL_DICT_H diff --git a/libavutil/internal.h b/libavutil/internal.h index ee11a0a9d2..5ed3fb81c3 100644 --- a/libavutil/internal.h +++ b/libavutil/internal.h @@ -37,6 +37,12 @@ #include "config.h" #include "attributes.h" #include "timer.h" +#include "dict.h" + +struct AVDictionary { + int count; + AVDictionaryEntry *elems; +}; #ifndef attribute_align_arg #if ARCH_X86_32 && AV_GCC_VERSION_AT_LEAST(4,2) |