FFmpeg
dict.c
Go to the documentation of this file.
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 <inttypes.h>
22 #include <stdio.h>
23 #include <string.h>
24 
25 #include "avassert.h"
26 #include "avstring.h"
27 #include "dict.h"
28 #include "dict_internal.h"
29 #include "error.h"
30 #include "mem.h"
31 #include "time_internal.h"
32 #include "bprint.h"
33 
34 struct AVDictionary {
35  int count;
37 };
38 
40 {
41  return m ? m->count : 0;
42 }
43 
45  const AVDictionaryEntry *prev)
46 {
47  int i = 0;
48 
49  if (!m)
50  return NULL;
51 
52  if (prev)
53  i = prev - m->elems + 1;
54 
55  av_assert2(i >= 0);
56  if (i >= m->count)
57  return NULL;
58 
59  return &m->elems[i];
60 }
61 
63  const AVDictionaryEntry *prev, int flags)
64 {
65  const AVDictionaryEntry *entry = prev;
66  unsigned int j;
67 
68  if (!key)
69  return NULL;
70 
71  while ((entry = av_dict_iterate(m, entry))) {
72  const char *s = entry->key;
74  for (j = 0; s[j] == key[j] && key[j]; j++)
75  ;
76  else
77  for (j = 0; av_toupper(s[j]) == av_toupper(key[j]) && key[j]; j++)
78  ;
79  if (key[j])
80  continue;
81  if (s[j] && !(flags & AV_DICT_IGNORE_SUFFIX))
82  continue;
83  return (AVDictionaryEntry *)entry;
84  }
85  return NULL;
86 }
87 
88 int av_dict_set(AVDictionary **pm, const char *key, const char *value,
89  int flags)
90 {
91  AVDictionary *m = *pm;
93  char *copy_key = NULL, *copy_value = NULL;
94  int err;
95 
97  copy_value = (void *)value;
98  else if (value)
99  copy_value = av_strdup(value);
100  if (!key) {
101  err = AVERROR(EINVAL);
102  goto err_out;
103  }
104  if (!(flags & AV_DICT_MULTIKEY)) {
105  tag = av_dict_get(m, key, NULL, flags);
106  }
108  copy_key = (void *)key;
109  else
110  copy_key = av_strdup(key);
111  if (!m)
112  m = *pm = av_mallocz(sizeof(*m));
113  if (!m || !copy_key || (value && !copy_value))
114  goto enomem;
115 
116  if (tag) {
118  av_free(copy_key);
119  av_free(copy_value);
120  return 0;
121  }
122  if (copy_value && flags & AV_DICT_APPEND) {
123  size_t oldlen = strlen(tag->value);
124  size_t new_part_len = strlen(copy_value);
125  size_t len = oldlen + new_part_len + 1;
126  char *newval = av_realloc(tag->value, len);
127  if (!newval)
128  goto enomem;
129  memcpy(newval + oldlen, copy_value, new_part_len + 1);
130  av_freep(&copy_value);
131  copy_value = newval;
132  } else
133  av_free(tag->value);
134  av_free(tag->key);
135  *tag = m->elems[--m->count];
136  } else if (copy_value) {
138  m->count + 1, sizeof(*m->elems));
139  if (!tmp)
140  goto enomem;
141  m->elems = tmp;
142  }
143  if (copy_value) {
144  m->elems[m->count].key = copy_key;
145  m->elems[m->count].value = copy_value;
146  m->count++;
147  } else {
148  if (!m->count) {
149  av_freep(&m->elems);
150  av_freep(pm);
151  }
152  av_freep(&copy_key);
153  }
154 
155  return 0;
156 
157 enomem:
158  err = AVERROR(ENOMEM);
159 err_out:
160  if (m && !m->count) {
161  av_freep(&m->elems);
162  av_freep(pm);
163  }
164  av_free(copy_key);
165  av_free(copy_value);
166  return err;
167 }
168 
169 int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value,
170  int flags)
171 {
172  char valuestr[22];
173  snprintf(valuestr, sizeof(valuestr), "%"PRId64, value);
175  return av_dict_set(pm, key, valuestr, flags);
176 }
177 
178 static int parse_key_value_pair(AVDictionary **pm, const char **buf,
179  const char *key_val_sep, const char *pairs_sep,
180  int flags)
181 {
182  char *key = av_get_token(buf, key_val_sep);
183  char *val = NULL;
184  int ret;
185 
186  if (key && *key && strspn(*buf, key_val_sep)) {
187  (*buf)++;
188  val = av_get_token(buf, pairs_sep);
189  }
190 
191  if (key && *key && val && *val)
192  ret = av_dict_set(pm, key, val, flags);
193  else
194  ret = AVERROR(EINVAL);
195 
196  av_freep(&key);
197  av_freep(&val);
198 
199  return ret;
200 }
201 
202 int av_dict_parse_string(AVDictionary **pm, const char *str,
203  const char *key_val_sep, const char *pairs_sep,
204  int flags)
205 {
206  int ret;
207 
208  if (!str)
209  return 0;
210 
211  /* ignore STRDUP flags */
213 
214  while (*str) {
215  if ((ret = parse_key_value_pair(pm, &str, key_val_sep, pairs_sep, flags)) < 0)
216  return ret;
217 
218  if (*str)
219  str++;
220  }
221 
222  return 0;
223 }
224 
226 {
227  AVDictionary *m = *pm;
228 
229  if (m) {
230  while (m->count--) {
231  av_freep(&m->elems[m->count].key);
232  av_freep(&m->elems[m->count].value);
233  }
234  av_freep(&m->elems);
235  }
236  av_freep(pm);
237 }
238 
240 {
241  const AVDictionaryEntry *t = NULL;
242 
243  while ((t = av_dict_iterate(src, t))) {
244  int ret = av_dict_set(dst, t->key, t->value, flags);
245  if (ret < 0)
246  return ret;
247  }
248 
249  return 0;
250 }
251 
253  const char key_val_sep, const char pairs_sep)
254 {
255  const AVDictionaryEntry *t = NULL;
256  AVBPrint bprint;
257  int cnt = 0;
258  char special_chars[] = {pairs_sep, key_val_sep, '\0'};
259 
260  if (!buffer || pairs_sep == '\0' || key_val_sep == '\0' || pairs_sep == key_val_sep ||
261  pairs_sep == '\\' || key_val_sep == '\\')
262  return AVERROR(EINVAL);
263 
264  if (!av_dict_count(m)) {
265  *buffer = av_strdup("");
266  return *buffer ? 0 : AVERROR(ENOMEM);
267  }
268 
270  while ((t = av_dict_iterate(m, t))) {
271  if (cnt++)
272  av_bprint_append_data(&bprint, &pairs_sep, 1);
273  av_bprint_escape(&bprint, t->key, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
274  av_bprint_append_data(&bprint, &key_val_sep, 1);
275  av_bprint_escape(&bprint, t->value, special_chars, AV_ESCAPE_MODE_BACKSLASH, 0);
276  }
277  return av_bprint_finalize(&bprint, buffer);
278 }
279 
280 int avpriv_dict_set_timestamp(AVDictionary **dict, const char *key, int64_t timestamp)
281 {
282  time_t seconds = timestamp / 1000000;
283  struct tm *ptm, tmbuf;
284  ptm = gmtime_r(&seconds, &tmbuf);
285  if (ptm) {
286  char buf[32];
287  if (!strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", ptm))
288  return AVERROR_EXTERNAL;
289  av_strlcatf(buf, sizeof(buf), ".%06dZ", (int)(timestamp % 1000000));
290  return av_dict_set(dict, key, buf, 0);
291  } else {
292  return AVERROR_EXTERNAL;
293  }
294 }
AV_BPRINT_SIZE_UNLIMITED
#define AV_BPRINT_SIZE_UNLIMITED
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
av_bprint_init
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition: bprint.c:69
av_dict_count
int av_dict_count(const AVDictionary *m)
Get number of entries in dictionary.
Definition: dict.c:39
AVDictionary::elems
AVDictionaryEntry * elems
Definition: dict.c:36
dict_internal.h
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
AV_DICT_APPEND
#define AV_DICT_APPEND
If the entry already exists, append to it.
Definition: dict.h:82
AV_DICT_IGNORE_SUFFIX
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key,...
Definition: dict.h:75
AVDictionary
Definition: dict.c:34
av_strlcatf
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
Definition: avstring.c:103
gmtime_r
#define gmtime_r
Definition: time_internal.h:34
val
static double val(void *priv, double ch)
Definition: aeval.c:78
AV_DICT_DONT_STRDUP_VAL
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that's been allocated with av_malloc() or another memory allocation functio...
Definition: dict.h:79
avassert.h
av_dict_get
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:62
s
#define s(width, name)
Definition: cbs_vp9.c:198
av_realloc_array
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:215
AVDictionaryEntry::key
char * key
Definition: dict.h:90
key
const char * key
Definition: hwcontext_opencl.c:174
time_internal.h
AVDictionary::count
int count
Definition: dict.c:35
NULL
#define NULL
Definition: coverity.c:32
AV_DICT_MULTIKEY
#define AV_DICT_MULTIKEY
Allow to store several equal keys in the dictionary.
Definition: dict.h:84
av_bprint_escape
void av_bprint_escape(AVBPrint *dstbuf, const char *src, const char *special_chars, enum AVEscapeMode mode, int flags)
Escape the content in src and append it to dstbuf.
Definition: bprint.c:268
AV_DICT_DONT_OVERWRITE
#define AV_DICT_DONT_OVERWRITE
Don't overwrite existing entries.
Definition: dict.h:81
error.h
av_bprint_finalize
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:240
AVERROR_EXTERNAL
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:59
av_dict_free
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition: dict.c:225
av_assert2
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:67
bprint.h
parse_key_value_pair
static int parse_key_value_pair(AVDictionary **pm, const char **buf, const char *key_val_sep, const char *pairs_sep, int flags)
Definition: dict.c:178
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
av_toupper
static av_const int av_toupper(int c)
Locale-independent conversion of ASCII characters to uppercase.
Definition: avstring.h:227
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:254
len
int len
Definition: vorbis_enc_data.h:426
tag
uint32_t tag
Definition: movenc.c:1737
ret
ret
Definition: filter_design.txt:187
dict.h
AV_DICT_MATCH_CASE
#define AV_DICT_MATCH_CASE
Only get an entry with exact-case key match.
Definition: dict.h:74
buffer
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
Definition: filter_design.txt:49
av_get_token
char * av_get_token(const char **buf, const char *term)
Unescape the given string until a non escaped terminating char, and return the token corresponding to...
Definition: avstring.c:143
av_dict_parse_string
int av_dict_parse_string(AVDictionary **pm, const char *str, const char *key_val_sep, const char *pairs_sep, int flags)
Parse the key/value pairs list and add the parsed entries to a dictionary.
Definition: dict.c:202
av_dict_set_int
int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value, int flags)
Convenience wrapper for av_dict_set() that converts the value to a string and stores it.
Definition: dict.c:169
av_strdup
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:270
mem.h
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
AVDictionaryEntry
Definition: dict.h:89
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
av_dict_set
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:88
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
av_dict_get_string
int av_dict_get_string(const AVDictionary *m, char **buffer, const char key_val_sep, const char pairs_sep)
Get dictionary entries as a string.
Definition: dict.c:252
av_dict_copy
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition: dict.c:239
AV_ESCAPE_MODE_BACKSLASH
@ AV_ESCAPE_MODE_BACKSLASH
Use backslash escaping.
Definition: avstring.h:316
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:474
AVDictionaryEntry::value
char * value
Definition: dict.h:91
avstring.h
av_bprint_append_data
void av_bprint_append_data(AVBPrint *buf, const char *data, unsigned size)
Append data to a print buffer.
Definition: bprint.c:163
snprintf
#define snprintf
Definition: snprintf.h:34
avpriv_dict_set_timestamp
int avpriv_dict_set_timestamp(AVDictionary **dict, const char *key, int64_t timestamp)
Set a dictionary value to an ISO-8601 compliant timestamp string.
Definition: dict.c:280
av_dict_iterate
const AVDictionaryEntry * av_dict_iterate(const AVDictionary *m, const AVDictionaryEntry *prev)
Iterate over a dictionary.
Definition: dict.c:44
AV_DICT_DONT_STRDUP_KEY
#define AV_DICT_DONT_STRDUP_KEY
Take ownership of a key that's been allocated with av_malloc() or another memory allocation function.
Definition: dict.h:77
av_realloc
void * av_realloc(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory.
Definition: mem.c:153