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 GstXmpTagTypeSimple = 0,
123 /* Not really a xmp type, this is a tag that in gst is represented with
124 * a single value and on xmp it needs 2 (or more) simple values
126 * e.g. GST_TAG_GEO_LOCATION_ELEVATION needs to be mapped into 2 complementary
127 * tags in the exif's schema. One of them stores the absolute elevation,
128 * and the other one stores if it is above of below sea level.
130 GstXmpTagTypeCompound
135 const gchar *gst_tag;
136 const gchar *tag_name;
139 /* Used for struct and compound types */
142 XmpSerializationFunc serialize;
143 XmpDeserializationFunc deserialize;
146 static GstTagMergeMode
147 xmp_tag_get_merge_mode (XmpTag * xmptag)
149 switch (xmptag->type) {
150 case GstXmpTagTypeBag:
151 case GstXmpTagTypeSeq:
152 return GST_TAG_MERGE_APPEND;
153 case GstXmpTagTypeSimple:
155 return GST_TAG_MERGE_KEEP;
160 xmp_tag_get_type_name (XmpTag * xmptag)
162 switch (xmptag->type) {
163 case GstXmpTagTypeSeq:
165 case GstXmpTagTypeBag:
168 g_assert_not_reached ();
172 struct _PendingXmpTag
177 typedef struct _PendingXmpTag PendingXmpTag;
180 * A schema is a mapping of strings (the tag name in gstreamer) to a list of
181 * tags in xmp (XmpTag).
183 typedef GHashTable GstXmpSchema;
184 #define gst_xmp_schema_lookup g_hash_table_lookup
185 #define gst_xmp_schema_insert g_hash_table_insert
186 static GstXmpSchema *
187 gst_xmp_schema_new ()
189 return g_hash_table_new (g_direct_hash, g_direct_equal);
193 * Mappings from schema names into the schema group of tags (GstXmpSchema)
195 static GHashTable *__xmp_schemas;
197 static GstXmpSchema *
198 _gst_xmp_get_schema (const gchar * name)
201 GstXmpSchema *schema;
203 key = g_quark_from_string (name);
205 schema = g_hash_table_lookup (__xmp_schemas, GUINT_TO_POINTER (key));
207 GST_WARNING ("Schema %s doesn't exist", name);
213 _gst_xmp_add_schema (const gchar * name, GstXmpSchema * schema)
217 key = g_quark_from_string (name);
219 if (g_hash_table_lookup (__xmp_schemas, GUINT_TO_POINTER (key))) {
220 GST_WARNING ("Schema %s already exists, ignoring", name);
221 g_assert_not_reached ();
225 g_hash_table_insert (__xmp_schemas, GUINT_TO_POINTER (key), schema);
229 _gst_xmp_schema_add_mapping (GstXmpSchema * schema, XmpTag * tag)
233 key = g_quark_from_string (tag->gst_tag);
235 if (gst_xmp_schema_lookup (schema, GUINT_TO_POINTER (key))) {
236 GST_WARNING ("Tag %s already present for the schema", tag->gst_tag);
237 g_assert_not_reached ();
240 gst_xmp_schema_insert (schema, GUINT_TO_POINTER (key), tag);
244 gst_xmp_tag_create (const gchar * gst_tag, const gchar * xmp_tag,
245 gint xmp_type, XmpSerializationFunc serialization_func,
246 XmpDeserializationFunc deserialization_func)
250 xmpinfo = g_slice_new (XmpTag);
251 xmpinfo->gst_tag = gst_tag;
252 xmpinfo->tag_name = xmp_tag;
253 xmpinfo->type = xmp_type;
254 xmpinfo->serialize = serialization_func;
255 xmpinfo->deserialize = deserialization_func;
256 xmpinfo->children = NULL;
262 gst_xmp_tag_create_compound (const gchar * gst_tag, const gchar * xmp_tag_a,
263 const gchar * xmp_tag_b, XmpSerializationFunc serialization_func_a,
264 XmpSerializationFunc serialization_func_b,
265 XmpDeserializationFunc deserialization_func)
269 gst_xmp_tag_create (gst_tag, xmp_tag_a, GstXmpTagTypeSimple,
270 serialization_func_a, deserialization_func);
272 gst_xmp_tag_create (gst_tag, xmp_tag_b, GstXmpTagTypeSimple,
273 serialization_func_b, deserialization_func);
276 gst_xmp_tag_create (gst_tag, NULL, GstXmpTagTypeCompound, NULL, NULL);
278 xmptag->children = g_slist_prepend (xmptag->children, xmptag_b);
279 xmptag->children = g_slist_prepend (xmptag->children, xmptag_a);
285 _gst_xmp_schema_add_simple_mapping (GstXmpSchema * schema,
286 const gchar * gst_tag, const gchar * xmp_tag, gint xmp_type,
287 XmpSerializationFunc serialization_func,
288 XmpDeserializationFunc deserialization_func)
290 _gst_xmp_schema_add_mapping (schema,
291 gst_xmp_tag_create (gst_tag, xmp_tag, xmp_type, serialization_func,
292 deserialization_func));
296 * We do not return a copy here because elements are
297 * appended, and the API is not public, so we shouldn't
298 * have our lists modified during usage
302 _xmp_tag_get_mapping (const gchar * gst_tag, XmpSerializationData * serdata)
304 GPtrArray *ret = NULL;
306 GQuark key = g_quark_from_string (gst_tag);
307 gpointer iterkey, value;
308 const gchar *schemaname;
310 g_hash_table_iter_init (&iter, __xmp_schemas);
311 while (!ret && g_hash_table_iter_next (&iter, &iterkey, &value)) {
312 GstXmpSchema *schema = (GstXmpSchema *) value;
314 schemaname = g_quark_to_string (GPOINTER_TO_UINT (iterkey));
315 if (xmp_serialization_data_use_schema (serdata, schemaname))
317 (GPtrArray *) gst_xmp_schema_lookup (schema, GUINT_TO_POINTER (key));
323 /* finds the gst tag that maps to this xmp tag in this schema */
325 _gst_xmp_schema_get_mapping_reverse (GstXmpSchema * schema,
326 const gchar * xmp_tag, XmpTag ** _xmp_tag)
330 const gchar *ret = NULL;
332 /* Iterate over the hashtable */
333 g_hash_table_iter_init (&iter, schema);
334 while (!ret && g_hash_table_iter_next (&iter, &key, &value)) {
335 XmpTag *xmpinfo = (XmpTag *) value;
337 if (xmpinfo->tag_name) {
338 if (strcmp (xmpinfo->tag_name, xmp_tag) == 0) {
340 ret = g_quark_to_string (GPOINTER_TO_UINT (key));
343 } else if (xmpinfo->children) {
345 for (iter = xmpinfo->children; iter; iter = g_slist_next (iter)) {
346 XmpTag *child = iter->data;
347 if (strcmp (child->tag_name, xmp_tag) == 0) {
349 ret = g_quark_to_string (GPOINTER_TO_UINT (key));
354 g_assert_not_reached ();
362 /* finds the gst tag that maps to this xmp tag (searches on all schemas) */
364 _gst_xmp_tag_get_mapping_reverse (const gchar * xmp_tag, XmpTag ** _xmp_tag)
368 const gchar *ret = NULL;
370 /* Iterate over the hashtable */
371 g_hash_table_iter_init (&iter, __xmp_schemas);
372 while (!ret && g_hash_table_iter_next (&iter, &key, &value)) {
373 ret = _gst_xmp_schema_get_mapping_reverse ((GstXmpSchema *) value, xmp_tag,
379 /* utility functions/macros */
381 #define METERS_PER_SECOND_TO_KILOMETERS_PER_HOUR (3.6)
382 #define KILOMETERS_PER_HOUR_TO_METERS_PER_SECOND (1/3.6)
383 #define MILES_PER_HOUR_TO_METERS_PER_SECOND (0.44704)
384 #define KNOTS_TO_METERS_PER_SECOND (0.514444)
387 double_to_fraction_string (gdouble num)
392 gst_util_double_to_fraction (num, &frac_n, &frac_d);
393 return g_strdup_printf ("%d/%d", frac_n, frac_d);
396 /* (de)serialize functions */
398 serialize_exif_gps_coordinate (const GValue * value, gchar pos, gchar neg)
403 gchar fraction[G_ASCII_DTOSTR_BUF_SIZE];
405 g_return_val_if_fail (G_VALUE_TYPE (value) == G_TYPE_DOUBLE, NULL);
407 num = g_value_get_double (value);
414 integer = (gint) num;
416 g_ascii_dtostr (fraction, sizeof (fraction), (num - integer) * 60);
418 /* FIXME review GPSCoordinate serialization spec for the .mm or ,ss
419 * decision. Couldn't understand it clearly */
420 return g_strdup_printf ("%d,%s%c", integer, fraction, c);
424 serialize_exif_latitude (const GValue * value)
426 return serialize_exif_gps_coordinate (value, 'N', 'S');
430 serialize_exif_longitude (const GValue * value)
432 return serialize_exif_gps_coordinate (value, 'E', 'W');
436 deserialize_exif_gps_coordinate (XmpTag * xmptag, GstTagList * taglist,
437 const gchar * gst_tag, const gchar * str, gchar pos, gchar neg)
440 gint d = 0, m = 0, s = 0;
443 const gchar *current;
445 /* get the degrees */
446 if (sscanf (str, "%d", &d) != 1)
449 /* find the beginning of the minutes */
450 current = strchr (str, ',');
455 /* check if it uses ,SS or .mm */
456 if (strchr (current, ',') != NULL) {
457 sscanf (current, "%d,%d%c", &m, &s, &c);
459 gchar *copy = g_strdup (current);
460 gint len = strlen (copy);
463 /* check the last letter */
464 for (i = len - 1; len >= 0; len--) {
465 if (g_ascii_isspace (copy[i]))
468 if (g_ascii_isalpha (copy[i])) {
475 /* something is wrong */
481 /* use a copy so we can change the last letter as E can cause
483 m2 = g_ascii_strtod (copy, NULL);
488 /* we can add them all as those that aren't parsed are 0 */
489 value = d + (m / 60.0) + (s / (60.0 * 60.0)) + (m2 / 60.0);
493 } else if (c == neg) {
499 gst_tag_list_add (taglist, xmp_tag_get_merge_mode (xmptag), gst_tag, value,
504 GST_WARNING ("Failed to deserialize gps coordinate: %s", str);
508 deserialize_exif_latitude (XmpTag * xmptag, GstTagList * taglist,
509 const gchar * gst_tag, const gchar * xmp_tag, const gchar * str,
510 GSList ** pending_tags)
512 deserialize_exif_gps_coordinate (xmptag, taglist, gst_tag, str, 'N', 'S');
516 deserialize_exif_longitude (XmpTag * xmptag, GstTagList * taglist,
517 const gchar * gst_tag, const gchar * xmp_tag, const gchar * str,
518 GSList ** pending_tags)
520 deserialize_exif_gps_coordinate (xmptag, taglist, gst_tag, str, 'E', 'W');
524 serialize_exif_altitude (const GValue * value)
528 num = g_value_get_double (value);
533 return double_to_fraction_string (num);
537 serialize_exif_altituderef (const GValue * value)
541 num = g_value_get_double (value);
543 /* 0 means above sea level, 1 means below */
545 return g_strdup ("0");
546 return g_strdup ("1");
550 deserialize_exif_altitude (XmpTag * xmptag, GstTagList * taglist,
551 const gchar * gst_tag, const gchar * xmp_tag, const gchar * str,
552 GSList ** pending_tags)
554 const gchar *altitude_str = NULL;
555 const gchar *altituderef_str = NULL;
561 PendingXmpTag *ptag = NULL;
563 /* find the other missing part */
564 if (strcmp (xmp_tag, "exif:GPSAltitude") == 0) {
567 for (entry = *pending_tags; entry; entry = g_slist_next (entry)) {
568 ptag = (PendingXmpTag *) entry->data;
570 if (strcmp (ptag->xmp_tag->tag_name, "exif:GPSAltitudeRef") == 0) {
571 altituderef_str = ptag->str;
576 } else if (strcmp (xmp_tag, "exif:GPSAltitudeRef") == 0) {
577 altituderef_str = str;
579 for (entry = *pending_tags; entry; entry = g_slist_next (entry)) {
580 ptag = (PendingXmpTag *) entry->data;
582 if (strcmp (ptag->xmp_tag->tag_name, "exif:GPSAltitude") == 0) {
583 altitude_str = ptag->str;
589 GST_WARNING ("Unexpected xmp tag %s", xmp_tag);
594 GST_WARNING ("Missing exif:GPSAltitude tag");
597 if (!altituderef_str) {
598 GST_WARNING ("Missing exif:GPSAltitudeRef tag");
602 if (sscanf (altitude_str, "%d/%d", &frac_n, &frac_d) != 2) {
603 GST_WARNING ("Failed to parse fraction: %s", altitude_str);
607 gst_util_fraction_to_double (frac_n, frac_d, &value);
609 if (altituderef_str[0] == '0') {
611 } else if (altituderef_str[0] == '1') {
614 GST_WARNING ("Unexpected exif:AltitudeRef value: %s", altituderef_str);
618 /* add to the taglist */
619 gst_tag_list_add (taglist, xmp_tag_get_merge_mode (xmptag),
620 GST_TAG_GEO_LOCATION_ELEVATION, value, NULL);
624 g_slice_free (PendingXmpTag, ptag);
625 *pending_tags = g_slist_delete_link (*pending_tags, entry);
629 serialize_exif_gps_speed (const GValue * value)
631 return double_to_fraction_string (g_value_get_double (value) *
632 METERS_PER_SECOND_TO_KILOMETERS_PER_HOUR);
636 serialize_exif_gps_speedref (const GValue * value)
638 /* we always use km/h */
639 return g_strdup ("K");
643 deserialize_exif_gps_speed (XmpTag * xmptag, GstTagList * taglist,
644 const gchar * gst_tag, const gchar * xmp_tag, const gchar * str,
645 GSList ** pending_tags)
647 const gchar *speed_str = NULL;
648 const gchar *speedref_str = NULL;
654 PendingXmpTag *ptag = NULL;
656 /* find the other missing part */
657 if (strcmp (xmp_tag, "exif:GPSSpeed") == 0) {
660 for (entry = *pending_tags; entry; entry = g_slist_next (entry)) {
661 ptag = (PendingXmpTag *) entry->data;
663 if (strcmp (ptag->xmp_tag->tag_name, "exif:GPSSpeedRef") == 0) {
664 speedref_str = ptag->str;
669 } else if (strcmp (xmp_tag, "exif:GPSSpeedRef") == 0) {
672 for (entry = *pending_tags; entry; entry = g_slist_next (entry)) {
673 ptag = (PendingXmpTag *) entry->data;
675 if (strcmp (ptag->xmp_tag->tag_name, "exif:GPSSpeed") == 0) {
676 speed_str = ptag->str;
682 GST_WARNING ("Unexpected xmp tag %s", xmp_tag);
687 GST_WARNING ("Missing exif:GPSSpeed tag");
691 GST_WARNING ("Missing exif:GPSSpeedRef tag");
695 if (sscanf (speed_str, "%d/%d", &frac_n, &frac_d) != 2) {
696 GST_WARNING ("Failed to parse fraction: %s", speed_str);
700 gst_util_fraction_to_double (frac_n, frac_d, &value);
702 if (speedref_str[0] == 'K') {
703 value *= KILOMETERS_PER_HOUR_TO_METERS_PER_SECOND;
704 } else if (speedref_str[0] == 'M') {
705 value *= MILES_PER_HOUR_TO_METERS_PER_SECOND;
706 } else if (speedref_str[0] == 'N') {
707 value *= KNOTS_TO_METERS_PER_SECOND;
709 GST_WARNING ("Unexpected exif:SpeedRef value: %s", speedref_str);
713 /* add to the taglist */
714 gst_tag_list_add (taglist, xmp_tag_get_merge_mode (xmptag),
715 GST_TAG_GEO_LOCATION_MOVEMENT_SPEED, value, NULL);
719 g_slice_free (PendingXmpTag, ptag);
720 *pending_tags = g_slist_delete_link (*pending_tags, entry);
724 serialize_exif_gps_direction (const GValue * value)
726 return double_to_fraction_string (g_value_get_double (value));
730 serialize_exif_gps_directionref (const GValue * value)
732 /* T for true geographic direction (M would mean magnetic) */
733 return g_strdup ("T");
737 deserialize_exif_gps_direction (XmpTag * xmptag, GstTagList * taglist,
738 const gchar * gst_tag, const gchar * xmp_tag, const gchar * str,
739 GSList ** pending_tags, const gchar * direction_tag,
740 const gchar * directionref_tag)
742 const gchar *dir_str = NULL;
743 const gchar *dirref_str = NULL;
749 PendingXmpTag *ptag = NULL;
751 /* find the other missing part */
752 if (strcmp (xmp_tag, direction_tag) == 0) {
755 for (entry = *pending_tags; entry; entry = g_slist_next (entry)) {
756 ptag = (PendingXmpTag *) entry->data;
758 if (strcmp (ptag->xmp_tag->tag_name, directionref_tag) == 0) {
759 dirref_str = ptag->str;
764 } else if (strcmp (xmp_tag, directionref_tag) == 0) {
767 for (entry = *pending_tags; entry; entry = g_slist_next (entry)) {
768 ptag = (PendingXmpTag *) entry->data;
770 if (strcmp (ptag->xmp_tag->tag_name, direction_tag) == 0) {
777 GST_WARNING ("Unexpected xmp tag %s", xmp_tag);
782 GST_WARNING ("Missing %s tag", dir_str);
786 GST_WARNING ("Missing %s tag", dirref_str);
790 if (sscanf (dir_str, "%d/%d", &frac_n, &frac_d) != 2) {
791 GST_WARNING ("Failed to parse fraction: %s", dir_str);
795 gst_util_fraction_to_double (frac_n, frac_d, &value);
797 if (dirref_str[0] == 'T') {
799 } else if (dirref_str[0] == 'M') {
800 GST_WARNING ("Magnetic direction tags aren't supported yet");
803 GST_WARNING ("Unexpected %s value: %s", directionref_tag, dirref_str);
807 /* add to the taglist */
808 gst_tag_list_add (taglist, xmp_tag_get_merge_mode (xmptag), gst_tag, value,
813 g_slice_free (PendingXmpTag, ptag);
814 *pending_tags = g_slist_delete_link (*pending_tags, entry);
818 deserialize_exif_gps_track (XmpTag * xmptag, GstTagList * taglist,
819 const gchar * gst_tag, const gchar * xmp_tag, const gchar * str,
820 GSList ** pending_tags)
822 deserialize_exif_gps_direction (xmptag, taglist, gst_tag, xmp_tag, str,
823 pending_tags, "exif:GPSTrack", "exif:GPSTrackRef");
827 deserialize_exif_gps_img_direction (XmpTag * xmptag, GstTagList * taglist,
828 const gchar * gst_tag, const gchar * xmp_tag, const gchar * str,
829 GSList ** pending_tags)
831 deserialize_exif_gps_direction (xmptag, taglist, gst_tag, xmp_tag, str,
832 pending_tags, "exif:GPSImgDirection", "exif:GPSImgDirectionRef");
836 deserialize_xmp_rating (XmpTag * xmptag, GstTagList * taglist,
837 const gchar * gst_tag, const gchar * xmp_tag, const gchar * str,
838 GSList ** pending_tags)
842 if (sscanf (str, "%u", &value) != 1) {
843 GST_WARNING ("Failed to parse xmp:Rating %s", str);
848 GST_WARNING ("Unsupported Rating tag %u (should be from 0 to 100), "
853 gst_tag_list_add (taglist, xmp_tag_get_merge_mode (xmptag), gst_tag, value,
858 serialize_tiff_orientation (const GValue * value)
863 str = g_value_get_string (value);
865 GST_WARNING ("Failed to get image orientation tag value");
869 num = __exif_tag_image_orientation_to_exif_value (str);
873 return g_strdup_printf ("%d", num);
877 deserialize_tiff_orientation (XmpTag * xmptag, GstTagList * taglist,
878 const gchar * gst_tag, const gchar * xmp_tag, const gchar * str,
879 GSList ** pending_tags)
882 const gchar *orientation = NULL;
884 if (sscanf (str, "%u", &value) != 1) {
885 GST_WARNING ("Failed to parse tiff:Orientation %s", str);
889 if (value < 1 || value > 8) {
890 GST_WARNING ("Invalid tiff:Orientation tag %u (should be from 1 to 8), "
895 orientation = __exif_tag_image_orientation_from_exif_value (value);
896 if (orientation == NULL)
898 gst_tag_list_add (taglist, xmp_tag_get_merge_mode (xmptag), gst_tag,
903 /* look at this page for addtional schemas
904 * http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/XMP.html
907 _init_xmp_tag_map (gpointer user_data)
910 GstXmpSchema *schema;
912 __xmp_schemas = g_hash_table_new (g_direct_hash, g_direct_equal);
915 /* dublic code metadata
916 * http://dublincore.org/documents/dces/
918 schema = gst_xmp_schema_new ();
919 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_ARTIST,
920 "dc:creator", GstXmpTagTypeSeq, NULL, NULL);
921 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_COPYRIGHT,
922 "dc:rights", GstXmpTagTypeSimple, NULL, NULL);
923 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_DATE, "dc:date",
924 GstXmpTagTypeSeq, NULL, NULL);
925 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_DESCRIPTION,
926 "dc:description", GstXmpTagTypeSimple, NULL, NULL);
927 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_KEYWORDS,
928 "dc:subject", GstXmpTagTypeBag, NULL, NULL);
929 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_TITLE, "dc:title",
930 GstXmpTagTypeSimple, NULL, NULL);
931 /* FIXME: we probably want GST_TAG_{,AUDIO_,VIDEO_}MIME_TYPE */
932 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_VIDEO_CODEC,
933 "dc:format", GstXmpTagTypeSimple, NULL, NULL);
934 _gst_xmp_add_schema ("dc", schema);
936 /* xap (xmp) schema */
937 schema = gst_xmp_schema_new ();
938 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_USER_RATING,
939 "xmp:Rating", GstXmpTagTypeSimple, NULL, deserialize_xmp_rating);
940 _gst_xmp_add_schema ("xap", schema);
943 schema = gst_xmp_schema_new ();
944 _gst_xmp_schema_add_simple_mapping (schema,
945 GST_TAG_DEVICE_MANUFACTURER, "tiff:Make", GstXmpTagTypeSimple, NULL,
947 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_DEVICE_MODEL,
948 "tiff:Model", GstXmpTagTypeSimple, NULL, NULL);
949 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_APPLICATION_NAME,
950 "tiff:Software", GstXmpTagTypeSimple, NULL, NULL);
951 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_IMAGE_ORIENTATION,
952 "tiff:Orientation", GstXmpTagTypeSimple, serialize_tiff_orientation,
953 deserialize_tiff_orientation);
954 _gst_xmp_add_schema ("tiff", schema);
957 schema = gst_xmp_schema_new ();
958 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_DATE_TIME,
959 "exif:DateTimeOriginal", GstXmpTagTypeSimple, NULL, NULL);
960 _gst_xmp_schema_add_simple_mapping (schema,
961 GST_TAG_GEO_LOCATION_LATITUDE, "exif:GPSLatitude",
962 GstXmpTagTypeSimple, serialize_exif_latitude, deserialize_exif_latitude);
963 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_GEO_LOCATION_LONGITUDE,
964 "exif:GPSLongitude", GstXmpTagTypeSimple, serialize_exif_longitude,
965 deserialize_exif_longitude);
966 _gst_xmp_schema_add_simple_mapping (schema,
967 GST_TAG_CAPTURING_EXPOSURE_COMPENSATION, "exif:ExposureBiasValue",
968 GstXmpTagTypeSimple, NULL, NULL);
970 /* compound exif tags */
971 xmpinfo = gst_xmp_tag_create_compound (GST_TAG_GEO_LOCATION_ELEVATION,
972 "exif:GPSAltitude", "exif:GPSAltitudeRef", serialize_exif_altitude,
973 serialize_exif_altituderef, deserialize_exif_altitude);
974 _gst_xmp_schema_add_mapping (schema, xmpinfo);
976 xmpinfo = gst_xmp_tag_create_compound (GST_TAG_GEO_LOCATION_MOVEMENT_SPEED,
977 "exif:GPSSpeed", "exif:GPSSpeedRef", serialize_exif_gps_speed,
978 serialize_exif_gps_speedref, deserialize_exif_gps_speed);
979 _gst_xmp_schema_add_mapping (schema, xmpinfo);
982 gst_xmp_tag_create_compound (GST_TAG_GEO_LOCATION_MOVEMENT_DIRECTION,
983 "exif:GPSTrack", "exif:GPSTrackRef", serialize_exif_gps_direction,
984 serialize_exif_gps_directionref, deserialize_exif_gps_track);
985 _gst_xmp_schema_add_mapping (schema, xmpinfo);
987 xmpinfo = gst_xmp_tag_create_compound (GST_TAG_GEO_LOCATION_CAPTURE_DIRECTION,
988 "exif:GPSImgDirection", "exif:GPSImgDirectionRef",
989 serialize_exif_gps_direction, serialize_exif_gps_directionref,
990 deserialize_exif_gps_img_direction);
991 _gst_xmp_schema_add_mapping (schema, xmpinfo);
993 _gst_xmp_add_schema ("exif", schema);
995 /* photoshop schema */
996 schema = gst_xmp_schema_new ();
997 _gst_xmp_schema_add_simple_mapping (schema,
998 GST_TAG_GEO_LOCATION_COUNTRY, "photoshop:Country",
999 GstXmpTagTypeSimple, NULL, NULL);
1000 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_GEO_LOCATION_CITY,
1001 "photoshop:City", GstXmpTagTypeSimple, NULL, NULL);
1002 _gst_xmp_add_schema ("photoshop", schema);
1004 /* iptc4xmpcore schema */
1005 schema = gst_xmp_schema_new ();
1006 _gst_xmp_schema_add_simple_mapping (schema,
1007 GST_TAG_GEO_LOCATION_SUBLOCATION, "Iptc4xmpCore:Location",
1008 GstXmpTagTypeSimple, NULL, NULL);
1009 _gst_xmp_add_schema ("Iptc4xmpCore", schema);
1011 /* iptc4xmpext schema */
1012 schema = gst_xmp_schema_new ();
1013 xmpinfo = gst_xmp_tag_create (NULL, "Iptc4xmpExt:LocationShown",
1014 GstXmpTagTypeStruct, NULL, NULL);
1015 xmpinfo->children = g_slist_prepend (xmpinfo->children,
1016 gst_xmp_tag_create (GST_TAG_GEO_LOCATION_SUBLOCATION,
1017 "LocationDetails:Sublocation", GstXmpTagTypeSimple, NULL, NULL));
1019 g_slist_prepend (xmpinfo->children,
1020 gst_xmp_tag_create (GST_TAG_GEO_LOCATION_CITY,
1021 "LocationDetails:City", GstXmpTagTypeSimple, NULL, NULL));
1023 g_slist_prepend (xmpinfo->children,
1024 gst_xmp_tag_create (GST_TAG_GEO_LOCATION_COUNTRY,
1025 "LocationDetails:Country", GstXmpTagTypeSimple, NULL, NULL));
1026 _gst_xmp_schema_add_mapping (schema, xmpinfo);
1027 _gst_xmp_add_schema ("Iptc4xmpExt", schema);
1033 xmp_tags_initialize ()
1035 static GOnce my_once = G_ONCE_INIT;
1036 g_once (&my_once, (GThreadFunc) _init_xmp_tag_map, NULL);
1039 typedef struct _GstXmpNamespaceMatch GstXmpNamespaceMatch;
1040 struct _GstXmpNamespaceMatch
1042 const gchar *ns_prefix;
1043 const gchar *ns_uri;
1046 static const GstXmpNamespaceMatch ns_match[] = {
1047 {"dc", "http://purl.org/dc/elements/1.1/"},
1048 {"exif", "http://ns.adobe.com/exif/1.0/"},
1049 {"tiff", "http://ns.adobe.com/tiff/1.0/"},
1050 {"xap", "http://ns.adobe.com/xap/1.0/"},
1051 {"photoshop", "http://ns.adobe.com/photoshop/1.0/"},
1052 {"Iptc4xmpCore", "http://iptc.org/std/Iptc4xmpCore/1.0/xmlns/"},
1053 {"Iptc4xmpExt", "http://iptc.org/std/Iptc4xmpExt/2008-02-29/"},
1057 typedef struct _GstXmpNamespaceMap GstXmpNamespaceMap;
1058 struct _GstXmpNamespaceMap
1060 const gchar *original_ns;
1061 gchar *gstreamer_ns;
1067 read_one_tag (GstTagList * list, XmpTag * xmptag,
1068 const gchar * v, GSList ** pending_tags)
1071 GstTagMergeMode merge_mode;
1072 const gchar *tag = xmptag->gst_tag;
1074 g_return_if_fail (tag != NULL);
1076 if (xmptag->deserialize) {
1077 xmptag->deserialize (xmptag, list, tag, xmptag->tag_name, v, pending_tags);
1081 merge_mode = xmp_tag_get_merge_mode (xmptag);
1082 tag_type = gst_tag_get_type (tag);
1084 /* add gstreamer tag depending on type */
1086 case G_TYPE_STRING:{
1087 gst_tag_list_add (list, merge_mode, tag, v, NULL);
1090 case G_TYPE_DOUBLE:{
1092 gint frac_n, frac_d;
1094 if (sscanf (v, "%d/%d", &frac_n, &frac_d) == 2) {
1095 gst_util_fraction_to_double (frac_n, frac_d, &value);
1096 gst_tag_list_add (list, merge_mode, tag, value, NULL);
1098 GST_WARNING ("Failed to parse fraction: %s", v);
1103 if (tag_type == GST_TYPE_DATE_TIME) {
1104 GstDateTime *datetime = NULL;
1105 gint year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0;
1107 gint gmt_offset_hour = -1, gmt_offset_min = -1, gmt_offset = -1;
1114 GST_WARNING ("Empty string for datetime parsing");
1118 GST_DEBUG ("Parsing %s into a datetime", v);
1120 ret = sscanf (v, "%04d-%02d-%02dT%02d:%02d:%02d.%15s",
1121 &year, &month, &day, &hour, &minute, &second, usec_str);
1123 /* FIXME theoretically, xmp can express datetimes with only year
1124 * or year and month, but gstdatetime doesn't support it */
1125 GST_WARNING ("Invalid datetime value: %s", v);
1128 /* parse the usecs */
1130 gint num_digits = 0;
1132 /* find the number of digits */
1133 while (isdigit ((gint) usec_str[num_digits++]) && num_digits < 6);
1135 if (num_digits > 0) {
1136 /* fill up to 6 digits with 0 */
1137 while (num_digits < 6) {
1138 usec_str[num_digits++] = 0;
1141 g_assert (num_digits == 6);
1143 usec_str[num_digits] = '\0';
1144 usecs = atoi (usec_str);
1148 /* parse the timezone info */
1149 if (v[len - 1] == 'Z') {
1150 GST_LOG ("UTC timezone");
1152 /* Having a Z at the end means UTC */
1153 datetime = gst_date_time_new (0, year, month, day, hour, minute,
1154 second + usecs / 1000000.0);
1156 gchar *plus_pos = NULL;
1157 gchar *neg_pos = NULL;
1160 GST_LOG ("Checking for timezone information");
1162 /* check if there is timezone info */
1163 plus_pos = strrchr (v, '+');
1164 neg_pos = strrchr (v, '-');
1167 } else if (neg_pos) {
1172 gint ret_tz = sscanf (pos, "%d:%d", &gmt_offset_hour,
1175 GST_DEBUG ("Parsing timezone: %s", pos);
1178 gmt_offset = gmt_offset_hour * 60 + gmt_offset_min;
1179 if (neg_pos != NULL && neg_pos + 1 == pos)
1182 GST_LOG ("Timezone offset: %f (%d minutes)", gmt_offset / 60.0,
1185 /* no way to know if it is DST or not */
1187 gst_date_time_new (gmt_offset / 60.0,
1188 year, month, day, hour, minute,
1189 second + usecs / ((gdouble) G_USEC_PER_SEC));
1191 GST_WARNING ("Failed to parse timezone information");
1194 GST_WARNING ("No timezone signal found");
1199 gst_tag_list_add (list, merge_mode, tag, datetime, NULL);
1200 gst_date_time_unref (datetime);
1203 } else if (tag_type == GST_TYPE_DATE) {
1207 /* this is ISO 8601 Date and Time Format
1208 * %F Equivalent to %Y-%m-%d (the ISO 8601 date format). (C99)
1209 * %T The time in 24-hour notation (%H:%M:%S). (SU)
1210 * e.g. 2009-05-30T18:26:14+03:00 */
1212 /* FIXME: this would be the proper way, but needs
1213 #define _XOPEN_SOURCE before #include <time.h>
1215 date = g_date_new ();
1217 strptime (dts, "%FT%TZ", &tm);
1218 g_date_set_time_t (date, mktime(&tm));
1220 /* FIXME: this cannot parse the date
1221 date = g_date_new ();
1222 g_date_set_parse (date, v);
1223 if (g_date_valid (date)) {
1224 gst_tag_list_add (list, merge_mode, tag,
1227 GST_WARNING ("unparsable date: '%s'", v);
1230 /* poor mans straw */
1231 sscanf (v, "%04d-%02d-%02dT", &y, &m, &d);
1232 date = g_date_new_dmy (d, m, y);
1233 gst_tag_list_add (list, merge_mode, tag, date, NULL);
1236 GST_WARNING ("unhandled type for %s from xmp", tag);
1243 * gst_tag_list_from_xmp_buffer:
1246 * Parse a xmp packet into a taglist.
1248 * Returns: new taglist or %NULL, free the list when done
1253 gst_tag_list_from_xmp_buffer (GstBuffer * buffer)
1255 GstTagList *list = NULL;
1256 gchar *xps, *xp1, *xp2, *xpe, *ns, *ne;
1257 gsize len, max_ft_len;
1261 XmpTag *last_xmp_tag = NULL;
1262 GSList *pending_tags = NULL;
1264 /* Used for strucuture xmp tags */
1265 XmpTag *context_tag = NULL;
1267 GstXmpNamespaceMap ns_map[] = {
1278 {"Iptc4xmpCore", NULL}
1280 {"Iptc4xmpExt", NULL}
1285 xmp_tags_initialize ();
1287 g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
1289 GST_LOG ("Starting xmp parsing");
1291 xps = gst_buffer_map (buffer, &len, NULL, GST_MAP_READ);
1292 g_return_val_if_fail (len > 0, NULL);
1294 xpe = &xps[len + 1];
1296 /* check header and footer */
1297 xp1 = g_strstr_len (xps, len, "<?xpacket begin");
1299 goto missing_header;
1300 xp1 = &xp1[strlen ("<?xpacket begin")];
1301 while (*xp1 != '>' && *xp1 != '<' && xp1 < xpe)
1304 goto missing_header;
1306 max_ft_len = 1 + strlen ("<?xpacket end=\".\"?>\n");
1307 if (len < max_ft_len)
1308 goto missing_footer;
1310 GST_DEBUG ("checking footer: [%s]", &xps[len - max_ft_len]);
1311 xp2 = g_strstr_len (&xps[len - max_ft_len], max_ft_len, "<?xpacket ");
1313 goto missing_footer;
1315 GST_INFO ("xmp header okay");
1317 /* skip > and text until first xml-node */
1319 while (*xp1 != '<' && xp1 < xpe)
1322 /* no tag can be longer that the whole buffer */
1323 part = g_malloc (xp2 - xp1);
1324 list = gst_tag_list_new ();
1326 /* parse data into a list of nodes */
1327 /* data is between xp1..xp2 */
1334 while (ne < xp2 && *ne != '>' && *ne != '<') {
1335 if (*ne == '\n' || *ne == '\t' || *ne == ' ') {
1336 while (ne < xp2 && (*ne == '\n' || *ne == '\t' || *ne == ' '))
1347 /* {XML, ns, ne-ns} */
1349 gchar *as = strchr (part, ' ');
1350 /* only log start nodes */
1351 GST_INFO ("xml: %s", part);
1356 /* skip ' ' and scan the attributes */
1360 /* split attr=value pairs */
1361 while (*ae != '\0') {
1363 /* attr/value delimmiter */
1365 } else if (*ae == '"') {
1370 while (*ae != '\0' && *ae != '"')
1375 GST_INFO (" : [%s][%s]", as, v);
1376 if (!strncmp (as, "xmlns:", 6)) {
1378 /* we need to rewrite known namespaces to what we use in
1380 while (ns_match[i].ns_prefix) {
1381 if (!strcmp (ns_match[i].ns_uri, v))
1385 if (ns_match[i].ns_prefix) {
1386 if (strcmp (ns_map[i].original_ns, &as[6])) {
1387 ns_map[i].gstreamer_ns = g_strdup (&as[6]);
1391 XmpTag *xmp_tag = NULL;
1392 /* FIXME: eventualy rewrite ns
1394 * check if ns before ':' is in ns_map and ns_map[i].gstreamer_ns!=NULL
1395 * do 2 stage filter in tag_matches
1400 for (iter = context_tag->children; iter;
1401 iter = g_slist_next (iter)) {
1402 XmpTag *child = iter->data;
1404 GST_DEBUG ("Looking at child tag %s : %s", child->tag_name,
1406 if (strcmp (child->tag_name, as) == 0) {
1413 GST_LOG ("Looking for tag: %s", as);
1414 _gst_xmp_tag_get_mapping_reverse (as, &xmp_tag);
1417 PendingXmpTag *ptag;
1419 GST_DEBUG ("Found xmp tag: %s -> %s", xmp_tag->tag_name,
1422 /* we shouldn't find a xmp structure here */
1423 g_assert (xmp_tag->gst_tag != NULL);
1425 ptag = g_slice_new (PendingXmpTag);
1426 ptag->xmp_tag = xmp_tag;
1427 ptag->str = g_strdup (v);
1429 pending_tags = g_slist_append (pending_tags, ptag);
1432 /* restore chars overwritten by '\0' */
1435 } else if (*ae == '\0' || *ae == ' ') {
1436 /* end of attr/value pair */
1439 /* to next char if not eos */
1445 <dc:type><rdf:Bag><rdf:li>Image</rdf:li></rdf:Bag></dc:type>
1446 <dc:creator><rdf:Seq><rdf:li/></rdf:Seq></dc:creator>
1448 /* FIXME: eventualy rewrite ns */
1450 /* skip rdf tags for now */
1451 if (strncmp (part, "rdf:", 4)) {
1452 /* if we're inside some struct, we look only on its children */
1456 /* check if this is the closing of the context */
1458 && strcmp (part + 1, context_tag->tag_name) == 0) {
1459 GST_DEBUG ("Closing context tag %s", part);
1463 for (iter = context_tag->children; iter;
1464 iter = g_slist_next (iter)) {
1465 XmpTag *child = iter->data;
1467 GST_DEBUG ("Looking at child tag %s : %s", child->tag_name,
1469 if (strcmp (child->tag_name, part) == 0) {
1470 last_xmp_tag = child;
1477 GST_LOG ("Looking for tag: %s", part);
1478 _gst_xmp_tag_get_mapping_reverse (part, &last_xmp_tag);
1479 if (last_xmp_tag && last_xmp_tag->type == GstXmpTagTypeStruct) {
1480 context_tag = last_xmp_tag;
1481 last_xmp_tag = NULL;
1487 GST_LOG ("Next cycle");
1497 while (ne < xp2 && *ne != '<') {
1503 /* {TXT, ns, (ne-ns)-1} */
1504 if (ns[0] != '\n' && &ns[1] <= ne) {
1505 /* only log non-newline nodes, we still have to parse them */
1506 GST_INFO ("txt: %s", part);
1508 PendingXmpTag *ptag;
1510 GST_DEBUG ("Found tag %s -> %s", last_xmp_tag->tag_name,
1511 last_xmp_tag->gst_tag);
1513 if (last_xmp_tag->type == GstXmpTagTypeStruct) {
1514 g_assert (context_tag == NULL); /* we can't handle struct nesting currently */
1516 context_tag = last_xmp_tag;
1518 ptag = g_slice_new (PendingXmpTag);
1519 ptag->xmp_tag = last_xmp_tag;
1520 ptag->str = g_strdup (part);
1522 pending_tags = g_slist_append (pending_tags, ptag);
1533 while (pending_tags) {
1534 PendingXmpTag *ptag = (PendingXmpTag *) pending_tags->data;
1536 pending_tags = g_slist_delete_link (pending_tags, pending_tags);
1538 read_one_tag (list, ptag->xmp_tag, ptag->str, &pending_tags);
1541 g_slice_free (PendingXmpTag, ptag);
1544 GST_INFO ("xmp packet parsed, %d entries",
1545 gst_structure_n_fields ((GstStructure *) list));
1547 /* free resources */
1549 while (ns_map[i].original_ns) {
1550 g_free (ns_map[i].gstreamer_ns);
1555 gst_buffer_unmap (buffer, xps, len);
1561 GST_WARNING ("malformed xmp packet header");
1564 GST_WARNING ("malformed xmp packet footer");
1567 GST_WARNING ("malformed xml tag: %s", part);
1575 string_open_tag (GString * string, const char *tag)
1577 g_string_append_c (string, '<');
1578 g_string_append (string, tag);
1579 g_string_append_c (string, '>');
1583 string_close_tag (GString * string, const char *tag)
1585 g_string_append (string, "</");
1586 g_string_append (string, tag);
1587 g_string_append (string, ">\n");
1591 gst_value_serialize_xmp (const GValue * value)
1593 switch (G_VALUE_TYPE (value)) {
1595 return g_markup_escape_text (g_value_get_string (value), -1);
1597 return g_strdup_printf ("%d", g_value_get_int (value));
1599 return g_strdup_printf ("%u", g_value_get_uint (value));
1601 return double_to_fraction_string (g_value_get_double (value));
1605 /* put non-switchable types here */
1606 if (G_VALUE_TYPE (value) == GST_TYPE_DATE) {
1607 const GDate *date = gst_value_get_date (value);
1609 return g_strdup_printf ("%04d-%02d-%02d",
1610 (gint) g_date_get_year (date), (gint) g_date_get_month (date),
1611 (gint) g_date_get_day (date));
1612 } else if (G_VALUE_TYPE (value) == GST_TYPE_DATE_TIME) {
1613 gint year, month, day, hour, min, sec, microsec;
1614 gfloat gmt_offset = 0;
1615 gint gmt_offset_hour, gmt_offset_min;
1616 GstDateTime *datetime = (GstDateTime *) g_value_get_boxed (value);
1618 year = gst_date_time_get_year (datetime);
1619 month = gst_date_time_get_month (datetime);
1620 day = gst_date_time_get_day (datetime);
1621 hour = gst_date_time_get_hour (datetime);
1622 min = gst_date_time_get_minute (datetime);
1623 sec = gst_date_time_get_second (datetime);
1624 microsec = gst_date_time_get_microsecond (datetime);
1625 gmt_offset = gst_date_time_get_time_zone_offset (datetime);
1626 if (gmt_offset == 0) {
1628 return g_strdup_printf ("%04d-%02d-%02dT%02d:%02d:%02d.%06dZ",
1629 year, month, day, hour, min, sec, microsec);
1631 gmt_offset_hour = ABS (gmt_offset);
1632 gmt_offset_min = (ABS (gmt_offset) - gmt_offset_hour) * 60;
1634 return g_strdup_printf ("%04d-%02d-%02dT%02d:%02d:%02d.%06d%c%02d:%02d",
1635 year, month, day, hour, min, sec, microsec,
1636 gmt_offset >= 0 ? '+' : '-', gmt_offset_hour, gmt_offset_min);
1644 write_one_tag (const GstTagList * list, XmpTag * xmp_tag, gpointer user_data)
1647 XmpSerializationData *serialization_data = user_data;
1648 GString *data = serialization_data->data;
1651 /* struct type handled differently */
1652 if (xmp_tag->type == GstXmpTagTypeStruct ||
1653 xmp_tag->type == GstXmpTagTypeCompound) {
1655 gboolean use_it = FALSE;
1657 /* check if any of the inner tags are present on the taglist */
1658 for (iter = xmp_tag->children; iter && !use_it; iter = g_slist_next (iter)) {
1659 XmpTag *child_tag = iter->data;
1661 if (gst_tag_list_get_value_index (list, child_tag->gst_tag, 0) != NULL) {
1668 if (xmp_tag->tag_name)
1669 string_open_tag (data, xmp_tag->tag_name);
1671 for (iter = xmp_tag->children; iter; iter = g_slist_next (iter)) {
1672 write_one_tag (list, iter->data, user_data);
1674 if (xmp_tag->tag_name)
1675 string_close_tag (data, xmp_tag->tag_name);
1680 /* at this point we must have a gst_tag */
1681 g_assert (xmp_tag->gst_tag);
1682 if (gst_tag_list_get_value_index (list, xmp_tag->gst_tag, 0) == NULL)
1685 ct = gst_tag_list_get_tag_size (list, xmp_tag->gst_tag);
1686 string_open_tag (data, xmp_tag->tag_name);
1688 /* fast path for single valued tag */
1689 if (ct == 1 || xmp_tag->type == GstXmpTagTypeSimple) {
1690 if (xmp_tag->serialize) {
1691 s = xmp_tag->serialize (gst_tag_list_get_value_index (list,
1692 xmp_tag->gst_tag, 0));
1694 s = gst_value_serialize_xmp (gst_tag_list_get_value_index (list,
1695 xmp_tag->gst_tag, 0));
1698 g_string_append (data, s);
1701 GST_WARNING ("unhandled type for %s to xmp", xmp_tag->gst_tag);
1704 const gchar *typename;
1706 typename = xmp_tag_get_type_name (xmp_tag);
1708 string_open_tag (data, typename);
1709 for (i = 0; i < ct; i++) {
1710 GST_DEBUG ("mapping %s[%u/%u] to xmp", xmp_tag->gst_tag, i, ct);
1711 if (xmp_tag->serialize) {
1712 s = xmp_tag->serialize (gst_tag_list_get_value_index (list,
1713 xmp_tag->gst_tag, i));
1715 s = gst_value_serialize_xmp (gst_tag_list_get_value_index (list,
1716 xmp_tag->gst_tag, i));
1719 string_open_tag (data, "rdf:li");
1720 g_string_append (data, s);
1721 string_close_tag (data, "rdf:li");
1724 GST_WARNING ("unhandled type for %s to xmp", xmp_tag->gst_tag);
1727 string_close_tag (data, typename);
1730 string_close_tag (data, xmp_tag->tag_name);
1734 * gst_tag_list_to_xmp_buffer_full:
1736 * @read_only: does the container forbid inplace editing
1737 * @schemas: %NULL terminated array of schemas to be used on serialization
1739 * Formats a taglist as a xmp packet using only the selected
1740 * schemas. An empty list (%NULL) means that all schemas should
1743 * Returns: new buffer or %NULL, unref the buffer when done
1748 gst_tag_list_to_xmp_buffer_full (const GstTagList * list, gboolean read_only,
1749 const gchar ** schemas)
1751 GstBuffer *buffer = NULL;
1752 XmpSerializationData serialization_data;
1758 serialization_data.data = g_string_sized_new (4096);
1759 serialization_data.schemas = schemas;
1760 data = serialization_data.data;
1762 xmp_tags_initialize ();
1764 g_return_val_if_fail (GST_IS_TAG_LIST (list), NULL);
1767 g_string_append (data,
1768 "<?xpacket begin=\"\xEF\xBB\xBF\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>\n");
1769 g_string_append (data,
1770 "<x:xmpmeta xmlns:x=\"adobe:ns:meta/\" x:xmptk=\"GStreamer\">\n");
1771 g_string_append (data,
1772 "<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"");
1774 while (ns_match[i].ns_prefix) {
1775 if (xmp_serialization_data_use_schema (&serialization_data,
1776 ns_match[i].ns_prefix))
1777 g_string_append_printf (data, " xmlns:%s=\"%s\"",
1778 ns_match[i].ns_prefix, ns_match[i].ns_uri);
1781 g_string_append (data, ">\n");
1782 g_string_append (data, "<rdf:Description rdf:about=\"\">\n");
1784 /* iterate the schemas */
1785 if (schemas == NULL) {
1786 /* use all schemas */
1787 schemas = gst_tag_xmp_list_schemas ();
1789 for (i = 0; schemas[i] != NULL; i++) {
1790 GstXmpSchema *schema = _gst_xmp_get_schema (schemas[i]);
1791 GHashTableIter iter;
1792 gpointer key, value;
1797 /* Iterate over the hashtable */
1798 g_hash_table_iter_init (&iter, schema);
1799 while (g_hash_table_iter_next (&iter, &key, &value)) {
1800 write_one_tag (list, value, (gpointer) & serialization_data);
1805 g_string_append (data, "</rdf:Description>\n");
1806 g_string_append (data, "</rdf:RDF>\n");
1807 g_string_append (data, "</x:xmpmeta>\n");
1810 /* the xmp spec recommand to add 2-4KB padding for in-place editable xmp */
1813 for (i = 0; i < 32; i++) {
1814 g_string_append (data, " " " "
1818 g_string_append_printf (data, "<?xpacket end=\"%c\"?>\n",
1819 (read_only ? 'r' : 'w'));
1821 bsize = data->len + 1;
1822 bdata = g_string_free (data, FALSE);
1824 buffer = gst_buffer_new ();
1825 gst_buffer_take_memory (buffer,
1826 gst_memory_new_wrapped (0, bdata, g_free, bsize, 0, bsize));
1832 * gst_tag_list_to_xmp_buffer:
1834 * @read_only: does the container forbid inplace editing
1836 * Formats a taglist as a xmp packet.
1838 * Returns: new buffer or %NULL, unref the buffer when done
1843 gst_tag_list_to_xmp_buffer (const GstTagList * list, gboolean read_only)
1845 return gst_tag_list_to_xmp_buffer_full (list, read_only, NULL);
1848 #undef gst_xmp_schema_lookup
1849 #undef gst_xmp_schema_insert