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 ();
180 struct _PendingXmpTag
185 typedef struct _PendingXmpTag PendingXmpTag;
188 * A schema is a mapping of strings (the tag name in gstreamer) to a list of
189 * tags in xmp (XmpTag).
191 typedef GHashTable GstXmpSchema;
192 #define gst_xmp_schema_lookup g_hash_table_lookup
193 #define gst_xmp_schema_insert g_hash_table_insert
194 static GstXmpSchema *
195 gst_xmp_schema_new ()
197 return g_hash_table_new (g_direct_hash, g_direct_equal);
201 * Mappings from schema names into the schema group of tags (GstXmpSchema)
203 static GHashTable *__xmp_schemas;
205 static GstXmpSchema *
206 _gst_xmp_get_schema (const gchar * name)
209 GstXmpSchema *schema;
211 key = g_quark_from_string (name);
213 schema = g_hash_table_lookup (__xmp_schemas, GUINT_TO_POINTER (key));
215 GST_WARNING ("Schema %s doesn't exist", name);
221 _gst_xmp_add_schema (const gchar * name, GstXmpSchema * schema)
225 key = g_quark_from_string (name);
227 if (g_hash_table_lookup (__xmp_schemas, GUINT_TO_POINTER (key))) {
228 GST_WARNING ("Schema %s already exists, ignoring", name);
229 g_assert_not_reached ();
233 g_hash_table_insert (__xmp_schemas, GUINT_TO_POINTER (key), schema);
237 _gst_xmp_schema_add_mapping (GstXmpSchema * schema, XmpTag * tag)
241 key = g_quark_from_string (tag->gst_tag);
243 if (gst_xmp_schema_lookup (schema, GUINT_TO_POINTER (key))) {
244 GST_WARNING ("Tag %s already present for the schema", tag->gst_tag);
245 g_assert_not_reached ();
248 gst_xmp_schema_insert (schema, GUINT_TO_POINTER (key), tag);
252 gst_xmp_tag_create (const gchar * gst_tag, const gchar * xmp_tag,
253 gint xmp_type, XmpSerializationFunc serialization_func,
254 XmpDeserializationFunc deserialization_func)
258 xmpinfo = g_slice_new (XmpTag);
259 xmpinfo->gst_tag = gst_tag;
260 xmpinfo->tag_name = xmp_tag;
261 xmpinfo->type = xmp_type;
262 xmpinfo->supertype = GstXmpTagTypeNone;
263 xmpinfo->parse_type = NULL;
264 xmpinfo->serialize = serialization_func;
265 xmpinfo->deserialize = deserialization_func;
266 xmpinfo->children = NULL;
272 gst_xmp_tag_create_compound (const gchar * gst_tag, const gchar * xmp_tag_a,
273 const gchar * xmp_tag_b, XmpSerializationFunc serialization_func_a,
274 XmpSerializationFunc serialization_func_b,
275 XmpDeserializationFunc deserialization_func)
279 gst_xmp_tag_create (gst_tag, xmp_tag_a, GstXmpTagTypeSimple,
280 serialization_func_a, deserialization_func);
282 gst_xmp_tag_create (gst_tag, xmp_tag_b, GstXmpTagTypeSimple,
283 serialization_func_b, deserialization_func);
286 gst_xmp_tag_create (gst_tag, NULL, GstXmpTagTypeCompound, NULL, NULL);
288 xmptag->children = g_slist_prepend (xmptag->children, xmptag_b);
289 xmptag->children = g_slist_prepend (xmptag->children, xmptag_a);
295 _gst_xmp_schema_add_simple_mapping (GstXmpSchema * schema,
296 const gchar * gst_tag, const gchar * xmp_tag, gint xmp_type,
297 XmpSerializationFunc serialization_func,
298 XmpDeserializationFunc deserialization_func)
300 _gst_xmp_schema_add_mapping (schema,
301 gst_xmp_tag_create (gst_tag, xmp_tag, xmp_type, serialization_func,
302 deserialization_func));
306 * We do not return a copy here because elements are
307 * appended, and the API is not public, so we shouldn't
308 * have our lists modified during usage
312 _xmp_tag_get_mapping (const gchar * gst_tag, XmpSerializationData * serdata)
314 GPtrArray *ret = NULL;
316 GQuark key = g_quark_from_string (gst_tag);
317 gpointer iterkey, value;
318 const gchar *schemaname;
320 g_hash_table_iter_init (&iter, __xmp_schemas);
321 while (!ret && g_hash_table_iter_next (&iter, &iterkey, &value)) {
322 GstXmpSchema *schema = (GstXmpSchema *) value;
324 schemaname = g_quark_to_string (GPOINTER_TO_UINT (iterkey));
325 if (xmp_serialization_data_use_schema (serdata, schemaname))
327 (GPtrArray *) gst_xmp_schema_lookup (schema, GUINT_TO_POINTER (key));
333 /* finds the gst tag that maps to this xmp tag in this schema */
335 _gst_xmp_schema_get_mapping_reverse (GstXmpSchema * schema,
336 const gchar * xmp_tag, XmpTag ** _xmp_tag)
340 const gchar *ret = NULL;
342 /* Iterate over the hashtable */
343 g_hash_table_iter_init (&iter, schema);
344 while (!ret && g_hash_table_iter_next (&iter, &key, &value)) {
345 XmpTag *xmpinfo = (XmpTag *) value;
347 if (xmpinfo->tag_name) {
348 if (strcmp (xmpinfo->tag_name, xmp_tag) == 0) {
350 ret = g_quark_to_string (GPOINTER_TO_UINT (key));
353 } else if (xmpinfo->children) {
355 for (iter = xmpinfo->children; iter; iter = g_slist_next (iter)) {
356 XmpTag *child = iter->data;
357 if (strcmp (child->tag_name, xmp_tag) == 0) {
359 ret = g_quark_to_string (GPOINTER_TO_UINT (key));
364 g_assert_not_reached ();
372 /* finds the gst tag that maps to this xmp tag (searches on all schemas) */
374 _gst_xmp_tag_get_mapping_reverse (const gchar * xmp_tag, XmpTag ** _xmp_tag)
378 const gchar *ret = NULL;
380 /* Iterate over the hashtable */
381 g_hash_table_iter_init (&iter, __xmp_schemas);
382 while (!ret && g_hash_table_iter_next (&iter, &key, &value)) {
383 ret = _gst_xmp_schema_get_mapping_reverse ((GstXmpSchema *) value, xmp_tag,
389 /* utility functions/macros */
391 #define METERS_PER_SECOND_TO_KILOMETERS_PER_HOUR (3.6)
392 #define KILOMETERS_PER_HOUR_TO_METERS_PER_SECOND (1/3.6)
393 #define MILES_PER_HOUR_TO_METERS_PER_SECOND (0.44704)
394 #define KNOTS_TO_METERS_PER_SECOND (0.514444)
397 double_to_fraction_string (gdouble num)
402 gst_util_double_to_fraction (num, &frac_n, &frac_d);
403 return g_strdup_printf ("%d/%d", frac_n, frac_d);
406 /* (de)serialize functions */
408 serialize_exif_gps_coordinate (const GValue * value, gchar pos, gchar neg)
413 gchar fraction[G_ASCII_DTOSTR_BUF_SIZE];
415 g_return_val_if_fail (G_VALUE_TYPE (value) == G_TYPE_DOUBLE, NULL);
417 num = g_value_get_double (value);
424 integer = (gint) num;
426 g_ascii_dtostr (fraction, sizeof (fraction), (num - integer) * 60);
428 /* FIXME review GPSCoordinate serialization spec for the .mm or ,ss
429 * decision. Couldn't understand it clearly */
430 return g_strdup_printf ("%d,%s%c", integer, fraction, c);
434 serialize_exif_latitude (const GValue * value)
436 return serialize_exif_gps_coordinate (value, 'N', 'S');
440 serialize_exif_longitude (const GValue * value)
442 return serialize_exif_gps_coordinate (value, 'E', 'W');
446 deserialize_exif_gps_coordinate (XmpTag * xmptag, GstTagList * taglist,
447 const gchar * gst_tag, const gchar * str, gchar pos, gchar neg)
450 gint d = 0, m = 0, s = 0;
453 const gchar *current;
455 /* get the degrees */
456 if (sscanf (str, "%d", &d) != 1)
459 /* find the beginning of the minutes */
460 current = strchr (str, ',');
465 /* check if it uses ,SS or .mm */
466 if (strchr (current, ',') != NULL) {
467 sscanf (current, "%d,%d%c", &m, &s, &c);
469 gchar *copy = g_strdup (current);
470 gint len = strlen (copy);
473 /* check the last letter */
474 for (i = len - 1; len >= 0; len--) {
475 if (g_ascii_isspace (copy[i]))
478 if (g_ascii_isalpha (copy[i])) {
485 /* something is wrong */
491 /* use a copy so we can change the last letter as E can cause
493 m2 = g_ascii_strtod (copy, NULL);
498 /* we can add them all as those that aren't parsed are 0 */
499 value = d + (m / 60.0) + (s / (60.0 * 60.0)) + (m2 / 60.0);
503 } else if (c == neg) {
509 gst_tag_list_add (taglist, xmp_tag_get_merge_mode (xmptag), gst_tag, value,
514 GST_WARNING ("Failed to deserialize gps coordinate: %s", str);
518 deserialize_exif_latitude (XmpTag * xmptag, GstTagList * taglist,
519 const gchar * gst_tag, const gchar * xmp_tag, const gchar * str,
520 GSList ** pending_tags)
522 deserialize_exif_gps_coordinate (xmptag, taglist, gst_tag, str, 'N', 'S');
526 deserialize_exif_longitude (XmpTag * xmptag, GstTagList * taglist,
527 const gchar * gst_tag, const gchar * xmp_tag, const gchar * str,
528 GSList ** pending_tags)
530 deserialize_exif_gps_coordinate (xmptag, taglist, gst_tag, str, 'E', 'W');
534 serialize_exif_altitude (const GValue * value)
538 num = g_value_get_double (value);
543 return double_to_fraction_string (num);
547 serialize_exif_altituderef (const GValue * value)
551 num = g_value_get_double (value);
553 /* 0 means above sea level, 1 means below */
555 return g_strdup ("0");
556 return g_strdup ("1");
560 deserialize_exif_altitude (XmpTag * xmptag, GstTagList * taglist,
561 const gchar * gst_tag, const gchar * xmp_tag, const gchar * str,
562 GSList ** pending_tags)
564 const gchar *altitude_str = NULL;
565 const gchar *altituderef_str = NULL;
571 PendingXmpTag *ptag = NULL;
573 /* find the other missing part */
574 if (strcmp (xmp_tag, "exif:GPSAltitude") == 0) {
577 for (entry = *pending_tags; entry; entry = g_slist_next (entry)) {
578 ptag = (PendingXmpTag *) entry->data;
580 if (strcmp (ptag->xmp_tag->tag_name, "exif:GPSAltitudeRef") == 0) {
581 altituderef_str = ptag->str;
586 } else if (strcmp (xmp_tag, "exif:GPSAltitudeRef") == 0) {
587 altituderef_str = str;
589 for (entry = *pending_tags; entry; entry = g_slist_next (entry)) {
590 ptag = (PendingXmpTag *) entry->data;
592 if (strcmp (ptag->xmp_tag->tag_name, "exif:GPSAltitude") == 0) {
593 altitude_str = ptag->str;
599 GST_WARNING ("Unexpected xmp tag %s", xmp_tag);
604 GST_WARNING ("Missing exif:GPSAltitude tag");
607 if (!altituderef_str) {
608 GST_WARNING ("Missing exif:GPSAltitudeRef tag");
612 if (sscanf (altitude_str, "%d/%d", &frac_n, &frac_d) != 2) {
613 GST_WARNING ("Failed to parse fraction: %s", altitude_str);
617 gst_util_fraction_to_double (frac_n, frac_d, &value);
619 if (altituderef_str[0] == '0') {
621 } else if (altituderef_str[0] == '1') {
624 GST_WARNING ("Unexpected exif:AltitudeRef value: %s", altituderef_str);
628 /* add to the taglist */
629 gst_tag_list_add (taglist, xmp_tag_get_merge_mode (xmptag),
630 GST_TAG_GEO_LOCATION_ELEVATION, value, NULL);
634 g_slice_free (PendingXmpTag, ptag);
635 *pending_tags = g_slist_delete_link (*pending_tags, entry);
639 serialize_exif_gps_speed (const GValue * value)
641 return double_to_fraction_string (g_value_get_double (value) *
642 METERS_PER_SECOND_TO_KILOMETERS_PER_HOUR);
646 serialize_exif_gps_speedref (const GValue * value)
648 /* we always use km/h */
649 return g_strdup ("K");
653 deserialize_exif_gps_speed (XmpTag * xmptag, GstTagList * taglist,
654 const gchar * gst_tag, const gchar * xmp_tag, const gchar * str,
655 GSList ** pending_tags)
657 const gchar *speed_str = NULL;
658 const gchar *speedref_str = NULL;
664 PendingXmpTag *ptag = NULL;
666 /* find the other missing part */
667 if (strcmp (xmp_tag, "exif:GPSSpeed") == 0) {
670 for (entry = *pending_tags; entry; entry = g_slist_next (entry)) {
671 ptag = (PendingXmpTag *) entry->data;
673 if (strcmp (ptag->xmp_tag->tag_name, "exif:GPSSpeedRef") == 0) {
674 speedref_str = ptag->str;
679 } else if (strcmp (xmp_tag, "exif:GPSSpeedRef") == 0) {
682 for (entry = *pending_tags; entry; entry = g_slist_next (entry)) {
683 ptag = (PendingXmpTag *) entry->data;
685 if (strcmp (ptag->xmp_tag->tag_name, "exif:GPSSpeed") == 0) {
686 speed_str = ptag->str;
692 GST_WARNING ("Unexpected xmp tag %s", xmp_tag);
697 GST_WARNING ("Missing exif:GPSSpeed tag");
701 GST_WARNING ("Missing exif:GPSSpeedRef tag");
705 if (sscanf (speed_str, "%d/%d", &frac_n, &frac_d) != 2) {
706 GST_WARNING ("Failed to parse fraction: %s", speed_str);
710 gst_util_fraction_to_double (frac_n, frac_d, &value);
712 if (speedref_str[0] == 'K') {
713 value *= KILOMETERS_PER_HOUR_TO_METERS_PER_SECOND;
714 } else if (speedref_str[0] == 'M') {
715 value *= MILES_PER_HOUR_TO_METERS_PER_SECOND;
716 } else if (speedref_str[0] == 'N') {
717 value *= KNOTS_TO_METERS_PER_SECOND;
719 GST_WARNING ("Unexpected exif:SpeedRef value: %s", speedref_str);
723 /* add to the taglist */
724 gst_tag_list_add (taglist, xmp_tag_get_merge_mode (xmptag),
725 GST_TAG_GEO_LOCATION_MOVEMENT_SPEED, value, NULL);
729 g_slice_free (PendingXmpTag, ptag);
730 *pending_tags = g_slist_delete_link (*pending_tags, entry);
734 serialize_exif_gps_direction (const GValue * value)
736 return double_to_fraction_string (g_value_get_double (value));
740 serialize_exif_gps_directionref (const GValue * value)
742 /* T for true geographic direction (M would mean magnetic) */
743 return g_strdup ("T");
747 deserialize_exif_gps_direction (XmpTag * xmptag, GstTagList * taglist,
748 const gchar * gst_tag, const gchar * xmp_tag, const gchar * str,
749 GSList ** pending_tags, const gchar * direction_tag,
750 const gchar * directionref_tag)
752 const gchar *dir_str = NULL;
753 const gchar *dirref_str = NULL;
759 PendingXmpTag *ptag = NULL;
761 /* find the other missing part */
762 if (strcmp (xmp_tag, direction_tag) == 0) {
765 for (entry = *pending_tags; entry; entry = g_slist_next (entry)) {
766 ptag = (PendingXmpTag *) entry->data;
768 if (strcmp (ptag->xmp_tag->tag_name, directionref_tag) == 0) {
769 dirref_str = ptag->str;
774 } else if (strcmp (xmp_tag, directionref_tag) == 0) {
777 for (entry = *pending_tags; entry; entry = g_slist_next (entry)) {
778 ptag = (PendingXmpTag *) entry->data;
780 if (strcmp (ptag->xmp_tag->tag_name, direction_tag) == 0) {
787 GST_WARNING ("Unexpected xmp tag %s", xmp_tag);
792 GST_WARNING ("Missing %s tag", dir_str);
796 GST_WARNING ("Missing %s tag", dirref_str);
800 if (sscanf (dir_str, "%d/%d", &frac_n, &frac_d) != 2) {
801 GST_WARNING ("Failed to parse fraction: %s", dir_str);
805 gst_util_fraction_to_double (frac_n, frac_d, &value);
807 if (dirref_str[0] == 'T') {
809 } else if (dirref_str[0] == 'M') {
810 GST_WARNING ("Magnetic direction tags aren't supported yet");
813 GST_WARNING ("Unexpected %s value: %s", directionref_tag, dirref_str);
817 /* add to the taglist */
818 gst_tag_list_add (taglist, xmp_tag_get_merge_mode (xmptag), gst_tag, value,
823 g_slice_free (PendingXmpTag, ptag);
824 *pending_tags = g_slist_delete_link (*pending_tags, entry);
828 deserialize_exif_gps_track (XmpTag * xmptag, GstTagList * taglist,
829 const gchar * gst_tag, const gchar * xmp_tag, const gchar * str,
830 GSList ** pending_tags)
832 deserialize_exif_gps_direction (xmptag, taglist, gst_tag, xmp_tag, str,
833 pending_tags, "exif:GPSTrack", "exif:GPSTrackRef");
837 deserialize_exif_gps_img_direction (XmpTag * xmptag, GstTagList * taglist,
838 const gchar * gst_tag, const gchar * xmp_tag, const gchar * str,
839 GSList ** pending_tags)
841 deserialize_exif_gps_direction (xmptag, taglist, gst_tag, xmp_tag, str,
842 pending_tags, "exif:GPSImgDirection", "exif:GPSImgDirectionRef");
846 deserialize_xmp_rating (XmpTag * xmptag, GstTagList * taglist,
847 const gchar * gst_tag, const gchar * xmp_tag, const gchar * str,
848 GSList ** pending_tags)
852 if (sscanf (str, "%u", &value) != 1) {
853 GST_WARNING ("Failed to parse xmp:Rating %s", str);
858 GST_WARNING ("Unsupported Rating tag %u (should be from 0 to 100), "
863 gst_tag_list_add (taglist, xmp_tag_get_merge_mode (xmptag), gst_tag, value,
868 serialize_tiff_orientation (const GValue * value)
873 str = g_value_get_string (value);
875 GST_WARNING ("Failed to get image orientation tag value");
879 num = __exif_tag_image_orientation_to_exif_value (str);
883 return g_strdup_printf ("%d", num);
887 deserialize_tiff_orientation (XmpTag * xmptag, GstTagList * taglist,
888 const gchar * gst_tag, const gchar * xmp_tag, const gchar * str,
889 GSList ** pending_tags)
892 const gchar *orientation = NULL;
894 if (sscanf (str, "%u", &value) != 1) {
895 GST_WARNING ("Failed to parse tiff:Orientation %s", str);
899 if (value < 1 || value > 8) {
900 GST_WARNING ("Invalid tiff:Orientation tag %u (should be from 1 to 8), "
905 orientation = __exif_tag_image_orientation_from_exif_value (value);
906 if (orientation == NULL)
908 gst_tag_list_add (taglist, xmp_tag_get_merge_mode (xmptag), gst_tag,
913 /* look at this page for addtional schemas
914 * http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/XMP.html
917 _init_xmp_tag_map (gpointer user_data)
920 GstXmpSchema *schema;
922 __xmp_schemas = g_hash_table_new (g_direct_hash, g_direct_equal);
925 /* dublic code metadata
926 * http://dublincore.org/documents/dces/
928 schema = gst_xmp_schema_new ();
929 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_ARTIST,
930 "dc:creator", GstXmpTagTypeSeq, NULL, NULL);
931 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_COPYRIGHT,
932 "dc:rights", GstXmpTagTypeSimple, NULL, NULL);
933 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_DATE, "dc:date",
934 GstXmpTagTypeSeq, NULL, NULL);
935 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_DESCRIPTION,
936 "dc:description", GstXmpTagTypeSimple, NULL, NULL);
937 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_KEYWORDS,
938 "dc:subject", GstXmpTagTypeBag, NULL, NULL);
939 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_TITLE, "dc:title",
940 GstXmpTagTypeSimple, NULL, NULL);
941 /* FIXME: we probably want GST_TAG_{,AUDIO_,VIDEO_}MIME_TYPE */
942 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_VIDEO_CODEC,
943 "dc:format", GstXmpTagTypeSimple, NULL, NULL);
944 _gst_xmp_add_schema ("dc", schema);
946 /* xap (xmp) schema */
947 schema = gst_xmp_schema_new ();
948 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_USER_RATING,
949 "xmp:Rating", GstXmpTagTypeSimple, NULL, deserialize_xmp_rating);
950 _gst_xmp_add_schema ("xap", schema);
953 schema = gst_xmp_schema_new ();
954 _gst_xmp_schema_add_simple_mapping (schema,
955 GST_TAG_DEVICE_MANUFACTURER, "tiff:Make", GstXmpTagTypeSimple, NULL,
957 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_DEVICE_MODEL,
958 "tiff:Model", GstXmpTagTypeSimple, NULL, NULL);
959 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_APPLICATION_NAME,
960 "tiff:Software", GstXmpTagTypeSimple, NULL, NULL);
961 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_IMAGE_ORIENTATION,
962 "tiff:Orientation", GstXmpTagTypeSimple, serialize_tiff_orientation,
963 deserialize_tiff_orientation);
964 _gst_xmp_add_schema ("tiff", schema);
967 schema = gst_xmp_schema_new ();
968 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_DATE_TIME,
969 "exif:DateTimeOriginal", GstXmpTagTypeSimple, NULL, NULL);
970 _gst_xmp_schema_add_simple_mapping (schema,
971 GST_TAG_GEO_LOCATION_LATITUDE, "exif:GPSLatitude",
972 GstXmpTagTypeSimple, serialize_exif_latitude, deserialize_exif_latitude);
973 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_GEO_LOCATION_LONGITUDE,
974 "exif:GPSLongitude", GstXmpTagTypeSimple, serialize_exif_longitude,
975 deserialize_exif_longitude);
976 _gst_xmp_schema_add_simple_mapping (schema,
977 GST_TAG_CAPTURING_EXPOSURE_COMPENSATION, "exif:ExposureBiasValue",
978 GstXmpTagTypeSimple, NULL, NULL);
980 /* compound exif tags */
981 xmpinfo = gst_xmp_tag_create_compound (GST_TAG_GEO_LOCATION_ELEVATION,
982 "exif:GPSAltitude", "exif:GPSAltitudeRef", serialize_exif_altitude,
983 serialize_exif_altituderef, deserialize_exif_altitude);
984 _gst_xmp_schema_add_mapping (schema, xmpinfo);
986 xmpinfo = gst_xmp_tag_create_compound (GST_TAG_GEO_LOCATION_MOVEMENT_SPEED,
987 "exif:GPSSpeed", "exif:GPSSpeedRef", serialize_exif_gps_speed,
988 serialize_exif_gps_speedref, deserialize_exif_gps_speed);
989 _gst_xmp_schema_add_mapping (schema, xmpinfo);
992 gst_xmp_tag_create_compound (GST_TAG_GEO_LOCATION_MOVEMENT_DIRECTION,
993 "exif:GPSTrack", "exif:GPSTrackRef", serialize_exif_gps_direction,
994 serialize_exif_gps_directionref, deserialize_exif_gps_track);
995 _gst_xmp_schema_add_mapping (schema, xmpinfo);
997 xmpinfo = gst_xmp_tag_create_compound (GST_TAG_GEO_LOCATION_CAPTURE_DIRECTION,
998 "exif:GPSImgDirection", "exif:GPSImgDirectionRef",
999 serialize_exif_gps_direction, serialize_exif_gps_directionref,
1000 deserialize_exif_gps_img_direction);
1001 _gst_xmp_schema_add_mapping (schema, xmpinfo);
1003 _gst_xmp_add_schema ("exif", schema);
1005 /* photoshop schema */
1006 schema = gst_xmp_schema_new ();
1007 _gst_xmp_schema_add_simple_mapping (schema,
1008 GST_TAG_GEO_LOCATION_COUNTRY, "photoshop:Country",
1009 GstXmpTagTypeSimple, NULL, NULL);
1010 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_GEO_LOCATION_CITY,
1011 "photoshop:City", GstXmpTagTypeSimple, NULL, NULL);
1012 _gst_xmp_add_schema ("photoshop", schema);
1014 /* iptc4xmpcore schema */
1015 schema = gst_xmp_schema_new ();
1016 _gst_xmp_schema_add_simple_mapping (schema,
1017 GST_TAG_GEO_LOCATION_SUBLOCATION, "Iptc4xmpCore:Location",
1018 GstXmpTagTypeSimple, NULL, NULL);
1019 _gst_xmp_add_schema ("Iptc4xmpCore", schema);
1021 /* iptc4xmpext schema */
1022 schema = gst_xmp_schema_new ();
1023 xmpinfo = gst_xmp_tag_create (NULL, "Iptc4xmpExt:LocationShown",
1024 GstXmpTagTypeStruct, NULL, NULL);
1025 xmpinfo->supertype = GstXmpTagTypeBag;
1026 xmpinfo->parse_type = "Resource";
1027 xmpinfo->children = g_slist_prepend (xmpinfo->children,
1028 gst_xmp_tag_create (GST_TAG_GEO_LOCATION_SUBLOCATION,
1029 "LocationDetails:Sublocation", GstXmpTagTypeSimple, NULL, NULL));
1031 g_slist_prepend (xmpinfo->children,
1032 gst_xmp_tag_create (GST_TAG_GEO_LOCATION_CITY,
1033 "LocationDetails:City", GstXmpTagTypeSimple, NULL, NULL));
1035 g_slist_prepend (xmpinfo->children,
1036 gst_xmp_tag_create (GST_TAG_GEO_LOCATION_COUNTRY,
1037 "LocationDetails:Country", GstXmpTagTypeSimple, NULL, NULL));
1038 _gst_xmp_schema_add_mapping (schema, xmpinfo);
1039 _gst_xmp_add_schema ("Iptc4xmpExt", schema);
1045 xmp_tags_initialize ()
1047 static GOnce my_once = G_ONCE_INIT;
1048 g_once (&my_once, (GThreadFunc) _init_xmp_tag_map, NULL);
1051 typedef struct _GstXmpNamespaceMatch GstXmpNamespaceMatch;
1052 struct _GstXmpNamespaceMatch
1054 const gchar *ns_prefix;
1055 const gchar *ns_uri;
1058 * Stores extra namespaces for array tags
1059 * The namespaces should be writen in the form:
1061 * xmlns:XpTo="http://some.org/your/ns/name/ (next ones)"
1063 const gchar *extra_ns;
1066 static const GstXmpNamespaceMatch ns_match[] = {
1067 {"dc", "http://purl.org/dc/elements/1.1/", NULL},
1068 {"exif", "http://ns.adobe.com/exif/1.0/", NULL},
1069 {"tiff", "http://ns.adobe.com/tiff/1.0/", NULL},
1070 {"xap", "http://ns.adobe.com/xap/1.0/", NULL},
1071 {"photoshop", "http://ns.adobe.com/photoshop/1.0/", NULL},
1072 {"Iptc4xmpCore", "http://iptc.org/std/Iptc4xmpCore/1.0/xmlns/", NULL},
1073 {"Iptc4xmpExt", "http://iptc.org/std/Iptc4xmpExt/2008-02-29/",
1074 "xmlns:LocationDetails=\"http://iptc.org/std/Iptc4xmpExt/2008-02-29/LocationDetails/\""},
1078 typedef struct _GstXmpNamespaceMap GstXmpNamespaceMap;
1079 struct _GstXmpNamespaceMap
1081 const gchar *original_ns;
1082 gchar *gstreamer_ns;
1088 read_one_tag (GstTagList * list, XmpTag * xmptag,
1089 const gchar * v, GSList ** pending_tags)
1092 GstTagMergeMode merge_mode;
1093 const gchar *tag = xmptag->gst_tag;
1095 g_return_if_fail (tag != NULL);
1097 if (xmptag->deserialize) {
1098 xmptag->deserialize (xmptag, list, tag, xmptag->tag_name, v, pending_tags);
1102 merge_mode = xmp_tag_get_merge_mode (xmptag);
1103 tag_type = gst_tag_get_type (tag);
1105 /* add gstreamer tag depending on type */
1107 case G_TYPE_STRING:{
1108 gst_tag_list_add (list, merge_mode, tag, v, NULL);
1111 case G_TYPE_DOUBLE:{
1113 gint frac_n, frac_d;
1115 if (sscanf (v, "%d/%d", &frac_n, &frac_d) == 2) {
1116 gst_util_fraction_to_double (frac_n, frac_d, &value);
1117 gst_tag_list_add (list, merge_mode, tag, value, NULL);
1119 GST_WARNING ("Failed to parse fraction: %s", v);
1124 if (tag_type == GST_TYPE_DATE_TIME) {
1125 GstDateTime *datetime = NULL;
1126 gint year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0;
1128 gint gmt_offset_hour = -1, gmt_offset_min = -1, gmt_offset = -1;
1135 GST_WARNING ("Empty string for datetime parsing");
1139 GST_DEBUG ("Parsing %s into a datetime", v);
1141 ret = sscanf (v, "%04d-%02d-%02dT%02d:%02d:%02d.%15s",
1142 &year, &month, &day, &hour, &minute, &second, usec_str);
1144 /* FIXME theoretically, xmp can express datetimes with only year
1145 * or year and month, but gstdatetime doesn't support it */
1146 GST_WARNING ("Invalid datetime value: %s", v);
1149 /* parse the usecs */
1151 gint num_digits = 0;
1153 /* find the number of digits */
1154 while (isdigit ((gint) usec_str[num_digits++]) && num_digits < 6);
1156 if (num_digits > 0) {
1157 /* fill up to 6 digits with 0 */
1158 while (num_digits < 6) {
1159 usec_str[num_digits++] = 0;
1162 g_assert (num_digits == 6);
1164 usec_str[num_digits] = '\0';
1165 usecs = atoi (usec_str);
1169 /* parse the timezone info */
1170 if (v[len - 1] == 'Z') {
1171 GST_LOG ("UTC timezone");
1173 /* Having a Z at the end means UTC */
1174 datetime = gst_date_time_new (0, year, month, day, hour, minute,
1175 second + usecs / 1000000.0);
1177 gchar *plus_pos = NULL;
1178 gchar *neg_pos = NULL;
1181 GST_LOG ("Checking for timezone information");
1183 /* check if there is timezone info */
1184 plus_pos = strrchr (v, '+');
1185 neg_pos = strrchr (v, '-');
1188 } else if (neg_pos) {
1193 gint ret_tz = sscanf (pos, "%d:%d", &gmt_offset_hour,
1196 GST_DEBUG ("Parsing timezone: %s", pos);
1199 gmt_offset = gmt_offset_hour * 60 + gmt_offset_min;
1200 if (neg_pos != NULL && neg_pos + 1 == pos)
1203 GST_LOG ("Timezone offset: %f (%d minutes)", gmt_offset / 60.0,
1206 /* no way to know if it is DST or not */
1208 gst_date_time_new (gmt_offset / 60.0,
1209 year, month, day, hour, minute,
1210 second + usecs / ((gdouble) G_USEC_PER_SEC));
1212 GST_WARNING ("Failed to parse timezone information");
1215 GST_WARNING ("No timezone signal found");
1220 gst_tag_list_add (list, merge_mode, tag, datetime, NULL);
1221 gst_date_time_unref (datetime);
1224 } else if (tag_type == GST_TYPE_DATE) {
1228 /* this is ISO 8601 Date and Time Format
1229 * %F Equivalent to %Y-%m-%d (the ISO 8601 date format). (C99)
1230 * %T The time in 24-hour notation (%H:%M:%S). (SU)
1231 * e.g. 2009-05-30T18:26:14+03:00 */
1233 /* FIXME: this would be the proper way, but needs
1234 #define _XOPEN_SOURCE before #include <time.h>
1236 date = g_date_new ();
1238 strptime (dts, "%FT%TZ", &tm);
1239 g_date_set_time_t (date, mktime(&tm));
1241 /* FIXME: this cannot parse the date
1242 date = g_date_new ();
1243 g_date_set_parse (date, v);
1244 if (g_date_valid (date)) {
1245 gst_tag_list_add (list, merge_mode, tag,
1248 GST_WARNING ("unparsable date: '%s'", v);
1251 /* poor mans straw */
1252 sscanf (v, "%04d-%02d-%02dT", &y, &m, &d);
1253 date = g_date_new_dmy (d, m, y);
1254 gst_tag_list_add (list, merge_mode, tag, date, NULL);
1257 GST_WARNING ("unhandled type for %s from xmp", tag);
1264 * gst_tag_list_from_xmp_buffer:
1267 * Parse a xmp packet into a taglist.
1269 * Returns: new taglist or %NULL, free the list when done
1274 gst_tag_list_from_xmp_buffer (GstBuffer * buffer)
1276 GstTagList *list = NULL;
1277 gchar *xps, *xp1, *xp2, *xpe, *ns, *ne;
1278 gsize len, max_ft_len;
1282 XmpTag *last_xmp_tag = NULL;
1283 GSList *pending_tags = NULL;
1285 /* Used for strucuture xmp tags */
1286 XmpTag *context_tag = NULL;
1288 GstXmpNamespaceMap ns_map[] = {
1299 {"Iptc4xmpCore", NULL}
1301 {"Iptc4xmpExt", NULL}
1306 xmp_tags_initialize ();
1308 g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
1310 GST_LOG ("Starting xmp parsing");
1312 xps = gst_buffer_map (buffer, &len, NULL, GST_MAP_READ);
1313 g_return_val_if_fail (len > 0, NULL);
1315 xpe = &xps[len + 1];
1317 /* check header and footer */
1318 xp1 = g_strstr_len (xps, len, "<?xpacket begin");
1320 goto missing_header;
1321 xp1 = &xp1[strlen ("<?xpacket begin")];
1322 while (*xp1 != '>' && *xp1 != '<' && xp1 < xpe)
1325 goto missing_header;
1327 max_ft_len = 1 + strlen ("<?xpacket end=\".\"?>\n");
1328 if (len < max_ft_len)
1329 goto missing_footer;
1331 GST_DEBUG ("checking footer: [%s]", &xps[len - max_ft_len]);
1332 xp2 = g_strstr_len (&xps[len - max_ft_len], max_ft_len, "<?xpacket ");
1334 goto missing_footer;
1336 GST_INFO ("xmp header okay");
1338 /* skip > and text until first xml-node */
1340 while (*xp1 != '<' && xp1 < xpe)
1343 /* no tag can be longer that the whole buffer */
1344 part = g_malloc (xp2 - xp1);
1345 list = gst_tag_list_new ();
1347 /* parse data into a list of nodes */
1348 /* data is between xp1..xp2 */
1355 while (ne < xp2 && *ne != '>' && *ne != '<') {
1356 if (*ne == '\n' || *ne == '\t' || *ne == ' ') {
1357 while (ne < xp2 && (*ne == '\n' || *ne == '\t' || *ne == ' '))
1368 /* {XML, ns, ne-ns} */
1370 gchar *as = strchr (part, ' ');
1371 /* only log start nodes */
1372 GST_INFO ("xml: %s", part);
1377 /* skip ' ' and scan the attributes */
1381 /* split attr=value pairs */
1382 while (*ae != '\0') {
1384 /* attr/value delimmiter */
1386 } else if (*ae == '"') {
1391 while (*ae != '\0' && *ae != '"')
1396 GST_INFO (" : [%s][%s]", as, v);
1397 if (!strncmp (as, "xmlns:", 6)) {
1399 /* we need to rewrite known namespaces to what we use in
1401 while (ns_match[i].ns_prefix) {
1402 if (!strcmp (ns_match[i].ns_uri, v))
1406 if (ns_match[i].ns_prefix) {
1407 if (strcmp (ns_map[i].original_ns, &as[6])) {
1408 ns_map[i].gstreamer_ns = g_strdup (&as[6]);
1412 XmpTag *xmp_tag = NULL;
1413 /* FIXME: eventualy rewrite ns
1415 * check if ns before ':' is in ns_map and ns_map[i].gstreamer_ns!=NULL
1416 * do 2 stage filter in tag_matches
1421 for (iter = context_tag->children; iter;
1422 iter = g_slist_next (iter)) {
1423 XmpTag *child = iter->data;
1425 GST_DEBUG ("Looking at child tag %s : %s", child->tag_name,
1427 if (strcmp (child->tag_name, as) == 0) {
1434 GST_LOG ("Looking for tag: %s", as);
1435 _gst_xmp_tag_get_mapping_reverse (as, &xmp_tag);
1438 PendingXmpTag *ptag;
1440 GST_DEBUG ("Found xmp tag: %s -> %s", xmp_tag->tag_name,
1443 /* we shouldn't find a xmp structure here */
1444 g_assert (xmp_tag->gst_tag != NULL);
1446 ptag = g_slice_new (PendingXmpTag);
1447 ptag->xmp_tag = xmp_tag;
1448 ptag->str = g_strdup (v);
1450 pending_tags = g_slist_append (pending_tags, ptag);
1453 /* restore chars overwritten by '\0' */
1456 } else if (*ae == '\0' || *ae == ' ') {
1457 /* end of attr/value pair */
1460 /* to next char if not eos */
1466 <dc:type><rdf:Bag><rdf:li>Image</rdf:li></rdf:Bag></dc:type>
1467 <dc:creator><rdf:Seq><rdf:li/></rdf:Seq></dc:creator>
1469 /* FIXME: eventualy rewrite ns */
1471 /* skip rdf tags for now */
1472 if (strncmp (part, "rdf:", 4)) {
1473 /* if we're inside some struct, we look only on its children */
1477 /* check if this is the closing of the context */
1479 && strcmp (part + 1, context_tag->tag_name) == 0) {
1480 GST_DEBUG ("Closing context tag %s", part);
1484 for (iter = context_tag->children; iter;
1485 iter = g_slist_next (iter)) {
1486 XmpTag *child = iter->data;
1488 GST_DEBUG ("Looking at child tag %s : %s", child->tag_name,
1490 if (strcmp (child->tag_name, part) == 0) {
1491 last_xmp_tag = child;
1498 GST_LOG ("Looking for tag: %s", part);
1499 _gst_xmp_tag_get_mapping_reverse (part, &last_xmp_tag);
1500 if (last_xmp_tag && last_xmp_tag->type == GstXmpTagTypeStruct) {
1501 context_tag = last_xmp_tag;
1502 last_xmp_tag = NULL;
1508 GST_LOG ("Next cycle");
1518 while (ne < xp2 && *ne != '<') {
1524 /* {TXT, ns, (ne-ns)-1} */
1525 if (ns[0] != '\n' && &ns[1] <= ne) {
1526 /* only log non-newline nodes, we still have to parse them */
1527 GST_INFO ("txt: %s", part);
1529 PendingXmpTag *ptag;
1531 GST_DEBUG ("Found tag %s -> %s", last_xmp_tag->tag_name,
1532 last_xmp_tag->gst_tag);
1534 if (last_xmp_tag->type == GstXmpTagTypeStruct) {
1535 g_assert (context_tag == NULL); /* we can't handle struct nesting currently */
1537 context_tag = last_xmp_tag;
1539 ptag = g_slice_new (PendingXmpTag);
1540 ptag->xmp_tag = last_xmp_tag;
1541 ptag->str = g_strdup (part);
1543 pending_tags = g_slist_append (pending_tags, ptag);
1554 while (pending_tags) {
1555 PendingXmpTag *ptag = (PendingXmpTag *) pending_tags->data;
1557 pending_tags = g_slist_delete_link (pending_tags, pending_tags);
1559 read_one_tag (list, ptag->xmp_tag, ptag->str, &pending_tags);
1562 g_slice_free (PendingXmpTag, ptag);
1565 GST_INFO ("xmp packet parsed, %d entries",
1566 gst_structure_n_fields ((GstStructure *) list));
1568 /* free resources */
1570 while (ns_map[i].original_ns) {
1571 g_free (ns_map[i].gstreamer_ns);
1576 gst_buffer_unmap (buffer, xps, len);
1582 GST_WARNING ("malformed xmp packet header");
1585 GST_WARNING ("malformed xmp packet footer");
1588 GST_WARNING ("malformed xml tag: %s", part);
1596 string_open_tag (GString * string, const char *tag)
1598 g_string_append_c (string, '<');
1599 g_string_append (string, tag);
1600 g_string_append_c (string, '>');
1604 string_close_tag (GString * string, const char *tag)
1606 g_string_append (string, "</");
1607 g_string_append (string, tag);
1608 g_string_append (string, ">\n");
1612 gst_value_serialize_xmp (const GValue * value)
1614 switch (G_VALUE_TYPE (value)) {
1616 return g_markup_escape_text (g_value_get_string (value), -1);
1618 return g_strdup_printf ("%d", g_value_get_int (value));
1620 return g_strdup_printf ("%u", g_value_get_uint (value));
1622 return double_to_fraction_string (g_value_get_double (value));
1626 /* put non-switchable types here */
1627 if (G_VALUE_TYPE (value) == GST_TYPE_DATE) {
1628 const GDate *date = gst_value_get_date (value);
1630 return g_strdup_printf ("%04d-%02d-%02d",
1631 (gint) g_date_get_year (date), (gint) g_date_get_month (date),
1632 (gint) g_date_get_day (date));
1633 } else if (G_VALUE_TYPE (value) == GST_TYPE_DATE_TIME) {
1634 gint year, month, day, hour, min, sec, microsec;
1635 gfloat gmt_offset = 0;
1636 gint gmt_offset_hour, gmt_offset_min;
1637 GstDateTime *datetime = (GstDateTime *) g_value_get_boxed (value);
1639 year = gst_date_time_get_year (datetime);
1640 month = gst_date_time_get_month (datetime);
1641 day = gst_date_time_get_day (datetime);
1642 hour = gst_date_time_get_hour (datetime);
1643 min = gst_date_time_get_minute (datetime);
1644 sec = gst_date_time_get_second (datetime);
1645 microsec = gst_date_time_get_microsecond (datetime);
1646 gmt_offset = gst_date_time_get_time_zone_offset (datetime);
1647 if (gmt_offset == 0) {
1649 return g_strdup_printf ("%04d-%02d-%02dT%02d:%02d:%02d.%06dZ",
1650 year, month, day, hour, min, sec, microsec);
1652 gmt_offset_hour = ABS (gmt_offset);
1653 gmt_offset_min = (ABS (gmt_offset) - gmt_offset_hour) * 60;
1655 return g_strdup_printf ("%04d-%02d-%02dT%02d:%02d:%02d.%06d%c%02d:%02d",
1656 year, month, day, hour, min, sec, microsec,
1657 gmt_offset >= 0 ? '+' : '-', gmt_offset_hour, gmt_offset_min);
1665 write_one_tag (const GstTagList * list, XmpTag * xmp_tag, gpointer user_data)
1668 XmpSerializationData *serialization_data = user_data;
1669 GString *data = serialization_data->data;
1672 /* struct type handled differently */
1673 if (xmp_tag->type == GstXmpTagTypeStruct ||
1674 xmp_tag->type == GstXmpTagTypeCompound) {
1676 gboolean use_it = FALSE;
1678 /* check if any of the inner tags are present on the taglist */
1679 for (iter = xmp_tag->children; iter && !use_it; iter = g_slist_next (iter)) {
1680 XmpTag *child_tag = iter->data;
1682 if (gst_tag_list_get_value_index (list, child_tag->gst_tag, 0) != NULL) {
1689 if (xmp_tag->tag_name)
1690 string_open_tag (data, xmp_tag->tag_name);
1692 if (xmp_tag->supertype) {
1693 string_open_tag (data, xmp_tag_type_get_name (xmp_tag->supertype));
1694 if (xmp_tag->parse_type) {
1695 g_string_append (data, "<rdf:li rdf:parseType=\"");
1696 g_string_append (data, xmp_tag->parse_type);
1697 g_string_append_c (data, '"');
1698 g_string_append_c (data, '>');
1700 string_open_tag (data, "rdf:li");
1705 for (iter = xmp_tag->children; iter; iter = g_slist_next (iter)) {
1706 write_one_tag (list, iter->data, user_data);
1709 if (xmp_tag->supertype) {
1710 string_close_tag (data, "rdf:li");
1711 string_close_tag (data, xmp_tag_type_get_name (xmp_tag->supertype));
1714 if (xmp_tag->tag_name)
1715 string_close_tag (data, xmp_tag->tag_name);
1720 /* at this point we must have a gst_tag */
1721 g_assert (xmp_tag->gst_tag);
1722 if (gst_tag_list_get_value_index (list, xmp_tag->gst_tag, 0) == NULL)
1725 ct = gst_tag_list_get_tag_size (list, xmp_tag->gst_tag);
1726 string_open_tag (data, xmp_tag->tag_name);
1728 /* fast path for single valued tag */
1729 if (ct == 1 || xmp_tag->type == GstXmpTagTypeSimple) {
1730 if (xmp_tag->serialize) {
1731 s = xmp_tag->serialize (gst_tag_list_get_value_index (list,
1732 xmp_tag->gst_tag, 0));
1734 s = gst_value_serialize_xmp (gst_tag_list_get_value_index (list,
1735 xmp_tag->gst_tag, 0));
1738 g_string_append (data, s);
1741 GST_WARNING ("unhandled type for %s to xmp", xmp_tag->gst_tag);
1744 const gchar *typename;
1746 typename = xmp_tag_type_get_name (xmp_tag->type);
1748 string_open_tag (data, typename);
1749 for (i = 0; i < ct; i++) {
1750 GST_DEBUG ("mapping %s[%u/%u] to xmp", xmp_tag->gst_tag, i, ct);
1751 if (xmp_tag->serialize) {
1752 s = xmp_tag->serialize (gst_tag_list_get_value_index (list,
1753 xmp_tag->gst_tag, i));
1755 s = gst_value_serialize_xmp (gst_tag_list_get_value_index (list,
1756 xmp_tag->gst_tag, i));
1759 string_open_tag (data, "rdf:li");
1760 g_string_append (data, s);
1761 string_close_tag (data, "rdf:li");
1764 GST_WARNING ("unhandled type for %s to xmp", xmp_tag->gst_tag);
1767 string_close_tag (data, typename);
1770 string_close_tag (data, xmp_tag->tag_name);
1774 * gst_tag_list_to_xmp_buffer_full:
1776 * @read_only: does the container forbid inplace editing
1777 * @schemas: %NULL terminated array of schemas to be used on serialization
1779 * Formats a taglist as a xmp packet using only the selected
1780 * schemas. An empty list (%NULL) means that all schemas should
1783 * Returns: new buffer or %NULL, unref the buffer when done
1788 gst_tag_list_to_xmp_buffer_full (const GstTagList * list, gboolean read_only,
1789 const gchar ** schemas)
1791 GstBuffer *buffer = NULL;
1792 XmpSerializationData serialization_data;
1798 serialization_data.data = g_string_sized_new (4096);
1799 serialization_data.schemas = schemas;
1800 data = serialization_data.data;
1802 xmp_tags_initialize ();
1804 g_return_val_if_fail (GST_IS_TAG_LIST (list), NULL);
1807 g_string_append (data,
1808 "<?xpacket begin=\"\xEF\xBB\xBF\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>\n");
1809 g_string_append (data,
1810 "<x:xmpmeta xmlns:x=\"adobe:ns:meta/\" x:xmptk=\"GStreamer\">\n");
1811 g_string_append (data,
1812 "<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"");
1814 while (ns_match[i].ns_prefix) {
1815 if (xmp_serialization_data_use_schema (&serialization_data,
1816 ns_match[i].ns_prefix)) {
1817 g_string_append_printf (data, " xmlns:%s=\"%s\"",
1818 ns_match[i].ns_prefix, ns_match[i].ns_uri);
1819 if (ns_match[i].extra_ns) {
1820 g_string_append_printf (data, " %s", ns_match[i].extra_ns);
1825 g_string_append (data, ">\n");
1826 g_string_append (data, "<rdf:Description rdf:about=\"\">\n");
1828 /* iterate the schemas */
1829 if (schemas == NULL) {
1830 /* use all schemas */
1831 schemas = gst_tag_xmp_list_schemas ();
1833 for (i = 0; schemas[i] != NULL; i++) {
1834 GstXmpSchema *schema = _gst_xmp_get_schema (schemas[i]);
1835 GHashTableIter iter;
1836 gpointer key, value;
1841 /* Iterate over the hashtable */
1842 g_hash_table_iter_init (&iter, schema);
1843 while (g_hash_table_iter_next (&iter, &key, &value)) {
1844 write_one_tag (list, value, (gpointer) & serialization_data);
1849 g_string_append (data, "</rdf:Description>\n");
1850 g_string_append (data, "</rdf:RDF>\n");
1851 g_string_append (data, "</x:xmpmeta>\n");
1854 /* the xmp spec recommand to add 2-4KB padding for in-place editable xmp */
1857 for (i = 0; i < 32; i++) {
1858 g_string_append (data, " " " "
1862 g_string_append_printf (data, "<?xpacket end=\"%c\"?>\n",
1863 (read_only ? 'r' : 'w'));
1865 bsize = data->len + 1;
1866 bdata = g_string_free (data, FALSE);
1868 buffer = gst_buffer_new ();
1869 gst_buffer_take_memory (buffer,
1870 gst_memory_new_wrapped (0, bdata, g_free, bsize, 0, bsize));
1876 * gst_tag_list_to_xmp_buffer:
1878 * @read_only: does the container forbid inplace editing
1880 * Formats a taglist as a xmp packet.
1882 * Returns: new buffer or %NULL, unref the buffer when done
1887 gst_tag_list_to_xmp_buffer (const GstTagList * list, gboolean read_only)
1889 return gst_tag_list_to_xmp_buffer_full (list, read_only, NULL);
1892 #undef gst_xmp_schema_lookup
1893 #undef gst_xmp_schema_insert