2 * Copyright (C) 2010 Stefan Kost <stefan.kost@nokia.com>
3 * Copyright (C) 2010 Thiago Santos <thiago.sousa.santos@collabora.co.uk>
5 * gstxmptag.c: library for reading / modifying xmp tags
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
25 * @short_description: tag mappings and support functions for plugins
26 * dealing with xmp packets
27 * @see_also: #GstTagList
29 * Contains various utility functions for plugins to parse or create
30 * xmp packets and map them to and from #GstTagList<!-- -->s.
32 * Please note that the xmp parser is very lightweight and not strict at all.
39 #include <gst/gsttagsetter.h>
40 #include "gsttageditingprivate.h"
47 static const gchar *schema_list[] = {
59 * gst_tag_xmp_list_schemas:
61 * Gets the list of supported schemas in the xmp lib
63 * Returns: a %NULL terminated array of strings with the schema names
68 gst_tag_xmp_list_schemas (void)
73 typedef struct _XmpSerializationData XmpSerializationData;
74 typedef struct _XmpTag XmpTag;
77 * Serializes a GValue into a string.
79 typedef gchar *(*XmpSerializationFunc) (const GValue * value);
82 * Deserializes @str that is the gstreamer tag @gst_tag represented in
83 * XMP as the @xmp_tag_value and adds the result to the @taglist.
85 * @pending_tags is passed so that compound xmp tags can search for its
86 * complements on the list and use them. Note that used complements should
87 * be freed and removed from the list.
88 * The list is of PendingXmpTag
90 typedef void (*XmpDeserializationFunc) (XmpTag * xmptag, GstTagList * taglist,
91 const gchar * gst_tag, const gchar * xmp_tag_value,
92 const gchar * str, GSList ** pending_tags);
94 struct _XmpSerializationData
97 const gchar **schemas;
101 xmp_serialization_data_use_schema (XmpSerializationData * serdata,
102 const gchar * schemaname)
105 if (serdata->schemas == NULL)
108 while (serdata->schemas[i] != NULL) {
109 if (strcmp (serdata->schemas[i], schemaname) == 0)
118 GstXmpTagTypeNone = 0,
124 /* Not really a xmp type, this is a tag that in gst is represented with
125 * a single value and on xmp it needs 2 (or more) simple values
127 * e.g. GST_TAG_GEO_LOCATION_ELEVATION needs to be mapped into 2 complementary
128 * tags in the exif's schema. One of them stores the absolute elevation,
129 * and the other one stores if it is above of below sea level.
131 GstXmpTagTypeCompound
136 const gchar *gst_tag;
137 const gchar *tag_name;
140 /* some tags must be inside a Bag even
141 * if they are a single entry. Set it here so we know */
142 GstXmpTagType supertype;
144 /* For tags that need a rdf:parseType attribute */
145 const gchar *parse_type;
147 /* Used for struct and compound types */
150 XmpSerializationFunc serialize;
151 XmpDeserializationFunc deserialize;
154 static GstTagMergeMode
155 xmp_tag_get_merge_mode (XmpTag * xmptag)
157 switch (xmptag->type) {
158 case GstXmpTagTypeBag:
159 case GstXmpTagTypeSeq:
160 return GST_TAG_MERGE_APPEND;
161 case GstXmpTagTypeSimple:
163 return GST_TAG_MERGE_KEEP;
168 xmp_tag_type_get_name (GstXmpTagType tagtype)
171 case GstXmpTagTypeSeq:
173 case GstXmpTagTypeBag:
176 g_assert_not_reached ();
178 return NULL; /* make compiler happy with -DG_DISABLE_ASSERT */
181 struct _PendingXmpTag
186 typedef struct _PendingXmpTag PendingXmpTag;
189 * A schema is a mapping of strings (the tag name in gstreamer) to a list of
190 * tags in xmp (XmpTag).
192 typedef GHashTable GstXmpSchema;
193 #define gst_xmp_schema_lookup g_hash_table_lookup
194 #define gst_xmp_schema_insert g_hash_table_insert
195 static GstXmpSchema *
196 gst_xmp_schema_new ()
198 return g_hash_table_new (g_direct_hash, g_direct_equal);
202 * Mappings from schema names into the schema group of tags (GstXmpSchema)
204 static GHashTable *__xmp_schemas;
206 static GstXmpSchema *
207 _gst_xmp_get_schema (const gchar * name)
210 GstXmpSchema *schema;
212 key = g_quark_from_string (name);
214 schema = g_hash_table_lookup (__xmp_schemas, GUINT_TO_POINTER (key));
216 GST_WARNING ("Schema %s doesn't exist", name);
222 _gst_xmp_add_schema (const gchar * name, GstXmpSchema * schema)
226 key = g_quark_from_string (name);
228 if (g_hash_table_lookup (__xmp_schemas, GUINT_TO_POINTER (key))) {
229 GST_WARNING ("Schema %s already exists, ignoring", name);
230 g_assert_not_reached ();
234 g_hash_table_insert (__xmp_schemas, GUINT_TO_POINTER (key), schema);
238 _gst_xmp_schema_add_mapping (GstXmpSchema * schema, XmpTag * tag)
242 key = g_quark_from_string (tag->gst_tag);
244 if (gst_xmp_schema_lookup (schema, GUINT_TO_POINTER (key))) {
245 GST_WARNING ("Tag %s already present for the schema", tag->gst_tag);
246 g_assert_not_reached ();
249 gst_xmp_schema_insert (schema, GUINT_TO_POINTER (key), tag);
253 gst_xmp_tag_create (const gchar * gst_tag, const gchar * xmp_tag,
254 gint xmp_type, XmpSerializationFunc serialization_func,
255 XmpDeserializationFunc deserialization_func)
259 xmpinfo = g_slice_new (XmpTag);
260 xmpinfo->gst_tag = gst_tag;
261 xmpinfo->tag_name = xmp_tag;
262 xmpinfo->type = xmp_type;
263 xmpinfo->supertype = GstXmpTagTypeNone;
264 xmpinfo->parse_type = NULL;
265 xmpinfo->serialize = serialization_func;
266 xmpinfo->deserialize = deserialization_func;
267 xmpinfo->children = NULL;
273 gst_xmp_tag_create_compound (const gchar * gst_tag, const gchar * xmp_tag_a,
274 const gchar * xmp_tag_b, XmpSerializationFunc serialization_func_a,
275 XmpSerializationFunc serialization_func_b,
276 XmpDeserializationFunc deserialization_func)
280 gst_xmp_tag_create (gst_tag, xmp_tag_a, GstXmpTagTypeSimple,
281 serialization_func_a, deserialization_func);
283 gst_xmp_tag_create (gst_tag, xmp_tag_b, GstXmpTagTypeSimple,
284 serialization_func_b, deserialization_func);
287 gst_xmp_tag_create (gst_tag, NULL, GstXmpTagTypeCompound, NULL, NULL);
289 xmptag->children = g_slist_prepend (xmptag->children, xmptag_b);
290 xmptag->children = g_slist_prepend (xmptag->children, xmptag_a);
296 _gst_xmp_schema_add_simple_mapping (GstXmpSchema * schema,
297 const gchar * gst_tag, const gchar * xmp_tag, gint xmp_type,
298 XmpSerializationFunc serialization_func,
299 XmpDeserializationFunc deserialization_func)
301 _gst_xmp_schema_add_mapping (schema,
302 gst_xmp_tag_create (gst_tag, xmp_tag, xmp_type, serialization_func,
303 deserialization_func));
307 * We do not return a copy here because elements are
308 * appended, and the API is not public, so we shouldn't
309 * have our lists modified during usage
313 _xmp_tag_get_mapping (const gchar * gst_tag, XmpSerializationData * serdata)
315 GPtrArray *ret = NULL;
317 GQuark key = g_quark_from_string (gst_tag);
318 gpointer iterkey, value;
319 const gchar *schemaname;
321 g_hash_table_iter_init (&iter, __xmp_schemas);
322 while (!ret && g_hash_table_iter_next (&iter, &iterkey, &value)) {
323 GstXmpSchema *schema = (GstXmpSchema *) value;
325 schemaname = g_quark_to_string (GPOINTER_TO_UINT (iterkey));
326 if (xmp_serialization_data_use_schema (serdata, schemaname))
328 (GPtrArray *) gst_xmp_schema_lookup (schema, GUINT_TO_POINTER (key));
334 /* finds the gst tag that maps to this xmp tag in this schema */
336 _gst_xmp_schema_get_mapping_reverse (GstXmpSchema * schema,
337 const gchar * xmp_tag, XmpTag ** _xmp_tag)
341 const gchar *ret = NULL;
343 /* Iterate over the hashtable */
344 g_hash_table_iter_init (&iter, schema);
345 while (!ret && g_hash_table_iter_next (&iter, &key, &value)) {
346 XmpTag *xmpinfo = (XmpTag *) value;
348 if (xmpinfo->tag_name) {
349 if (strcmp (xmpinfo->tag_name, xmp_tag) == 0) {
351 ret = g_quark_to_string (GPOINTER_TO_UINT (key));
354 } else if (xmpinfo->children) {
356 for (iter = xmpinfo->children; iter; iter = g_slist_next (iter)) {
357 XmpTag *child = iter->data;
358 if (strcmp (child->tag_name, xmp_tag) == 0) {
360 ret = g_quark_to_string (GPOINTER_TO_UINT (key));
365 g_assert_not_reached ();
373 /* finds the gst tag that maps to this xmp tag (searches on all schemas) */
375 _gst_xmp_tag_get_mapping_reverse (const gchar * xmp_tag, XmpTag ** _xmp_tag)
379 const gchar *ret = NULL;
381 /* Iterate over the hashtable */
382 g_hash_table_iter_init (&iter, __xmp_schemas);
383 while (!ret && g_hash_table_iter_next (&iter, &key, &value)) {
384 ret = _gst_xmp_schema_get_mapping_reverse ((GstXmpSchema *) value, xmp_tag,
390 /* utility functions/macros */
392 #define METERS_PER_SECOND_TO_KILOMETERS_PER_HOUR (3.6)
393 #define KILOMETERS_PER_HOUR_TO_METERS_PER_SECOND (1/3.6)
394 #define MILES_PER_HOUR_TO_METERS_PER_SECOND (0.44704)
395 #define KNOTS_TO_METERS_PER_SECOND (0.514444)
398 double_to_fraction_string (gdouble num)
403 gst_util_double_to_fraction (num, &frac_n, &frac_d);
404 return g_strdup_printf ("%d/%d", frac_n, frac_d);
407 /* (de)serialize functions */
409 serialize_exif_gps_coordinate (const GValue * value, gchar pos, gchar neg)
414 gchar fraction[G_ASCII_DTOSTR_BUF_SIZE];
416 g_return_val_if_fail (G_VALUE_TYPE (value) == G_TYPE_DOUBLE, NULL);
418 num = g_value_get_double (value);
425 integer = (gint) num;
427 g_ascii_dtostr (fraction, sizeof (fraction), (num - integer) * 60);
429 /* FIXME review GPSCoordinate serialization spec for the .mm or ,ss
430 * decision. Couldn't understand it clearly */
431 return g_strdup_printf ("%d,%s%c", integer, fraction, c);
435 serialize_exif_latitude (const GValue * value)
437 return serialize_exif_gps_coordinate (value, 'N', 'S');
441 serialize_exif_longitude (const GValue * value)
443 return serialize_exif_gps_coordinate (value, 'E', 'W');
447 deserialize_exif_gps_coordinate (XmpTag * xmptag, GstTagList * taglist,
448 const gchar * gst_tag, const gchar * str, gchar pos, gchar neg)
451 gint d = 0, m = 0, s = 0;
454 const gchar *current;
456 /* get the degrees */
457 if (sscanf (str, "%d", &d) != 1)
460 /* find the beginning of the minutes */
461 current = strchr (str, ',');
466 /* check if it uses ,SS or .mm */
467 if (strchr (current, ',') != NULL) {
468 sscanf (current, "%d,%d%c", &m, &s, &c);
470 gchar *copy = g_strdup (current);
471 gint len = strlen (copy);
474 /* check the last letter */
475 for (i = len - 1; len >= 0; len--) {
476 if (g_ascii_isspace (copy[i]))
479 if (g_ascii_isalpha (copy[i])) {
486 /* something is wrong */
492 /* use a copy so we can change the last letter as E can cause
494 m2 = g_ascii_strtod (copy, NULL);
499 /* we can add them all as those that aren't parsed are 0 */
500 value = d + (m / 60.0) + (s / (60.0 * 60.0)) + (m2 / 60.0);
504 } else if (c == neg) {
510 gst_tag_list_add (taglist, xmp_tag_get_merge_mode (xmptag), gst_tag, value,
515 GST_WARNING ("Failed to deserialize gps coordinate: %s", str);
519 deserialize_exif_latitude (XmpTag * xmptag, GstTagList * taglist,
520 const gchar * gst_tag, const gchar * xmp_tag, const gchar * str,
521 GSList ** pending_tags)
523 deserialize_exif_gps_coordinate (xmptag, taglist, gst_tag, str, 'N', 'S');
527 deserialize_exif_longitude (XmpTag * xmptag, GstTagList * taglist,
528 const gchar * gst_tag, const gchar * xmp_tag, const gchar * str,
529 GSList ** pending_tags)
531 deserialize_exif_gps_coordinate (xmptag, taglist, gst_tag, str, 'E', 'W');
535 serialize_exif_altitude (const GValue * value)
539 num = g_value_get_double (value);
544 return double_to_fraction_string (num);
548 serialize_exif_altituderef (const GValue * value)
552 num = g_value_get_double (value);
554 /* 0 means above sea level, 1 means below */
556 return g_strdup ("0");
557 return g_strdup ("1");
561 deserialize_exif_altitude (XmpTag * xmptag, GstTagList * taglist,
562 const gchar * gst_tag, const gchar * xmp_tag, const gchar * str,
563 GSList ** pending_tags)
565 const gchar *altitude_str = NULL;
566 const gchar *altituderef_str = NULL;
572 PendingXmpTag *ptag = NULL;
574 /* find the other missing part */
575 if (strcmp (xmp_tag, "exif:GPSAltitude") == 0) {
578 for (entry = *pending_tags; entry; entry = g_slist_next (entry)) {
579 ptag = (PendingXmpTag *) entry->data;
581 if (strcmp (ptag->xmp_tag->tag_name, "exif:GPSAltitudeRef") == 0) {
582 altituderef_str = ptag->str;
587 } else if (strcmp (xmp_tag, "exif:GPSAltitudeRef") == 0) {
588 altituderef_str = str;
590 for (entry = *pending_tags; entry; entry = g_slist_next (entry)) {
591 ptag = (PendingXmpTag *) entry->data;
593 if (strcmp (ptag->xmp_tag->tag_name, "exif:GPSAltitude") == 0) {
594 altitude_str = ptag->str;
600 GST_WARNING ("Unexpected xmp tag %s", xmp_tag);
605 GST_WARNING ("Missing exif:GPSAltitude tag");
608 if (!altituderef_str) {
609 GST_WARNING ("Missing exif:GPSAltitudeRef tag");
613 if (sscanf (altitude_str, "%d/%d", &frac_n, &frac_d) != 2) {
614 GST_WARNING ("Failed to parse fraction: %s", altitude_str);
618 gst_util_fraction_to_double (frac_n, frac_d, &value);
620 if (altituderef_str[0] == '0') {
622 } else if (altituderef_str[0] == '1') {
625 GST_WARNING ("Unexpected exif:AltitudeRef value: %s", altituderef_str);
629 /* add to the taglist */
630 gst_tag_list_add (taglist, xmp_tag_get_merge_mode (xmptag),
631 GST_TAG_GEO_LOCATION_ELEVATION, value, NULL);
635 g_slice_free (PendingXmpTag, ptag);
636 *pending_tags = g_slist_delete_link (*pending_tags, entry);
640 serialize_exif_gps_speed (const GValue * value)
642 return double_to_fraction_string (g_value_get_double (value) *
643 METERS_PER_SECOND_TO_KILOMETERS_PER_HOUR);
647 serialize_exif_gps_speedref (const GValue * value)
649 /* we always use km/h */
650 return g_strdup ("K");
654 deserialize_exif_gps_speed (XmpTag * xmptag, GstTagList * taglist,
655 const gchar * gst_tag, const gchar * xmp_tag, const gchar * str,
656 GSList ** pending_tags)
658 const gchar *speed_str = NULL;
659 const gchar *speedref_str = NULL;
665 PendingXmpTag *ptag = NULL;
667 /* find the other missing part */
668 if (strcmp (xmp_tag, "exif:GPSSpeed") == 0) {
671 for (entry = *pending_tags; entry; entry = g_slist_next (entry)) {
672 ptag = (PendingXmpTag *) entry->data;
674 if (strcmp (ptag->xmp_tag->tag_name, "exif:GPSSpeedRef") == 0) {
675 speedref_str = ptag->str;
680 } else if (strcmp (xmp_tag, "exif:GPSSpeedRef") == 0) {
683 for (entry = *pending_tags; entry; entry = g_slist_next (entry)) {
684 ptag = (PendingXmpTag *) entry->data;
686 if (strcmp (ptag->xmp_tag->tag_name, "exif:GPSSpeed") == 0) {
687 speed_str = ptag->str;
693 GST_WARNING ("Unexpected xmp tag %s", xmp_tag);
698 GST_WARNING ("Missing exif:GPSSpeed tag");
702 GST_WARNING ("Missing exif:GPSSpeedRef tag");
706 if (sscanf (speed_str, "%d/%d", &frac_n, &frac_d) != 2) {
707 GST_WARNING ("Failed to parse fraction: %s", speed_str);
711 gst_util_fraction_to_double (frac_n, frac_d, &value);
713 if (speedref_str[0] == 'K') {
714 value *= KILOMETERS_PER_HOUR_TO_METERS_PER_SECOND;
715 } else if (speedref_str[0] == 'M') {
716 value *= MILES_PER_HOUR_TO_METERS_PER_SECOND;
717 } else if (speedref_str[0] == 'N') {
718 value *= KNOTS_TO_METERS_PER_SECOND;
720 GST_WARNING ("Unexpected exif:SpeedRef value: %s", speedref_str);
724 /* add to the taglist */
725 gst_tag_list_add (taglist, xmp_tag_get_merge_mode (xmptag),
726 GST_TAG_GEO_LOCATION_MOVEMENT_SPEED, value, NULL);
730 g_slice_free (PendingXmpTag, ptag);
731 *pending_tags = g_slist_delete_link (*pending_tags, entry);
735 serialize_exif_gps_direction (const GValue * value)
737 return double_to_fraction_string (g_value_get_double (value));
741 serialize_exif_gps_directionref (const GValue * value)
743 /* T for true geographic direction (M would mean magnetic) */
744 return g_strdup ("T");
748 deserialize_exif_gps_direction (XmpTag * xmptag, GstTagList * taglist,
749 const gchar * gst_tag, const gchar * xmp_tag, const gchar * str,
750 GSList ** pending_tags, const gchar * direction_tag,
751 const gchar * directionref_tag)
753 const gchar *dir_str = NULL;
754 const gchar *dirref_str = NULL;
760 PendingXmpTag *ptag = NULL;
762 /* find the other missing part */
763 if (strcmp (xmp_tag, direction_tag) == 0) {
766 for (entry = *pending_tags; entry; entry = g_slist_next (entry)) {
767 ptag = (PendingXmpTag *) entry->data;
769 if (strcmp (ptag->xmp_tag->tag_name, directionref_tag) == 0) {
770 dirref_str = ptag->str;
775 } else if (strcmp (xmp_tag, directionref_tag) == 0) {
778 for (entry = *pending_tags; entry; entry = g_slist_next (entry)) {
779 ptag = (PendingXmpTag *) entry->data;
781 if (strcmp (ptag->xmp_tag->tag_name, direction_tag) == 0) {
788 GST_WARNING ("Unexpected xmp tag %s", xmp_tag);
793 GST_WARNING ("Missing %s tag", dir_str);
797 GST_WARNING ("Missing %s tag", dirref_str);
801 if (sscanf (dir_str, "%d/%d", &frac_n, &frac_d) != 2) {
802 GST_WARNING ("Failed to parse fraction: %s", dir_str);
806 gst_util_fraction_to_double (frac_n, frac_d, &value);
808 if (dirref_str[0] == 'T') {
810 } else if (dirref_str[0] == 'M') {
811 GST_WARNING ("Magnetic direction tags aren't supported yet");
814 GST_WARNING ("Unexpected %s value: %s", directionref_tag, dirref_str);
818 /* add to the taglist */
819 gst_tag_list_add (taglist, xmp_tag_get_merge_mode (xmptag), gst_tag, value,
824 g_slice_free (PendingXmpTag, ptag);
825 *pending_tags = g_slist_delete_link (*pending_tags, entry);
829 deserialize_exif_gps_track (XmpTag * xmptag, GstTagList * taglist,
830 const gchar * gst_tag, const gchar * xmp_tag, const gchar * str,
831 GSList ** pending_tags)
833 deserialize_exif_gps_direction (xmptag, taglist, gst_tag, xmp_tag, str,
834 pending_tags, "exif:GPSTrack", "exif:GPSTrackRef");
838 deserialize_exif_gps_img_direction (XmpTag * xmptag, GstTagList * taglist,
839 const gchar * gst_tag, const gchar * xmp_tag, const gchar * str,
840 GSList ** pending_tags)
842 deserialize_exif_gps_direction (xmptag, taglist, gst_tag, xmp_tag, str,
843 pending_tags, "exif:GPSImgDirection", "exif:GPSImgDirectionRef");
847 deserialize_xmp_rating (XmpTag * xmptag, GstTagList * taglist,
848 const gchar * gst_tag, const gchar * xmp_tag, const gchar * str,
849 GSList ** pending_tags)
853 if (sscanf (str, "%u", &value) != 1) {
854 GST_WARNING ("Failed to parse xmp:Rating %s", str);
859 GST_WARNING ("Unsupported Rating tag %u (should be from 0 to 100), "
864 gst_tag_list_add (taglist, xmp_tag_get_merge_mode (xmptag), gst_tag, value,
869 serialize_tiff_orientation (const GValue * value)
874 str = g_value_get_string (value);
876 GST_WARNING ("Failed to get image orientation tag value");
880 num = __exif_tag_image_orientation_to_exif_value (str);
884 return g_strdup_printf ("%d", num);
888 deserialize_tiff_orientation (XmpTag * xmptag, GstTagList * taglist,
889 const gchar * gst_tag, const gchar * xmp_tag, const gchar * str,
890 GSList ** pending_tags)
893 const gchar *orientation = NULL;
895 if (sscanf (str, "%u", &value) != 1) {
896 GST_WARNING ("Failed to parse tiff:Orientation %s", str);
900 if (value < 1 || value > 8) {
901 GST_WARNING ("Invalid tiff:Orientation tag %u (should be from 1 to 8), "
906 orientation = __exif_tag_image_orientation_from_exif_value (value);
907 if (orientation == NULL)
909 gst_tag_list_add (taglist, xmp_tag_get_merge_mode (xmptag), gst_tag,
914 /* look at this page for addtional schemas
915 * http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/XMP.html
918 _init_xmp_tag_map (gpointer user_data)
921 GstXmpSchema *schema;
923 __xmp_schemas = g_hash_table_new (g_direct_hash, g_direct_equal);
926 /* dublic code metadata
927 * http://dublincore.org/documents/dces/
929 schema = gst_xmp_schema_new ();
930 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_ARTIST,
931 "dc:creator", GstXmpTagTypeSeq, NULL, NULL);
932 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_COPYRIGHT,
933 "dc:rights", GstXmpTagTypeSimple, NULL, NULL);
934 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_DATE, "dc:date",
935 GstXmpTagTypeSeq, NULL, NULL);
936 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_DESCRIPTION,
937 "dc:description", GstXmpTagTypeSimple, NULL, NULL);
938 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_KEYWORDS,
939 "dc:subject", GstXmpTagTypeBag, NULL, NULL);
940 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_TITLE, "dc:title",
941 GstXmpTagTypeSimple, NULL, NULL);
942 /* FIXME: we probably want GST_TAG_{,AUDIO_,VIDEO_}MIME_TYPE */
943 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_VIDEO_CODEC,
944 "dc:format", GstXmpTagTypeSimple, NULL, NULL);
945 _gst_xmp_add_schema ("dc", schema);
947 /* xap (xmp) schema */
948 schema = gst_xmp_schema_new ();
949 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_USER_RATING,
950 "xmp:Rating", GstXmpTagTypeSimple, NULL, deserialize_xmp_rating);
951 _gst_xmp_add_schema ("xap", schema);
954 schema = gst_xmp_schema_new ();
955 _gst_xmp_schema_add_simple_mapping (schema,
956 GST_TAG_DEVICE_MANUFACTURER, "tiff:Make", GstXmpTagTypeSimple, NULL,
958 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_DEVICE_MODEL,
959 "tiff:Model", GstXmpTagTypeSimple, NULL, NULL);
960 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_APPLICATION_NAME,
961 "tiff:Software", GstXmpTagTypeSimple, NULL, NULL);
962 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_IMAGE_ORIENTATION,
963 "tiff:Orientation", GstXmpTagTypeSimple, serialize_tiff_orientation,
964 deserialize_tiff_orientation);
965 _gst_xmp_add_schema ("tiff", schema);
968 schema = gst_xmp_schema_new ();
969 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_DATE_TIME,
970 "exif:DateTimeOriginal", GstXmpTagTypeSimple, NULL, NULL);
971 _gst_xmp_schema_add_simple_mapping (schema,
972 GST_TAG_GEO_LOCATION_LATITUDE, "exif:GPSLatitude",
973 GstXmpTagTypeSimple, serialize_exif_latitude, deserialize_exif_latitude);
974 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_GEO_LOCATION_LONGITUDE,
975 "exif:GPSLongitude", GstXmpTagTypeSimple, serialize_exif_longitude,
976 deserialize_exif_longitude);
977 _gst_xmp_schema_add_simple_mapping (schema,
978 GST_TAG_CAPTURING_EXPOSURE_COMPENSATION, "exif:ExposureBiasValue",
979 GstXmpTagTypeSimple, NULL, NULL);
981 /* compound exif tags */
982 xmpinfo = gst_xmp_tag_create_compound (GST_TAG_GEO_LOCATION_ELEVATION,
983 "exif:GPSAltitude", "exif:GPSAltitudeRef", serialize_exif_altitude,
984 serialize_exif_altituderef, deserialize_exif_altitude);
985 _gst_xmp_schema_add_mapping (schema, xmpinfo);
987 xmpinfo = gst_xmp_tag_create_compound (GST_TAG_GEO_LOCATION_MOVEMENT_SPEED,
988 "exif:GPSSpeed", "exif:GPSSpeedRef", serialize_exif_gps_speed,
989 serialize_exif_gps_speedref, deserialize_exif_gps_speed);
990 _gst_xmp_schema_add_mapping (schema, xmpinfo);
993 gst_xmp_tag_create_compound (GST_TAG_GEO_LOCATION_MOVEMENT_DIRECTION,
994 "exif:GPSTrack", "exif:GPSTrackRef", serialize_exif_gps_direction,
995 serialize_exif_gps_directionref, deserialize_exif_gps_track);
996 _gst_xmp_schema_add_mapping (schema, xmpinfo);
998 xmpinfo = gst_xmp_tag_create_compound (GST_TAG_GEO_LOCATION_CAPTURE_DIRECTION,
999 "exif:GPSImgDirection", "exif:GPSImgDirectionRef",
1000 serialize_exif_gps_direction, serialize_exif_gps_directionref,
1001 deserialize_exif_gps_img_direction);
1002 _gst_xmp_schema_add_mapping (schema, xmpinfo);
1004 _gst_xmp_add_schema ("exif", schema);
1006 /* photoshop schema */
1007 schema = gst_xmp_schema_new ();
1008 _gst_xmp_schema_add_simple_mapping (schema,
1009 GST_TAG_GEO_LOCATION_COUNTRY, "photoshop:Country",
1010 GstXmpTagTypeSimple, NULL, NULL);
1011 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_GEO_LOCATION_CITY,
1012 "photoshop:City", GstXmpTagTypeSimple, NULL, NULL);
1013 _gst_xmp_add_schema ("photoshop", schema);
1015 /* iptc4xmpcore schema */
1016 schema = gst_xmp_schema_new ();
1017 _gst_xmp_schema_add_simple_mapping (schema,
1018 GST_TAG_GEO_LOCATION_SUBLOCATION, "Iptc4xmpCore:Location",
1019 GstXmpTagTypeSimple, NULL, NULL);
1020 _gst_xmp_add_schema ("Iptc4xmpCore", schema);
1022 /* iptc4xmpext schema */
1023 schema = gst_xmp_schema_new ();
1024 xmpinfo = gst_xmp_tag_create (NULL, "Iptc4xmpExt:LocationShown",
1025 GstXmpTagTypeStruct, NULL, NULL);
1026 xmpinfo->supertype = GstXmpTagTypeBag;
1027 xmpinfo->parse_type = "Resource";
1028 xmpinfo->children = g_slist_prepend (xmpinfo->children,
1029 gst_xmp_tag_create (GST_TAG_GEO_LOCATION_SUBLOCATION,
1030 "LocationDetails:Sublocation", GstXmpTagTypeSimple, NULL, NULL));
1032 g_slist_prepend (xmpinfo->children,
1033 gst_xmp_tag_create (GST_TAG_GEO_LOCATION_CITY,
1034 "LocationDetails:City", GstXmpTagTypeSimple, NULL, NULL));
1036 g_slist_prepend (xmpinfo->children,
1037 gst_xmp_tag_create (GST_TAG_GEO_LOCATION_COUNTRY,
1038 "LocationDetails:Country", GstXmpTagTypeSimple, NULL, NULL));
1039 _gst_xmp_schema_add_mapping (schema, xmpinfo);
1040 _gst_xmp_add_schema ("Iptc4xmpExt", schema);
1046 xmp_tags_initialize ()
1048 static GOnce my_once = G_ONCE_INIT;
1049 g_once (&my_once, (GThreadFunc) _init_xmp_tag_map, NULL);
1052 typedef struct _GstXmpNamespaceMatch GstXmpNamespaceMatch;
1053 struct _GstXmpNamespaceMatch
1055 const gchar *ns_prefix;
1056 const gchar *ns_uri;
1059 * Stores extra namespaces for array tags
1060 * The namespaces should be writen in the form:
1062 * xmlns:XpTo="http://some.org/your/ns/name/ (next ones)"
1064 const gchar *extra_ns;
1067 static const GstXmpNamespaceMatch ns_match[] = {
1068 {"dc", "http://purl.org/dc/elements/1.1/", NULL},
1069 {"exif", "http://ns.adobe.com/exif/1.0/", NULL},
1070 {"tiff", "http://ns.adobe.com/tiff/1.0/", NULL},
1071 {"xap", "http://ns.adobe.com/xap/1.0/", NULL},
1072 {"photoshop", "http://ns.adobe.com/photoshop/1.0/", NULL},
1073 {"Iptc4xmpCore", "http://iptc.org/std/Iptc4xmpCore/1.0/xmlns/", NULL},
1074 {"Iptc4xmpExt", "http://iptc.org/std/Iptc4xmpExt/2008-02-29/",
1075 "xmlns:LocationDetails=\"http://iptc.org/std/Iptc4xmpExt/2008-02-29/LocationDetails/\""},
1079 typedef struct _GstXmpNamespaceMap GstXmpNamespaceMap;
1080 struct _GstXmpNamespaceMap
1082 const gchar *original_ns;
1083 gchar *gstreamer_ns;
1089 read_one_tag (GstTagList * list, XmpTag * xmptag,
1090 const gchar * v, GSList ** pending_tags)
1093 GstTagMergeMode merge_mode;
1094 const gchar *tag = xmptag->gst_tag;
1096 g_return_if_fail (tag != NULL);
1098 if (xmptag->deserialize) {
1099 xmptag->deserialize (xmptag, list, tag, xmptag->tag_name, v, pending_tags);
1103 merge_mode = xmp_tag_get_merge_mode (xmptag);
1104 tag_type = gst_tag_get_type (tag);
1106 /* add gstreamer tag depending on type */
1108 case G_TYPE_STRING:{
1109 gst_tag_list_add (list, merge_mode, tag, v, NULL);
1112 case G_TYPE_DOUBLE:{
1114 gint frac_n, frac_d;
1116 if (sscanf (v, "%d/%d", &frac_n, &frac_d) == 2) {
1117 gst_util_fraction_to_double (frac_n, frac_d, &value);
1118 gst_tag_list_add (list, merge_mode, tag, value, NULL);
1120 GST_WARNING ("Failed to parse fraction: %s", v);
1125 if (tag_type == GST_TYPE_DATE_TIME) {
1126 GstDateTime *datetime = NULL;
1127 gint year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0;
1129 gint gmt_offset_hour = -1, gmt_offset_min = -1, gmt_offset = -1;
1136 GST_WARNING ("Empty string for datetime parsing");
1140 GST_DEBUG ("Parsing %s into a datetime", v);
1142 ret = sscanf (v, "%04d-%02d-%02dT%02d:%02d:%02d.%15s",
1143 &year, &month, &day, &hour, &minute, &second, usec_str);
1145 /* FIXME theoretically, xmp can express datetimes with only year
1146 * or year and month, but gstdatetime doesn't support it */
1147 GST_WARNING ("Invalid datetime value: %s", v);
1150 /* parse the usecs */
1152 gint num_digits = 0;
1154 /* find the number of digits */
1155 while (isdigit ((gint) usec_str[num_digits++]) && num_digits < 6);
1157 if (num_digits > 0) {
1158 /* fill up to 6 digits with 0 */
1159 while (num_digits < 6) {
1160 usec_str[num_digits++] = 0;
1163 g_assert (num_digits == 6);
1165 usec_str[num_digits] = '\0';
1166 usecs = atoi (usec_str);
1170 /* parse the timezone info */
1171 if (v[len - 1] == 'Z') {
1172 GST_LOG ("UTC timezone");
1174 /* Having a Z at the end means UTC */
1175 datetime = gst_date_time_new (0, year, month, day, hour, minute,
1176 second + usecs / 1000000.0);
1178 gchar *plus_pos = NULL;
1179 gchar *neg_pos = NULL;
1182 GST_LOG ("Checking for timezone information");
1184 /* check if there is timezone info */
1185 plus_pos = strrchr (v, '+');
1186 neg_pos = strrchr (v, '-');
1189 } else if (neg_pos) {
1194 gint ret_tz = sscanf (pos, "%d:%d", &gmt_offset_hour,
1197 GST_DEBUG ("Parsing timezone: %s", pos);
1200 gmt_offset = gmt_offset_hour * 60 + gmt_offset_min;
1201 if (neg_pos != NULL && neg_pos + 1 == pos)
1204 GST_LOG ("Timezone offset: %f (%d minutes)", gmt_offset / 60.0,
1207 /* no way to know if it is DST or not */
1209 gst_date_time_new (gmt_offset / 60.0,
1210 year, month, day, hour, minute,
1211 second + usecs / ((gdouble) G_USEC_PER_SEC));
1213 GST_WARNING ("Failed to parse timezone information");
1216 GST_WARNING ("No timezone signal found");
1221 gst_tag_list_add (list, merge_mode, tag, datetime, NULL);
1222 gst_date_time_unref (datetime);
1225 } else if (tag_type == G_TYPE_DATE) {
1229 /* this is ISO 8601 Date and Time Format
1230 * %F Equivalent to %Y-%m-%d (the ISO 8601 date format). (C99)
1231 * %T The time in 24-hour notation (%H:%M:%S). (SU)
1232 * e.g. 2009-05-30T18:26:14+03:00 */
1234 /* FIXME: this would be the proper way, but needs
1235 #define _XOPEN_SOURCE before #include <time.h>
1237 date = g_date_new ();
1239 strptime (dts, "%FT%TZ", &tm);
1240 g_date_set_time_t (date, mktime(&tm));
1242 /* FIXME: this cannot parse the date
1243 date = g_date_new ();
1244 g_date_set_parse (date, v);
1245 if (g_date_valid (date)) {
1246 gst_tag_list_add (list, merge_mode, tag,
1249 GST_WARNING ("unparsable date: '%s'", v);
1252 /* poor mans straw */
1253 sscanf (v, "%04d-%02d-%02dT", &y, &m, &d);
1254 date = g_date_new_dmy (d, m, y);
1255 gst_tag_list_add (list, merge_mode, tag, date, NULL);
1258 GST_WARNING ("unhandled type for %s from xmp", tag);
1265 * gst_tag_list_from_xmp_buffer:
1268 * Parse a xmp packet into a taglist.
1270 * Returns: new taglist or %NULL, free the list when done
1275 gst_tag_list_from_xmp_buffer (GstBuffer * buffer)
1277 GstTagList *list = NULL;
1278 gchar *xps, *xp1, *xp2, *xpe, *ns, *ne;
1279 gsize len, max_ft_len;
1283 XmpTag *last_xmp_tag = NULL;
1284 GSList *pending_tags = NULL;
1286 /* Used for strucuture xmp tags */
1287 XmpTag *context_tag = NULL;
1289 GstXmpNamespaceMap ns_map[] = {
1300 {"Iptc4xmpCore", NULL}
1302 {"Iptc4xmpExt", NULL}
1307 xmp_tags_initialize ();
1309 g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
1311 GST_LOG ("Starting xmp parsing");
1313 xps = gst_buffer_map (buffer, &len, NULL, GST_MAP_READ);
1314 g_return_val_if_fail (len > 0, NULL);
1316 xpe = &xps[len + 1];
1318 /* check header and footer */
1319 xp1 = g_strstr_len (xps, len, "<?xpacket begin");
1321 goto missing_header;
1322 xp1 = &xp1[strlen ("<?xpacket begin")];
1323 while (*xp1 != '>' && *xp1 != '<' && xp1 < xpe)
1326 goto missing_header;
1328 /* Use 2 here to count for an extra trailing \n that was added
1329 * in old versions, this makes it able to parse xmp packets with
1330 * and without this trailing char */
1331 max_ft_len = 2 + strlen ("<?xpacket end=\".\"?>");
1332 if (len < max_ft_len)
1333 goto missing_footer;
1335 GST_DEBUG ("checking footer: [%s]", &xps[len - max_ft_len]);
1336 xp2 = g_strstr_len (&xps[len - max_ft_len], max_ft_len, "<?xpacket ");
1338 goto missing_footer;
1340 GST_INFO ("xmp header okay");
1342 /* skip > and text until first xml-node */
1344 while (*xp1 != '<' && xp1 < xpe)
1347 /* no tag can be longer that the whole buffer */
1348 part = g_malloc (xp2 - xp1);
1349 list = gst_tag_list_new_empty ();
1351 /* parse data into a list of nodes */
1352 /* data is between xp1..xp2 */
1359 while (ne < xp2 && *ne != '>' && *ne != '<') {
1360 if (*ne == '\n' || *ne == '\t' || *ne == ' ') {
1361 while (ne < xp2 && (*ne == '\n' || *ne == '\t' || *ne == ' '))
1372 /* {XML, ns, ne-ns} */
1374 gchar *as = strchr (part, ' ');
1375 /* only log start nodes */
1376 GST_INFO ("xml: %s", part);
1381 /* skip ' ' and scan the attributes */
1385 /* split attr=value pairs */
1386 while (*ae != '\0') {
1388 /* attr/value delimmiter */
1390 } else if (*ae == '"') {
1395 while (*ae != '\0' && *ae != '"')
1400 GST_INFO (" : [%s][%s]", as, v);
1401 if (!strncmp (as, "xmlns:", 6)) {
1403 /* we need to rewrite known namespaces to what we use in
1405 while (ns_match[i].ns_prefix) {
1406 if (!strcmp (ns_match[i].ns_uri, v))
1410 if (ns_match[i].ns_prefix) {
1411 if (strcmp (ns_map[i].original_ns, &as[6])) {
1412 ns_map[i].gstreamer_ns = g_strdup (&as[6]);
1416 XmpTag *xmp_tag = NULL;
1417 /* FIXME: eventually rewrite ns
1419 * check if ns before ':' is in ns_map and ns_map[i].gstreamer_ns!=NULL
1420 * do 2 stage filter in tag_matches
1425 for (iter = context_tag->children; iter;
1426 iter = g_slist_next (iter)) {
1427 XmpTag *child = iter->data;
1429 GST_DEBUG ("Looking at child tag %s : %s", child->tag_name,
1431 if (strcmp (child->tag_name, as) == 0) {
1438 GST_LOG ("Looking for tag: %s", as);
1439 _gst_xmp_tag_get_mapping_reverse (as, &xmp_tag);
1442 PendingXmpTag *ptag;
1444 GST_DEBUG ("Found xmp tag: %s -> %s", xmp_tag->tag_name,
1447 /* we shouldn't find a xmp structure here */
1448 g_assert (xmp_tag->gst_tag != NULL);
1450 ptag = g_slice_new (PendingXmpTag);
1451 ptag->xmp_tag = xmp_tag;
1452 ptag->str = g_strdup (v);
1454 pending_tags = g_slist_append (pending_tags, ptag);
1457 /* restore chars overwritten by '\0' */
1460 } else if (*ae == '\0' || *ae == ' ') {
1461 /* end of attr/value pair */
1464 /* to next char if not eos */
1470 <dc:type><rdf:Bag><rdf:li>Image</rdf:li></rdf:Bag></dc:type>
1471 <dc:creator><rdf:Seq><rdf:li/></rdf:Seq></dc:creator>
1473 /* FIXME: eventually rewrite ns */
1475 /* skip rdf tags for now */
1476 if (strncmp (part, "rdf:", 4)) {
1477 /* if we're inside some struct, we look only on its children */
1481 /* check if this is the closing of the context */
1483 && strcmp (part + 1, context_tag->tag_name) == 0) {
1484 GST_DEBUG ("Closing context tag %s", part);
1488 for (iter = context_tag->children; iter;
1489 iter = g_slist_next (iter)) {
1490 XmpTag *child = iter->data;
1492 GST_DEBUG ("Looking at child tag %s : %s", child->tag_name,
1494 if (strcmp (child->tag_name, part) == 0) {
1495 last_xmp_tag = child;
1502 GST_LOG ("Looking for tag: %s", part);
1503 _gst_xmp_tag_get_mapping_reverse (part, &last_xmp_tag);
1504 if (last_xmp_tag && last_xmp_tag->type == GstXmpTagTypeStruct) {
1505 context_tag = last_xmp_tag;
1506 last_xmp_tag = NULL;
1512 GST_LOG ("Next cycle");
1522 while (ne < xp2 && *ne != '<') {
1528 /* {TXT, ns, (ne-ns)-1} */
1529 if (ns[0] != '\n' && &ns[1] <= ne) {
1530 /* only log non-newline nodes, we still have to parse them */
1531 GST_INFO ("txt: %s", part);
1533 PendingXmpTag *ptag;
1535 GST_DEBUG ("Found tag %s -> %s", last_xmp_tag->tag_name,
1536 last_xmp_tag->gst_tag);
1538 if (last_xmp_tag->type == GstXmpTagTypeStruct) {
1539 g_assert (context_tag == NULL); /* we can't handle struct nesting currently */
1541 context_tag = last_xmp_tag;
1543 ptag = g_slice_new (PendingXmpTag);
1544 ptag->xmp_tag = last_xmp_tag;
1545 ptag->str = g_strdup (part);
1547 pending_tags = g_slist_append (pending_tags, ptag);
1558 while (pending_tags) {
1559 PendingXmpTag *ptag = (PendingXmpTag *) pending_tags->data;
1561 pending_tags = g_slist_delete_link (pending_tags, pending_tags);
1563 read_one_tag (list, ptag->xmp_tag, ptag->str, &pending_tags);
1566 g_slice_free (PendingXmpTag, ptag);
1569 GST_INFO ("xmp packet parsed, %d entries",
1570 gst_structure_n_fields ((GstStructure *) list));
1572 /* free resources */
1574 while (ns_map[i].original_ns) {
1575 g_free (ns_map[i].gstreamer_ns);
1580 gst_buffer_unmap (buffer, xps, len);
1586 GST_WARNING ("malformed xmp packet header");
1589 GST_WARNING ("malformed xmp packet footer");
1592 GST_WARNING ("malformed xml tag: %s", part);
1600 string_open_tag (GString * string, const char *tag)
1602 g_string_append_c (string, '<');
1603 g_string_append (string, tag);
1604 g_string_append_c (string, '>');
1608 string_close_tag (GString * string, const char *tag)
1610 g_string_append (string, "</");
1611 g_string_append (string, tag);
1612 g_string_append (string, ">\n");
1616 gst_value_serialize_xmp (const GValue * value)
1618 switch (G_VALUE_TYPE (value)) {
1620 return g_markup_escape_text (g_value_get_string (value), -1);
1622 return g_strdup_printf ("%d", g_value_get_int (value));
1624 return g_strdup_printf ("%u", g_value_get_uint (value));
1626 return double_to_fraction_string (g_value_get_double (value));
1630 /* put non-switchable types here */
1631 if (G_VALUE_TYPE (value) == G_TYPE_DATE) {
1632 const GDate *date = g_value_get_boxed (value);
1634 return g_strdup_printf ("%04d-%02d-%02d",
1635 (gint) g_date_get_year (date), (gint) g_date_get_month (date),
1636 (gint) g_date_get_day (date));
1637 } else if (G_VALUE_TYPE (value) == GST_TYPE_DATE_TIME) {
1638 gint year, month, day, hour, min, sec, microsec;
1639 gfloat gmt_offset = 0;
1640 gint gmt_offset_hour, gmt_offset_min;
1641 GstDateTime *datetime = (GstDateTime *) g_value_get_boxed (value);
1643 year = gst_date_time_get_year (datetime);
1644 month = gst_date_time_get_month (datetime);
1645 day = gst_date_time_get_day (datetime);
1646 hour = gst_date_time_get_hour (datetime);
1647 min = gst_date_time_get_minute (datetime);
1648 sec = gst_date_time_get_second (datetime);
1649 microsec = gst_date_time_get_microsecond (datetime);
1650 gmt_offset = gst_date_time_get_time_zone_offset (datetime);
1651 if (gmt_offset == 0) {
1653 return g_strdup_printf ("%04d-%02d-%02dT%02d:%02d:%02d.%06dZ",
1654 year, month, day, hour, min, sec, microsec);
1656 gmt_offset_hour = ABS (gmt_offset);
1657 gmt_offset_min = (ABS (gmt_offset) - gmt_offset_hour) * 60;
1659 return g_strdup_printf ("%04d-%02d-%02dT%02d:%02d:%02d.%06d%c%02d:%02d",
1660 year, month, day, hour, min, sec, microsec,
1661 gmt_offset >= 0 ? '+' : '-', gmt_offset_hour, gmt_offset_min);
1669 write_one_tag (const GstTagList * list, XmpTag * xmp_tag, gpointer user_data)
1672 XmpSerializationData *serialization_data = user_data;
1673 GString *data = serialization_data->data;
1676 /* struct type handled differently */
1677 if (xmp_tag->type == GstXmpTagTypeStruct ||
1678 xmp_tag->type == GstXmpTagTypeCompound) {
1680 gboolean use_it = FALSE;
1682 /* check if any of the inner tags are present on the taglist */
1683 for (iter = xmp_tag->children; iter && !use_it; iter = g_slist_next (iter)) {
1684 XmpTag *child_tag = iter->data;
1686 if (gst_tag_list_get_value_index (list, child_tag->gst_tag, 0) != NULL) {
1693 if (xmp_tag->tag_name)
1694 string_open_tag (data, xmp_tag->tag_name);
1696 if (xmp_tag->supertype) {
1697 string_open_tag (data, xmp_tag_type_get_name (xmp_tag->supertype));
1698 if (xmp_tag->parse_type) {
1699 g_string_append (data, "<rdf:li rdf:parseType=\"");
1700 g_string_append (data, xmp_tag->parse_type);
1701 g_string_append_c (data, '"');
1702 g_string_append_c (data, '>');
1704 string_open_tag (data, "rdf:li");
1709 for (iter = xmp_tag->children; iter; iter = g_slist_next (iter)) {
1710 write_one_tag (list, iter->data, user_data);
1713 if (xmp_tag->supertype) {
1714 string_close_tag (data, "rdf:li");
1715 string_close_tag (data, xmp_tag_type_get_name (xmp_tag->supertype));
1718 if (xmp_tag->tag_name)
1719 string_close_tag (data, xmp_tag->tag_name);
1724 /* at this point we must have a gst_tag */
1725 g_assert (xmp_tag->gst_tag);
1726 if (gst_tag_list_get_value_index (list, xmp_tag->gst_tag, 0) == NULL)
1729 ct = gst_tag_list_get_tag_size (list, xmp_tag->gst_tag);
1730 string_open_tag (data, xmp_tag->tag_name);
1732 /* fast path for single valued tag */
1733 if (ct == 1 || xmp_tag->type == GstXmpTagTypeSimple) {
1734 if (xmp_tag->serialize) {
1735 s = xmp_tag->serialize (gst_tag_list_get_value_index (list,
1736 xmp_tag->gst_tag, 0));
1738 s = gst_value_serialize_xmp (gst_tag_list_get_value_index (list,
1739 xmp_tag->gst_tag, 0));
1742 g_string_append (data, s);
1745 GST_WARNING ("unhandled type for %s to xmp", xmp_tag->gst_tag);
1748 const gchar *typename;
1750 typename = xmp_tag_type_get_name (xmp_tag->type);
1752 string_open_tag (data, typename);
1753 for (i = 0; i < ct; i++) {
1754 GST_DEBUG ("mapping %s[%u/%u] to xmp", xmp_tag->gst_tag, i, ct);
1755 if (xmp_tag->serialize) {
1756 s = xmp_tag->serialize (gst_tag_list_get_value_index (list,
1757 xmp_tag->gst_tag, i));
1759 s = gst_value_serialize_xmp (gst_tag_list_get_value_index (list,
1760 xmp_tag->gst_tag, i));
1763 string_open_tag (data, "rdf:li");
1764 g_string_append (data, s);
1765 string_close_tag (data, "rdf:li");
1768 GST_WARNING ("unhandled type for %s to xmp", xmp_tag->gst_tag);
1771 string_close_tag (data, typename);
1774 string_close_tag (data, xmp_tag->tag_name);
1778 * gst_tag_list_to_xmp_buffer_full:
1780 * @read_only: does the container forbid inplace editing
1781 * @schemas: %NULL terminated array of schemas to be used on serialization
1783 * Formats a taglist as a xmp packet using only the selected
1784 * schemas. An empty list (%NULL) means that all schemas should
1787 * Returns: new buffer or %NULL, unref the buffer when done
1792 gst_tag_list_to_xmp_buffer_full (const GstTagList * list, gboolean read_only,
1793 const gchar ** schemas)
1795 GstBuffer *buffer = NULL;
1796 XmpSerializationData serialization_data;
1802 serialization_data.data = g_string_sized_new (4096);
1803 serialization_data.schemas = schemas;
1804 data = serialization_data.data;
1806 xmp_tags_initialize ();
1808 g_return_val_if_fail (GST_IS_TAG_LIST (list), NULL);
1811 g_string_append (data,
1812 "<?xpacket begin=\"\xEF\xBB\xBF\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>\n");
1813 g_string_append (data,
1814 "<x:xmpmeta xmlns:x=\"adobe:ns:meta/\" x:xmptk=\"GStreamer\">\n");
1815 g_string_append (data,
1816 "<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"");
1818 while (ns_match[i].ns_prefix) {
1819 if (xmp_serialization_data_use_schema (&serialization_data,
1820 ns_match[i].ns_prefix)) {
1821 g_string_append_printf (data, " xmlns:%s=\"%s\"",
1822 ns_match[i].ns_prefix, ns_match[i].ns_uri);
1823 if (ns_match[i].extra_ns) {
1824 g_string_append_printf (data, " %s", ns_match[i].extra_ns);
1829 g_string_append (data, ">\n");
1830 g_string_append (data, "<rdf:Description rdf:about=\"\">\n");
1832 /* iterate the schemas */
1833 if (schemas == NULL) {
1834 /* use all schemas */
1835 schemas = gst_tag_xmp_list_schemas ();
1837 for (i = 0; schemas[i] != NULL; i++) {
1838 GstXmpSchema *schema = _gst_xmp_get_schema (schemas[i]);
1839 GHashTableIter iter;
1840 gpointer key, value;
1845 /* Iterate over the hashtable */
1846 g_hash_table_iter_init (&iter, schema);
1847 while (g_hash_table_iter_next (&iter, &key, &value)) {
1848 write_one_tag (list, value, (gpointer) & serialization_data);
1853 g_string_append (data, "</rdf:Description>\n");
1854 g_string_append (data, "</rdf:RDF>\n");
1855 g_string_append (data, "</x:xmpmeta>\n");
1858 /* the xmp spec recommends to add 2-4KB padding for in-place editable xmp */
1861 for (i = 0; i < 32; i++) {
1862 g_string_append (data, " " " "
1866 g_string_append_printf (data, "<?xpacket end=\"%c\"?>",
1867 (read_only ? 'r' : 'w'));
1870 bdata = g_string_free (data, FALSE);
1872 buffer = gst_buffer_new ();
1873 gst_buffer_take_memory (buffer, -1,
1874 gst_memory_new_wrapped (0, bdata, g_free, bsize, 0, bsize));
1880 * gst_tag_list_to_xmp_buffer:
1882 * @read_only: does the container forbid inplace editing
1884 * Formats a taglist as a xmp packet.
1886 * Returns: new buffer or %NULL, unref the buffer when done
1891 gst_tag_list_to_xmp_buffer (const GstTagList * list, gboolean read_only)
1893 return gst_tag_list_to_xmp_buffer_full (list, read_only, NULL);
1896 #undef gst_xmp_schema_lookup
1897 #undef gst_xmp_schema_insert