tag: Add/fix various annotations
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-base / gst-libs / gst / tag / gstxmptag.c
1 /* GStreamer
2  * Copyright (C) 2010 Stefan Kost <stefan.kost@nokia.com>
3  * Copyright (C) 2010 Thiago Santos <thiago.sousa.santos@collabora.co.uk>
4  *
5  * gstxmptag.c: library for reading / modifying xmp tags
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 /**
24  * SECTION:gsttagxmp
25  * @title: GstXmptag
26  * @short_description: tag mappings and support functions for plugins
27  *                     dealing with xmp packets
28  * @see_also: #GstTagList
29  *
30  * Contains various utility functions for plugins to parse or create
31  * xmp packets and map them to and from #GstTagList<!-- -->s.
32  *
33  * Please note that the xmp parser is very lightweight and not strict at all.
34  */
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39 #include "tag.h"
40 #include <gst/gsttagsetter.h>
41 #include "gsttageditingprivate.h"
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <time.h>
46 #include <ctype.h>
47
48 #define GST_CAT_DEFAULT gst_tag_ensure_debug_category()
49
50 static GstDebugCategory *
51 gst_tag_ensure_debug_category (void)
52 {
53   static gsize cat_gonce = 0;
54
55   if (g_once_init_enter (&cat_gonce)) {
56     GstDebugCategory *cat = NULL;
57
58     GST_DEBUG_CATEGORY_INIT (cat, "xmp-tags", 0, "XMP GstTag helper functions");
59
60     g_once_init_leave (&cat_gonce, (gsize) cat);
61   }
62
63   return (GstDebugCategory *) cat_gonce;
64 }
65
66 static const gchar *schema_list[] = {
67   "dc",
68   "xap",
69   "tiff",
70   "exif",
71   "photoshop",
72   "Iptc4xmpCore",
73   "Iptc4xmpExt",
74   NULL
75 };
76
77 /**
78  * gst_tag_xmp_list_schemas:
79  *
80  * Gets the list of supported schemas in the xmp lib
81  *
82  * Returns: (transfer none) (array zero-terminated=1): a %NULL terminated array of strings with the
83  *     schema names
84  */
85 const gchar **
86 gst_tag_xmp_list_schemas (void)
87 {
88   return schema_list;
89 }
90
91 typedef struct _XmpSerializationData XmpSerializationData;
92 typedef struct _XmpTag XmpTag;
93
94 /*
95  * Serializes a GValue into a string.
96  */
97 typedef gchar *(*XmpSerializationFunc) (const GValue * value);
98
99 /*
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.
102  *
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
107  */
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);
111
112 struct _XmpSerializationData
113 {
114   GString *data;
115   const gchar **schemas;
116 };
117
118 static gboolean
119 xmp_serialization_data_use_schema (XmpSerializationData * serdata,
120     const gchar * schemaname)
121 {
122   gint i = 0;
123   if (serdata->schemas == NULL)
124     return TRUE;
125
126   while (serdata->schemas[i] != NULL) {
127     if (strcmp (serdata->schemas[i], schemaname) == 0)
128       return TRUE;
129     i++;
130   }
131   return FALSE;
132 }
133
134 typedef enum
135 {
136   GstXmpTagTypeNone = 0,
137   GstXmpTagTypeSimple,
138   GstXmpTagTypeBag,
139   GstXmpTagTypeSeq,
140   GstXmpTagTypeStruct,
141
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
144    *
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.
148    */
149   GstXmpTagTypeCompound
150 } GstXmpTagType;
151
152 struct _XmpTag
153 {
154   const gchar *gst_tag;
155   const gchar *tag_name;
156   GstXmpTagType type;
157
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;
161
162   /* For tags that need a rdf:parseType attribute */
163   const gchar *parse_type;
164
165   /* Used for struct and compound types */
166   GSList *children;
167
168   XmpSerializationFunc serialize;
169   XmpDeserializationFunc deserialize;
170 };
171
172 static GstTagMergeMode
173 xmp_tag_get_merge_mode (XmpTag * xmptag)
174 {
175   switch (xmptag->type) {
176     case GstXmpTagTypeBag:
177     case GstXmpTagTypeSeq:
178       return GST_TAG_MERGE_APPEND;
179     case GstXmpTagTypeSimple:
180     default:
181       return GST_TAG_MERGE_KEEP;
182   }
183 }
184
185 static const gchar *
186 xmp_tag_type_get_name (GstXmpTagType tagtype)
187 {
188   switch (tagtype) {
189     case GstXmpTagTypeSeq:
190       return "rdf:Seq";
191     case GstXmpTagTypeBag:
192       return "rdf:Bag";
193     default:
194       break;
195   }
196
197   /* Make compiler happy */
198   g_return_val_if_reached ("");
199 }
200
201 struct _PendingXmpTag
202 {
203   XmpTag *xmp_tag;
204   gchar *str;
205 };
206 typedef struct _PendingXmpTag PendingXmpTag;
207
208 /*
209  * A schema is a mapping of strings (the tag name in gstreamer) to a list of
210  * tags in xmp (XmpTag).
211  */
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 ()
217 {
218   return g_hash_table_new (g_direct_hash, g_direct_equal);
219 }
220
221 /*
222  * Mappings from schema names into the schema group of tags (GstXmpSchema)
223  */
224 static GHashTable *__xmp_schemas;
225
226 static GstXmpSchema *
227 _gst_xmp_get_schema (const gchar * name)
228 {
229   GQuark key;
230   GstXmpSchema *schema;
231
232   key = g_quark_from_string (name);
233
234   schema = g_hash_table_lookup (__xmp_schemas, GUINT_TO_POINTER (key));
235   if (!schema) {
236     GST_WARNING ("Schema %s doesn't exist", name);
237   }
238   return schema;
239 }
240
241 static void
242 _gst_xmp_add_schema (const gchar * name, GstXmpSchema * schema)
243 {
244   GQuark key;
245
246   key = g_quark_from_string (name);
247
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 ();
251     return;
252   }
253
254   g_hash_table_insert (__xmp_schemas, GUINT_TO_POINTER (key), schema);
255 }
256
257 static void
258 _gst_xmp_schema_add_mapping (GstXmpSchema * schema, XmpTag * tag)
259 {
260   GQuark key;
261
262   key = g_quark_from_string (tag->gst_tag);
263
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 ();
267     return;
268   }
269   gst_xmp_schema_insert (schema, GUINT_TO_POINTER (key), tag);
270 }
271
272 static XmpTag *
273 gst_xmp_tag_create (const gchar * gst_tag, const gchar * xmp_tag,
274     gint xmp_type, XmpSerializationFunc serialization_func,
275     XmpDeserializationFunc deserialization_func)
276 {
277   XmpTag *xmpinfo;
278
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;
288
289   return xmpinfo;
290 }
291
292 static XmpTag *
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)
297 {
298   XmpTag *xmptag;
299   XmpTag *xmptag_a =
300       gst_xmp_tag_create (gst_tag, xmp_tag_a, GstXmpTagTypeSimple,
301       serialization_func_a, deserialization_func);
302   XmpTag *xmptag_b =
303       gst_xmp_tag_create (gst_tag, xmp_tag_b, GstXmpTagTypeSimple,
304       serialization_func_b, deserialization_func);
305
306   xmptag =
307       gst_xmp_tag_create (gst_tag, NULL, GstXmpTagTypeCompound, NULL, NULL);
308
309   xmptag->children = g_slist_prepend (xmptag->children, xmptag_b);
310   xmptag->children = g_slist_prepend (xmptag->children, xmptag_a);
311
312   return xmptag;
313 }
314
315 static void
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)
320 {
321   _gst_xmp_schema_add_mapping (schema,
322       gst_xmp_tag_create (gst_tag, xmp_tag, xmp_type, serialization_func,
323           deserialization_func));
324 }
325
326 /*
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
330  */
331 #if 0
332 static GPtrArray *
333 _xmp_tag_get_mapping (const gchar * gst_tag, XmpSerializationData * serdata)
334 {
335   GPtrArray *ret = NULL;
336   GHashTableIter iter;
337   GQuark key = g_quark_from_string (gst_tag);
338   gpointer iterkey, value;
339   const gchar *schemaname;
340
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;
344
345     schemaname = g_quark_to_string (GPOINTER_TO_UINT (iterkey));
346     if (xmp_serialization_data_use_schema (serdata, schemaname))
347       ret =
348           (GPtrArray *) gst_xmp_schema_lookup (schema, GUINT_TO_POINTER (key));
349   }
350   return ret;
351 }
352 #endif
353
354 /* finds the gst tag that maps to this xmp tag in this schema */
355 static const gchar *
356 _gst_xmp_schema_get_mapping_reverse (GstXmpSchema * schema,
357     const gchar * xmp_tag, XmpTag ** _xmp_tag)
358 {
359   GHashTableIter iter;
360   gpointer key, value;
361   const gchar *ret = NULL;
362
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;
367
368     if (xmpinfo->tag_name) {
369       if (strcmp (xmpinfo->tag_name, xmp_tag) == 0) {
370         *_xmp_tag = xmpinfo;
371         ret = g_quark_to_string (GPOINTER_TO_UINT (key));
372         goto out;
373       }
374     } else if (xmpinfo->children) {
375       GSList *iter;
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) {
379           *_xmp_tag = child;
380           ret = g_quark_to_string (GPOINTER_TO_UINT (key));
381           goto out;
382         }
383       }
384     } else {
385       g_assert_not_reached ();
386     }
387   }
388
389 out:
390   return ret;
391 }
392
393 /* finds the gst tag that maps to this xmp tag (searches on all schemas) */
394 static const gchar *
395 _gst_xmp_tag_get_mapping_reverse (const gchar * xmp_tag, XmpTag ** _xmp_tag)
396 {
397   GHashTableIter iter;
398   gpointer key, value;
399   const gchar *ret = NULL;
400
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,
405         _xmp_tag);
406   }
407   return ret;
408 }
409
410 /* utility functions/macros */
411
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)
416
417 static gchar *
418 double_to_fraction_string (gdouble num)
419 {
420   gint frac_n;
421   gint frac_d;
422
423   gst_util_double_to_fraction (num, &frac_n, &frac_d);
424   return g_strdup_printf ("%d/%d", frac_n, frac_d);
425 }
426
427 /* (de)serialize functions */
428 static gchar *
429 serialize_exif_gps_coordinate (const GValue * value, gchar pos, gchar neg)
430 {
431   gdouble num;
432   gchar c;
433   gint integer;
434   gchar fraction[G_ASCII_DTOSTR_BUF_SIZE];
435
436   g_return_val_if_fail (G_VALUE_TYPE (value) == G_TYPE_DOUBLE, NULL);
437
438   num = g_value_get_double (value);
439   if (num < 0) {
440     c = neg;
441     num *= -1;
442   } else {
443     c = pos;
444   }
445   integer = (gint) num;
446
447   g_ascii_dtostr (fraction, sizeof (fraction), (num - integer) * 60);
448
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);
452 }
453
454 static gchar *
455 serialize_exif_latitude (const GValue * value)
456 {
457   return serialize_exif_gps_coordinate (value, 'N', 'S');
458 }
459
460 static gchar *
461 serialize_exif_longitude (const GValue * value)
462 {
463   return serialize_exif_gps_coordinate (value, 'E', 'W');
464 }
465
466 static void
467 deserialize_exif_gps_coordinate (XmpTag * xmptag, GstTagList * taglist,
468     const gchar * gst_tag, const gchar * str, gchar pos, gchar neg)
469 {
470   gdouble value = 0;
471   gint d = 0, m = 0, s = 0;
472   gdouble m2 = 0;
473   gchar c = 0;
474   const gchar *current;
475
476   /* get the degrees */
477   if (sscanf (str, "%d", &d) != 1)
478     goto error;
479
480   /* find the beginning of the minutes */
481   current = strchr (str, ',');
482   if (current == NULL)
483     goto end;
484   current += 1;
485
486   /* check if it uses ,SS or .mm */
487   if (strchr (current, ',') != NULL) {
488     if (!sscanf (current, "%d,%d%c", &m, &s, &c))
489       goto error;
490   } else {
491     gchar *copy = g_strdup (current);
492     gint len = strlen (copy);
493     gint i;
494
495     /* check the last letter */
496     for (i = len - 1; len >= 0; len--) {
497       if (g_ascii_isspace (copy[i]))
498         continue;
499
500       if (g_ascii_isalpha (copy[i])) {
501         /* found it */
502         c = copy[i];
503         copy[i] = '\0';
504         break;
505
506       } else {
507         /* something is wrong */
508         g_free (copy);
509         goto error;
510       }
511     }
512
513     /* use a copy so we can change the last letter as E can cause
514      * problems here */
515     m2 = g_ascii_strtod (copy, NULL);
516     g_free (copy);
517   }
518
519 end:
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);
522
523   if (c == pos) {
524     //NOP
525   } else if (c == neg) {
526     value *= -1;
527   } else {
528     goto error;
529   }
530
531   gst_tag_list_add (taglist, xmp_tag_get_merge_mode (xmptag), gst_tag, value,
532       NULL);
533   return;
534
535 error:
536   GST_WARNING ("Failed to deserialize gps coordinate: %s", str);
537 }
538
539 static void
540 deserialize_exif_latitude (XmpTag * xmptag, GstTagList * taglist,
541     const gchar * gst_tag, const gchar * xmp_tag, const gchar * str,
542     GSList ** pending_tags)
543 {
544   deserialize_exif_gps_coordinate (xmptag, taglist, gst_tag, str, 'N', 'S');
545 }
546
547 static void
548 deserialize_exif_longitude (XmpTag * xmptag, GstTagList * taglist,
549     const gchar * gst_tag, const gchar * xmp_tag, const gchar * str,
550     GSList ** pending_tags)
551 {
552   deserialize_exif_gps_coordinate (xmptag, taglist, gst_tag, str, 'E', 'W');
553 }
554
555 static gchar *
556 serialize_exif_altitude (const GValue * value)
557 {
558   gdouble num;
559
560   num = g_value_get_double (value);
561
562   if (num < 0)
563     num *= -1;
564
565   return double_to_fraction_string (num);
566 }
567
568 static gchar *
569 serialize_exif_altituderef (const GValue * value)
570 {
571   gdouble num;
572
573   num = g_value_get_double (value);
574
575   /* 0 means above sea level, 1 means below */
576   if (num >= 0)
577     return g_strdup ("0");
578   return g_strdup ("1");
579 }
580
581 static void
582 deserialize_exif_altitude (XmpTag * xmptag, GstTagList * taglist,
583     const gchar * gst_tag, const gchar * xmp_tag, const gchar * str,
584     GSList ** pending_tags)
585 {
586   const gchar *altitude_str = NULL;
587   const gchar *altituderef_str = NULL;
588   gint frac_n;
589   gint frac_d;
590   gdouble value;
591
592   GSList *entry;
593   PendingXmpTag *ptag = NULL;
594
595   /* find the other missing part */
596   if (strcmp (xmp_tag, "exif:GPSAltitude") == 0) {
597     altitude_str = str;
598
599     for (entry = *pending_tags; entry; entry = g_slist_next (entry)) {
600       ptag = (PendingXmpTag *) entry->data;
601
602       if (strcmp (ptag->xmp_tag->tag_name, "exif:GPSAltitudeRef") == 0) {
603         altituderef_str = ptag->str;
604         break;
605       }
606     }
607
608   } else if (strcmp (xmp_tag, "exif:GPSAltitudeRef") == 0) {
609     altituderef_str = str;
610
611     for (entry = *pending_tags; entry; entry = g_slist_next (entry)) {
612       ptag = (PendingXmpTag *) entry->data;
613
614       if (strcmp (ptag->xmp_tag->tag_name, "exif:GPSAltitude") == 0) {
615         altitude_str = ptag->str;
616         break;
617       }
618     }
619
620   } else {
621     GST_WARNING ("Unexpected xmp tag %s", xmp_tag);
622     return;
623   }
624
625   if (!altitude_str) {
626     GST_WARNING ("Missing exif:GPSAltitude tag");
627     return;
628   }
629   if (!altituderef_str) {
630     GST_WARNING ("Missing exif:GPSAltitudeRef tag");
631     return;
632   }
633
634   if (sscanf (altitude_str, "%d/%d", &frac_n, &frac_d) != 2) {
635     GST_WARNING ("Failed to parse fraction: %s", altitude_str);
636     return;
637   }
638
639   gst_util_fraction_to_double (frac_n, frac_d, &value);
640
641   if (altituderef_str[0] == '0') {
642     /* nop */
643   } else if (altituderef_str[0] == '1') {
644     value *= -1;
645   } else {
646     GST_WARNING ("Unexpected exif:AltitudeRef value: %s", altituderef_str);
647     return;
648   }
649
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);
653
654   /* clean up entry */
655   g_free (ptag->str);
656   g_slice_free (PendingXmpTag, ptag);
657   *pending_tags = g_slist_delete_link (*pending_tags, entry);
658 }
659
660 static gchar *
661 serialize_exif_gps_speed (const GValue * value)
662 {
663   return double_to_fraction_string (g_value_get_double (value) *
664       METERS_PER_SECOND_TO_KILOMETERS_PER_HOUR);
665 }
666
667 static gchar *
668 serialize_exif_gps_speedref (const GValue * value)
669 {
670   /* we always use km/h */
671   return g_strdup ("K");
672 }
673
674 static void
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)
678 {
679   const gchar *speed_str = NULL;
680   const gchar *speedref_str = NULL;
681   gint frac_n;
682   gint frac_d;
683   gdouble value;
684
685   GSList *entry;
686   PendingXmpTag *ptag = NULL;
687
688   /* find the other missing part */
689   if (strcmp (xmp_tag, "exif:GPSSpeed") == 0) {
690     speed_str = str;
691
692     for (entry = *pending_tags; entry; entry = g_slist_next (entry)) {
693       ptag = (PendingXmpTag *) entry->data;
694
695       if (strcmp (ptag->xmp_tag->tag_name, "exif:GPSSpeedRef") == 0) {
696         speedref_str = ptag->str;
697         break;
698       }
699     }
700
701   } else if (strcmp (xmp_tag, "exif:GPSSpeedRef") == 0) {
702     speedref_str = str;
703
704     for (entry = *pending_tags; entry; entry = g_slist_next (entry)) {
705       ptag = (PendingXmpTag *) entry->data;
706
707       if (strcmp (ptag->xmp_tag->tag_name, "exif:GPSSpeed") == 0) {
708         speed_str = ptag->str;
709         break;
710       }
711     }
712
713   } else {
714     GST_WARNING ("Unexpected xmp tag %s", xmp_tag);
715     return;
716   }
717
718   if (!speed_str) {
719     GST_WARNING ("Missing exif:GPSSpeed tag");
720     return;
721   }
722   if (!speedref_str) {
723     GST_WARNING ("Missing exif:GPSSpeedRef tag");
724     return;
725   }
726
727   if (sscanf (speed_str, "%d/%d", &frac_n, &frac_d) != 2) {
728     GST_WARNING ("Failed to parse fraction: %s", speed_str);
729     return;
730   }
731
732   gst_util_fraction_to_double (frac_n, frac_d, &value);
733
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;
740   } else {
741     GST_WARNING ("Unexpected exif:SpeedRef value: %s", speedref_str);
742     return;
743   }
744
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);
748
749   /* clean up entry */
750   g_free (ptag->str);
751   g_slice_free (PendingXmpTag, ptag);
752   *pending_tags = g_slist_delete_link (*pending_tags, entry);
753 }
754
755 static gchar *
756 serialize_exif_gps_direction (const GValue * value)
757 {
758   return double_to_fraction_string (g_value_get_double (value));
759 }
760
761 static gchar *
762 serialize_exif_gps_directionref (const GValue * value)
763 {
764   /* T for true geographic direction (M would mean magnetic) */
765   return g_strdup ("T");
766 }
767
768 static void
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)
773 {
774   const gchar *dir_str = NULL;
775   const gchar *dirref_str = NULL;
776   gint frac_n;
777   gint frac_d;
778   gdouble value;
779
780   GSList *entry;
781   PendingXmpTag *ptag = NULL;
782
783   /* find the other missing part */
784   if (strcmp (xmp_tag, direction_tag) == 0) {
785     dir_str = str;
786
787     for (entry = *pending_tags; entry; entry = g_slist_next (entry)) {
788       ptag = (PendingXmpTag *) entry->data;
789
790       if (strcmp (ptag->xmp_tag->tag_name, directionref_tag) == 0) {
791         dirref_str = ptag->str;
792         break;
793       }
794     }
795
796   } else if (strcmp (xmp_tag, directionref_tag) == 0) {
797     dirref_str = str;
798
799     for (entry = *pending_tags; entry; entry = g_slist_next (entry)) {
800       ptag = (PendingXmpTag *) entry->data;
801
802       if (strcmp (ptag->xmp_tag->tag_name, direction_tag) == 0) {
803         dir_str = ptag->str;
804         break;
805       }
806     }
807
808   } else {
809     GST_WARNING ("Unexpected xmp tag %s", xmp_tag);
810     return;
811   }
812
813   if (!dir_str) {
814     GST_WARNING ("Missing GPSImgDirection tag");
815     return;
816   }
817   if (!dirref_str) {
818     GST_WARNING ("Missing GPSImgDirectionRef tag");
819     return;
820   }
821
822   if (sscanf (dir_str, "%d/%d", &frac_n, &frac_d) != 2) {
823     GST_WARNING ("Failed to parse fraction: %s", dir_str);
824     return;
825   }
826
827   gst_util_fraction_to_double (frac_n, frac_d, &value);
828
829   if (dirref_str[0] == 'T') {
830     /* nop */
831   } else if (dirref_str[0] == 'M') {
832     GST_WARNING ("Magnetic direction tags aren't supported yet");
833     return;
834   } else {
835     GST_WARNING ("Unexpected %s value: %s", directionref_tag, dirref_str);
836     return;
837   }
838
839   /* add to the taglist */
840   gst_tag_list_add (taglist, xmp_tag_get_merge_mode (xmptag), gst_tag, value,
841       NULL);
842
843   /* clean up entry */
844   g_free (ptag->str);
845   g_slice_free (PendingXmpTag, ptag);
846   *pending_tags = g_slist_delete_link (*pending_tags, entry);
847 }
848
849 static void
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)
853 {
854   deserialize_exif_gps_direction (xmptag, taglist, gst_tag, xmp_tag, str,
855       pending_tags, "exif:GPSTrack", "exif:GPSTrackRef");
856 }
857
858 static void
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)
862 {
863   deserialize_exif_gps_direction (xmptag, taglist, gst_tag, xmp_tag, str,
864       pending_tags, "exif:GPSImgDirection", "exif:GPSImgDirectionRef");
865 }
866
867 static void
868 deserialize_xmp_rating (XmpTag * xmptag, GstTagList * taglist,
869     const gchar * gst_tag, const gchar * xmp_tag, const gchar * str,
870     GSList ** pending_tags)
871 {
872   guint value;
873
874   if (sscanf (str, "%u", &value) != 1) {
875     GST_WARNING ("Failed to parse xmp:Rating %s", str);
876     return;
877   }
878
879   if (value > 100) {
880     GST_WARNING ("Unsupported Rating tag %u (should be from 0 to 100), "
881         "ignoring", value);
882     return;
883   }
884
885   gst_tag_list_add (taglist, xmp_tag_get_merge_mode (xmptag), gst_tag, value,
886       NULL);
887 }
888
889 static gchar *
890 serialize_tiff_orientation (const GValue * value)
891 {
892   const gchar *str;
893   gint num;
894
895   str = g_value_get_string (value);
896   if (str == NULL) {
897     GST_WARNING ("Failed to get image orientation tag value");
898     return NULL;
899   }
900
901   num = __exif_tag_image_orientation_to_exif_value (str);
902   if (num == -1)
903     return NULL;
904
905   return g_strdup_printf ("%d", num);
906 }
907
908 static void
909 deserialize_tiff_orientation (XmpTag * xmptag, GstTagList * taglist,
910     const gchar * gst_tag, const gchar * xmp_tag, const gchar * str,
911     GSList ** pending_tags)
912 {
913   guint value;
914   const gchar *orientation = NULL;
915
916   if (sscanf (str, "%u", &value) != 1) {
917     GST_WARNING ("Failed to parse tiff:Orientation %s", str);
918     return;
919   }
920
921   if (value < 1 || value > 8) {
922     GST_WARNING ("Invalid tiff:Orientation tag %u (should be from 1 to 8), "
923         "ignoring", value);
924     return;
925   }
926
927   orientation = __exif_tag_image_orientation_from_exif_value (value);
928   if (orientation == NULL)
929     return;
930   gst_tag_list_add (taglist, xmp_tag_get_merge_mode (xmptag), gst_tag,
931       orientation, NULL);
932 }
933
934
935 /* look at this page for additional schemas
936  * http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/XMP.html
937  */
938 static gpointer
939 _init_xmp_tag_map (gpointer user_data)
940 {
941   XmpTag *xmpinfo;
942   GstXmpSchema *schema;
943
944   gst_tag_register_musicbrainz_tags ();
945
946   __xmp_schemas = g_hash_table_new (g_direct_hash, g_direct_equal);
947
948   /* add the maps */
949   /* dublic code metadata
950    * http://dublincore.org/documents/dces/
951    */
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);
969
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);
975
976   /* tiff */
977   schema = gst_xmp_schema_new ();
978   _gst_xmp_schema_add_simple_mapping (schema,
979       GST_TAG_DEVICE_MANUFACTURER, "tiff:Make", GstXmpTagTypeSimple, NULL,
980       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);
989
990   /* exif 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);
1003
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);
1009
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);
1014
1015   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);
1020
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);
1026
1027   _gst_xmp_add_schema ("exif", schema);
1028
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);
1037
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);
1044
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));
1054   xmpinfo->children =
1055       g_slist_prepend (xmpinfo->children,
1056       gst_xmp_tag_create (GST_TAG_GEO_LOCATION_CITY,
1057           "LocationDetails:City", GstXmpTagTypeSimple, NULL, NULL));
1058   xmpinfo->children =
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);
1064
1065   return NULL;
1066 }
1067
1068 static void
1069 xmp_tags_initialize ()
1070 {
1071   static GOnce my_once = G_ONCE_INIT;
1072   g_once (&my_once, (GThreadFunc) _init_xmp_tag_map, NULL);
1073 }
1074
1075 typedef struct _GstXmpNamespaceMatch GstXmpNamespaceMatch;
1076 struct _GstXmpNamespaceMatch
1077 {
1078   const gchar *ns_prefix;
1079   const gchar *ns_uri;
1080
1081   /*
1082    * Stores extra namespaces for array tags
1083    * The namespaces should be written in the form:
1084    *
1085    * xmlns:XpTo="http://some.org/your/ns/name/ (next ones)"
1086    */
1087   const gchar *extra_ns;
1088 };
1089
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/\""},
1099   {NULL, NULL, NULL}
1100 };
1101
1102 typedef struct _GstXmpNamespaceMap GstXmpNamespaceMap;
1103 struct _GstXmpNamespaceMap
1104 {
1105   const gchar *original_ns;
1106   gchar *gstreamer_ns;
1107 };
1108
1109 /* parsing */
1110
1111 static void
1112 read_one_tag (GstTagList * list, XmpTag * xmptag,
1113     const gchar * v, GSList ** pending_tags)
1114 {
1115   GType tag_type;
1116   GstTagMergeMode merge_mode;
1117   const gchar *tag = xmptag->gst_tag;
1118
1119   g_return_if_fail (tag != NULL);
1120
1121   if (xmptag->deserialize) {
1122     xmptag->deserialize (xmptag, list, tag, xmptag->tag_name, v, pending_tags);
1123     return;
1124   }
1125
1126   merge_mode = xmp_tag_get_merge_mode (xmptag);
1127   tag_type = gst_tag_get_type (tag);
1128
1129   /* add gstreamer tag depending on type */
1130   switch (tag_type) {
1131     case G_TYPE_STRING:{
1132       gst_tag_list_add (list, merge_mode, tag, v, NULL);
1133       break;
1134     }
1135     case G_TYPE_DOUBLE:{
1136       gdouble value = 0;
1137       gint frac_n, frac_d;
1138
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);
1142       } else {
1143         GST_WARNING ("Failed to parse fraction: %s", v);
1144       }
1145       break;
1146     }
1147     default:
1148       if (tag_type == GST_TYPE_DATE_TIME) {
1149         GstDateTime *datetime;
1150
1151         if (v == NULL || *v == '\0') {
1152           GST_WARNING ("Empty string for datetime parsing");
1153           return;
1154         }
1155
1156         GST_DEBUG ("Parsing %s into a datetime", v);
1157         datetime = gst_date_time_new_from_iso8601_string (v);
1158         if (datetime) {
1159           gst_tag_list_add (list, merge_mode, tag, datetime, NULL);
1160           gst_date_time_unref (datetime);
1161         }
1162
1163       } else if (tag_type == G_TYPE_DATE) {
1164         GST_ERROR ("Use GST_TYPE_DATE_TIME in tags instead of G_TYPE_DATE");
1165       } else {
1166         GST_WARNING ("unhandled type for %s from xmp", tag);
1167       }
1168       break;
1169   }
1170 }
1171
1172 /**
1173  * gst_tag_list_from_xmp_buffer:
1174  * @buffer: buffer
1175  *
1176  * Parse a xmp packet into a taglist.
1177  *
1178  * Returns: (transfer full) (nullable): new taglist or %NULL, free the list when done
1179  */
1180 GstTagList *
1181 gst_tag_list_from_xmp_buffer (GstBuffer * buffer)
1182 {
1183   GstTagList *list = NULL;
1184   GstMapInfo info;
1185   gchar *xps, *xp1, *xp2, *xpe, *ns, *ne;
1186   gsize len, max_ft_len;
1187   gboolean in_tag;
1188   gchar *part = NULL, *pp;
1189   guint i;
1190   XmpTag *last_xmp_tag = NULL;
1191   GSList *pending_tags = NULL;
1192
1193   /* Used for strucuture xmp tags */
1194   XmpTag *context_tag = NULL;
1195
1196   GstXmpNamespaceMap ns_map[] = {
1197     {"dc", NULL}
1198     ,
1199     {"exif", NULL}
1200     ,
1201     {"tiff", NULL}
1202     ,
1203     {"xap", NULL}
1204     ,
1205     {"photoshop", NULL}
1206     ,
1207     {"Iptc4xmpCore", NULL}
1208     ,
1209     {"Iptc4xmpExt", NULL}
1210     ,
1211     {NULL, NULL}
1212   };
1213
1214   xmp_tags_initialize ();
1215
1216   g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
1217
1218   GST_LOG ("Starting xmp parsing");
1219
1220   gst_buffer_map (buffer, &info, GST_MAP_READ);
1221   xps = (gchar *) info.data;
1222   len = info.size;
1223   g_return_val_if_fail (len > 0, NULL);
1224
1225   xpe = &xps[len + 1];
1226
1227   /* check header and footer */
1228   xp1 = g_strstr_len (xps, len, "<?xpacket begin");
1229   if (!xp1)
1230     goto missing_header;
1231   xp1 = &xp1[strlen ("<?xpacket begin")];
1232   while (*xp1 != '>' && *xp1 != '<' && xp1 < xpe)
1233     xp1++;
1234   if (*xp1 != '>')
1235     goto missing_header;
1236
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;
1243
1244   xp2 = g_strstr_len (&xps[len - max_ft_len], max_ft_len, "<?xpacket ");
1245   if (!xp2)
1246     goto missing_footer;
1247
1248   GST_INFO ("xmp header okay");
1249
1250   /* skip > and text until first xml-node */
1251   xp1++;
1252   while (*xp1 != '<' && xp1 < xpe)
1253     xp1++;
1254
1255   /* no tag can be longer than the whole buffer */
1256   part = g_malloc (xp2 - xp1);
1257   list = gst_tag_list_new_empty ();
1258
1259   /* parse data into a list of nodes */
1260   /* data is between xp1..xp2 */
1261   in_tag = TRUE;
1262   ns = ne = xp1;
1263   pp = part;
1264   while (ne < xp2) {
1265     if (in_tag) {
1266       ne++;
1267       while (ne < xp2 && *ne != '>' && *ne != '<') {
1268         if (*ne == '\n' || *ne == '\t' || *ne == ' ') {
1269           while (ne < xp2 && (*ne == '\n' || *ne == '\t' || *ne == ' '))
1270             ne++;
1271           *pp++ = ' ';
1272         } else {
1273           *pp++ = *ne++;
1274         }
1275       }
1276       *pp = '\0';
1277       if (*ne != '>')
1278         goto broken_xml;
1279       /* create node */
1280       /* {XML, ns, ne-ns} */
1281       if (ns[0] != '/') {
1282         gchar *as = strchr (part, ' ');
1283         /* only log start nodes */
1284         GST_INFO ("xml: %s", part);
1285
1286         if (as) {
1287           gchar *ae, *d;
1288
1289           /* skip ' ' and scan the attributes */
1290           as++;
1291           d = ae = as;
1292
1293           /* split attr=value pairs */
1294           while (*ae != '\0') {
1295             if (*ae == '=') {
1296               /* attr/value delimmiter */
1297               d = ae;
1298             } else if (*ae == '"') {
1299               /* scan values */
1300               gchar *v;
1301
1302               ae++;
1303               while (*ae != '\0' && *ae != '"')
1304                 ae++;
1305
1306               *d = *ae = '\0';
1307               v = &d[2];
1308               GST_INFO ("   : [%s][%s]", as, v);
1309               if (!strncmp (as, "xmlns:", 6)) {
1310                 i = 0;
1311                 /* we need to rewrite known namespaces to what we use in
1312                  * tag_matches */
1313                 while (ns_match[i].ns_prefix) {
1314                   if (!strcmp (ns_match[i].ns_uri, v))
1315                     break;
1316                   i++;
1317                 }
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]);
1322                   }
1323                 }
1324               } else {
1325                 XmpTag *xmp_tag = NULL;
1326                 /* FIXME: eventually rewrite ns
1327                  * find ':'
1328                  * check if ns before ':' is in ns_map and ns_map[i].gstreamer_ns!=NULL
1329                  * do 2 stage filter in tag_matches
1330                  */
1331                 if (context_tag) {
1332                   GSList *iter;
1333
1334                   for (iter = context_tag->children; iter;
1335                       iter = g_slist_next (iter)) {
1336                     XmpTag *child = iter->data;
1337
1338                     GST_DEBUG ("Looking at child tag %s : %s", child->tag_name,
1339                         as);
1340                     if (strcmp (child->tag_name, as) == 0) {
1341                       xmp_tag = child;
1342                       break;
1343                     }
1344                   }
1345
1346                 } else {
1347                   GST_LOG ("Looking for tag: %s", as);
1348                   _gst_xmp_tag_get_mapping_reverse (as, &xmp_tag);
1349                 }
1350                 if (xmp_tag) {
1351                   PendingXmpTag *ptag;
1352
1353                   GST_DEBUG ("Found xmp tag: %s -> %s", xmp_tag->tag_name,
1354                       xmp_tag->gst_tag);
1355
1356                   /* we shouldn't find a xmp structure here */
1357                   g_assert (xmp_tag->gst_tag != NULL);
1358
1359                   ptag = g_slice_new (PendingXmpTag);
1360                   ptag->xmp_tag = xmp_tag;
1361                   ptag->str = g_strdup (v);
1362
1363                   pending_tags = g_slist_prepend (pending_tags, ptag);
1364                 }
1365               }
1366               /* restore chars overwritten by '\0' */
1367               *d = '=';
1368               *ae = '"';
1369             } else if (*ae == '\0' || *ae == ' ') {
1370               /* end of attr/value pair */
1371               as = &ae[1];
1372             }
1373             /* to next char if not eos */
1374             if (*ae != '\0')
1375               ae++;
1376           }
1377         } else {
1378           /*
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>
1381            */
1382           /* FIXME: eventually rewrite ns */
1383
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 */
1387             if (context_tag) {
1388               GSList *iter;
1389
1390               /* check if this is the closing of the context */
1391               if (part[0] == '/'
1392                   && strcmp (part + 1, context_tag->tag_name) == 0) {
1393                 GST_DEBUG ("Closing context tag %s", part);
1394                 context_tag = NULL;
1395               } else {
1396
1397                 for (iter = context_tag->children; iter;
1398                     iter = g_slist_next (iter)) {
1399                   XmpTag *child = iter->data;
1400
1401                   GST_DEBUG ("Looking at child tag %s : %s", child->tag_name,
1402                       part);
1403                   if (strcmp (child->tag_name, part) == 0) {
1404                     last_xmp_tag = child;
1405                     break;
1406                   }
1407                 }
1408               }
1409
1410             } else {
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;
1416               }
1417             }
1418           }
1419         }
1420       }
1421       GST_LOG ("Next cycle");
1422       /* next cycle */
1423       ne++;
1424       if (ne < xp2) {
1425         if (*ne != '<')
1426           in_tag = FALSE;
1427         ns = ne;
1428         pp = part;
1429       }
1430     } else {
1431       while (ne < xp2 && *ne != '<') {
1432         *pp++ = *ne;
1433         ne++;
1434       }
1435       *pp = '\0';
1436       /* create node */
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);
1441         if (last_xmp_tag) {
1442           PendingXmpTag *ptag;
1443
1444           GST_DEBUG ("Found tag %s -> %s", last_xmp_tag->tag_name,
1445               last_xmp_tag->gst_tag);
1446
1447           if (last_xmp_tag->type == GstXmpTagTypeStruct) {
1448             g_assert (context_tag == NULL);     /* we can't handle struct nesting currently */
1449
1450             context_tag = last_xmp_tag;
1451           } else {
1452             ptag = g_slice_new (PendingXmpTag);
1453             ptag->xmp_tag = last_xmp_tag;
1454             ptag->str = g_strdup (part);
1455
1456             pending_tags = g_slist_prepend (pending_tags, ptag);
1457           }
1458         }
1459       }
1460       /* next cycle */
1461       in_tag = TRUE;
1462       ns = ne;
1463       pp = part;
1464     }
1465   }
1466
1467   pending_tags = g_slist_reverse (pending_tags);
1468
1469   GST_DEBUG ("Done accumulating tags, now handling them");
1470
1471   while (pending_tags) {
1472     PendingXmpTag *ptag = (PendingXmpTag *) pending_tags->data;
1473
1474     pending_tags = g_slist_delete_link (pending_tags, pending_tags);
1475
1476     read_one_tag (list, ptag->xmp_tag, ptag->str, &pending_tags);
1477
1478     g_free (ptag->str);
1479     g_slice_free (PendingXmpTag, ptag);
1480   }
1481
1482   GST_INFO ("xmp packet parsed, %d entries", gst_tag_list_n_tags (list));
1483
1484 out:
1485
1486   /* free resources */
1487   i = 0;
1488   while (ns_map[i].original_ns) {
1489     g_free (ns_map[i].gstreamer_ns);
1490     i++;
1491   }
1492
1493   g_free (part);
1494
1495   gst_buffer_unmap (buffer, &info);
1496
1497   return list;
1498
1499   /* Errors */
1500 missing_header:
1501   GST_WARNING ("malformed xmp packet header");
1502   goto out;
1503 missing_footer:
1504   GST_WARNING ("malformed xmp packet footer");
1505   goto out;
1506 broken_xml:
1507   GST_WARNING ("malformed xml tag: %s", part);
1508   gst_tag_list_unref (list);
1509   list = NULL;
1510   goto out;
1511 }
1512
1513
1514 /* formatting */
1515
1516 static void
1517 string_open_tag (GString * string, const char *tag)
1518 {
1519   g_string_append_c (string, '<');
1520   g_string_append (string, tag);
1521   g_string_append_c (string, '>');
1522 }
1523
1524 static void
1525 string_close_tag (GString * string, const char *tag)
1526 {
1527   g_string_append (string, "</");
1528   g_string_append (string, tag);
1529   g_string_append (string, ">\n");
1530 }
1531
1532 static char *
1533 gst_value_serialize_xmp (const GValue * value)
1534 {
1535   switch (G_VALUE_TYPE (value)) {
1536     case G_TYPE_STRING:
1537       return g_markup_escape_text (g_value_get_string (value), -1);
1538     case G_TYPE_INT:
1539       return g_strdup_printf ("%d", g_value_get_int (value));
1540     case G_TYPE_UINT:
1541       return g_strdup_printf ("%u", g_value_get_uint (value));
1542     case G_TYPE_DOUBLE:
1543       return double_to_fraction_string (g_value_get_double (value));
1544     default:
1545       break;
1546   }
1547   /* put non-switchable types here */
1548   if (G_VALUE_TYPE (value) == G_TYPE_DATE) {
1549     const GDate *date = g_value_get_boxed (value);
1550
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);
1559
1560     if (!gst_date_time_has_time (datetime))
1561       return gst_date_time_to_iso8601_string (datetime);
1562
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) {
1574       /* UTC */
1575       return g_strdup_printf ("%04d-%02d-%02dT%02d:%02d:%02d.%06dZ",
1576           year, month, day, hour, min, sec, microsec);
1577     } else {
1578       gmt_offset_hour = ABS (gmt_offset);
1579       gmt_offset_min = (ABS (gmt_offset) - gmt_offset_hour) * 60;
1580
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);
1584     }
1585   } else {
1586     return NULL;
1587   }
1588 }
1589
1590 static void
1591 write_one_tag (const GstTagList * list, XmpTag * xmp_tag, gpointer user_data)
1592 {
1593   guint i = 0, ct;
1594   XmpSerializationData *serialization_data = user_data;
1595   GString *data = serialization_data->data;
1596   char *s;
1597
1598   /* struct type handled differently */
1599   if (xmp_tag->type == GstXmpTagTypeStruct ||
1600       xmp_tag->type == GstXmpTagTypeCompound) {
1601     GSList *iter;
1602     gboolean use_it = FALSE;
1603
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;
1607
1608       if (gst_tag_list_get_value_index (list, child_tag->gst_tag, 0) != NULL) {
1609         use_it = TRUE;
1610         break;
1611       }
1612     }
1613
1614     if (use_it) {
1615       if (xmp_tag->tag_name)
1616         string_open_tag (data, xmp_tag->tag_name);
1617
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, '>');
1625         } else {
1626           string_open_tag (data, "rdf:li");
1627         }
1628       }
1629
1630       /* now write it */
1631       for (iter = xmp_tag->children; iter; iter = g_slist_next (iter)) {
1632         write_one_tag (list, iter->data, user_data);
1633       }
1634
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));
1638       }
1639
1640       if (xmp_tag->tag_name)
1641         string_close_tag (data, xmp_tag->tag_name);
1642     }
1643     return;
1644   }
1645
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)
1649     return;
1650
1651   ct = gst_tag_list_get_tag_size (list, xmp_tag->gst_tag);
1652   string_open_tag (data, xmp_tag->tag_name);
1653
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));
1659     } else {
1660       s = gst_value_serialize_xmp (gst_tag_list_get_value_index (list,
1661               xmp_tag->gst_tag, 0));
1662     }
1663     if (s) {
1664       g_string_append (data, s);
1665       g_free (s);
1666     } else {
1667       GST_WARNING ("unhandled type for %s to xmp", xmp_tag->gst_tag);
1668     }
1669   } else {
1670     const gchar *typename;
1671
1672     typename = xmp_tag_type_get_name (xmp_tag->type);
1673
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));
1680       } else {
1681         s = gst_value_serialize_xmp (gst_tag_list_get_value_index (list,
1682                 xmp_tag->gst_tag, i));
1683       }
1684       if (s) {
1685         string_open_tag (data, "rdf:li");
1686         g_string_append (data, s);
1687         string_close_tag (data, "rdf:li");
1688         g_free (s);
1689       } else {
1690         GST_WARNING ("unhandled type for %s to xmp", xmp_tag->gst_tag);
1691       }
1692     }
1693     string_close_tag (data, typename);
1694   }
1695
1696   string_close_tag (data, xmp_tag->tag_name);
1697 }
1698
1699 /**
1700  * gst_tag_list_to_xmp_buffer:
1701  * @list: tags
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
1705  *
1706  * Formats a taglist as a xmp packet using only the selected
1707  * schemas. An empty list (%NULL) means that all schemas should
1708  * be used
1709  *
1710  * Returns: (transfer full) (nullable): new buffer or %NULL, unref the buffer when done
1711  */
1712 GstBuffer *
1713 gst_tag_list_to_xmp_buffer (const GstTagList * list, gboolean read_only,
1714     const gchar ** schemas)
1715 {
1716   GstBuffer *buffer = NULL;
1717   XmpSerializationData serialization_data;
1718   GString *data;
1719   guint i;
1720   gsize bsize;
1721   gpointer bdata;
1722
1723   serialization_data.data = g_string_sized_new (4096);
1724   serialization_data.schemas = schemas;
1725   data = serialization_data.data;
1726
1727   xmp_tags_initialize ();
1728
1729   g_return_val_if_fail (GST_IS_TAG_LIST (list), NULL);
1730
1731   /* xmp header */
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#\"");
1738   i = 0;
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);
1746       }
1747     }
1748     i++;
1749   }
1750   g_string_append (data, ">\n");
1751   g_string_append (data, "<rdf:Description rdf:about=\"\">\n");
1752
1753   /* iterate the schemas */
1754   if (schemas == NULL) {
1755     /* use all schemas */
1756     schemas = gst_tag_xmp_list_schemas ();
1757   }
1758   for (i = 0; schemas[i] != NULL; i++) {
1759     GstXmpSchema *schema = _gst_xmp_get_schema (schemas[i]);
1760     GHashTableIter iter;
1761     gpointer key, value;
1762
1763     if (schema == NULL)
1764       continue;
1765
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);
1770     }
1771   }
1772
1773   /* xmp footer */
1774   g_string_append (data, "</rdf:Description>\n");
1775   g_string_append (data, "</rdf:RDF>\n");
1776   g_string_append (data, "</x:xmpmeta>\n");
1777
1778   if (!read_only) {
1779     /* the xmp spec recommends to add 2-4KB padding for in-place editable xmp */
1780     guint i;
1781
1782     for (i = 0; i < 32; i++) {
1783       g_string_append (data, "                " "                "
1784           "                " "                " "\n");
1785     }
1786   }
1787   g_string_append_printf (data, "<?xpacket end=\"%c\"?>",
1788       (read_only ? 'r' : 'w'));
1789
1790   bsize = data->len;
1791   bdata = g_string_free (data, FALSE);
1792
1793   buffer = gst_buffer_new_wrapped (bdata, bsize);
1794
1795   return buffer;
1796 }
1797
1798 #undef gst_xmp_schema_lookup
1799 #undef gst_xmp_schema_insert