19ebb5876c7ec03db7a27896afdaee68e4afb817
[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 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/gst.h>
27 #include "tag.h"
28
29 #include <string.h>
30
31 /**
32  * SECTION:gsttag
33  * @short_description: additional tag definitions for plugins and applications
34  * @see_also: #GstTagList
35  * 
36  * <refsect2>
37  * <para>
38  * Contains additional standardized GStreamer tag definitions for plugins
39  * and applications, and functions to register them with the GStreamer
40  * tag system.
41  * </para>
42  * </refsect2>
43  */
44
45
46 static gpointer
47 gst_tag_register_tags_internal (gpointer unused)
48 {
49 #ifdef ENABLE_NLS
50   GST_DEBUG ("binding text domain %s to locale dir %s", GETTEXT_PACKAGE,
51       LOCALEDIR);
52   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
53 #endif
54
55   /* musicbrainz tags */
56   gst_tag_register (GST_TAG_MUSICBRAINZ_TRACKID, GST_TAG_FLAG_META,
57       G_TYPE_STRING, _("track ID"), _("MusicBrainz track ID"), NULL);
58   gst_tag_register (GST_TAG_MUSICBRAINZ_ARTISTID, GST_TAG_FLAG_META,
59       G_TYPE_STRING, _("artist ID"), _("MusicBrainz artist ID"), NULL);
60   gst_tag_register (GST_TAG_MUSICBRAINZ_ALBUMID, GST_TAG_FLAG_META,
61       G_TYPE_STRING, _("album ID"), _("MusicBrainz album ID"), NULL);
62   gst_tag_register (GST_TAG_MUSICBRAINZ_ALBUMARTISTID, GST_TAG_FLAG_META,
63       G_TYPE_STRING,
64       _("album artist ID"), _("MusicBrainz album artist ID"), NULL);
65   gst_tag_register (GST_TAG_MUSICBRAINZ_TRMID, GST_TAG_FLAG_META,
66       G_TYPE_STRING, _("track TRM ID"), _("MusicBrainz TRM ID"), NULL);
67   gst_tag_register (GST_TAG_MUSICBRAINZ_SORTNAME, GST_TAG_FLAG_META,
68       G_TYPE_STRING,
69       _("artist sortname"), _("MusicBrainz artist sortname"), NULL);
70
71   /* CDDA tags */
72   gst_tag_register (GST_TAG_CDDA_CDDB_DISCID, GST_TAG_FLAG_META,
73       G_TYPE_STRING, "discid", "CDDB discid for metadata retrieval",
74       gst_tag_merge_use_first);
75
76   gst_tag_register (GST_TAG_CDDA_CDDB_DISCID_FULL, GST_TAG_FLAG_META,
77       G_TYPE_STRING, "discid full",
78       "CDDB discid for metadata retrieval (full)", gst_tag_merge_use_first);
79
80   gst_tag_register (GST_TAG_CDDA_MUSICBRAINZ_DISCID, GST_TAG_FLAG_META,
81       G_TYPE_STRING, "musicbrainz-discid",
82       "Musicbrainz discid for metadata retrieval", gst_tag_merge_use_first);
83
84   gst_tag_register (GST_TAG_CDDA_MUSICBRAINZ_DISCID_FULL, GST_TAG_FLAG_META,
85       G_TYPE_STRING, "musicbrainz-discid-full",
86       "Musicbrainz discid for metadata retrieval (full)",
87       gst_tag_merge_use_first);
88
89   return NULL;
90 }
91
92 /* FIXME 0.11: rename this to gst_tag_init() or gst_tag_register_tags() */
93 /**
94  * gst_tag_register_musicbrainz_tags
95  *
96  * Registers additional musicbrainz-specific tags with the GStreamer tag
97  * system. Plugins and applications that use these tags should call this
98  * function before using them. Can be called multiple times.
99  */
100 void
101 gst_tag_register_musicbrainz_tags (void)
102 {
103   static GOnce mb_once = G_ONCE_INIT;
104
105   g_once (&mb_once, gst_tag_register_tags_internal, NULL);
106 }
107
108 static void
109 register_tag_image_type_enum (GType * id)
110 {
111   static const GEnumValue image_types[] = {
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
152 GType
153 gst_tag_image_type_get_type (void)
154 {
155   static GType id;
156   static GOnce once = G_ONCE_INIT;
157
158   g_once (&once, (GThreadFunc) register_tag_image_type_enum, &id);
159   return id;
160 }
161
162 /**
163  * gst_tag_parse_extended_comment:
164  * @ext_comment: an extended comment string, see #GST_TAG_EXTENDED_COMMENT
165  * @key: return location for the comment description key, or NULL
166  * @lang: return location for the comment ISO-639 language code, or NULL
167  * @value: return location for the actual comment string, or NULL
168  * @fail_if_no_key: whether to fail if strings are not in key=value form
169  *
170  * Convenience function to parse a GST_TAG_EXTENDED_COMMENT string and
171  * separate it into its components.
172  *
173  * If successful, @key, @lang and/or @value will be set to newly allocated
174  * strings that you need to free with g_free() when done. @key and @lang
175  * may also be set to NULL by this function if there is no key or no language
176  * code in the extended comment string.
177  *
178  * Returns: TRUE if the string could be parsed, otherwise FALSE
179  *
180  * Since: 0.10.10
181  */
182 gboolean
183 gst_tag_parse_extended_comment (const gchar * ext_comment, gchar ** key,
184     gchar ** lang, gchar ** value, gboolean fail_if_no_key)
185 {
186   const gchar *div, *bop, *bcl;
187
188   g_return_val_if_fail (ext_comment != NULL, FALSE);
189   g_return_val_if_fail (g_utf8_validate (ext_comment, -1, NULL), FALSE);
190
191   if (key)
192     *key = NULL;
193   if (lang)
194     *lang = NULL;
195
196   div = strchr (ext_comment, '=');
197   bop = strchr (ext_comment, '[');
198   bcl = strchr (ext_comment, ']');
199
200   if (div == NULL) {
201     if (fail_if_no_key)
202       return FALSE;
203     if (value)
204       *value = g_strdup (ext_comment);
205     return TRUE;
206   }
207
208   if (bop != NULL && bop < div) {
209     if (bcl < bop || bcl > div)
210       return FALSE;
211     if (key)
212       *key = g_strndup (ext_comment, bop - ext_comment);
213     if (lang)
214       *lang = g_strndup (bop + 1, bcl - bop - 1);
215   } else {
216     if (key)
217       *key = g_strndup (ext_comment, div - ext_comment);
218   }
219
220   if (value)
221     *value = g_strdup (div + 1);
222
223   return TRUE;
224 }
225
226 /**
227  * gst_tag_freeform_string_to_utf8:
228  * @data: string data
229  * @size: length of string data, or -1 if the string is NUL-terminated
230  * @env_vars: a NULL-terminated string array of environment variable names,
231  *            or NULL
232  *
233  * Convenience function to read a string with unknown character encoding. If
234  * the string is already in UTF-8 encoding, it will be returned right away.
235  * Otherwise, the environment will be searched for a number of environment
236  * variables (whose names are specified in the NULL-terminated string array
237  * @env_vars) containing a list of character encodings to try/use. If none
238  * are specified, the current locale will be tried. If that also doesn't work,
239  * ISO-8859-1 is assumed (which will almost always succeed).
240  *
241  * Returns: a newly-allocated string in UTF-8 encoding, or NULL
242  *
243  * Since: 0.10.13
244  */
245 gchar *
246 gst_tag_freeform_string_to_utf8 (const gchar * data, gint size,
247     const gchar ** env_vars)
248 {
249   const gchar *cur_loc = NULL;
250   gsize bytes_read;
251   gchar *utf8 = NULL;
252
253   g_return_val_if_fail (data != NULL, NULL);
254
255   if (size < 0)
256     size = strlen (data);
257
258   /* chop off trailing string terminators to make sure utf8_validate doesn't
259    * get to see them (since that would make the utf8 check fail) */
260   while (size > 0 && data[size - 1] == '\0')
261     --size;
262
263   /* Should we try the charsets specified
264    * via environment variables FIRST ? */
265   if (g_utf8_validate (data, size, NULL)) {
266     utf8 = g_strndup (data, size);
267     GST_LOG ("String '%s' is valid UTF-8 already", utf8);
268     goto beach;
269   }
270
271   while (env_vars && *env_vars != NULL) {
272     const gchar *env = NULL;
273
274     /* Try charsets specified via the environment */
275     env = g_getenv (*env_vars);
276     if (env != NULL && *env != '\0') {
277       gchar **c, **csets;
278
279       csets = g_strsplit (env, G_SEARCHPATH_SEPARATOR_S, -1);
280
281       for (c = csets; c && *c; ++c) {
282         GST_LOG ("Trying to convert freeform string to UTF-8 from '%s'", *c);
283         if ((utf8 =
284                 g_convert (data, size, "UTF-8", *c, &bytes_read, NULL, NULL))) {
285           if (bytes_read == size) {
286             g_strfreev (csets);
287             goto beach;
288           }
289           g_free (utf8);
290           utf8 = NULL;
291         }
292       }
293
294       g_strfreev (csets);
295     }
296     ++env_vars;
297   }
298
299   /* Try current locale (if not UTF-8) */
300   if (!g_get_charset (&cur_loc)) {
301     GST_LOG ("Trying to convert freeform string using locale ('%s')", cur_loc);
302     if ((utf8 = g_locale_to_utf8 (data, size, &bytes_read, NULL, NULL))) {
303       if (bytes_read == size) {
304         goto beach;
305       }
306       g_free (utf8);
307       utf8 = NULL;
308     }
309   }
310
311   /* Try ISO-8859-1 */
312   GST_LOG ("Trying to convert freeform string using ISO-8859-1 fallback");
313   utf8 = g_convert (data, size, "UTF-8", "ISO-8859-1", &bytes_read, NULL, NULL);
314   if (utf8 != NULL && bytes_read == size) {
315     goto beach;
316   }
317
318   g_free (utf8);
319   return NULL;
320
321 beach:
322
323   g_strchomp (utf8);
324   if (utf8 && utf8[0] != '\0') {
325     GST_LOG ("Returning '%s'", utf8);
326     return utf8;
327   }
328
329   g_free (utf8);
330   return NULL;
331 }