Add -Wwrite-strings to configure
[platform/upstream/gstreamer.git] / gst-libs / gst / tag / tags.c
1 /* GStreamer non-core tag registration and tag utility functions
2  * Copyright (C) 2005 Ross Burton <ross@burtonini.com>
3  * Copyright (C) 2006-2008 Tim-Philipp Müller <tim centricular net>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <gst/gst-i18n-plugin.h>
26 #include <gst/base/gsttypefindhelper.h>
27 #include <gst/gst.h>
28 #include "tag.h"
29
30 #include <string.h>
31
32 /**
33  * SECTION:gsttag
34  * @short_description: additional tag definitions for plugins and applications
35  * @see_also: #GstTagList
36  * 
37  * <refsect2>
38  * <para>
39  * Contains additional standardized GStreamer tag definitions for plugins
40  * and applications, and functions to register them with the GStreamer
41  * tag system.
42  * </para>
43  * </refsect2>
44  */
45
46
47 static gpointer
48 gst_tag_register_tags_internal (gpointer unused)
49 {
50 #ifdef ENABLE_NLS
51   GST_DEBUG ("binding text domain %s to locale dir %s", GETTEXT_PACKAGE,
52       LOCALEDIR);
53   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
54   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
55 #endif
56
57   /* musicbrainz tags */
58   gst_tag_register (GST_TAG_MUSICBRAINZ_TRACKID, GST_TAG_FLAG_META,
59       G_TYPE_STRING, _("track ID"), _("MusicBrainz track ID"), NULL);
60   gst_tag_register (GST_TAG_MUSICBRAINZ_ARTISTID, GST_TAG_FLAG_META,
61       G_TYPE_STRING, _("artist ID"), _("MusicBrainz artist ID"), NULL);
62   gst_tag_register (GST_TAG_MUSICBRAINZ_ALBUMID, GST_TAG_FLAG_META,
63       G_TYPE_STRING, _("album ID"), _("MusicBrainz album ID"), NULL);
64   gst_tag_register (GST_TAG_MUSICBRAINZ_ALBUMARTISTID, GST_TAG_FLAG_META,
65       G_TYPE_STRING,
66       _("album artist ID"), _("MusicBrainz album artist ID"), NULL);
67   gst_tag_register (GST_TAG_MUSICBRAINZ_TRMID, GST_TAG_FLAG_META,
68       G_TYPE_STRING, _("track TRM ID"), _("MusicBrainz TRM ID"), NULL);
69
70   /* CDDA tags */
71   gst_tag_register (GST_TAG_CDDA_CDDB_DISCID, GST_TAG_FLAG_META,
72       G_TYPE_STRING, "discid", "CDDB discid for metadata retrieval",
73       gst_tag_merge_use_first);
74
75   gst_tag_register (GST_TAG_CDDA_CDDB_DISCID_FULL, GST_TAG_FLAG_META,
76       G_TYPE_STRING, "discid full",
77       "CDDB discid for metadata retrieval (full)", gst_tag_merge_use_first);
78
79   gst_tag_register (GST_TAG_CDDA_MUSICBRAINZ_DISCID, GST_TAG_FLAG_META,
80       G_TYPE_STRING, "musicbrainz-discid",
81       "Musicbrainz discid for metadata retrieval", gst_tag_merge_use_first);
82
83   gst_tag_register (GST_TAG_CDDA_MUSICBRAINZ_DISCID_FULL, GST_TAG_FLAG_META,
84       G_TYPE_STRING, "musicbrainz-discid-full",
85       "Musicbrainz discid for metadata retrieval (full)",
86       gst_tag_merge_use_first);
87
88   return NULL;
89 }
90
91 /* FIXME 0.11: rename this to gst_tag_init() or gst_tag_register_tags() */
92 /**
93  * gst_tag_register_musicbrainz_tags
94  *
95  * Registers additional musicbrainz-specific tags with the GStreamer tag
96  * system. Plugins and applications that use these tags should call this
97  * function before using them. Can be called multiple times.
98  */
99 void
100 gst_tag_register_musicbrainz_tags (void)
101 {
102   static GOnce mb_once = G_ONCE_INIT;
103
104   g_once (&mb_once, gst_tag_register_tags_internal, NULL);
105 }
106
107 static void
108 register_tag_image_type_enum (GType * id)
109 {
110   static const GEnumValue image_types[] = {
111     {GST_TAG_IMAGE_TYPE_NONE, "GST_TAG_IMAGE_TYPE_NONE", "none"},
112     {GST_TAG_IMAGE_TYPE_UNDEFINED, "GST_TAG_IMAGE_TYPE_UNDEFINED", "undefined"},
113     {GST_TAG_IMAGE_TYPE_FRONT_COVER, "GST_TAG_IMAGE_TYPE_FRONT_COVER",
114         "front-cover"},
115     {GST_TAG_IMAGE_TYPE_BACK_COVER, "GST_TAG_IMAGE_TYPE_BACK_COVER",
116         "back-cover"},
117     {GST_TAG_IMAGE_TYPE_LEAFLET_PAGE, "GST_TAG_IMAGE_TYPE_LEAFLET_PAGE",
118         "leaflet-page"},
119     {GST_TAG_IMAGE_TYPE_MEDIUM, "GST_TAG_IMAGE_TYPE_MEDIUM", "medium"},
120     {GST_TAG_IMAGE_TYPE_LEAD_ARTIST, "GST_TAG_IMAGE_TYPE_LEAD_ARTIST",
121         "lead-artist"},
122     {GST_TAG_IMAGE_TYPE_ARTIST, "GST_TAG_IMAGE_TYPE_ARTIST", "artist"},
123     {GST_TAG_IMAGE_TYPE_CONDUCTOR, "GST_TAG_IMAGE_TYPE_CONDUCTOR", "conductor"},
124     {GST_TAG_IMAGE_TYPE_BAND_ORCHESTRA, "GST_TAG_IMAGE_TYPE_BAND_ORCHESTRA",
125         "band-orchestra"},
126     {GST_TAG_IMAGE_TYPE_COMPOSER, "GST_TAG_IMAGE_TYPE_COMPOSER", "composer"},
127     {GST_TAG_IMAGE_TYPE_LYRICIST, "GST_TAG_IMAGE_TYPE_LYRICIST", "lyricist"},
128     {GST_TAG_IMAGE_TYPE_RECORDING_LOCATION,
129           "GST_TAG_IMAGE_TYPE_RECORDING_LOCATION",
130         "recording-location"},
131     {GST_TAG_IMAGE_TYPE_DURING_RECORDING, "GST_TAG_IMAGE_TYPE_DURING_RECORDING",
132         "during-recording"},
133     {GST_TAG_IMAGE_TYPE_DURING_PERFORMANCE,
134           "GST_TAG_IMAGE_TYPE_DURING_PERFORMANCE",
135         "during-performance"},
136     {GST_TAG_IMAGE_TYPE_VIDEO_CAPTURE, "GST_TAG_IMAGE_TYPE_VIDEO_CAPTURE",
137         "video-capture"},
138     {GST_TAG_IMAGE_TYPE_FISH, "GST_TAG_IMAGE_TYPE_FISH", "fish"},
139     {GST_TAG_IMAGE_TYPE_ILLUSTRATION, "GST_TAG_IMAGE_TYPE_ILLUSTRATION",
140         "illustration"},
141     {GST_TAG_IMAGE_TYPE_BAND_ARTIST_LOGO, "GST_TAG_IMAGE_TYPE_BAND_ARTIST_LOGO",
142         "artist-logo"},
143     {GST_TAG_IMAGE_TYPE_PUBLISHER_STUDIO_LOGO,
144           "GST_TAG_IMAGE_TYPE_PUBLISHER_STUDIO_LOGO",
145         "publisher-studio-logo"},
146     {0, NULL, NULL}
147   };
148
149   *id = g_enum_register_static ("GstTagImageType", image_types);
150
151   /* work around thread-safety issue with class creation in GLib */
152   g_type_class_ref (*id);
153 }
154
155 GType
156 gst_tag_image_type_get_type (void)
157 {
158   static GType id;
159
160   static GOnce once = G_ONCE_INIT;
161
162   g_once (&once, (GThreadFunc) register_tag_image_type_enum, &id);
163   return id;
164 }
165
166 static inline gboolean
167 gst_tag_image_type_is_valid (GstTagImageType type)
168 {
169   GEnumClass *klass;
170
171   gboolean res;
172
173   klass = g_type_class_ref (gst_tag_image_type_get_type ());
174   res = (g_enum_get_value (klass, type) != NULL);
175   g_type_class_unref (klass);
176
177   return res;
178 }
179
180 /**
181  * gst_tag_parse_extended_comment:
182  * @ext_comment: an extended comment string, see #GST_TAG_EXTENDED_COMMENT
183  * @key: return location for the comment description key, or NULL
184  * @lang: return location for the comment ISO-639 language code, or NULL
185  * @value: return location for the actual comment string, or NULL
186  * @fail_if_no_key: whether to fail if strings are not in key=value form
187  *
188  * Convenience function to parse a GST_TAG_EXTENDED_COMMENT string and
189  * separate it into its components.
190  *
191  * If successful, @key, @lang and/or @value will be set to newly allocated
192  * strings that you need to free with g_free() when done. @key and @lang
193  * may also be set to NULL by this function if there is no key or no language
194  * code in the extended comment string.
195  *
196  * Returns: TRUE if the string could be parsed, otherwise FALSE
197  *
198  * Since: 0.10.10
199  */
200 gboolean
201 gst_tag_parse_extended_comment (const gchar * ext_comment, gchar ** key,
202     gchar ** lang, gchar ** value, gboolean fail_if_no_key)
203 {
204   const gchar *div, *bop, *bcl;
205
206   g_return_val_if_fail (ext_comment != NULL, FALSE);
207   g_return_val_if_fail (g_utf8_validate (ext_comment, -1, NULL), FALSE);
208
209   if (key)
210     *key = NULL;
211   if (lang)
212     *lang = NULL;
213
214   div = strchr (ext_comment, '=');
215   bop = strchr (ext_comment, '[');
216   bcl = strchr (ext_comment, ']');
217
218   if (div == NULL) {
219     if (fail_if_no_key)
220       return FALSE;
221     if (value)
222       *value = g_strdup (ext_comment);
223     return TRUE;
224   }
225
226   if (bop != NULL && bop < div) {
227     if (bcl < bop || bcl > div)
228       return FALSE;
229     if (key)
230       *key = g_strndup (ext_comment, bop - ext_comment);
231     if (lang)
232       *lang = g_strndup (bop + 1, bcl - bop - 1);
233   } else {
234     if (key)
235       *key = g_strndup (ext_comment, div - ext_comment);
236   }
237
238   if (value)
239     *value = g_strdup (div + 1);
240
241   return TRUE;
242 }
243
244 /**
245  * gst_tag_freeform_string_to_utf8:
246  * @data: string data
247  * @size: length of string data, or -1 if the string is NUL-terminated
248  * @env_vars: a NULL-terminated string array of environment variable names,
249  *            or NULL
250  *
251  * Convenience function to read a string with unknown character encoding. If
252  * the string is already in UTF-8 encoding, it will be returned right away.
253  * If not it tries to detect byte-order-mark for UTF-16/32 cases and use that.
254  * Otherwise, the environment will be searched for a number of environment
255  * variables (whose names are specified in the NULL-terminated string array
256  * @env_vars) containing a list of character encodings to try/use. If none
257  * are specified, the current locale will be tried. If that also doesn't work,
258  * ISO-8859-1 is assumed (which will almost always succeed).
259  *
260  * Returns: a newly-allocated string in UTF-8 encoding, or NULL
261  *
262  * Since: 0.10.13
263  */
264 gchar *
265 gst_tag_freeform_string_to_utf8 (const gchar * data, gint size,
266     const gchar ** env_vars)
267 {
268   const gchar *cur_loc = NULL;
269
270   gsize bytes_read;
271
272   gchar *utf8 = NULL;
273
274   g_return_val_if_fail (data != NULL, NULL);
275
276   if (size < 0)
277     size = strlen (data);
278
279   /* chop off trailing string terminators to make sure utf8_validate doesn't
280    * get to see them (since that would make the utf8 check fail) */
281   while (size > 0 && data[size - 1] == '\0')
282     --size;
283
284   /* Should we try the charsets specified
285    * via environment variables FIRST ? */
286   if (g_utf8_validate (data, size, NULL)) {
287     utf8 = g_strndup (data, size);
288     GST_LOG ("String '%s' is valid UTF-8 already", utf8);
289     goto beach;
290   }
291
292   /* check for and use byte-order-mark for UTF-16/32 cases */
293   if (size >= 2) {
294     const gchar *c = NULL;
295     gint prefix, ssize;
296
297     if (size >= 4) {
298       prefix = 4;
299       ssize = GST_ROUND_DOWN_4 (size - 4);
300       switch (GST_READ_UINT32_BE (data)) {
301         case 0x0000FEFF:
302           c = "UTF-32BE";
303           break;
304         case 0xFFFE0000:
305           c = "UTF-32LE";
306           break;
307         default:
308           break;
309       }
310     }
311     if (!c) {
312       prefix = 2;
313       ssize = GST_ROUND_DOWN_2 (size - 2);
314       switch (GST_READ_UINT16_BE (data)) {
315         case 0xFEFF:
316           c = "UTF-16BE";
317           break;
318         case 0xFFFE:
319           c = "UTF-16LE";
320           break;
321         default:
322           break;
323       }
324     }
325     if (c) {
326       GST_LOG ("Trying to convert freeform string to UTF-8 from '%s'", c);
327       if ((utf8 =
328               g_convert (data + prefix, ssize, "UTF-8", c, &bytes_read, NULL,
329                   NULL))) {
330         if (bytes_read == ssize)
331           goto beach;
332         g_free (utf8);
333         utf8 = NULL;
334       }
335     }
336   }
337
338   while (env_vars && *env_vars != NULL) {
339     const gchar *env = NULL;
340
341     /* Try charsets specified via the environment */
342     env = g_getenv (*env_vars);
343     if (env != NULL && *env != '\0') {
344       gchar **c, **csets;
345
346       csets = g_strsplit (env, G_SEARCHPATH_SEPARATOR_S, -1);
347
348       for (c = csets; c && *c; ++c) {
349         GST_LOG ("Trying to convert freeform string to UTF-8 from '%s'", *c);
350         if ((utf8 =
351                 g_convert (data, size, "UTF-8", *c, &bytes_read, NULL, NULL))) {
352           if (bytes_read == size) {
353             g_strfreev (csets);
354             goto beach;
355           }
356           g_free (utf8);
357           utf8 = NULL;
358         }
359       }
360
361       g_strfreev (csets);
362     }
363     ++env_vars;
364   }
365
366   /* Try current locale (if not UTF-8) */
367   if (!g_get_charset (&cur_loc)) {
368     GST_LOG ("Trying to convert freeform string using locale ('%s')", cur_loc);
369     if ((utf8 = g_locale_to_utf8 (data, size, &bytes_read, NULL, NULL))) {
370       if (bytes_read == size) {
371         goto beach;
372       }
373       g_free (utf8);
374       utf8 = NULL;
375     }
376   }
377
378   /* Try ISO-8859-1 */
379   GST_LOG ("Trying to convert freeform string using ISO-8859-1 fallback");
380   utf8 = g_convert (data, size, "UTF-8", "ISO-8859-1", &bytes_read, NULL, NULL);
381   if (utf8 != NULL && bytes_read == size) {
382     goto beach;
383   }
384
385   g_free (utf8);
386   return NULL;
387
388 beach:
389
390   g_strchomp (utf8);
391   if (utf8 && utf8[0] != '\0') {
392     GST_LOG ("Returning '%s'", utf8);
393     return utf8;
394   }
395
396   g_free (utf8);
397   return NULL;
398 }
399
400 /**
401  * gst_tag_image_data_to_image_buffer:
402  * @image_data: the (encoded) image
403  * @image_data_len: the length of the encoded image data at @image_data
404  * @image_type: type of the image, or #GST_TAG_IMAGE_TYPE_UNDEFINED. Pass
405  *     #GST_TAG_IMAGE_TYPE_NONE if no image type should be set at all (e.g.
406  *     for preview images)
407  *
408  * Helper function for tag-reading plugins to create a #GstBuffer suitable to
409  * add to a #GstTagList as an image tag (such as #GST_TAG_IMAGE or
410  * #GST_TAG_PREVIEW_IMAGE) from the encoded image data and an (optional) image
411  * type.
412  *
413  * Background: cover art and other images in tags are usually stored as a
414  * blob of binary image data, often accompanied by a MIME type or some other
415  * content type string (e.g. 'png', 'jpeg', 'jpg'). Sometimes there is also an
416  * 'image type' to indicate what kind of image this is (e.g. front cover,
417  * back cover, artist, etc.). The image data may also be an URI to the image
418  * rather than the image itself.
419  *
420  * In GStreamer, image tags are #GstBuffer<!-- -->s containing the raw image
421  * data, with the buffer caps describing the content type of the image
422  * (e.g. image/jpeg, image/png, text/uri-list). The buffer caps may contain
423  * an additional 'image-type' field of #GST_TYPE_TAG_IMAGE_TYPE to describe
424  * the type of image (front cover, back cover etc.). #GST_TAG_PREVIEW_IMAGE
425  * tags should not carry an image type, their type is already indicated via
426  * the special tag name.
427  *
428  * This function will do various checks and typefind the encoded image
429  * data (we can't trust the declared mime type).
430  *
431  * Returns: a newly-allocated image buffer for use in tag lists, or NULL
432  *
433  * Since: 0.10.20
434  */
435 GstBuffer *
436 gst_tag_image_data_to_image_buffer (const guint8 * image_data,
437     guint image_data_len, GstTagImageType image_type)
438 {
439   const gchar *name;
440
441   GstBuffer *image;
442
443   GstCaps *caps;
444
445   g_return_val_if_fail (image_data != NULL, NULL);
446   g_return_val_if_fail (image_data_len > 0, NULL);
447   g_return_val_if_fail (gst_tag_image_type_is_valid (image_type), NULL);
448
449   GST_DEBUG ("image data len: %u bytes", image_data_len);
450
451   /* allocate space for a NUL terminator for an uri too */
452   image = gst_buffer_try_new_and_alloc (image_data_len + 1);
453   if (image == NULL) {
454     GST_WARNING ("failed to allocate buffer of %d for image", image_data_len);
455     return NULL;
456   }
457
458   memcpy (GST_BUFFER_DATA (image), image_data, image_data_len);
459   GST_BUFFER_DATA (image)[image_data_len] = '\0';
460
461   /* Find GStreamer media type, can't trust declared type */
462   caps = gst_type_find_helper_for_buffer (NULL, image, NULL);
463
464   if (caps == NULL)
465     goto no_type;
466
467   GST_DEBUG ("Found GStreamer media type: %" GST_PTR_FORMAT, caps);
468
469   /* sanity check: make sure typefound/declared caps are either URI or image */
470   name = gst_structure_get_name (gst_caps_get_structure (caps, 0));
471
472   if (!g_str_has_prefix (name, "image/") &&
473       !g_str_has_prefix (name, "video/") &&
474       !g_str_equal (name, "text/uri-list")) {
475     GST_DEBUG ("Unexpected image type '%s', ignoring image frame", name);
476     goto error;
477   }
478
479   /* Decrease size by 1 if we don't have an URI list
480    * to keep the original size of the image
481    */
482   if (!g_str_equal (name, "text/uri-list"))
483     GST_BUFFER_SIZE (image) = image_data_len;
484
485   if (image_type != GST_TAG_IMAGE_TYPE_NONE) {
486     GST_LOG ("Setting image type: %d", image_type);
487     caps = gst_caps_make_writable (caps);
488     gst_caps_set_simple (caps, "image-type", GST_TYPE_TAG_IMAGE_TYPE,
489         image_type, NULL);
490   }
491
492   gst_buffer_set_caps (image, caps);
493   gst_caps_unref (caps);
494   return image;
495
496 /* ERRORS */
497 no_type:
498   {
499     GST_DEBUG ("Could not determine GStreamer media type, ignoring image");
500     /* fall through */
501   }
502 error:
503   {
504     if (image)
505       gst_buffer_unref (image);
506     if (caps)
507       gst_caps_unref (caps);
508     return NULL;
509   }
510 }