caps, structure, taglist: micro-optimisations
[platform/upstream/gstreamer.git] / gst / gsttaglist.c
1 /* GStreamer
2  * Copyright (C) 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
3  *
4  * gsttaglist.c: tag support (aka metadata)
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library 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  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 /**
23  * SECTION:gsttaglist
24  * @short_description: List of tags and values used to describe media metadata
25  *
26  * List of tags and values used to describe media metadata.
27  *
28  * Strings must be in ASCII or UTF-8 encoding. No other encodings are allowed.
29  *
30  * Last reviewed on 2009-06-09 (0.10.23)
31  */
32
33 #ifdef HAVE_CONFIG_H
34 #  include "config.h"
35 #endif
36
37 #include "gst_private.h"
38 #include "math-compat.h"
39 #include "gst-i18n-lib.h"
40 #include "gsttaglist.h"
41 #include "gstinfo.h"
42 #include "gstvalue.h"
43 #include "gstbuffer.h"
44 #include "gstquark.h"
45 #include "gststructure.h"
46
47 #include <gobject/gvaluecollector.h>
48 #include <string.h>
49
50 /* FIXME: add category for tags */
51 #define GST_CAT_TAGS GST_CAT_DEFAULT
52
53 #define GST_TAG_IS_VALID(tag)           (gst_tag_get_info (tag) != NULL)
54
55 typedef struct _GstTagListImpl
56 {
57   GstTagList taglist;
58
59   GstStructure *structure;
60   GstTagScope scope;
61 } GstTagListImpl;
62
63 #define GST_TAG_LIST_STRUCTURE(taglist)  ((GstTagListImpl*)(taglist))->structure
64 #define GST_TAG_LIST_SCOPE(taglist)  ((GstTagListImpl*)(taglist))->scope
65
66 typedef struct
67 {
68   GType type;                   /* type the data is in */
69
70   const gchar *nick;            /* translated short description */
71   const gchar *blurb;           /* translated long description  */
72
73   GstTagMergeFunc merge_func;   /* functions to merge the values */
74   GstTagFlag flag;              /* type of tag */
75   GQuark name_quark;            /* quark for the name */
76 }
77 GstTagInfo;
78
79 #define g_value_get_char g_value_get_schar
80
81 static GMutex __tag_mutex;
82 #define TAG_LOCK g_mutex_lock (&__tag_mutex)
83 #define TAG_UNLOCK g_mutex_unlock (&__tag_mutex)
84
85 /* tags hash table: maps tag name string => GstTagInfo */
86 static GHashTable *__tags;
87
88 GST_DEFINE_MINI_OBJECT_TYPE (GstTagList, gst_tag_list);
89
90 static void __gst_tag_list_free (GstTagList * list);
91 static GstTagList *__gst_tag_list_copy (const GstTagList * list);
92
93 /* FIXME: had code:
94  *    g_value_register_transform_func (_gst_tag_list_type, G_TYPE_STRING,
95  *      _gst_structure_transform_to_string);
96  */
97 void
98 _priv_gst_tag_initialize (void)
99 {
100   g_mutex_init (&__tag_mutex);
101
102   __tags = g_hash_table_new (g_str_hash, g_str_equal);
103   gst_tag_register_static (GST_TAG_TITLE, GST_TAG_FLAG_META,
104       G_TYPE_STRING,
105       _("title"), _("commonly used title"), gst_tag_merge_strings_with_comma);
106   gst_tag_register_static (GST_TAG_TITLE_SORTNAME, GST_TAG_FLAG_META,
107       G_TYPE_STRING,
108       _("title sortname"), _("commonly used title for sorting purposes"), NULL);
109   gst_tag_register_static (GST_TAG_ARTIST, GST_TAG_FLAG_META,
110       G_TYPE_STRING,
111       _("artist"),
112       _("person(s) responsible for the recording"),
113       gst_tag_merge_strings_with_comma);
114   gst_tag_register_static (GST_TAG_ARTIST_SORTNAME, GST_TAG_FLAG_META,
115       G_TYPE_STRING,
116       _("artist sortname"),
117       _("person(s) responsible for the recording for sorting purposes"), NULL);
118   gst_tag_register_static (GST_TAG_ALBUM, GST_TAG_FLAG_META,
119       G_TYPE_STRING,
120       _("album"),
121       _("album containing this data"), gst_tag_merge_strings_with_comma);
122   gst_tag_register_static (GST_TAG_ALBUM_SORTNAME, GST_TAG_FLAG_META,
123       G_TYPE_STRING,
124       _("album sortname"),
125       _("album containing this data for sorting purposes"), NULL);
126   gst_tag_register_static (GST_TAG_ALBUM_ARTIST, GST_TAG_FLAG_META,
127       G_TYPE_STRING,
128       _("album artist"),
129       _("The artist of the entire album, as it should be displayed"),
130       gst_tag_merge_strings_with_comma);
131   gst_tag_register_static (GST_TAG_ALBUM_ARTIST_SORTNAME, GST_TAG_FLAG_META,
132       G_TYPE_STRING,
133       _("album artist sortname"),
134       _("The artist of the entire album, as it should be sorted"), NULL);
135   gst_tag_register_static (GST_TAG_DATE, GST_TAG_FLAG_META, G_TYPE_DATE,
136       _("date"), _("date the data was created (as a GDate structure)"), NULL);
137   gst_tag_register_static (GST_TAG_DATE_TIME, GST_TAG_FLAG_META,
138       GST_TYPE_DATE_TIME, _("datetime"),
139       _("date and time the data was created (as a GstDateTime structure)"),
140       NULL);
141   gst_tag_register_static (GST_TAG_GENRE, GST_TAG_FLAG_META,
142       G_TYPE_STRING,
143       _("genre"),
144       _("genre this data belongs to"), gst_tag_merge_strings_with_comma);
145   gst_tag_register_static (GST_TAG_COMMENT, GST_TAG_FLAG_META,
146       G_TYPE_STRING,
147       _("comment"),
148       _("free text commenting the data"), gst_tag_merge_use_first);
149   gst_tag_register_static (GST_TAG_EXTENDED_COMMENT, GST_TAG_FLAG_META,
150       G_TYPE_STRING,
151       _("extended comment"),
152       _("free text commenting the data in key=value or key[en]=comment form"),
153       gst_tag_merge_use_first);
154   gst_tag_register_static (GST_TAG_TRACK_NUMBER, GST_TAG_FLAG_META,
155       G_TYPE_UINT,
156       _("track number"),
157       _("track number inside a collection"), gst_tag_merge_use_first);
158   gst_tag_register_static (GST_TAG_TRACK_COUNT, GST_TAG_FLAG_META,
159       G_TYPE_UINT,
160       _("track count"),
161       _("count of tracks inside collection this track belongs to"),
162       gst_tag_merge_use_first);
163   gst_tag_register_static (GST_TAG_ALBUM_VOLUME_NUMBER, GST_TAG_FLAG_META,
164       G_TYPE_UINT,
165       _("disc number"),
166       _("disc number inside a collection"), gst_tag_merge_use_first);
167   gst_tag_register_static (GST_TAG_ALBUM_VOLUME_COUNT, GST_TAG_FLAG_META,
168       G_TYPE_UINT,
169       _("disc count"),
170       _("count of discs inside collection this disc belongs to"),
171       gst_tag_merge_use_first);
172   gst_tag_register_static (GST_TAG_LOCATION, GST_TAG_FLAG_META,
173       G_TYPE_STRING,
174       _("location"), _("Origin of media as a URI (location, where the "
175           "original of the file or stream is hosted)"),
176       gst_tag_merge_strings_with_comma);
177   gst_tag_register_static (GST_TAG_HOMEPAGE, GST_TAG_FLAG_META,
178       G_TYPE_STRING,
179       _("homepage"),
180       _("Homepage for this media (i.e. artist or movie homepage)"),
181       gst_tag_merge_strings_with_comma);
182   gst_tag_register_static (GST_TAG_DESCRIPTION, GST_TAG_FLAG_META,
183       G_TYPE_STRING, _("description"),
184       _("short text describing the content of the data"),
185       gst_tag_merge_strings_with_comma);
186   gst_tag_register_static (GST_TAG_VERSION, GST_TAG_FLAG_META, G_TYPE_STRING,
187       _("version"), _("version of this data"), NULL);
188   gst_tag_register_static (GST_TAG_ISRC, GST_TAG_FLAG_META, G_TYPE_STRING,
189       _("ISRC"),
190       _
191       ("International Standard Recording Code - see http://www.ifpi.org/isrc/"),
192       NULL);
193   /* FIXME: organization (fix what? tpm) */
194   gst_tag_register_static (GST_TAG_ORGANIZATION, GST_TAG_FLAG_META,
195       G_TYPE_STRING, _("organization"), _("organization"),
196       gst_tag_merge_strings_with_comma);
197   gst_tag_register_static (GST_TAG_COPYRIGHT, GST_TAG_FLAG_META,
198       G_TYPE_STRING, _("copyright"), _("copyright notice of the data"), NULL);
199   gst_tag_register_static (GST_TAG_COPYRIGHT_URI, GST_TAG_FLAG_META,
200       G_TYPE_STRING, _("copyright uri"),
201       _("URI to the copyright notice of the data"), NULL);
202   gst_tag_register_static (GST_TAG_ENCODED_BY, GST_TAG_FLAG_META, G_TYPE_STRING,
203       _("encoded by"), _("name of the encoding person or organization"),
204       gst_tag_merge_strings_with_comma);
205   gst_tag_register_static (GST_TAG_CONTACT, GST_TAG_FLAG_META,
206       G_TYPE_STRING,
207       _("contact"), _("contact information"), gst_tag_merge_strings_with_comma);
208   gst_tag_register_static (GST_TAG_LICENSE, GST_TAG_FLAG_META,
209       G_TYPE_STRING, _("license"), _("license of data"), NULL);
210   gst_tag_register_static (GST_TAG_LICENSE_URI, GST_TAG_FLAG_META,
211       G_TYPE_STRING, _("license uri"),
212       _("URI to the license of the data"), NULL);
213   gst_tag_register_static (GST_TAG_PERFORMER, GST_TAG_FLAG_META,
214       G_TYPE_STRING,
215       _("performer"),
216       _("person(s) performing"), gst_tag_merge_strings_with_comma);
217   gst_tag_register_static (GST_TAG_COMPOSER, GST_TAG_FLAG_META,
218       G_TYPE_STRING,
219       _("composer"),
220       _("person(s) who composed the recording"),
221       gst_tag_merge_strings_with_comma);
222   gst_tag_register_static (GST_TAG_DURATION, GST_TAG_FLAG_DECODED,
223       G_TYPE_UINT64,
224       _("duration"), _("length in GStreamer time units (nanoseconds)"), NULL);
225   gst_tag_register_static (GST_TAG_CODEC, GST_TAG_FLAG_ENCODED,
226       G_TYPE_STRING,
227       _("codec"),
228       _("codec the data is stored in"), gst_tag_merge_strings_with_comma);
229   gst_tag_register_static (GST_TAG_VIDEO_CODEC, GST_TAG_FLAG_ENCODED,
230       G_TYPE_STRING,
231       _("video codec"), _("codec the video data is stored in"), NULL);
232   gst_tag_register_static (GST_TAG_AUDIO_CODEC, GST_TAG_FLAG_ENCODED,
233       G_TYPE_STRING,
234       _("audio codec"), _("codec the audio data is stored in"), NULL);
235   gst_tag_register_static (GST_TAG_SUBTITLE_CODEC, GST_TAG_FLAG_ENCODED,
236       G_TYPE_STRING,
237       _("subtitle codec"), _("codec the subtitle data is stored in"), NULL);
238   gst_tag_register_static (GST_TAG_CONTAINER_FORMAT, GST_TAG_FLAG_ENCODED,
239       G_TYPE_STRING, _("container format"),
240       _("container format the data is stored in"), NULL);
241   gst_tag_register_static (GST_TAG_BITRATE, GST_TAG_FLAG_ENCODED,
242       G_TYPE_UINT, _("bitrate"), _("exact or average bitrate in bits/s"), NULL);
243   gst_tag_register_static (GST_TAG_NOMINAL_BITRATE, GST_TAG_FLAG_ENCODED,
244       G_TYPE_UINT, _("nominal bitrate"), _("nominal bitrate in bits/s"), NULL);
245   gst_tag_register_static (GST_TAG_MINIMUM_BITRATE, GST_TAG_FLAG_ENCODED,
246       G_TYPE_UINT, _("minimum bitrate"), _("minimum bitrate in bits/s"), NULL);
247   gst_tag_register_static (GST_TAG_MAXIMUM_BITRATE, GST_TAG_FLAG_ENCODED,
248       G_TYPE_UINT, _("maximum bitrate"), _("maximum bitrate in bits/s"), NULL);
249   gst_tag_register_static (GST_TAG_ENCODER, GST_TAG_FLAG_ENCODED,
250       G_TYPE_STRING,
251       _("encoder"), _("encoder used to encode this stream"), NULL);
252   gst_tag_register_static (GST_TAG_ENCODER_VERSION, GST_TAG_FLAG_ENCODED,
253       G_TYPE_UINT,
254       _("encoder version"),
255       _("version of the encoder used to encode this stream"), NULL);
256   gst_tag_register_static (GST_TAG_SERIAL, GST_TAG_FLAG_ENCODED,
257       G_TYPE_UINT, _("serial"), _("serial number of track"), NULL);
258   gst_tag_register_static (GST_TAG_TRACK_GAIN, GST_TAG_FLAG_META,
259       G_TYPE_DOUBLE, _("replaygain track gain"), _("track gain in db"), NULL);
260   gst_tag_register_static (GST_TAG_TRACK_PEAK, GST_TAG_FLAG_META,
261       G_TYPE_DOUBLE, _("replaygain track peak"), _("peak of the track"), NULL);
262   gst_tag_register_static (GST_TAG_ALBUM_GAIN, GST_TAG_FLAG_META,
263       G_TYPE_DOUBLE, _("replaygain album gain"), _("album gain in db"), NULL);
264   gst_tag_register_static (GST_TAG_ALBUM_PEAK, GST_TAG_FLAG_META,
265       G_TYPE_DOUBLE, _("replaygain album peak"), _("peak of the album"), NULL);
266   gst_tag_register_static (GST_TAG_REFERENCE_LEVEL, GST_TAG_FLAG_META,
267       G_TYPE_DOUBLE, _("replaygain reference level"),
268       _("reference level of track and album gain values"), NULL);
269   gst_tag_register_static (GST_TAG_LANGUAGE_CODE, GST_TAG_FLAG_META,
270       G_TYPE_STRING, _("language code"),
271       _("language code for this stream, conforming to ISO-639-1 or ISO-639-2"),
272       NULL);
273   gst_tag_register_static (GST_TAG_LANGUAGE_NAME, GST_TAG_FLAG_META,
274       G_TYPE_STRING, _("language name"),
275       _("freeform name of the language this stream is in"), NULL);
276   gst_tag_register_static (GST_TAG_IMAGE, GST_TAG_FLAG_META, GST_TYPE_SAMPLE,
277       _("image"), _("image related to this stream"), gst_tag_merge_use_first);
278   gst_tag_register_static (GST_TAG_PREVIEW_IMAGE, GST_TAG_FLAG_META,
279       GST_TYPE_SAMPLE,
280       /* TRANSLATORS: 'preview image' = image that shows a preview of the full image */
281       _("preview image"), _("preview image related to this stream"), NULL);
282   gst_tag_register_static (GST_TAG_ATTACHMENT, GST_TAG_FLAG_META,
283       GST_TYPE_SAMPLE, _("attachment"), _("file attached to this stream"),
284       gst_tag_merge_use_first);
285   gst_tag_register_static (GST_TAG_BEATS_PER_MINUTE, GST_TAG_FLAG_META,
286       G_TYPE_DOUBLE, _("beats per minute"),
287       _("number of beats per minute in audio"), NULL);
288   gst_tag_register_static (GST_TAG_KEYWORDS, GST_TAG_FLAG_META, G_TYPE_STRING,
289       _("keywords"), _("comma separated keywords describing the content"),
290       gst_tag_merge_strings_with_comma);
291   gst_tag_register_static (GST_TAG_GEO_LOCATION_NAME, GST_TAG_FLAG_META,
292       G_TYPE_STRING, _("geo location name"),
293       _("human readable descriptive location of where "
294           "the media has been recorded or produced"), NULL);
295   gst_tag_register_static (GST_TAG_GEO_LOCATION_LATITUDE, GST_TAG_FLAG_META,
296       G_TYPE_DOUBLE, _("geo location latitude"),
297       _("geo latitude location of where the media has been recorded or "
298           "produced in degrees according to WGS84 (zero at the equator, "
299           "negative values for southern latitudes)"), NULL);
300   gst_tag_register_static (GST_TAG_GEO_LOCATION_LONGITUDE, GST_TAG_FLAG_META,
301       G_TYPE_DOUBLE, _("geo location longitude"),
302       _("geo longitude location of where the media has been recorded or "
303           "produced in degrees according to WGS84 (zero at the prime meridian "
304           "in Greenwich/UK,  negative values for western longitudes)"), NULL);
305   gst_tag_register_static (GST_TAG_GEO_LOCATION_ELEVATION, GST_TAG_FLAG_META,
306       G_TYPE_DOUBLE, _("geo location elevation"),
307       _("geo elevation of where the media has been recorded or produced in "
308           "meters according to WGS84 (zero is average sea level)"), NULL);
309   gst_tag_register_static (GST_TAG_GEO_LOCATION_COUNTRY, GST_TAG_FLAG_META,
310       G_TYPE_STRING, _("geo location country"),
311       _("country (english name) where the media has been recorded "
312           "or produced"), NULL);
313   gst_tag_register_static (GST_TAG_GEO_LOCATION_CITY, GST_TAG_FLAG_META,
314       G_TYPE_STRING, _("geo location city"),
315       _("city (english name) where the media has been recorded "
316           "or produced"), NULL);
317   gst_tag_register_static (GST_TAG_GEO_LOCATION_SUBLOCATION, GST_TAG_FLAG_META,
318       G_TYPE_STRING, _("geo location sublocation"),
319       _("a location within a city where the media has been produced "
320           "or created (e.g. the neighborhood)"), NULL);
321   gst_tag_register_static (GST_TAG_GEO_LOCATION_HORIZONTAL_ERROR,
322       GST_TAG_FLAG_META, G_TYPE_DOUBLE, _("geo location horizontal error"),
323       _("expected error of the horizontal positioning measures (in meters)"),
324       NULL);
325   gst_tag_register_static (GST_TAG_GEO_LOCATION_MOVEMENT_SPEED,
326       GST_TAG_FLAG_META, G_TYPE_DOUBLE, _("geo location movement speed"),
327       _("movement speed of the capturing device while performing the capture "
328           "in m/s"), NULL);
329   gst_tag_register_static (GST_TAG_GEO_LOCATION_MOVEMENT_DIRECTION,
330       GST_TAG_FLAG_META, G_TYPE_DOUBLE, _("geo location movement direction"),
331       _("indicates the movement direction of the device performing the capture"
332           " of a media. It is represented as degrees in floating point "
333           "representation, 0 means the geographic north, and increases "
334           "clockwise"), NULL);
335   gst_tag_register_static (GST_TAG_GEO_LOCATION_CAPTURE_DIRECTION,
336       GST_TAG_FLAG_META, G_TYPE_DOUBLE, _("geo location capture direction"),
337       _("indicates the direction the device is pointing to when capturing "
338           " a media. It is represented as degrees in floating point "
339           " representation, 0 means the geographic north, and increases "
340           "clockwise"), NULL);
341   gst_tag_register_static (GST_TAG_SHOW_NAME, GST_TAG_FLAG_META, G_TYPE_STRING,
342       /* TRANSLATORS: 'show name' = 'TV/radio/podcast show name' here */
343       _("show name"),
344       _("Name of the tv/podcast/series show the media is from"),
345       gst_tag_merge_strings_with_comma);
346   gst_tag_register_static (GST_TAG_SHOW_SORTNAME, GST_TAG_FLAG_META,
347       G_TYPE_STRING,
348       /* TRANSLATORS: 'show sortname' = 'TV/radio/podcast show name as used for sorting purposes' here */
349       _("show sortname"),
350       _("Name of the tv/podcast/series show the media is from, for sorting "
351           "purposes"), NULL);
352   gst_tag_register_static (GST_TAG_SHOW_EPISODE_NUMBER, GST_TAG_FLAG_META,
353       G_TYPE_UINT, _("episode number"),
354       _("The episode number in the season the media is part of"),
355       gst_tag_merge_use_first);
356   gst_tag_register_static (GST_TAG_SHOW_SEASON_NUMBER, GST_TAG_FLAG_META,
357       G_TYPE_UINT, _("season number"),
358       _("The season number of the show the media is part of"),
359       gst_tag_merge_use_first);
360   gst_tag_register_static (GST_TAG_LYRICS, GST_TAG_FLAG_META, G_TYPE_STRING,
361       _("lyrics"), _("The lyrics of the media, commonly used for songs"),
362       gst_tag_merge_strings_with_comma);
363   gst_tag_register_static (GST_TAG_COMPOSER_SORTNAME, GST_TAG_FLAG_META,
364       G_TYPE_STRING, _("composer sortname"),
365       _("person(s) who composed the recording, for sorting purposes"), NULL);
366   gst_tag_register_static (GST_TAG_GROUPING, GST_TAG_FLAG_META, G_TYPE_STRING,
367       _("grouping"),
368       _("Groups related media that spans multiple tracks, like the different "
369           "pieces of a concerto. It is a higher level than a track, "
370           "but lower than an album"), NULL);
371   gst_tag_register_static (GST_TAG_USER_RATING, GST_TAG_FLAG_META, G_TYPE_UINT,
372       _("user rating"),
373       _("Rating attributed by a user. The higher the rank, "
374           "the more the user likes this media"), NULL);
375   gst_tag_register_static (GST_TAG_DEVICE_MANUFACTURER, GST_TAG_FLAG_META,
376       G_TYPE_STRING, _("device manufacturer"),
377       _("Manufacturer of the device used to create this media"), NULL);
378   gst_tag_register_static (GST_TAG_DEVICE_MODEL, GST_TAG_FLAG_META,
379       G_TYPE_STRING, _("device model"),
380       _("Model of the device used to create this media"), NULL);
381   gst_tag_register_static (GST_TAG_APPLICATION_NAME, GST_TAG_FLAG_META,
382       G_TYPE_STRING, _("application name"),
383       _("Application used to create the media"), NULL);
384   gst_tag_register_static (GST_TAG_APPLICATION_DATA, GST_TAG_FLAG_META,
385       GST_TYPE_SAMPLE, _("application data"),
386       _("Arbitrary application data to be serialized into the media"), NULL);
387   gst_tag_register_static (GST_TAG_IMAGE_ORIENTATION, GST_TAG_FLAG_META,
388       G_TYPE_STRING, _("image orientation"),
389       _("How the image should be rotated or flipped before display"), NULL);
390 }
391
392 /**
393  * gst_tag_merge_use_first:
394  * @dest: (out caller-allocates): uninitialized GValue to store result in
395  * @src: GValue to copy from
396  *
397  * This is a convenience function for the func argument of gst_tag_register().
398  * It creates a copy of the first value from the list.
399  */
400 void
401 gst_tag_merge_use_first (GValue * dest, const GValue * src)
402 {
403   const GValue *ret = gst_value_list_get_value (src, 0);
404
405   g_value_init (dest, G_VALUE_TYPE (ret));
406   g_value_copy (ret, dest);
407 }
408
409 /**
410  * gst_tag_merge_strings_with_comma:
411  * @dest: (out caller-allocates): uninitialized GValue to store result in
412  * @src: GValue to copy from
413  *
414  * This is a convenience function for the func argument of gst_tag_register().
415  * It concatenates all given strings using a comma. The tag must be registered
416  * as a G_TYPE_STRING or this function will fail.
417  */
418 void
419 gst_tag_merge_strings_with_comma (GValue * dest, const GValue * src)
420 {
421   GString *str;
422   gint i, count;
423
424   count = gst_value_list_get_size (src);
425   str = g_string_new (g_value_get_string (gst_value_list_get_value (src, 0)));
426   for (i = 1; i < count; i++) {
427     /* separator between two strings */
428     g_string_append (str, _(", "));
429     g_string_append (str,
430         g_value_get_string (gst_value_list_get_value (src, i)));
431   }
432
433   g_value_init (dest, G_TYPE_STRING);
434   g_value_take_string (dest, str->str);
435   g_string_free (str, FALSE);
436 }
437
438 static GstTagInfo *
439 gst_tag_lookup (const gchar * tag_name)
440 {
441   GstTagInfo *ret;
442
443   TAG_LOCK;
444   ret = g_hash_table_lookup (__tags, (gpointer) tag_name);
445   TAG_UNLOCK;
446
447   return ret;
448 }
449
450 /**
451  * gst_tag_register:
452  * @name: the name or identifier string
453  * @flag: a flag describing the type of tag info
454  * @type: the type this data is in
455  * @nick: human-readable name
456  * @blurb: a human-readable description about this tag
457  * @func: function for merging multiple values of this tag, or NULL
458  *
459  * Registers a new tag type for the use with GStreamer's type system. If a type
460  * with that name is already registered, that one is used.
461  * The old registration may have used a different type however. So don't rely
462  * on your supplied values.
463  *
464  * Important: if you do not supply a merge function the implication will be
465  * that there can only be one single value for this tag in a tag list and
466  * any additional values will silenty be discarded when being added (unless
467  * #GST_TAG_MERGE_REPLACE, #GST_TAG_MERGE_REPLACE_ALL, or
468  * #GST_TAG_MERGE_PREPEND is used as merge mode, in which case the new
469  * value will replace the old one in the list).
470  *
471  * The merge function will be called from gst_tag_list_copy_value() when
472  * it is required that one or more values for a tag be condensed into
473  * one single value. This may happen from gst_tag_list_get_string(),
474  * gst_tag_list_get_int(), gst_tag_list_get_double() etc. What will happen
475  * exactly in that case depends on how the tag was registered and if a
476  * merge function was supplied and if so which one.
477  *
478  * Two default merge functions are provided: gst_tag_merge_use_first() and
479  * gst_tag_merge_strings_with_comma().
480  */
481 void
482 gst_tag_register (const gchar * name, GstTagFlag flag, GType type,
483     const gchar * nick, const gchar * blurb, GstTagMergeFunc func)
484 {
485   g_return_if_fail (name != NULL);
486   g_return_if_fail (nick != NULL);
487   g_return_if_fail (blurb != NULL);
488   g_return_if_fail (type != 0 && type != GST_TYPE_LIST);
489
490   return gst_tag_register_static (g_intern_string (name), flag, type,
491       g_intern_string (nick), g_intern_string (blurb), func);
492 }
493
494 /**
495  * gst_tag_register_static:
496  * @name: the name or identifier string (string constant)
497  * @flag: a flag describing the type of tag info
498  * @type: the type this data is in
499  * @nick: human-readable name or short description (string constant)
500  * @blurb: a human-readable description for this tag (string constant)
501  * @func: function for merging multiple values of this tag, or NULL
502  *
503  * Registers a new tag type for the use with GStreamer's type system.
504  *
505  * Same as gst_tag_register(), but @name, @nick, and @blurb must be
506  * static strings or inlined strings, as they will not be copied. (GStreamer
507  * plugins will be made resident once loaded, so this function can be used
508  * even from dynamically loaded plugins.)
509  */
510 void
511 gst_tag_register_static (const gchar * name, GstTagFlag flag, GType type,
512     const gchar * nick, const gchar * blurb, GstTagMergeFunc func)
513 {
514   GstTagInfo *info;
515
516   g_return_if_fail (name != NULL);
517   g_return_if_fail (nick != NULL);
518   g_return_if_fail (blurb != NULL);
519   g_return_if_fail (type != 0 && type != GST_TYPE_LIST);
520
521   info = gst_tag_lookup (name);
522
523   if (info) {
524     g_return_if_fail (info->type == type);
525     return;
526   }
527
528   info = g_slice_new (GstTagInfo);
529   info->flag = flag;
530   info->type = type;
531   info->name_quark = g_quark_from_static_string (name);
532   info->nick = nick;
533   info->blurb = blurb;
534   info->merge_func = func;
535
536   TAG_LOCK;
537   g_hash_table_insert (__tags, (gpointer) name, info);
538   TAG_UNLOCK;
539 }
540
541 /**
542  * gst_tag_exists:
543  * @tag: name of the tag
544  *
545  * Checks if the given type is already registered.
546  *
547  * Returns: TRUE if the type is already registered
548  */
549 gboolean
550 gst_tag_exists (const gchar * tag)
551 {
552   g_return_val_if_fail (tag != NULL, FALSE);
553
554   return gst_tag_lookup (tag) != NULL;
555 }
556
557 /**
558  * gst_tag_get_type:
559  * @tag: the tag
560  *
561  * Gets the #GType used for this tag.
562  *
563  * Returns: the #GType of this tag
564  */
565 GType
566 gst_tag_get_type (const gchar * tag)
567 {
568   GstTagInfo *info;
569
570   g_return_val_if_fail (tag != NULL, 0);
571   info = gst_tag_lookup (tag);
572   g_return_val_if_fail (info != NULL, 0);
573
574   return info->type;
575 }
576
577 /**
578  * gst_tag_get_nick:
579  * @tag: the tag
580  *
581  * Returns the human-readable name of this tag, You must not change or free
582  * this string.
583  *
584  * Returns: the human-readable name of this tag
585  */
586 const gchar *
587 gst_tag_get_nick (const gchar * tag)
588 {
589   GstTagInfo *info;
590
591   g_return_val_if_fail (tag != NULL, NULL);
592   info = gst_tag_lookup (tag);
593   g_return_val_if_fail (info != NULL, NULL);
594
595   return info->nick;
596 }
597
598 /**
599  * gst_tag_get_description:
600  * @tag: the tag
601  *
602  * Returns the human-readable description of this tag, You must not change or
603  * free this string.
604  *
605  * Returns: the human-readable description of this tag
606  */
607 const gchar *
608 gst_tag_get_description (const gchar * tag)
609 {
610   GstTagInfo *info;
611
612   g_return_val_if_fail (tag != NULL, NULL);
613   info = gst_tag_lookup (tag);
614   g_return_val_if_fail (info != NULL, NULL);
615
616   return info->blurb;
617 }
618
619 /**
620  * gst_tag_get_flag:
621  * @tag: the tag
622  *
623  * Gets the flag of @tag.
624  *
625  * Returns: the flag of this tag.
626  */
627 GstTagFlag
628 gst_tag_get_flag (const gchar * tag)
629 {
630   GstTagInfo *info;
631
632   g_return_val_if_fail (tag != NULL, GST_TAG_FLAG_UNDEFINED);
633   info = gst_tag_lookup (tag);
634   g_return_val_if_fail (info != NULL, GST_TAG_FLAG_UNDEFINED);
635
636   return info->flag;
637 }
638
639 /**
640  * gst_tag_is_fixed:
641  * @tag: tag to check
642  *
643  * Checks if the given tag is fixed. A fixed tag can only contain one value.
644  * Unfixed tags can contain lists of values.
645  *
646  * Returns: TRUE, if the given tag is fixed.
647  */
648 gboolean
649 gst_tag_is_fixed (const gchar * tag)
650 {
651   GstTagInfo *info;
652
653   g_return_val_if_fail (tag != NULL, FALSE);
654   info = gst_tag_lookup (tag);
655   g_return_val_if_fail (info != NULL, FALSE);
656
657   return info->merge_func == NULL;
658 }
659
660 /* takes ownership of the structure */
661 static GstTagList *
662 gst_tag_list_new_internal (GstStructure * s)
663 {
664   GstTagList *tag_list;
665
666   g_assert (s != NULL);
667
668   tag_list = (GstTagList *) g_slice_new (GstTagListImpl);
669
670   gst_mini_object_init (GST_MINI_OBJECT_CAST (tag_list), 0, GST_TYPE_TAG_LIST,
671       (GstMiniObjectCopyFunction) __gst_tag_list_copy, NULL,
672       (GstMiniObjectFreeFunction) __gst_tag_list_free);
673
674   GST_TAG_LIST_STRUCTURE (tag_list) = s;
675   GST_TAG_LIST_SCOPE (tag_list) = GST_TAG_SCOPE_STREAM;
676
677 #ifdef DEBUG_REFCOUNT
678   GST_CAT_TRACE (GST_CAT_TAGS, "created taglist %p", tag_list);
679 #endif
680
681   return tag_list;
682 }
683
684 static void
685 __gst_tag_list_free (GstTagList * list)
686 {
687   g_return_if_fail (GST_IS_TAG_LIST (list));
688
689 #ifdef DEBUG_REFCOUNT
690   GST_CAT_TRACE (GST_CAT_TAGS, "freeing taglist %p", list);
691 #endif
692
693   gst_structure_free (GST_TAG_LIST_STRUCTURE (list));
694
695   g_slice_free1 (sizeof (GstTagListImpl), list);
696 }
697
698 static GstTagList *
699 __gst_tag_list_copy (const GstTagList * list)
700 {
701   const GstStructure *s;
702
703   g_return_val_if_fail (GST_IS_TAG_LIST (list), NULL);
704
705   s = GST_TAG_LIST_STRUCTURE (list);
706   return gst_tag_list_new_internal (gst_structure_copy (s));
707 }
708
709 /**
710  * gst_tag_list_new_empty:
711  *
712  * Creates a new empty GstTagList.
713  *
714  * Free-function: gst_tag_list_unref
715  *
716  * Returns: (transfer full): An empty tag list
717  */
718 GstTagList *
719 gst_tag_list_new_empty (void)
720 {
721   GstStructure *s;
722   GstTagList *tag_list;
723
724   s = gst_structure_new_id_empty (GST_QUARK (TAGLIST));
725   tag_list = gst_tag_list_new_internal (s);
726   return tag_list;
727 }
728
729 /**
730  * gst_tag_list_new:
731  * @tag: tag
732  * @...: NULL-terminated list of values to set
733  *
734  * Creates a new taglist and appends the values for the given tags. It expects
735  * tag-value pairs like gst_tag_list_add(), and a NULL terminator after the
736  * last pair. The type of the values is implicit and is documented in the API
737  * reference, but can also be queried at runtime with gst_tag_get_type(). It
738  * is an error to pass a value of a type not matching the tag type into this
739  * function. The tag list will make copies of any arguments passed
740  * (e.g. strings, buffers).
741  *
742  * After creation you might also want to set a #GstTagScope on the returned
743  * taglist to signal if the contained tags are global or stream tags. By
744  * default stream scope is assumes. See gst_tag_list_set_scope().
745  *
746  * Free-function: gst_tag_list_unref
747  *
748  * Returns: (transfer full): a new #GstTagList. Free with gst_tag_list_unref()
749  *     when no longer needed.
750  */
751 GstTagList *
752 gst_tag_list_new (const gchar * tag, ...)
753 {
754   GstTagList *list;
755   va_list args;
756
757   g_return_val_if_fail (tag != NULL, NULL);
758
759   list = gst_tag_list_new_empty ();
760   va_start (args, tag);
761   gst_tag_list_add_valist (list, GST_TAG_MERGE_APPEND, tag, args);
762   va_end (args);
763
764   return list;
765 }
766
767 /**
768  * gst_tag_list_new_valist:
769  * @var_args: tag / value pairs to set
770  *
771  * Just like gst_tag_list_new(), only that it takes a va_list argument.
772  * Useful mostly for language bindings.
773  *
774  * Free-function: gst_tag_list_unref
775  *
776  * Returns: (transfer full): a new #GstTagList. Free with gst_tag_list_unref()
777  *     when no longer needed.
778  */
779 GstTagList *
780 gst_tag_list_new_valist (va_list var_args)
781 {
782   GstTagList *list;
783   const gchar *tag;
784
785   list = gst_tag_list_new_empty ();
786
787   tag = va_arg (var_args, gchar *);
788   gst_tag_list_add_valist (list, GST_TAG_MERGE_APPEND, tag, var_args);
789
790   return list;
791 }
792
793 /**
794  * gst_tag_list_set_scope:
795  * @list: a #GstTagList
796  * @scope: new scope for @list
797  *
798  * Sets the scope of @list to @scope. By default the scope
799  * of a taglist is stream scope.
800  *
801  */
802 void
803 gst_tag_list_set_scope (GstTagList * list, GstTagScope scope)
804 {
805   g_return_if_fail (GST_IS_TAG_LIST (list));
806   g_return_if_fail (gst_tag_list_is_writable (list));
807
808   GST_TAG_LIST_SCOPE (list) = scope;
809 }
810
811 /**
812  * gst_tag_list_get_scope:
813  * @list: a #GstTagList
814  *
815  * Gets the scope of @list.
816  *
817  * Returns: The scope of @list
818  */
819 GstTagScope
820 gst_tag_list_get_scope (const GstTagList * list)
821 {
822   g_return_val_if_fail (GST_IS_TAG_LIST (list), GST_TAG_SCOPE_STREAM);
823
824   return GST_TAG_LIST_SCOPE (list);
825 }
826
827 /**
828  * gst_tag_list_to_string:
829  * @list: a #GstTagList
830  *
831  * Serializes a tag list to a string.
832  *
833  * Returns: a newly-allocated string, or NULL in case of an error. The
834  *    string must be freed with g_free() when no longer needed.
835  */
836 gchar *
837 gst_tag_list_to_string (const GstTagList * list)
838 {
839   g_return_val_if_fail (GST_IS_TAG_LIST (list), NULL);
840
841   return gst_structure_to_string (GST_TAG_LIST_STRUCTURE (list));
842 }
843
844 /**
845  * gst_tag_list_new_from_string:
846  * @str: a string created with gst_tag_list_to_string()
847  *
848  * Deserializes a tag list.
849  *
850  * Returns: a new #GstTagList, or NULL in case of an error.
851  */
852 GstTagList *
853 gst_tag_list_new_from_string (const gchar * str)
854 {
855   GstTagList *tag_list;
856
857   g_return_val_if_fail (str != NULL, NULL);
858   g_return_val_if_fail (g_str_has_prefix (str, "taglist"), NULL);
859
860   tag_list = gst_tag_list_new_internal (gst_structure_from_string (str, NULL));
861   return tag_list;
862 }
863
864 /**
865  * gst_tag_list_n_tags:
866  * @list: A #GstTagList.
867  *
868  * Get the number of tags in @list.
869  *
870  * Returns: The number of tags in @list.
871  */
872 gint
873 gst_tag_list_n_tags (const GstTagList * list)
874 {
875   g_return_val_if_fail (list != NULL, 0);
876   g_return_val_if_fail (GST_IS_TAG_LIST (list), 0);
877
878   return gst_structure_n_fields (GST_TAG_LIST_STRUCTURE (list));
879 }
880
881 /**
882  * gst_tag_list_nth_tag_name:
883  * @list: A #GstTagList.
884  * @index: the index
885  *
886  * Get the name of the tag in @list at @index.
887  *
888  * Returns: The name of the tag at @index.
889  */
890 const gchar *
891 gst_tag_list_nth_tag_name (const GstTagList * list, guint index)
892 {
893   g_return_val_if_fail (list != NULL, 0);
894   g_return_val_if_fail (GST_IS_TAG_LIST (list), 0);
895
896   return gst_structure_nth_field_name (GST_TAG_LIST_STRUCTURE (list), index);
897 }
898
899 /**
900  * gst_tag_list_is_empty:
901  * @list: A #GstTagList.
902  *
903  * Checks if the given taglist is empty.
904  *
905  * Returns: TRUE if the taglist is empty, otherwise FALSE.
906  */
907 gboolean
908 gst_tag_list_is_empty (const GstTagList * list)
909 {
910   g_return_val_if_fail (list != NULL, FALSE);
911   g_return_val_if_fail (GST_IS_TAG_LIST (list), FALSE);
912
913   return (gst_structure_n_fields (GST_TAG_LIST_STRUCTURE (list)) == 0);
914 }
915
916 static gboolean
917 gst_tag_list_fields_equal (const GValue * value1, const GValue * value2)
918 {
919   gdouble d1, d2;
920
921   if (gst_value_compare (value1, value2) == GST_VALUE_EQUAL)
922     return TRUE;
923
924   /* fields not equal: add some tolerance for doubles, otherwise bail out */
925   if (!G_VALUE_HOLDS_DOUBLE (value1) || !G_VALUE_HOLDS_DOUBLE (value2))
926     return FALSE;
927
928   d1 = g_value_get_double (value1);
929   d2 = g_value_get_double (value2);
930
931   /* This will only work for 'normal' values and values around 0,
932    * which should be good enough for our purposes here
933    * FIXME: maybe add this to gst_value_compare_double() ? */
934   return (fabs (d1 - d2) < 0.0000001);
935 }
936
937 /**
938  * gst_tag_list_is_equal:
939  * @list1: a #GstTagList.
940  * @list2: a #GstTagList.
941  *
942  * Checks if the two given taglists are equal.
943  *
944  * Returns: TRUE if the taglists are equal, otherwise FALSE
945  */
946 gboolean
947 gst_tag_list_is_equal (const GstTagList * list1, const GstTagList * list2)
948 {
949   const GstStructure *s1, *s2;
950   gint num_fields1, num_fields2, i;
951
952   g_return_val_if_fail (GST_IS_TAG_LIST (list1), FALSE);
953   g_return_val_if_fail (GST_IS_TAG_LIST (list2), FALSE);
954
955   /* we don't just use gst_structure_is_equal() here so we can add some
956    * tolerance for doubles, though maybe we should just add that to
957    * gst_value_compare_double() as well? */
958   s1 = GST_TAG_LIST_STRUCTURE (list1);
959   s2 = GST_TAG_LIST_STRUCTURE (list2);
960
961   num_fields1 = gst_structure_n_fields (s1);
962   num_fields2 = gst_structure_n_fields (s2);
963
964   if (num_fields1 != num_fields2)
965     return FALSE;
966
967   for (i = 0; i < num_fields1; i++) {
968     const GValue *value1, *value2;
969     const gchar *tag_name;
970
971     tag_name = gst_structure_nth_field_name (s1, i);
972     value1 = gst_structure_get_value (s1, tag_name);
973     value2 = gst_structure_get_value (s2, tag_name);
974
975     if (value2 == NULL)
976       return FALSE;
977
978     if (!gst_tag_list_fields_equal (value1, value2))
979       return FALSE;
980   }
981
982   return TRUE;
983 }
984
985 typedef struct
986 {
987   GstTagList *list;
988   GstTagMergeMode mode;
989 }
990 GstTagCopyData;
991
992 static void
993 gst_tag_list_add_value_internal (GstTagList * tag_list, GstTagMergeMode mode,
994     const gchar * tag, const GValue * value, GstTagInfo * info)
995 {
996   GstStructure *list = GST_TAG_LIST_STRUCTURE (tag_list);
997   const GValue *value2;
998   GQuark tag_quark;
999
1000   if (info == NULL) {
1001     info = gst_tag_lookup (tag);
1002     if (G_UNLIKELY (info == NULL)) {
1003       g_warning ("unknown tag '%s'", tag);
1004       return;
1005     }
1006   }
1007
1008   if (G_UNLIKELY (!G_VALUE_HOLDS (value, info->type) &&
1009           !GST_VALUE_HOLDS_LIST (value))) {
1010     g_warning ("tag '%s' should hold value of type '%s', but value of "
1011         "type '%s' passed", info->nick, g_type_name (info->type),
1012         g_type_name (G_VALUE_TYPE (value)));
1013     return;
1014   }
1015
1016   tag_quark = info->name_quark;
1017
1018   if (info->merge_func
1019       && (value2 = gst_structure_id_get_value (list, tag_quark)) != NULL) {
1020     GValue dest = { 0, };
1021
1022     switch (mode) {
1023       case GST_TAG_MERGE_REPLACE_ALL:
1024       case GST_TAG_MERGE_REPLACE:
1025         gst_structure_id_set_value (list, tag_quark, value);
1026         break;
1027       case GST_TAG_MERGE_PREPEND:
1028         gst_value_list_merge (&dest, value, value2);
1029         gst_structure_id_take_value (list, tag_quark, &dest);
1030         break;
1031       case GST_TAG_MERGE_APPEND:
1032         gst_value_list_merge (&dest, value2, value);
1033         gst_structure_id_take_value (list, tag_quark, &dest);
1034         break;
1035       case GST_TAG_MERGE_KEEP:
1036       case GST_TAG_MERGE_KEEP_ALL:
1037         break;
1038       default:
1039         g_assert_not_reached ();
1040         break;
1041     }
1042   } else {
1043     switch (mode) {
1044       case GST_TAG_MERGE_APPEND:
1045       case GST_TAG_MERGE_KEEP:
1046         if (gst_structure_id_get_value (list, tag_quark) != NULL)
1047           break;
1048         /* fall through */
1049       case GST_TAG_MERGE_REPLACE_ALL:
1050       case GST_TAG_MERGE_REPLACE:
1051       case GST_TAG_MERGE_PREPEND:
1052         gst_structure_id_set_value (list, tag_quark, value);
1053         break;
1054       case GST_TAG_MERGE_KEEP_ALL:
1055         break;
1056       default:
1057         g_assert_not_reached ();
1058         break;
1059     }
1060   }
1061 }
1062
1063 static gboolean
1064 gst_tag_list_copy_foreach (GQuark tag_quark, const GValue * value,
1065     gpointer user_data)
1066 {
1067   GstTagCopyData *copy = (GstTagCopyData *) user_data;
1068   const gchar *tag;
1069
1070   tag = g_quark_to_string (tag_quark);
1071   gst_tag_list_add_value_internal (copy->list, copy->mode, tag, value, NULL);
1072
1073   return TRUE;
1074 }
1075
1076 /**
1077  * gst_tag_list_insert:
1078  * @into: list to merge into
1079  * @from: list to merge from
1080  * @mode: the mode to use
1081  *
1082  * Inserts the tags of the @from list into the first list using the given mode.
1083  */
1084 void
1085 gst_tag_list_insert (GstTagList * into, const GstTagList * from,
1086     GstTagMergeMode mode)
1087 {
1088   GstTagCopyData data;
1089
1090   g_return_if_fail (GST_IS_TAG_LIST (into));
1091   g_return_if_fail (gst_tag_list_is_writable (into));
1092   g_return_if_fail (GST_IS_TAG_LIST (from));
1093   g_return_if_fail (GST_TAG_MODE_IS_VALID (mode));
1094
1095   data.list = into;
1096   data.mode = mode;
1097   if (mode == GST_TAG_MERGE_REPLACE_ALL) {
1098     gst_structure_remove_all_fields (GST_TAG_LIST_STRUCTURE (into));
1099   }
1100   gst_structure_foreach (GST_TAG_LIST_STRUCTURE (from),
1101       gst_tag_list_copy_foreach, &data);
1102 }
1103
1104 /**
1105  * gst_tag_list_merge:
1106  * @list1: first list to merge
1107  * @list2: second list to merge
1108  * @mode: the mode to use
1109  *
1110  * Merges the two given lists into a new list. If one of the lists is NULL, a
1111  * copy of the other is returned. If both lists are NULL, NULL is returned.
1112  *
1113  * Free-function: gst_tag_list_unref
1114  *
1115  * Returns: (transfer full): the new list
1116  */
1117 GstTagList *
1118 gst_tag_list_merge (const GstTagList * list1, const GstTagList * list2,
1119     GstTagMergeMode mode)
1120 {
1121   GstTagList *list1_cp;
1122   const GstTagList *list2_cp;
1123
1124   g_return_val_if_fail (list1 == NULL || GST_IS_TAG_LIST (list1), NULL);
1125   g_return_val_if_fail (list2 == NULL || GST_IS_TAG_LIST (list2), NULL);
1126   g_return_val_if_fail (GST_TAG_MODE_IS_VALID (mode), NULL);
1127
1128   /* nothing to merge */
1129   if (!list1 && !list2) {
1130     return NULL;
1131   }
1132
1133   /* create empty list, we need to do this to correctly handling merge modes */
1134   list1_cp = (list1) ? gst_tag_list_copy (list1) : gst_tag_list_new_empty ();
1135   list2_cp = (list2) ? list2 : gst_tag_list_new_empty ();
1136
1137   gst_tag_list_insert (list1_cp, list2_cp, mode);
1138
1139   if (!list2)
1140     gst_tag_list_unref ((GstTagList *) list2_cp);
1141
1142   return list1_cp;
1143 }
1144
1145 /**
1146  * gst_tag_list_get_tag_size:
1147  * @list: a taglist
1148  * @tag: the tag to query
1149  *
1150  * Checks how many value are stored in this tag list for the given tag.
1151  *
1152  * Returns: The number of tags stored
1153  */
1154 guint
1155 gst_tag_list_get_tag_size (const GstTagList * list, const gchar * tag)
1156 {
1157   const GValue *value;
1158
1159   g_return_val_if_fail (GST_IS_TAG_LIST (list), 0);
1160
1161   value = gst_structure_get_value (GST_TAG_LIST_STRUCTURE (list), tag);
1162   if (value == NULL)
1163     return 0;
1164   if (G_VALUE_TYPE (value) != GST_TYPE_LIST)
1165     return 1;
1166
1167   return gst_value_list_get_size (value);
1168 }
1169
1170 /**
1171  * gst_tag_list_add:
1172  * @list: list to set tags in
1173  * @mode: the mode to use
1174  * @tag: tag
1175  * @...: NULL-terminated list of values to set
1176  *
1177  * Sets the values for the given tags using the specified mode.
1178  */
1179 void
1180 gst_tag_list_add (GstTagList * list, GstTagMergeMode mode, const gchar * tag,
1181     ...)
1182 {
1183   va_list args;
1184
1185   g_return_if_fail (GST_IS_TAG_LIST (list));
1186   g_return_if_fail (gst_tag_list_is_writable (list));
1187   g_return_if_fail (GST_TAG_MODE_IS_VALID (mode));
1188   g_return_if_fail (tag != NULL);
1189
1190   va_start (args, tag);
1191   gst_tag_list_add_valist (list, mode, tag, args);
1192   va_end (args);
1193 }
1194
1195 /**
1196  * gst_tag_list_add_values:
1197  * @list: list to set tags in
1198  * @mode: the mode to use
1199  * @tag: tag
1200  * @...: GValues to set
1201  *
1202  * Sets the GValues for the given tags using the specified mode.
1203  */
1204 void
1205 gst_tag_list_add_values (GstTagList * list, GstTagMergeMode mode,
1206     const gchar * tag, ...)
1207 {
1208   va_list args;
1209
1210   g_return_if_fail (GST_IS_TAG_LIST (list));
1211   g_return_if_fail (gst_tag_list_is_writable (list));
1212   g_return_if_fail (GST_TAG_MODE_IS_VALID (mode));
1213   g_return_if_fail (tag != NULL);
1214
1215   va_start (args, tag);
1216   gst_tag_list_add_valist_values (list, mode, tag, args);
1217   va_end (args);
1218 }
1219
1220 /**
1221  * gst_tag_list_add_valist:
1222  * @list: list to set tags in
1223  * @mode: the mode to use
1224  * @tag: tag
1225  * @var_args: tag / value pairs to set
1226  *
1227  * Sets the values for the given tags using the specified mode.
1228  */
1229 void
1230 gst_tag_list_add_valist (GstTagList * list, GstTagMergeMode mode,
1231     const gchar * tag, va_list var_args)
1232 {
1233   GstTagInfo *info;
1234   gchar *error = NULL;
1235
1236   g_return_if_fail (GST_IS_TAG_LIST (list));
1237   g_return_if_fail (gst_tag_list_is_writable (list));
1238   g_return_if_fail (GST_TAG_MODE_IS_VALID (mode));
1239   g_return_if_fail (tag != NULL);
1240
1241   if (mode == GST_TAG_MERGE_REPLACE_ALL) {
1242     gst_structure_remove_all_fields (GST_TAG_LIST_STRUCTURE (list));
1243   }
1244
1245   while (tag != NULL) {
1246     GValue value = { 0, };
1247
1248     info = gst_tag_lookup (tag);
1249     if (G_UNLIKELY (info == NULL)) {
1250       g_warning ("unknown tag '%s'", tag);
1251       return;
1252     }
1253     G_VALUE_COLLECT_INIT (&value, info->type, var_args, 0, &error);
1254     if (error) {
1255       g_warning ("%s: %s", G_STRLOC, error);
1256       g_free (error);
1257       /* we purposely leak the value here, it might not be
1258        * in a sane state if an error condition occoured
1259        */
1260       return;
1261     }
1262     /* Facilitate GstBuffer -> GstSample transition */
1263     if (G_UNLIKELY (info->type == GST_TYPE_SAMPLE &&
1264             !GST_IS_SAMPLE (value.data[0].v_pointer))) {
1265       g_warning ("Expected GstSample argument for tag '%s'", tag);
1266     } else {
1267       gst_tag_list_add_value_internal (list, mode, tag, &value, info);
1268     }
1269     g_value_unset (&value);
1270     tag = va_arg (var_args, gchar *);
1271   }
1272 }
1273
1274 /**
1275  * gst_tag_list_add_valist_values:
1276  * @list: list to set tags in
1277  * @mode: the mode to use
1278  * @tag: tag
1279  * @var_args: tag / GValue pairs to set
1280  *
1281  * Sets the GValues for the given tags using the specified mode.
1282  */
1283 void
1284 gst_tag_list_add_valist_values (GstTagList * list, GstTagMergeMode mode,
1285     const gchar * tag, va_list var_args)
1286 {
1287   g_return_if_fail (GST_IS_TAG_LIST (list));
1288   g_return_if_fail (gst_tag_list_is_writable (list));
1289   g_return_if_fail (GST_TAG_MODE_IS_VALID (mode));
1290   g_return_if_fail (tag != NULL);
1291
1292   if (mode == GST_TAG_MERGE_REPLACE_ALL) {
1293     gst_structure_remove_all_fields (GST_TAG_LIST_STRUCTURE (list));
1294   }
1295
1296   while (tag != NULL) {
1297     GstTagInfo *info;
1298
1299     info = gst_tag_lookup (tag);
1300     if (G_UNLIKELY (info == NULL)) {
1301       g_warning ("unknown tag '%s'", tag);
1302       return;
1303     }
1304     gst_tag_list_add_value_internal (list, mode, tag, va_arg (var_args,
1305             GValue *), info);
1306     tag = va_arg (var_args, gchar *);
1307   }
1308 }
1309
1310 /**
1311  * gst_tag_list_add_value:
1312  * @list: list to set tags in
1313  * @mode: the mode to use
1314  * @tag: tag
1315  * @value: GValue for this tag
1316  *
1317  * Sets the GValue for a given tag using the specified mode.
1318  */
1319 void
1320 gst_tag_list_add_value (GstTagList * list, GstTagMergeMode mode,
1321     const gchar * tag, const GValue * value)
1322 {
1323   g_return_if_fail (GST_IS_TAG_LIST (list));
1324   g_return_if_fail (gst_tag_list_is_writable (list));
1325   g_return_if_fail (GST_TAG_MODE_IS_VALID (mode));
1326   g_return_if_fail (tag != NULL);
1327
1328   gst_tag_list_add_value_internal (list, mode, tag, value, NULL);
1329 }
1330
1331 /**
1332  * gst_tag_list_remove_tag:
1333  * @list: list to remove tag from
1334  * @tag: tag to remove
1335  *
1336  * Removes the given tag from the taglist.
1337  */
1338 void
1339 gst_tag_list_remove_tag (GstTagList * list, const gchar * tag)
1340 {
1341   g_return_if_fail (GST_IS_TAG_LIST (list));
1342   g_return_if_fail (tag != NULL);
1343
1344   gst_structure_remove_field (GST_TAG_LIST_STRUCTURE (list), tag);
1345 }
1346
1347 typedef struct
1348 {
1349   GstTagForeachFunc func;
1350   const GstTagList *tag_list;
1351   gpointer data;
1352 }
1353 TagForeachData;
1354
1355 static int
1356 structure_foreach_wrapper (GQuark field_id, const GValue * value,
1357     gpointer user_data)
1358 {
1359   TagForeachData *data = (TagForeachData *) user_data;
1360
1361   data->func (data->tag_list, g_quark_to_string (field_id), data->data);
1362   return TRUE;
1363 }
1364
1365 /**
1366  * gst_tag_list_foreach:
1367  * @list: list to iterate over
1368  * @func: (scope call): function to be called for each tag
1369  * @user_data: (closure): user specified data
1370  *
1371  * Calls the given function for each tag inside the tag list. Note that if there
1372  * is no tag, the function won't be called at all.
1373  */
1374 void
1375 gst_tag_list_foreach (const GstTagList * list, GstTagForeachFunc func,
1376     gpointer user_data)
1377 {
1378   TagForeachData data;
1379
1380   g_return_if_fail (GST_IS_TAG_LIST (list));
1381   g_return_if_fail (func != NULL);
1382
1383   data.func = func;
1384   data.tag_list = list;
1385   data.data = user_data;
1386   gst_structure_foreach (GST_TAG_LIST_STRUCTURE (list),
1387       structure_foreach_wrapper, &data);
1388 }
1389
1390 /**
1391  * gst_tag_list_get_value_index:
1392  * @list: a #GstTagList
1393  * @tag: tag to read out
1394  * @index: number of entry to read out
1395  *
1396  * Gets the value that is at the given index for the given tag in the given
1397  * list.
1398  *
1399  * Returns: (transfer none): The GValue for the specified entry or NULL if the
1400  *          tag wasn't available or the tag doesn't have as many entries
1401  */
1402 const GValue *
1403 gst_tag_list_get_value_index (const GstTagList * list, const gchar * tag,
1404     guint index)
1405 {
1406   const GValue *value;
1407
1408   g_return_val_if_fail (GST_IS_TAG_LIST (list), NULL);
1409   g_return_val_if_fail (tag != NULL, NULL);
1410
1411   value = gst_structure_get_value (GST_TAG_LIST_STRUCTURE (list), tag);
1412   if (value == NULL)
1413     return NULL;
1414
1415   if (GST_VALUE_HOLDS_LIST (value)) {
1416     if (index >= gst_value_list_get_size (value))
1417       return NULL;
1418     return gst_value_list_get_value (value, index);
1419   } else {
1420     if (index > 0)
1421       return NULL;
1422     return value;
1423   }
1424 }
1425
1426 /**
1427  * gst_tag_list_copy_value:
1428  * @dest: (out caller-allocates): uninitialized #GValue to copy into
1429  * @list: list to get the tag from
1430  * @tag: tag to read out
1431  *
1432  * Copies the contents for the given tag into the value,
1433  * merging multiple values into one if multiple values are associated
1434  * with the tag.
1435  * You must g_value_unset() the value after use.
1436  *
1437  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1438  *          given list.
1439  */
1440 gboolean
1441 gst_tag_list_copy_value (GValue * dest, const GstTagList * list,
1442     const gchar * tag)
1443 {
1444   const GValue *src;
1445
1446   g_return_val_if_fail (GST_IS_TAG_LIST (list), FALSE);
1447   g_return_val_if_fail (tag != NULL, FALSE);
1448   g_return_val_if_fail (dest != NULL, FALSE);
1449   g_return_val_if_fail (G_VALUE_TYPE (dest) == 0, FALSE);
1450
1451   src = gst_structure_get_value (GST_TAG_LIST_STRUCTURE (list), tag);
1452   if (!src)
1453     return FALSE;
1454
1455   if (G_VALUE_TYPE (src) == GST_TYPE_LIST) {
1456     GstTagInfo *info = gst_tag_lookup (tag);
1457
1458     if (!info)
1459       return FALSE;
1460
1461     /* must be there or lists aren't allowed */
1462     g_assert (info->merge_func);
1463     info->merge_func (dest, src);
1464   } else {
1465     g_value_init (dest, G_VALUE_TYPE (src));
1466     g_value_copy (src, dest);
1467   }
1468   return TRUE;
1469 }
1470
1471 /* FIXME 0.11: this whole merge function business is overdesigned, and the
1472  * _get_foo() API is misleading as well - how many application developers will
1473  * expect gst_tag_list_get_string (list, GST_TAG_ARTIST, &val) might return a
1474  * string with multiple comma-separated artists? _get_foo() should just be
1475  * a convenience wrapper around _get_foo_index (list, tag, 0, &val),
1476  * supplemented by a special _tag_list_get_string_merged() function if needed
1477  * (unless someone can actually think of real use cases where the merge
1478  * function is not 'use first' for non-strings and merge for strings) */
1479
1480 /***** evil macros to get all the gst_tag_list_get_*() functions right *****/
1481
1482 #define TAG_MERGE_FUNCS(name,type,ret)                                  \
1483 gboolean                                                                \
1484 gst_tag_list_get_ ## name (const GstTagList *list, const gchar *tag,    \
1485                            type *value)                                 \
1486 {                                                                       \
1487   GValue v = { 0, };                                                    \
1488                                                                         \
1489   g_return_val_if_fail (GST_IS_TAG_LIST (list), FALSE);                 \
1490   g_return_val_if_fail (tag != NULL, FALSE);                            \
1491   g_return_val_if_fail (value != NULL, FALSE);                          \
1492                                                                         \
1493   if (!gst_tag_list_copy_value (&v, list, tag))                         \
1494       return FALSE;                                                     \
1495   *value = COPY_FUNC (g_value_get_ ## name (&v));                       \
1496   g_value_unset (&v);                                                   \
1497   return ret;                                                           \
1498 }                                                                       \
1499                                                                         \
1500 gboolean                                                                \
1501 gst_tag_list_get_ ## name ## _index (const GstTagList *list,            \
1502                                      const gchar *tag,                  \
1503                                      guint index, type *value)          \
1504 {                                                                       \
1505   const GValue *v;                                                      \
1506                                                                         \
1507   g_return_val_if_fail (GST_IS_TAG_LIST (list), FALSE);                 \
1508   g_return_val_if_fail (tag != NULL, FALSE);                            \
1509   g_return_val_if_fail (value != NULL, FALSE);                          \
1510                                                                         \
1511   if ((v = gst_tag_list_get_value_index (list, tag, index)) == NULL)    \
1512       return FALSE;                                                     \
1513   *value = COPY_FUNC (g_value_get_ ## name (v));                        \
1514   return ret;                                                           \
1515 }
1516
1517 #define COPY_FUNC /**/
1518 /**
1519  * gst_tag_list_get_boolean:
1520  * @list: a #GstTagList to get the tag from
1521  * @tag: tag to read out
1522  * @value: (out): location for the result
1523  *
1524  * Copies the contents for the given tag into the value, merging multiple values
1525  * into one if multiple values are associated with the tag.
1526  *
1527  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1528  *              given list.
1529  */
1530 /**
1531  * gst_tag_list_get_boolean_index:
1532  * @list: a #GstTagList to get the tag from
1533  * @tag: tag to read out
1534  * @index: number of entry to read out
1535  * @value: (out): location for the result
1536  *
1537  * Gets the value that is at the given index for the given tag in the given
1538  * list.
1539  *
1540  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1541  *              given list.
1542  */
1543 TAG_MERGE_FUNCS (boolean, gboolean, TRUE);
1544 /**
1545  * gst_tag_list_get_int:
1546  * @list: a #GstTagList to get the tag from
1547  * @tag: tag to read out
1548  * @value: (out): location for the result
1549  *
1550  * Copies the contents for the given tag into the value, merging multiple values
1551  * into one if multiple values are associated with the tag.
1552  *
1553  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1554  *              given list.
1555  */
1556 /**
1557  * gst_tag_list_get_int_index:
1558  * @list: a #GstTagList to get the tag from
1559  * @tag: tag to read out
1560  * @index: number of entry to read out
1561  * @value: (out): location for the result
1562  *
1563  * Gets the value that is at the given index for the given tag in the given
1564  * list.
1565  *
1566  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1567  *              given list.
1568  */
1569 TAG_MERGE_FUNCS (int, gint, TRUE);
1570 /**
1571  * gst_tag_list_get_uint:
1572  * @list: a #GstTagList to get the tag from
1573  * @tag: tag to read out
1574  * @value: (out): location for the result
1575  *
1576  * Copies the contents for the given tag into the value, merging multiple values
1577  * into one if multiple values are associated with the tag.
1578  *
1579  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1580  *              given list.
1581  */
1582 /**
1583  * gst_tag_list_get_uint_index:
1584  * @list: a #GstTagList to get the tag from
1585  * @tag: tag to read out
1586  * @index: number of entry to read out
1587  * @value: (out): location for the result
1588  *
1589  * Gets the value that is at the given index for the given tag in the given
1590  * list.
1591  *
1592  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1593  *              given list.
1594  */
1595 TAG_MERGE_FUNCS (uint, guint, TRUE);
1596 /**
1597  * gst_tag_list_get_int64_index:
1598  * @list: a #GstTagList to get the tag from
1599  * @tag: tag to read out
1600  * @index: number of entry to read out
1601  * @value: (out): location for the result
1602  *
1603  * Gets the value that is at the given index for the given tag in the given
1604  * list.
1605  *
1606  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1607  *              given list.
1608  */
1609 TAG_MERGE_FUNCS (int64, gint64, TRUE);
1610 /**
1611  * gst_tag_list_get_uint64:
1612  * @list: a #GstTagList to get the tag from
1613  * @tag: tag to read out
1614  * @value: (out): location for the result
1615  *
1616  * Copies the contents for the given tag into the value, merging multiple values
1617  * into one if multiple values are associated with the tag.
1618  *
1619  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1620  *              given list.
1621  */
1622 /**
1623  * gst_tag_list_get_uint64_index:
1624  * @list: a #GstTagList to get the tag from
1625  * @tag: tag to read out
1626  * @index: number of entry to read out
1627  * @value: (out): location for the result
1628  *
1629  * Gets the value that is at the given index for the given tag in the given
1630  * list.
1631  *
1632  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1633  *              given list.
1634  */
1635 TAG_MERGE_FUNCS (uint64, guint64, TRUE);
1636 /**
1637  * gst_tag_list_get_float:
1638  * @list: a #GstTagList to get the tag from
1639  * @tag: tag to read out
1640  * @value: (out): location for the result
1641  *
1642  * Copies the contents for the given tag into the value, merging multiple values
1643  * into one if multiple values are associated with the tag.
1644  *
1645  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1646  *              given list.
1647  */
1648 /**
1649  * gst_tag_list_get_float_index:
1650  * @list: a #GstTagList to get the tag from
1651  * @tag: tag to read out
1652  * @index: number of entry to read out
1653  * @value: (out): location for the result
1654  *
1655  * Gets the value that is at the given index for the given tag in the given
1656  * list.
1657  *
1658  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1659  *              given list.
1660  */
1661 TAG_MERGE_FUNCS (float, gfloat, TRUE);
1662 /**
1663  * gst_tag_list_get_double:
1664  * @list: a #GstTagList to get the tag from
1665  * @tag: tag to read out
1666  * @value: (out): location for the result
1667  *
1668  * Copies the contents for the given tag into the value, merging multiple values
1669  * into one if multiple values are associated with the tag.
1670  *
1671  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1672  *              given list.
1673  */
1674 /**
1675  * gst_tag_list_get_double_index:
1676  * @list: a #GstTagList to get the tag from
1677  * @tag: tag to read out
1678  * @index: number of entry to read out
1679  * @value: (out): location for the result
1680  *
1681  * Gets the value that is at the given index for the given tag in the given
1682  * list.
1683  *
1684  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1685  *              given list.
1686  */
1687 TAG_MERGE_FUNCS (double, gdouble, TRUE);
1688 /**
1689  * gst_tag_list_get_pointer:
1690  * @list: a #GstTagList to get the tag from
1691  * @tag: tag to read out
1692  * @value: (out) (transfer none): location for the result
1693  *
1694  * Copies the contents for the given tag into the value, merging multiple values
1695  * into one if multiple values are associated with the tag.
1696  *
1697  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1698  *              given list.
1699  */
1700 /**
1701  * gst_tag_list_get_pointer_index:
1702  * @list: a #GstTagList to get the tag from
1703  * @tag: tag to read out
1704  * @index: number of entry to read out
1705  * @value: (out) (transfer none): location for the result
1706  *
1707  * Gets the value that is at the given index for the given tag in the given
1708  * list.
1709  *
1710  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1711  *              given list.
1712  */
1713 TAG_MERGE_FUNCS (pointer, gpointer, (*value != NULL));
1714
1715 static inline gchar *
1716 _gst_strdup0 (const gchar * s)
1717 {
1718   if (s == NULL || *s == '\0')
1719     return NULL;
1720
1721   return g_strdup (s);
1722 }
1723
1724 #undef COPY_FUNC
1725 #define COPY_FUNC _gst_strdup0
1726
1727 /**
1728  * gst_tag_list_get_string:
1729  * @list: a #GstTagList to get the tag from
1730  * @tag: tag to read out
1731  * @value: (out callee-allocates) (transfer full): location for the result
1732  *
1733  * Copies the contents for the given tag into the value, possibly merging
1734  * multiple values into one if multiple values are associated with the tag.
1735  *
1736  * Use gst_tag_list_get_string_index (list, tag, 0, value) if you want
1737  * to retrieve the first string associated with this tag unmodified.
1738  *
1739  * The resulting string in @value will be in UTF-8 encoding and should be
1740  * freed by the caller using g_free when no longer needed. The
1741  * returned string is also guaranteed to be non-NULL and non-empty.
1742  *
1743  * Free-function: g_free
1744  *
1745  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1746  *              given list.
1747  */
1748 /**
1749  * gst_tag_list_get_string_index:
1750  * @list: a #GstTagList to get the tag from
1751  * @tag: tag to read out
1752  * @index: number of entry to read out
1753  * @value: (out callee-allocates) (transfer full): location for the result
1754  *
1755  * Gets the value that is at the given index for the given tag in the given
1756  * list.
1757  *
1758  * The resulting string in @value will be in UTF-8 encoding and should be
1759  * freed by the caller using g_free when no longer needed. The
1760  * returned string is also guaranteed to be non-NULL and non-empty.
1761  *
1762  * Free-function: g_free
1763  *
1764  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1765  *              given list.
1766  */
1767 TAG_MERGE_FUNCS (string, gchar *, (*value != NULL));
1768
1769 /*
1770  *FIXME 0.11: Instead of _peek (non-copy) and _get (copy), we could have
1771  *            _get (non-copy) and _dup (copy) for strings, seems more
1772  *            widely used
1773  */
1774 /**
1775  * gst_tag_list_peek_string_index:
1776  * @list: a #GstTagList to get the tag from
1777  * @tag: tag to read out
1778  * @index: number of entry to read out
1779  * @value: (out) (transfer none): location for the result
1780  *
1781  * Peeks at the value that is at the given index for the given tag in the given
1782  * list.
1783  *
1784  * The resulting string in @value will be in UTF-8 encoding and doesn't need
1785  * to be freed by the caller. The returned string is also guaranteed to
1786  * be non-NULL and non-empty.
1787  *
1788  * Returns: TRUE, if a value was set, FALSE if the tag didn't exist in the
1789  *              given list.
1790  */
1791 gboolean
1792 gst_tag_list_peek_string_index (const GstTagList * list,
1793     const gchar * tag, guint index, const gchar ** value)
1794 {
1795   const GValue *v;
1796
1797   g_return_val_if_fail (GST_IS_TAG_LIST (list), FALSE);
1798   g_return_val_if_fail (tag != NULL, FALSE);
1799   g_return_val_if_fail (value != NULL, FALSE);
1800
1801   if ((v = gst_tag_list_get_value_index (list, tag, index)) == NULL)
1802     return FALSE;
1803   *value = g_value_get_string (v);
1804   return *value != NULL && **value != '\0';
1805 }
1806
1807 /**
1808  * gst_tag_list_get_date:
1809  * @list: a #GstTagList to get the tag from
1810  * @tag: tag to read out
1811  * @value: (out callee-allocates) (transfer full): address of a GDate pointer
1812  *     variable to store the result into
1813  *
1814  * Copies the first date for the given tag in the taglist into the variable
1815  * pointed to by @value. Free the date with g_date_free() when it is no longer
1816  * needed.
1817  *
1818  * Free-function: g_date_free
1819  *
1820  * Returns: TRUE, if a date was copied, FALSE if the tag didn't exist in the
1821  *              given list or if it was #NULL.
1822  */
1823 gboolean
1824 gst_tag_list_get_date (const GstTagList * list, const gchar * tag,
1825     GDate ** value)
1826 {
1827   GValue v = { 0, };
1828
1829   g_return_val_if_fail (GST_IS_TAG_LIST (list), FALSE);
1830   g_return_val_if_fail (tag != NULL, FALSE);
1831   g_return_val_if_fail (value != NULL, FALSE);
1832
1833   if (!gst_tag_list_copy_value (&v, list, tag))
1834     return FALSE;
1835   *value = (GDate *) g_value_dup_boxed (&v);
1836   g_value_unset (&v);
1837   return (*value != NULL);
1838 }
1839
1840 /**
1841  * gst_tag_list_get_date_index:
1842  * @list: a #GstTagList to get the tag from
1843  * @tag: tag to read out
1844  * @index: number of entry to read out
1845  * @value: (out callee-allocates) (transfer full): location for the result
1846  *
1847  * Gets the date that is at the given index for the given tag in the given
1848  * list and copies it into the variable pointed to by @value. Free the date
1849  * with g_date_free() when it is no longer needed.
1850  *
1851  * Free-function: g_date_free
1852  *
1853  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1854  *              given list or if it was #NULL.
1855  */
1856 gboolean
1857 gst_tag_list_get_date_index (const GstTagList * list,
1858     const gchar * tag, guint index, GDate ** value)
1859 {
1860   const GValue *v;
1861
1862   g_return_val_if_fail (GST_IS_TAG_LIST (list), FALSE);
1863   g_return_val_if_fail (tag != NULL, FALSE);
1864   g_return_val_if_fail (value != NULL, FALSE);
1865
1866   if ((v = gst_tag_list_get_value_index (list, tag, index)) == NULL)
1867     return FALSE;
1868   *value = (GDate *) g_value_dup_boxed (v);
1869   return (*value != NULL);
1870 }
1871
1872 /**
1873  * gst_tag_list_get_date_time:
1874  * @list: a #GstTagList to get the tag from
1875  * @tag: tag to read out
1876  * @value: (out callee-allocates) (transfer full): address of a #GstDateTime
1877  *     pointer variable to store the result into
1878  *
1879  * Copies the first datetime for the given tag in the taglist into the variable
1880  * pointed to by @value. Unref the date with gst_date_time_unref() when
1881  * it is no longer needed.
1882  *
1883  * Free-function: gst_date_time_unref
1884  *
1885  * Returns: TRUE, if a datetime was copied, FALSE if the tag didn't exist in
1886  *              thegiven list or if it was #NULL.
1887  */
1888 gboolean
1889 gst_tag_list_get_date_time (const GstTagList * list, const gchar * tag,
1890     GstDateTime ** value)
1891 {
1892   GValue v = { 0, };
1893
1894   g_return_val_if_fail (GST_IS_TAG_LIST (list), FALSE);
1895   g_return_val_if_fail (tag != NULL, FALSE);
1896   g_return_val_if_fail (value != NULL, FALSE);
1897
1898   if (!gst_tag_list_copy_value (&v, list, tag))
1899     return FALSE;
1900
1901   g_return_val_if_fail (GST_VALUE_HOLDS_DATE_TIME (&v), FALSE);
1902
1903   *value = (GstDateTime *) g_value_dup_boxed (&v);
1904   g_value_unset (&v);
1905   return (*value != NULL);
1906 }
1907
1908 /**
1909  * gst_tag_list_get_date_time_index:
1910  * @list: a #GstTagList to get the tag from
1911  * @tag: tag to read out
1912  * @index: number of entry to read out
1913  * @value: (out callee-allocates) (transfer full): location for the result
1914  *
1915  * Gets the datetime that is at the given index for the given tag in the given
1916  * list and copies it into the variable pointed to by @value. Unref the datetime
1917  * with gst_date_time_unref() when it is no longer needed.
1918  *
1919  * Free-function: gst_date_time_unref
1920  *
1921  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1922  *              given list or if it was #NULL.
1923  */
1924 gboolean
1925 gst_tag_list_get_date_time_index (const GstTagList * list,
1926     const gchar * tag, guint index, GstDateTime ** value)
1927 {
1928   const GValue *v;
1929
1930   g_return_val_if_fail (GST_IS_TAG_LIST (list), FALSE);
1931   g_return_val_if_fail (tag != NULL, FALSE);
1932   g_return_val_if_fail (value != NULL, FALSE);
1933
1934   if ((v = gst_tag_list_get_value_index (list, tag, index)) == NULL)
1935     return FALSE;
1936   *value = (GstDateTime *) g_value_dup_boxed (v);
1937   return (*value != NULL);
1938 }
1939
1940 /**
1941  * gst_tag_list_get_sample:
1942  * @list: a #GstTagList to get the tag from
1943  * @tag: tag to read out
1944  * @sample: (out callee-allocates) (transfer full): address of a GstSample
1945  *     pointer variable to store the result into
1946  *
1947  * Copies the first sample for the given tag in the taglist into the variable
1948  * pointed to by @sample. Free the sample with gst_sample_unref() when it is
1949  * no longer needed. You can retrieve the buffer from the sample using
1950  * gst_sample_get_buffer() and the associated caps (if any) with
1951  * gst_sample_get_caps().
1952  *
1953  * Free-function: gst_sample_unref
1954  *
1955  * Returns: TRUE, if a sample was returned, FALSE if the tag didn't exist in
1956  *              the given list or if it was #NULL.
1957  */
1958 gboolean
1959 gst_tag_list_get_sample (const GstTagList * list, const gchar * tag,
1960     GstSample ** sample)
1961 {
1962   GValue v = { 0, };
1963
1964   g_return_val_if_fail (GST_IS_TAG_LIST (list), FALSE);
1965   g_return_val_if_fail (tag != NULL, FALSE);
1966   g_return_val_if_fail (sample != NULL, FALSE);
1967
1968   if (!gst_tag_list_copy_value (&v, list, tag))
1969     return FALSE;
1970   *sample = g_value_dup_boxed (&v);
1971   g_value_unset (&v);
1972   return (*sample != NULL);
1973 }
1974
1975 /**
1976  * gst_tag_list_get_sample_index:
1977  * @list: a #GstTagList to get the tag from
1978  * @tag: tag to read out
1979  * @index: number of entry to read out
1980  * @sample: (out callee-allocates) (transfer full): address of a GstSample
1981  *     pointer variable to store the result into
1982  *
1983  * Gets the sample that is at the given index for the given tag in the given
1984  * list and copies it into the variable pointed to by @smple. Free the sample
1985  * with gst_sample_unref() when it is no longer needed. You can retrieve the
1986  * buffer from the sample using gst_sample_get_buffer() and the associated
1987  * caps (if any) with gst_sample_get_caps().
1988  *
1989  * Free-function: gst_sample_unref
1990  *
1991  * Returns: TRUE, if a sample was copied, FALSE if the tag didn't exist in the
1992  *              given list or if it was #NULL.
1993  */
1994 gboolean
1995 gst_tag_list_get_sample_index (const GstTagList * list,
1996     const gchar * tag, guint index, GstSample ** sample)
1997 {
1998   const GValue *v;
1999
2000   g_return_val_if_fail (GST_IS_TAG_LIST (list), FALSE);
2001   g_return_val_if_fail (tag != NULL, FALSE);
2002   g_return_val_if_fail (sample != NULL, FALSE);
2003
2004   if ((v = gst_tag_list_get_value_index (list, tag, index)) == NULL)
2005     return FALSE;
2006   *sample = g_value_dup_boxed (v);
2007   return (*sample != NULL);
2008 }