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., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
26 * @short_description: tag mappings and support functions for plugins
27 * dealing with xmp packets
28 * @see_also: #GstTagList
30 * Contains various utility functions for plugins to parse or create
31 * xmp packets and map them to and from #GstTagList<!-- -->s.
33 * Please note that the xmp parser is very lightweight and not strict at all.
40 #include <gst/gsttagsetter.h>
41 #include "gsttageditingprivate.h"
48 #define GST_CAT_DEFAULT gst_tag_ensure_debug_category()
50 static GstDebugCategory *
51 gst_tag_ensure_debug_category (void)
53 static gsize cat_gonce = 0;
55 if (g_once_init_enter (&cat_gonce)) {
56 GstDebugCategory *cat = NULL;
58 GST_DEBUG_CATEGORY_INIT (cat, "xmp-tags", 0, "XMP GstTag helper functions");
60 g_once_init_leave (&cat_gonce, (gsize) cat);
63 return (GstDebugCategory *) cat_gonce;
66 static const gchar *schema_list[] = {
78 * gst_tag_xmp_list_schemas:
80 * Gets the list of supported schemas in the xmp lib
82 * Returns: (transfer none) (array zero-terminated=1): a %NULL terminated array of strings with the
86 gst_tag_xmp_list_schemas (void)
91 typedef struct _XmpSerializationData XmpSerializationData;
92 typedef struct _XmpTag XmpTag;
95 * Serializes a GValue into a string.
97 typedef gchar *(*XmpSerializationFunc) (const GValue * value);
100 * Deserializes @str that is the gstreamer tag @gst_tag represented in
101 * XMP as the @xmp_tag_value and adds the result to the @taglist.
103 * @pending_tags is passed so that compound xmp tags can search for its
104 * complements on the list and use them. Note that used complements should
105 * be freed and removed from the list.
106 * The list is of PendingXmpTag
108 typedef void (*XmpDeserializationFunc) (XmpTag * xmptag, GstTagList * taglist,
109 const gchar * gst_tag, const gchar * xmp_tag_value,
110 const gchar * str, GSList ** pending_tags);
112 struct _XmpSerializationData
115 const gchar **schemas;
119 xmp_serialization_data_use_schema (XmpSerializationData * serdata,
120 const gchar * schemaname)
123 if (serdata->schemas == NULL)
126 while (serdata->schemas[i] != NULL) {
127 if (strcmp (serdata->schemas[i], schemaname) == 0)
136 GstXmpTagTypeNone = 0,
142 /* Not really a xmp type, this is a tag that in gst is represented with
143 * a single value and on xmp it needs 2 (or more) simple values
145 * e.g. GST_TAG_GEO_LOCATION_ELEVATION needs to be mapped into 2 complementary
146 * tags in the exif's schema. One of them stores the absolute elevation,
147 * and the other one stores if it is above of below sea level.
149 GstXmpTagTypeCompound
154 const gchar *gst_tag;
155 const gchar *tag_name;
158 /* some tags must be inside a Bag even
159 * if they are a single entry. Set it here so we know */
160 GstXmpTagType supertype;
162 /* For tags that need a rdf:parseType attribute */
163 const gchar *parse_type;
165 /* Used for struct and compound types */
168 XmpSerializationFunc serialize;
169 XmpDeserializationFunc deserialize;
172 static GstTagMergeMode
173 xmp_tag_get_merge_mode (XmpTag * xmptag)
175 switch (xmptag->type) {
176 case GstXmpTagTypeBag:
177 case GstXmpTagTypeSeq:
178 return GST_TAG_MERGE_APPEND;
179 case GstXmpTagTypeSimple:
181 return GST_TAG_MERGE_KEEP;
186 xmp_tag_type_get_name (GstXmpTagType tagtype)
189 case GstXmpTagTypeSeq:
191 case GstXmpTagTypeBag:
197 /* Make compiler happy */
198 g_return_val_if_reached ("");
201 struct _PendingXmpTag
206 typedef struct _PendingXmpTag PendingXmpTag;
209 * A schema is a mapping of strings (the tag name in gstreamer) to a list of
210 * tags in xmp (XmpTag).
212 typedef GHashTable GstXmpSchema;
213 #define gst_xmp_schema_lookup g_hash_table_lookup
214 #define gst_xmp_schema_insert g_hash_table_insert
215 static GstXmpSchema *
216 gst_xmp_schema_new ()
218 return g_hash_table_new (g_direct_hash, g_direct_equal);
222 * Mappings from schema names into the schema group of tags (GstXmpSchema)
224 static GHashTable *__xmp_schemas;
226 static GstXmpSchema *
227 _gst_xmp_get_schema (const gchar * name)
230 GstXmpSchema *schema;
232 key = g_quark_from_string (name);
234 schema = g_hash_table_lookup (__xmp_schemas, GUINT_TO_POINTER (key));
236 GST_WARNING ("Schema %s doesn't exist", name);
242 _gst_xmp_add_schema (const gchar * name, GstXmpSchema * schema)
246 key = g_quark_from_string (name);
248 if (g_hash_table_lookup (__xmp_schemas, GUINT_TO_POINTER (key))) {
249 GST_WARNING ("Schema %s already exists, ignoring", name);
250 g_assert_not_reached ();
254 g_hash_table_insert (__xmp_schemas, GUINT_TO_POINTER (key), schema);
258 _gst_xmp_schema_add_mapping (GstXmpSchema * schema, XmpTag * tag)
262 key = g_quark_from_string (tag->gst_tag);
264 if (gst_xmp_schema_lookup (schema, GUINT_TO_POINTER (key))) {
265 GST_WARNING ("Tag %s already present for the schema", tag->gst_tag);
266 g_assert_not_reached ();
269 gst_xmp_schema_insert (schema, GUINT_TO_POINTER (key), tag);
273 gst_xmp_tag_create (const gchar * gst_tag, const gchar * xmp_tag,
274 gint xmp_type, XmpSerializationFunc serialization_func,
275 XmpDeserializationFunc deserialization_func)
279 xmpinfo = g_slice_new (XmpTag);
280 xmpinfo->gst_tag = gst_tag;
281 xmpinfo->tag_name = xmp_tag;
282 xmpinfo->type = xmp_type;
283 xmpinfo->supertype = GstXmpTagTypeNone;
284 xmpinfo->parse_type = NULL;
285 xmpinfo->serialize = serialization_func;
286 xmpinfo->deserialize = deserialization_func;
287 xmpinfo->children = NULL;
293 gst_xmp_tag_create_compound (const gchar * gst_tag, const gchar * xmp_tag_a,
294 const gchar * xmp_tag_b, XmpSerializationFunc serialization_func_a,
295 XmpSerializationFunc serialization_func_b,
296 XmpDeserializationFunc deserialization_func)
300 gst_xmp_tag_create (gst_tag, xmp_tag_a, GstXmpTagTypeSimple,
301 serialization_func_a, deserialization_func);
303 gst_xmp_tag_create (gst_tag, xmp_tag_b, GstXmpTagTypeSimple,
304 serialization_func_b, deserialization_func);
307 gst_xmp_tag_create (gst_tag, NULL, GstXmpTagTypeCompound, NULL, NULL);
309 xmptag->children = g_slist_prepend (xmptag->children, xmptag_b);
310 xmptag->children = g_slist_prepend (xmptag->children, xmptag_a);
316 _gst_xmp_schema_add_simple_mapping (GstXmpSchema * schema,
317 const gchar * gst_tag, const gchar * xmp_tag, gint xmp_type,
318 XmpSerializationFunc serialization_func,
319 XmpDeserializationFunc deserialization_func)
321 _gst_xmp_schema_add_mapping (schema,
322 gst_xmp_tag_create (gst_tag, xmp_tag, xmp_type, serialization_func,
323 deserialization_func));
327 * We do not return a copy here because elements are
328 * appended, and the API is not public, so we shouldn't
329 * have our lists modified during usage
333 _xmp_tag_get_mapping (const gchar * gst_tag, XmpSerializationData * serdata)
335 GPtrArray *ret = NULL;
337 GQuark key = g_quark_from_string (gst_tag);
338 gpointer iterkey, value;
339 const gchar *schemaname;
341 g_hash_table_iter_init (&iter, __xmp_schemas);
342 while (!ret && g_hash_table_iter_next (&iter, &iterkey, &value)) {
343 GstXmpSchema *schema = (GstXmpSchema *) value;
345 schemaname = g_quark_to_string (GPOINTER_TO_UINT (iterkey));
346 if (xmp_serialization_data_use_schema (serdata, schemaname))
348 (GPtrArray *) gst_xmp_schema_lookup (schema, GUINT_TO_POINTER (key));
354 /* finds the gst tag that maps to this xmp tag in this schema */
356 _gst_xmp_schema_get_mapping_reverse (GstXmpSchema * schema,
357 const gchar * xmp_tag, XmpTag ** _xmp_tag)
361 const gchar *ret = NULL;
363 /* Iterate over the hashtable */
364 g_hash_table_iter_init (&iter, schema);
365 while (!ret && g_hash_table_iter_next (&iter, &key, &value)) {
366 XmpTag *xmpinfo = (XmpTag *) value;
368 if (xmpinfo->tag_name) {
369 if (strcmp (xmpinfo->tag_name, xmp_tag) == 0) {
371 ret = g_quark_to_string (GPOINTER_TO_UINT (key));
374 } else if (xmpinfo->children) {
376 for (iter = xmpinfo->children; iter; iter = g_slist_next (iter)) {
377 XmpTag *child = iter->data;
378 if (strcmp (child->tag_name, xmp_tag) == 0) {
380 ret = g_quark_to_string (GPOINTER_TO_UINT (key));
385 g_assert_not_reached ();
393 /* finds the gst tag that maps to this xmp tag (searches on all schemas) */
395 _gst_xmp_tag_get_mapping_reverse (const gchar * xmp_tag, XmpTag ** _xmp_tag)
399 const gchar *ret = NULL;
401 /* Iterate over the hashtable */
402 g_hash_table_iter_init (&iter, __xmp_schemas);
403 while (!ret && g_hash_table_iter_next (&iter, &key, &value)) {
404 ret = _gst_xmp_schema_get_mapping_reverse ((GstXmpSchema *) value, xmp_tag,
410 /* utility functions/macros */
412 #define METERS_PER_SECOND_TO_KILOMETERS_PER_HOUR (3.6)
413 #define KILOMETERS_PER_HOUR_TO_METERS_PER_SECOND (1/3.6)
414 #define MILES_PER_HOUR_TO_METERS_PER_SECOND (0.44704)
415 #define KNOTS_TO_METERS_PER_SECOND (0.514444)
418 double_to_fraction_string (gdouble num)
423 gst_util_double_to_fraction (num, &frac_n, &frac_d);
424 return g_strdup_printf ("%d/%d", frac_n, frac_d);
427 /* (de)serialize functions */
429 serialize_exif_gps_coordinate (const GValue * value, gchar pos, gchar neg)
434 gchar fraction[G_ASCII_DTOSTR_BUF_SIZE];
436 g_return_val_if_fail (G_VALUE_TYPE (value) == G_TYPE_DOUBLE, NULL);
438 num = g_value_get_double (value);
445 integer = (gint) num;
447 g_ascii_dtostr (fraction, sizeof (fraction), (num - integer) * 60);
449 /* FIXME review GPSCoordinate serialization spec for the .mm or ,ss
450 * decision. Couldn't understand it clearly */
451 return g_strdup_printf ("%d,%s%c", integer, fraction, c);
455 serialize_exif_latitude (const GValue * value)
457 return serialize_exif_gps_coordinate (value, 'N', 'S');
461 serialize_exif_longitude (const GValue * value)
463 return serialize_exif_gps_coordinate (value, 'E', 'W');
467 deserialize_exif_gps_coordinate (XmpTag * xmptag, GstTagList * taglist,
468 const gchar * gst_tag, const gchar * str, gchar pos, gchar neg)
471 gint d = 0, m = 0, s = 0;
474 const gchar *current;
476 /* get the degrees */
477 if (sscanf (str, "%d", &d) != 1)
480 /* find the beginning of the minutes */
481 current = strchr (str, ',');
486 /* check if it uses ,SS or .mm */
487 if (strchr (current, ',') != NULL) {
488 if (!sscanf (current, "%d,%d%c", &m, &s, &c))
491 gchar *copy = g_strdup (current);
492 gint len = strlen (copy);
495 /* check the last letter */
496 for (i = len - 1; len >= 0; len--) {
497 if (g_ascii_isspace (copy[i]))
500 if (g_ascii_isalpha (copy[i])) {
507 /* something is wrong */
513 /* use a copy so we can change the last letter as E can cause
515 m2 = g_ascii_strtod (copy, NULL);
520 /* we can add them all as those that aren't parsed are 0 */
521 value = d + (m / 60.0) + (s / (60.0 * 60.0)) + (m2 / 60.0);
525 } else if (c == neg) {
531 gst_tag_list_add (taglist, xmp_tag_get_merge_mode (xmptag), gst_tag, value,
536 GST_WARNING ("Failed to deserialize gps coordinate: %s", str);
540 deserialize_exif_latitude (XmpTag * xmptag, GstTagList * taglist,
541 const gchar * gst_tag, const gchar * xmp_tag, const gchar * str,
542 GSList ** pending_tags)
544 deserialize_exif_gps_coordinate (xmptag, taglist, gst_tag, str, 'N', 'S');
548 deserialize_exif_longitude (XmpTag * xmptag, GstTagList * taglist,
549 const gchar * gst_tag, const gchar * xmp_tag, const gchar * str,
550 GSList ** pending_tags)
552 deserialize_exif_gps_coordinate (xmptag, taglist, gst_tag, str, 'E', 'W');
556 serialize_exif_altitude (const GValue * value)
560 num = g_value_get_double (value);
565 return double_to_fraction_string (num);
569 serialize_exif_altituderef (const GValue * value)
573 num = g_value_get_double (value);
575 /* 0 means above sea level, 1 means below */
577 return g_strdup ("0");
578 return g_strdup ("1");
582 deserialize_exif_altitude (XmpTag * xmptag, GstTagList * taglist,
583 const gchar * gst_tag, const gchar * xmp_tag, const gchar * str,
584 GSList ** pending_tags)
586 const gchar *altitude_str = NULL;
587 const gchar *altituderef_str = NULL;
593 PendingXmpTag *ptag = NULL;
595 /* find the other missing part */
596 if (strcmp (xmp_tag, "exif:GPSAltitude") == 0) {
599 for (entry = *pending_tags; entry; entry = g_slist_next (entry)) {
600 ptag = (PendingXmpTag *) entry->data;
602 if (strcmp (ptag->xmp_tag->tag_name, "exif:GPSAltitudeRef") == 0) {
603 altituderef_str = ptag->str;
608 } else if (strcmp (xmp_tag, "exif:GPSAltitudeRef") == 0) {
609 altituderef_str = str;
611 for (entry = *pending_tags; entry; entry = g_slist_next (entry)) {
612 ptag = (PendingXmpTag *) entry->data;
614 if (strcmp (ptag->xmp_tag->tag_name, "exif:GPSAltitude") == 0) {
615 altitude_str = ptag->str;
621 GST_WARNING ("Unexpected xmp tag %s", xmp_tag);
626 GST_WARNING ("Missing exif:GPSAltitude tag");
629 if (!altituderef_str) {
630 GST_WARNING ("Missing exif:GPSAltitudeRef tag");
634 if (sscanf (altitude_str, "%d/%d", &frac_n, &frac_d) != 2) {
635 GST_WARNING ("Failed to parse fraction: %s", altitude_str);
639 gst_util_fraction_to_double (frac_n, frac_d, &value);
641 if (altituderef_str[0] == '0') {
643 } else if (altituderef_str[0] == '1') {
646 GST_WARNING ("Unexpected exif:AltitudeRef value: %s", altituderef_str);
650 /* add to the taglist */
651 gst_tag_list_add (taglist, xmp_tag_get_merge_mode (xmptag),
652 GST_TAG_GEO_LOCATION_ELEVATION, value, NULL);
656 g_slice_free (PendingXmpTag, ptag);
657 *pending_tags = g_slist_delete_link (*pending_tags, entry);
661 serialize_exif_gps_speed (const GValue * value)
663 return double_to_fraction_string (g_value_get_double (value) *
664 METERS_PER_SECOND_TO_KILOMETERS_PER_HOUR);
668 serialize_exif_gps_speedref (const GValue * value)
670 /* we always use km/h */
671 return g_strdup ("K");
675 deserialize_exif_gps_speed (XmpTag * xmptag, GstTagList * taglist,
676 const gchar * gst_tag, const gchar * xmp_tag, const gchar * str,
677 GSList ** pending_tags)
679 const gchar *speed_str = NULL;
680 const gchar *speedref_str = NULL;
686 PendingXmpTag *ptag = NULL;
688 /* find the other missing part */
689 if (strcmp (xmp_tag, "exif:GPSSpeed") == 0) {
692 for (entry = *pending_tags; entry; entry = g_slist_next (entry)) {
693 ptag = (PendingXmpTag *) entry->data;
695 if (strcmp (ptag->xmp_tag->tag_name, "exif:GPSSpeedRef") == 0) {
696 speedref_str = ptag->str;
701 } else if (strcmp (xmp_tag, "exif:GPSSpeedRef") == 0) {
704 for (entry = *pending_tags; entry; entry = g_slist_next (entry)) {
705 ptag = (PendingXmpTag *) entry->data;
707 if (strcmp (ptag->xmp_tag->tag_name, "exif:GPSSpeed") == 0) {
708 speed_str = ptag->str;
714 GST_WARNING ("Unexpected xmp tag %s", xmp_tag);
719 GST_WARNING ("Missing exif:GPSSpeed tag");
723 GST_WARNING ("Missing exif:GPSSpeedRef tag");
727 if (sscanf (speed_str, "%d/%d", &frac_n, &frac_d) != 2) {
728 GST_WARNING ("Failed to parse fraction: %s", speed_str);
732 gst_util_fraction_to_double (frac_n, frac_d, &value);
734 if (speedref_str[0] == 'K') {
735 value *= KILOMETERS_PER_HOUR_TO_METERS_PER_SECOND;
736 } else if (speedref_str[0] == 'M') {
737 value *= MILES_PER_HOUR_TO_METERS_PER_SECOND;
738 } else if (speedref_str[0] == 'N') {
739 value *= KNOTS_TO_METERS_PER_SECOND;
741 GST_WARNING ("Unexpected exif:SpeedRef value: %s", speedref_str);
745 /* add to the taglist */
746 gst_tag_list_add (taglist, xmp_tag_get_merge_mode (xmptag),
747 GST_TAG_GEO_LOCATION_MOVEMENT_SPEED, value, NULL);
751 g_slice_free (PendingXmpTag, ptag);
752 *pending_tags = g_slist_delete_link (*pending_tags, entry);
756 serialize_exif_gps_direction (const GValue * value)
758 return double_to_fraction_string (g_value_get_double (value));
762 serialize_exif_gps_directionref (const GValue * value)
764 /* T for true geographic direction (M would mean magnetic) */
765 return g_strdup ("T");
769 deserialize_exif_gps_direction (XmpTag * xmptag, GstTagList * taglist,
770 const gchar * gst_tag, const gchar * xmp_tag, const gchar * str,
771 GSList ** pending_tags, const gchar * direction_tag,
772 const gchar * directionref_tag)
774 const gchar *dir_str = NULL;
775 const gchar *dirref_str = NULL;
781 PendingXmpTag *ptag = NULL;
783 /* find the other missing part */
784 if (strcmp (xmp_tag, direction_tag) == 0) {
787 for (entry = *pending_tags; entry; entry = g_slist_next (entry)) {
788 ptag = (PendingXmpTag *) entry->data;
790 if (strcmp (ptag->xmp_tag->tag_name, directionref_tag) == 0) {
791 dirref_str = ptag->str;
796 } else if (strcmp (xmp_tag, directionref_tag) == 0) {
799 for (entry = *pending_tags; entry; entry = g_slist_next (entry)) {
800 ptag = (PendingXmpTag *) entry->data;
802 if (strcmp (ptag->xmp_tag->tag_name, direction_tag) == 0) {
809 GST_WARNING ("Unexpected xmp tag %s", xmp_tag);
814 GST_WARNING ("Missing GPSImgDirection tag");
818 GST_WARNING ("Missing GPSImgDirectionRef tag");
822 if (sscanf (dir_str, "%d/%d", &frac_n, &frac_d) != 2) {
823 GST_WARNING ("Failed to parse fraction: %s", dir_str);
827 gst_util_fraction_to_double (frac_n, frac_d, &value);
829 if (dirref_str[0] == 'T') {
831 } else if (dirref_str[0] == 'M') {
832 GST_WARNING ("Magnetic direction tags aren't supported yet");
835 GST_WARNING ("Unexpected %s value: %s", directionref_tag, dirref_str);
839 /* add to the taglist */
840 gst_tag_list_add (taglist, xmp_tag_get_merge_mode (xmptag), gst_tag, value,
845 g_slice_free (PendingXmpTag, ptag);
846 *pending_tags = g_slist_delete_link (*pending_tags, entry);
850 deserialize_exif_gps_track (XmpTag * xmptag, GstTagList * taglist,
851 const gchar * gst_tag, const gchar * xmp_tag, const gchar * str,
852 GSList ** pending_tags)
854 deserialize_exif_gps_direction (xmptag, taglist, gst_tag, xmp_tag, str,
855 pending_tags, "exif:GPSTrack", "exif:GPSTrackRef");
859 deserialize_exif_gps_img_direction (XmpTag * xmptag, GstTagList * taglist,
860 const gchar * gst_tag, const gchar * xmp_tag, const gchar * str,
861 GSList ** pending_tags)
863 deserialize_exif_gps_direction (xmptag, taglist, gst_tag, xmp_tag, str,
864 pending_tags, "exif:GPSImgDirection", "exif:GPSImgDirectionRef");
868 deserialize_xmp_rating (XmpTag * xmptag, GstTagList * taglist,
869 const gchar * gst_tag, const gchar * xmp_tag, const gchar * str,
870 GSList ** pending_tags)
874 if (sscanf (str, "%u", &value) != 1) {
875 GST_WARNING ("Failed to parse xmp:Rating %s", str);
880 GST_WARNING ("Unsupported Rating tag %u (should be from 0 to 100), "
885 gst_tag_list_add (taglist, xmp_tag_get_merge_mode (xmptag), gst_tag, value,
890 serialize_tiff_orientation (const GValue * value)
895 str = g_value_get_string (value);
897 GST_WARNING ("Failed to get image orientation tag value");
901 num = __exif_tag_image_orientation_to_exif_value (str);
905 return g_strdup_printf ("%d", num);
909 deserialize_tiff_orientation (XmpTag * xmptag, GstTagList * taglist,
910 const gchar * gst_tag, const gchar * xmp_tag, const gchar * str,
911 GSList ** pending_tags)
914 const gchar *orientation = NULL;
916 if (sscanf (str, "%u", &value) != 1) {
917 GST_WARNING ("Failed to parse tiff:Orientation %s", str);
921 if (value < 1 || value > 8) {
922 GST_WARNING ("Invalid tiff:Orientation tag %u (should be from 1 to 8), "
927 orientation = __exif_tag_image_orientation_from_exif_value (value);
928 if (orientation == NULL)
930 gst_tag_list_add (taglist, xmp_tag_get_merge_mode (xmptag), gst_tag,
935 /* look at this page for additional schemas
936 * http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/XMP.html
939 _init_xmp_tag_map (gpointer user_data)
942 GstXmpSchema *schema;
944 gst_tag_register_musicbrainz_tags ();
946 __xmp_schemas = g_hash_table_new (g_direct_hash, g_direct_equal);
949 /* dublic code metadata
950 * http://dublincore.org/documents/dces/
952 schema = gst_xmp_schema_new ();
953 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_ARTIST,
954 "dc:creator", GstXmpTagTypeSeq, NULL, NULL);
955 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_COPYRIGHT,
956 "dc:rights", GstXmpTagTypeSimple, NULL, NULL);
957 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_DATE_TIME, "dc:date",
958 GstXmpTagTypeSeq, NULL, NULL);
959 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_DESCRIPTION,
960 "dc:description", GstXmpTagTypeSimple, NULL, NULL);
961 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_KEYWORDS,
962 "dc:subject", GstXmpTagTypeBag, NULL, NULL);
963 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_TITLE, "dc:title",
964 GstXmpTagTypeSimple, NULL, NULL);
965 /* FIXME: we probably want GST_TAG_{,AUDIO_,VIDEO_}MIME_TYPE */
966 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_VIDEO_CODEC,
967 "dc:format", GstXmpTagTypeSimple, NULL, NULL);
968 _gst_xmp_add_schema ("dc", schema);
970 /* xap (xmp) schema */
971 schema = gst_xmp_schema_new ();
972 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_USER_RATING,
973 "xmp:Rating", GstXmpTagTypeSimple, NULL, deserialize_xmp_rating);
974 _gst_xmp_add_schema ("xap", schema);
977 schema = gst_xmp_schema_new ();
978 _gst_xmp_schema_add_simple_mapping (schema,
979 GST_TAG_DEVICE_MANUFACTURER, "tiff:Make", GstXmpTagTypeSimple, NULL,
981 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_DEVICE_MODEL,
982 "tiff:Model", GstXmpTagTypeSimple, NULL, NULL);
983 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_APPLICATION_NAME,
984 "tiff:Software", GstXmpTagTypeSimple, NULL, NULL);
985 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_IMAGE_ORIENTATION,
986 "tiff:Orientation", GstXmpTagTypeSimple, serialize_tiff_orientation,
987 deserialize_tiff_orientation);
988 _gst_xmp_add_schema ("tiff", schema);
991 schema = gst_xmp_schema_new ();
992 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_DATE_TIME,
993 "exif:DateTimeOriginal", GstXmpTagTypeSimple, NULL, NULL);
994 _gst_xmp_schema_add_simple_mapping (schema,
995 GST_TAG_GEO_LOCATION_LATITUDE, "exif:GPSLatitude",
996 GstXmpTagTypeSimple, serialize_exif_latitude, deserialize_exif_latitude);
997 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_GEO_LOCATION_LONGITUDE,
998 "exif:GPSLongitude", GstXmpTagTypeSimple, serialize_exif_longitude,
999 deserialize_exif_longitude);
1000 _gst_xmp_schema_add_simple_mapping (schema,
1001 GST_TAG_CAPTURING_EXPOSURE_COMPENSATION, "exif:ExposureBiasValue",
1002 GstXmpTagTypeSimple, NULL, NULL);
1004 /* compound exif tags */
1005 xmpinfo = gst_xmp_tag_create_compound (GST_TAG_GEO_LOCATION_ELEVATION,
1006 "exif:GPSAltitude", "exif:GPSAltitudeRef", serialize_exif_altitude,
1007 serialize_exif_altituderef, deserialize_exif_altitude);
1008 _gst_xmp_schema_add_mapping (schema, xmpinfo);
1010 xmpinfo = gst_xmp_tag_create_compound (GST_TAG_GEO_LOCATION_MOVEMENT_SPEED,
1011 "exif:GPSSpeed", "exif:GPSSpeedRef", serialize_exif_gps_speed,
1012 serialize_exif_gps_speedref, deserialize_exif_gps_speed);
1013 _gst_xmp_schema_add_mapping (schema, xmpinfo);
1016 gst_xmp_tag_create_compound (GST_TAG_GEO_LOCATION_MOVEMENT_DIRECTION,
1017 "exif:GPSTrack", "exif:GPSTrackRef", serialize_exif_gps_direction,
1018 serialize_exif_gps_directionref, deserialize_exif_gps_track);
1019 _gst_xmp_schema_add_mapping (schema, xmpinfo);
1021 xmpinfo = gst_xmp_tag_create_compound (GST_TAG_GEO_LOCATION_CAPTURE_DIRECTION,
1022 "exif:GPSImgDirection", "exif:GPSImgDirectionRef",
1023 serialize_exif_gps_direction, serialize_exif_gps_directionref,
1024 deserialize_exif_gps_img_direction);
1025 _gst_xmp_schema_add_mapping (schema, xmpinfo);
1027 _gst_xmp_add_schema ("exif", schema);
1029 /* photoshop schema */
1030 schema = gst_xmp_schema_new ();
1031 _gst_xmp_schema_add_simple_mapping (schema,
1032 GST_TAG_GEO_LOCATION_COUNTRY, "photoshop:Country",
1033 GstXmpTagTypeSimple, NULL, NULL);
1034 _gst_xmp_schema_add_simple_mapping (schema, GST_TAG_GEO_LOCATION_CITY,
1035 "photoshop:City", GstXmpTagTypeSimple, NULL, NULL);
1036 _gst_xmp_add_schema ("photoshop", schema);
1038 /* iptc4xmpcore schema */
1039 schema = gst_xmp_schema_new ();
1040 _gst_xmp_schema_add_simple_mapping (schema,
1041 GST_TAG_GEO_LOCATION_SUBLOCATION, "Iptc4xmpCore:Location",
1042 GstXmpTagTypeSimple, NULL, NULL);
1043 _gst_xmp_add_schema ("Iptc4xmpCore", schema);
1045 /* iptc4xmpext schema */
1046 schema = gst_xmp_schema_new ();
1047 xmpinfo = gst_xmp_tag_create (NULL, "Iptc4xmpExt:LocationShown",
1048 GstXmpTagTypeStruct, NULL, NULL);
1049 xmpinfo->supertype = GstXmpTagTypeBag;
1050 xmpinfo->parse_type = "Resource";
1051 xmpinfo->children = g_slist_prepend (xmpinfo->children,
1052 gst_xmp_tag_create (GST_TAG_GEO_LOCATION_SUBLOCATION,
1053 "LocationDetails:Sublocation", GstXmpTagTypeSimple, NULL, NULL));
1055 g_slist_prepend (xmpinfo->children,
1056 gst_xmp_tag_create (GST_TAG_GEO_LOCATION_CITY,
1057 "LocationDetails:City", GstXmpTagTypeSimple, NULL, NULL));
1059 g_slist_prepend (xmpinfo->children,
1060 gst_xmp_tag_create (GST_TAG_GEO_LOCATION_COUNTRY,
1061 "LocationDetails:Country", GstXmpTagTypeSimple, NULL, NULL));
1062 _gst_xmp_schema_add_mapping (schema, xmpinfo);
1063 _gst_xmp_add_schema ("Iptc4xmpExt", schema);
1069 xmp_tags_initialize ()
1071 static GOnce my_once = G_ONCE_INIT;
1072 g_once (&my_once, (GThreadFunc) _init_xmp_tag_map, NULL);
1075 typedef struct _GstXmpNamespaceMatch GstXmpNamespaceMatch;
1076 struct _GstXmpNamespaceMatch
1078 const gchar *ns_prefix;
1079 const gchar *ns_uri;
1082 * Stores extra namespaces for array tags
1083 * The namespaces should be written in the form:
1085 * xmlns:XpTo="http://some.org/your/ns/name/ (next ones)"
1087 const gchar *extra_ns;
1090 static const GstXmpNamespaceMatch ns_match[] = {
1091 {"dc", "http://purl.org/dc/elements/1.1/", NULL},
1092 {"exif", "http://ns.adobe.com/exif/1.0/", NULL},
1093 {"tiff", "http://ns.adobe.com/tiff/1.0/", NULL},
1094 {"xap", "http://ns.adobe.com/xap/1.0/", NULL},
1095 {"photoshop", "http://ns.adobe.com/photoshop/1.0/", NULL},
1096 {"Iptc4xmpCore", "http://iptc.org/std/Iptc4xmpCore/1.0/xmlns/", NULL},
1097 {"Iptc4xmpExt", "http://iptc.org/std/Iptc4xmpExt/2008-02-29/",
1098 "xmlns:LocationDetails=\"http://iptc.org/std/Iptc4xmpExt/2008-02-29/LocationDetails/\""},
1102 typedef struct _GstXmpNamespaceMap GstXmpNamespaceMap;
1103 struct _GstXmpNamespaceMap
1105 const gchar *original_ns;
1106 gchar *gstreamer_ns;
1112 read_one_tag (GstTagList * list, XmpTag * xmptag,
1113 const gchar * v, GSList ** pending_tags)
1116 GstTagMergeMode merge_mode;
1117 const gchar *tag = xmptag->gst_tag;
1119 g_return_if_fail (tag != NULL);
1121 if (xmptag->deserialize) {
1122 xmptag->deserialize (xmptag, list, tag, xmptag->tag_name, v, pending_tags);
1126 merge_mode = xmp_tag_get_merge_mode (xmptag);
1127 tag_type = gst_tag_get_type (tag);
1129 /* add gstreamer tag depending on type */
1131 case G_TYPE_STRING:{
1132 gst_tag_list_add (list, merge_mode, tag, v, NULL);
1135 case G_TYPE_DOUBLE:{
1137 gint frac_n, frac_d;
1139 if (sscanf (v, "%d/%d", &frac_n, &frac_d) == 2) {
1140 gst_util_fraction_to_double (frac_n, frac_d, &value);
1141 gst_tag_list_add (list, merge_mode, tag, value, NULL);
1143 GST_WARNING ("Failed to parse fraction: %s", v);
1148 if (tag_type == GST_TYPE_DATE_TIME) {
1149 GstDateTime *datetime;
1151 if (v == NULL || *v == '\0') {
1152 GST_WARNING ("Empty string for datetime parsing");
1156 GST_DEBUG ("Parsing %s into a datetime", v);
1157 datetime = gst_date_time_new_from_iso8601_string (v);
1159 gst_tag_list_add (list, merge_mode, tag, datetime, NULL);
1160 gst_date_time_unref (datetime);
1163 } else if (tag_type == G_TYPE_DATE) {
1164 GST_ERROR ("Use GST_TYPE_DATE_TIME in tags instead of G_TYPE_DATE");
1166 GST_WARNING ("unhandled type for %s from xmp", tag);
1173 * gst_tag_list_from_xmp_buffer:
1176 * Parse a xmp packet into a taglist.
1178 * Returns: (transfer full) (nullable): new taglist or %NULL, free the list when done
1181 gst_tag_list_from_xmp_buffer (GstBuffer * buffer)
1183 GstTagList *list = NULL;
1185 gchar *xps, *xp1, *xp2, *xpe, *ns, *ne;
1186 gsize len, max_ft_len;
1188 gchar *part = NULL, *pp;
1190 XmpTag *last_xmp_tag = NULL;
1191 GSList *pending_tags = NULL;
1193 /* Used for strucuture xmp tags */
1194 XmpTag *context_tag = NULL;
1196 GstXmpNamespaceMap ns_map[] = {
1207 {"Iptc4xmpCore", NULL}
1209 {"Iptc4xmpExt", NULL}
1214 xmp_tags_initialize ();
1216 g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
1218 GST_LOG ("Starting xmp parsing");
1220 gst_buffer_map (buffer, &info, GST_MAP_READ);
1221 xps = (gchar *) info.data;
1223 g_return_val_if_fail (len > 0, NULL);
1225 xpe = &xps[len + 1];
1227 /* check header and footer */
1228 xp1 = g_strstr_len (xps, len, "<?xpacket begin");
1230 goto missing_header;
1231 xp1 = &xp1[strlen ("<?xpacket begin")];
1232 while (*xp1 != '>' && *xp1 != '<' && xp1 < xpe)
1235 goto missing_header;
1237 /* Use 2 here to count for an extra trailing \n that was added
1238 * in old versions, this makes it able to parse xmp packets with
1239 * and without this trailing char */
1240 max_ft_len = 2 + strlen ("<?xpacket end=\".\"?>");
1241 if (len < max_ft_len)
1242 goto missing_footer;
1244 xp2 = g_strstr_len (&xps[len - max_ft_len], max_ft_len, "<?xpacket ");
1246 goto missing_footer;
1248 GST_INFO ("xmp header okay");
1250 /* skip > and text until first xml-node */
1252 while (*xp1 != '<' && xp1 < xpe)
1255 /* no tag can be longer than the whole buffer */
1256 part = g_malloc (xp2 - xp1);
1257 list = gst_tag_list_new_empty ();
1259 /* parse data into a list of nodes */
1260 /* data is between xp1..xp2 */
1267 while (ne < xp2 && *ne != '>' && *ne != '<') {
1268 if (*ne == '\n' || *ne == '\t' || *ne == ' ') {
1269 while (ne < xp2 && (*ne == '\n' || *ne == '\t' || *ne == ' '))
1280 /* {XML, ns, ne-ns} */
1282 gchar *as = strchr (part, ' ');
1283 /* only log start nodes */
1284 GST_INFO ("xml: %s", part);
1289 /* skip ' ' and scan the attributes */
1293 /* split attr=value pairs */
1294 while (*ae != '\0') {
1296 /* attr/value delimmiter */
1298 } else if (*ae == '"') {
1303 while (*ae != '\0' && *ae != '"')
1308 GST_INFO (" : [%s][%s]", as, v);
1309 if (!strncmp (as, "xmlns:", 6)) {
1311 /* we need to rewrite known namespaces to what we use in
1313 while (ns_match[i].ns_prefix) {
1314 if (!strcmp (ns_match[i].ns_uri, v))
1318 if (ns_match[i].ns_prefix) {
1319 if (strcmp (ns_map[i].original_ns, &as[6])) {
1320 g_free (ns_map[i].gstreamer_ns);
1321 ns_map[i].gstreamer_ns = g_strdup (&as[6]);
1325 XmpTag *xmp_tag = NULL;
1326 /* FIXME: eventually rewrite ns
1328 * check if ns before ':' is in ns_map and ns_map[i].gstreamer_ns!=NULL
1329 * do 2 stage filter in tag_matches
1334 for (iter = context_tag->children; iter;
1335 iter = g_slist_next (iter)) {
1336 XmpTag *child = iter->data;
1338 GST_DEBUG ("Looking at child tag %s : %s", child->tag_name,
1340 if (strcmp (child->tag_name, as) == 0) {
1347 GST_LOG ("Looking for tag: %s", as);
1348 _gst_xmp_tag_get_mapping_reverse (as, &xmp_tag);
1351 PendingXmpTag *ptag;
1353 GST_DEBUG ("Found xmp tag: %s -> %s", xmp_tag->tag_name,
1356 /* we shouldn't find a xmp structure here */
1357 g_assert (xmp_tag->gst_tag != NULL);
1359 ptag = g_slice_new (PendingXmpTag);
1360 ptag->xmp_tag = xmp_tag;
1361 ptag->str = g_strdup (v);
1363 pending_tags = g_slist_prepend (pending_tags, ptag);
1366 /* restore chars overwritten by '\0' */
1369 } else if (*ae == '\0' || *ae == ' ') {
1370 /* end of attr/value pair */
1373 /* to next char if not eos */
1379 <dc:type><rdf:Bag><rdf:li>Image</rdf:li></rdf:Bag></dc:type>
1380 <dc:creator><rdf:Seq><rdf:li/></rdf:Seq></dc:creator>
1382 /* FIXME: eventually rewrite ns */
1384 /* skip rdf tags for now */
1385 if (strncmp (part, "rdf:", 4)) {
1386 /* if we're inside some struct, we look only on its children */
1390 /* check if this is the closing of the context */
1392 && strcmp (part + 1, context_tag->tag_name) == 0) {
1393 GST_DEBUG ("Closing context tag %s", part);
1397 for (iter = context_tag->children; iter;
1398 iter = g_slist_next (iter)) {
1399 XmpTag *child = iter->data;
1401 GST_DEBUG ("Looking at child tag %s : %s", child->tag_name,
1403 if (strcmp (child->tag_name, part) == 0) {
1404 last_xmp_tag = child;
1411 GST_LOG ("Looking for tag: %s", part);
1412 _gst_xmp_tag_get_mapping_reverse (part, &last_xmp_tag);
1413 if (last_xmp_tag && last_xmp_tag->type == GstXmpTagTypeStruct) {
1414 context_tag = last_xmp_tag;
1415 last_xmp_tag = NULL;
1421 GST_LOG ("Next cycle");
1431 while (ne < xp2 && *ne != '<') {
1437 /* {TXT, ns, (ne-ns)-1} */
1438 if (ns[0] != '\n' && &ns[1] <= ne) {
1439 /* only log non-newline nodes, we still have to parse them */
1440 GST_INFO ("txt: %s", part);
1442 PendingXmpTag *ptag;
1444 GST_DEBUG ("Found tag %s -> %s", last_xmp_tag->tag_name,
1445 last_xmp_tag->gst_tag);
1447 if (last_xmp_tag->type == GstXmpTagTypeStruct) {
1448 g_assert (context_tag == NULL); /* we can't handle struct nesting currently */
1450 context_tag = last_xmp_tag;
1452 ptag = g_slice_new (PendingXmpTag);
1453 ptag->xmp_tag = last_xmp_tag;
1454 ptag->str = g_strdup (part);
1456 pending_tags = g_slist_prepend (pending_tags, ptag);
1467 pending_tags = g_slist_reverse (pending_tags);
1469 GST_DEBUG ("Done accumulating tags, now handling them");
1471 while (pending_tags) {
1472 PendingXmpTag *ptag = (PendingXmpTag *) pending_tags->data;
1474 pending_tags = g_slist_delete_link (pending_tags, pending_tags);
1476 read_one_tag (list, ptag->xmp_tag, ptag->str, &pending_tags);
1479 g_slice_free (PendingXmpTag, ptag);
1482 GST_INFO ("xmp packet parsed, %d entries", gst_tag_list_n_tags (list));
1486 /* free resources */
1488 while (ns_map[i].original_ns) {
1489 g_free (ns_map[i].gstreamer_ns);
1495 gst_buffer_unmap (buffer, &info);
1501 GST_WARNING ("malformed xmp packet header");
1504 GST_WARNING ("malformed xmp packet footer");
1507 GST_WARNING ("malformed xml tag: %s", part);
1508 gst_tag_list_unref (list);
1517 string_open_tag (GString * string, const char *tag)
1519 g_string_append_c (string, '<');
1520 g_string_append (string, tag);
1521 g_string_append_c (string, '>');
1525 string_close_tag (GString * string, const char *tag)
1527 g_string_append (string, "</");
1528 g_string_append (string, tag);
1529 g_string_append (string, ">\n");
1533 gst_value_serialize_xmp (const GValue * value)
1535 switch (G_VALUE_TYPE (value)) {
1537 return g_markup_escape_text (g_value_get_string (value), -1);
1539 return g_strdup_printf ("%d", g_value_get_int (value));
1541 return g_strdup_printf ("%u", g_value_get_uint (value));
1543 return double_to_fraction_string (g_value_get_double (value));
1547 /* put non-switchable types here */
1548 if (G_VALUE_TYPE (value) == G_TYPE_DATE) {
1549 const GDate *date = g_value_get_boxed (value);
1551 return g_strdup_printf ("%04d-%02d-%02d",
1552 (gint) g_date_get_year (date), (gint) g_date_get_month (date),
1553 (gint) g_date_get_day (date));
1554 } else if (G_VALUE_TYPE (value) == GST_TYPE_DATE_TIME) {
1555 gint year, month, day, hour, min, sec, microsec;
1556 gfloat gmt_offset = 0;
1557 gint gmt_offset_hour, gmt_offset_min;
1558 GstDateTime *datetime = (GstDateTime *) g_value_get_boxed (value);
1560 if (!gst_date_time_has_time (datetime))
1561 return gst_date_time_to_iso8601_string (datetime);
1563 /* can't just use gst_date_time_to_iso8601_string() here because we need
1564 * the timezone info with a colon, i.e. as +03:00 instead of +0300 */
1565 year = gst_date_time_get_year (datetime);
1566 month = gst_date_time_get_month (datetime);
1567 day = gst_date_time_get_day (datetime);
1568 hour = gst_date_time_get_hour (datetime);
1569 min = gst_date_time_get_minute (datetime);
1570 sec = gst_date_time_get_second (datetime);
1571 microsec = gst_date_time_get_microsecond (datetime);
1572 gmt_offset = gst_date_time_get_time_zone_offset (datetime);
1573 if (gmt_offset == 0) {
1575 return g_strdup_printf ("%04d-%02d-%02dT%02d:%02d:%02d.%06dZ",
1576 year, month, day, hour, min, sec, microsec);
1578 gmt_offset_hour = ABS (gmt_offset);
1579 gmt_offset_min = (ABS (gmt_offset) - gmt_offset_hour) * 60;
1581 return g_strdup_printf ("%04d-%02d-%02dT%02d:%02d:%02d.%06d%c%02d:%02d",
1582 year, month, day, hour, min, sec, microsec,
1583 gmt_offset >= 0 ? '+' : '-', gmt_offset_hour, gmt_offset_min);
1591 write_one_tag (const GstTagList * list, XmpTag * xmp_tag, gpointer user_data)
1594 XmpSerializationData *serialization_data = user_data;
1595 GString *data = serialization_data->data;
1598 /* struct type handled differently */
1599 if (xmp_tag->type == GstXmpTagTypeStruct ||
1600 xmp_tag->type == GstXmpTagTypeCompound) {
1602 gboolean use_it = FALSE;
1604 /* check if any of the inner tags are present on the taglist */
1605 for (iter = xmp_tag->children; iter && !use_it; iter = g_slist_next (iter)) {
1606 XmpTag *child_tag = iter->data;
1608 if (gst_tag_list_get_value_index (list, child_tag->gst_tag, 0) != NULL) {
1615 if (xmp_tag->tag_name)
1616 string_open_tag (data, xmp_tag->tag_name);
1618 if (xmp_tag->supertype) {
1619 string_open_tag (data, xmp_tag_type_get_name (xmp_tag->supertype));
1620 if (xmp_tag->parse_type) {
1621 g_string_append (data, "<rdf:li rdf:parseType=\"");
1622 g_string_append (data, xmp_tag->parse_type);
1623 g_string_append_c (data, '"');
1624 g_string_append_c (data, '>');
1626 string_open_tag (data, "rdf:li");
1631 for (iter = xmp_tag->children; iter; iter = g_slist_next (iter)) {
1632 write_one_tag (list, iter->data, user_data);
1635 if (xmp_tag->supertype) {
1636 string_close_tag (data, "rdf:li");
1637 string_close_tag (data, xmp_tag_type_get_name (xmp_tag->supertype));
1640 if (xmp_tag->tag_name)
1641 string_close_tag (data, xmp_tag->tag_name);
1646 /* at this point we must have a gst_tag */
1647 g_assert (xmp_tag->gst_tag);
1648 if (gst_tag_list_get_value_index (list, xmp_tag->gst_tag, 0) == NULL)
1651 ct = gst_tag_list_get_tag_size (list, xmp_tag->gst_tag);
1652 string_open_tag (data, xmp_tag->tag_name);
1654 /* fast path for single valued tag */
1655 if (ct == 1 || xmp_tag->type == GstXmpTagTypeSimple) {
1656 if (xmp_tag->serialize) {
1657 s = xmp_tag->serialize (gst_tag_list_get_value_index (list,
1658 xmp_tag->gst_tag, 0));
1660 s = gst_value_serialize_xmp (gst_tag_list_get_value_index (list,
1661 xmp_tag->gst_tag, 0));
1664 g_string_append (data, s);
1667 GST_WARNING ("unhandled type for %s to xmp", xmp_tag->gst_tag);
1670 const gchar *typename;
1672 typename = xmp_tag_type_get_name (xmp_tag->type);
1674 string_open_tag (data, typename);
1675 for (i = 0; i < ct; i++) {
1676 GST_DEBUG ("mapping %s[%u/%u] to xmp", xmp_tag->gst_tag, i, ct);
1677 if (xmp_tag->serialize) {
1678 s = xmp_tag->serialize (gst_tag_list_get_value_index (list,
1679 xmp_tag->gst_tag, i));
1681 s = gst_value_serialize_xmp (gst_tag_list_get_value_index (list,
1682 xmp_tag->gst_tag, i));
1685 string_open_tag (data, "rdf:li");
1686 g_string_append (data, s);
1687 string_close_tag (data, "rdf:li");
1690 GST_WARNING ("unhandled type for %s to xmp", xmp_tag->gst_tag);
1693 string_close_tag (data, typename);
1696 string_close_tag (data, xmp_tag->tag_name);
1700 * gst_tag_list_to_xmp_buffer:
1702 * @read_only: does the container forbid inplace editing
1703 * @schemas: (array zero-terminated=1):
1704 * %NULL terminated array of schemas to be used on serialization
1706 * Formats a taglist as a xmp packet using only the selected
1707 * schemas. An empty list (%NULL) means that all schemas should
1710 * Returns: (transfer full) (nullable): new buffer or %NULL, unref the buffer when done
1713 gst_tag_list_to_xmp_buffer (const GstTagList * list, gboolean read_only,
1714 const gchar ** schemas)
1716 GstBuffer *buffer = NULL;
1717 XmpSerializationData serialization_data;
1723 serialization_data.data = g_string_sized_new (4096);
1724 serialization_data.schemas = schemas;
1725 data = serialization_data.data;
1727 xmp_tags_initialize ();
1729 g_return_val_if_fail (GST_IS_TAG_LIST (list), NULL);
1732 g_string_append (data,
1733 "<?xpacket begin=\"\xEF\xBB\xBF\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>\n");
1734 g_string_append (data,
1735 "<x:xmpmeta xmlns:x=\"adobe:ns:meta/\" x:xmptk=\"GStreamer\">\n");
1736 g_string_append (data,
1737 "<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"");
1739 while (ns_match[i].ns_prefix) {
1740 if (xmp_serialization_data_use_schema (&serialization_data,
1741 ns_match[i].ns_prefix)) {
1742 g_string_append_printf (data, " xmlns:%s=\"%s\"",
1743 ns_match[i].ns_prefix, ns_match[i].ns_uri);
1744 if (ns_match[i].extra_ns) {
1745 g_string_append_printf (data, " %s", ns_match[i].extra_ns);
1750 g_string_append (data, ">\n");
1751 g_string_append (data, "<rdf:Description rdf:about=\"\">\n");
1753 /* iterate the schemas */
1754 if (schemas == NULL) {
1755 /* use all schemas */
1756 schemas = gst_tag_xmp_list_schemas ();
1758 for (i = 0; schemas[i] != NULL; i++) {
1759 GstXmpSchema *schema = _gst_xmp_get_schema (schemas[i]);
1760 GHashTableIter iter;
1761 gpointer key, value;
1766 /* Iterate over the hashtable */
1767 g_hash_table_iter_init (&iter, schema);
1768 while (g_hash_table_iter_next (&iter, &key, &value)) {
1769 write_one_tag (list, value, (gpointer) & serialization_data);
1774 g_string_append (data, "</rdf:Description>\n");
1775 g_string_append (data, "</rdf:RDF>\n");
1776 g_string_append (data, "</x:xmpmeta>\n");
1779 /* the xmp spec recommends to add 2-4KB padding for in-place editable xmp */
1782 for (i = 0; i < 32; i++) {
1783 g_string_append (data, " " " "
1787 g_string_append_printf (data, "<?xpacket end=\"%c\"?>",
1788 (read_only ? 'r' : 'w'));
1791 bdata = g_string_free (data, FALSE);
1793 buffer = gst_buffer_new_wrapped (bdata, bsize);
1798 #undef gst_xmp_schema_lookup
1799 #undef gst_xmp_schema_insert