gst-libs/gst/tag/tags.c: Don't increase the size of non-string image buffers by one...
[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 #endif
55
56   /* musicbrainz tags */
57   gst_tag_register (GST_TAG_MUSICBRAINZ_TRACKID, GST_TAG_FLAG_META,
58       G_TYPE_STRING, _("track ID"), _("MusicBrainz track ID"), NULL);
59   gst_tag_register (GST_TAG_MUSICBRAINZ_ARTISTID, GST_TAG_FLAG_META,
60       G_TYPE_STRING, _("artist ID"), _("MusicBrainz artist ID"), NULL);
61   gst_tag_register (GST_TAG_MUSICBRAINZ_ALBUMID, GST_TAG_FLAG_META,
62       G_TYPE_STRING, _("album ID"), _("MusicBrainz album ID"), NULL);
63   gst_tag_register (GST_TAG_MUSICBRAINZ_ALBUMARTISTID, GST_TAG_FLAG_META,
64       G_TYPE_STRING,
65       _("album artist ID"), _("MusicBrainz album artist ID"), NULL);
66   gst_tag_register (GST_TAG_MUSICBRAINZ_TRMID, GST_TAG_FLAG_META,
67       G_TYPE_STRING, _("track TRM ID"), _("MusicBrainz TRM ID"), NULL);
68
69   /* CDDA tags */
70   gst_tag_register (GST_TAG_CDDA_CDDB_DISCID, GST_TAG_FLAG_META,
71       G_TYPE_STRING, "discid", "CDDB discid for metadata retrieval",
72       gst_tag_merge_use_first);
73
74   gst_tag_register (GST_TAG_CDDA_CDDB_DISCID_FULL, GST_TAG_FLAG_META,
75       G_TYPE_STRING, "discid full",
76       "CDDB discid for metadata retrieval (full)", gst_tag_merge_use_first);
77
78   gst_tag_register (GST_TAG_CDDA_MUSICBRAINZ_DISCID, GST_TAG_FLAG_META,
79       G_TYPE_STRING, "musicbrainz-discid",
80       "Musicbrainz discid for metadata retrieval", gst_tag_merge_use_first);
81
82   gst_tag_register (GST_TAG_CDDA_MUSICBRAINZ_DISCID_FULL, GST_TAG_FLAG_META,
83       G_TYPE_STRING, "musicbrainz-discid-full",
84       "Musicbrainz discid for metadata retrieval (full)",
85       gst_tag_merge_use_first);
86
87   return NULL;
88 }
89
90 /* FIXME 0.11: rename this to gst_tag_init() or gst_tag_register_tags() */
91 /**
92  * gst_tag_register_musicbrainz_tags
93  *
94  * Registers additional musicbrainz-specific tags with the GStreamer tag
95  * system. Plugins and applications that use these tags should call this
96  * function before using them. Can be called multiple times.
97  */
98 void
99 gst_tag_register_musicbrainz_tags (void)
100 {
101   static GOnce mb_once = G_ONCE_INIT;
102
103   g_once (&mb_once, gst_tag_register_tags_internal, NULL);
104 }
105
106 static void
107 register_tag_image_type_enum (GType * id)
108 {
109   static const GEnumValue image_types[] = {
110     {GST_TAG_IMAGE_TYPE_NONE, "GST_TAG_IMAGE_TYPE_NONE", "none"},
111     {GST_TAG_IMAGE_TYPE_UNDEFINED, "GST_TAG_IMAGE_TYPE_UNDEFINED", "undefined"},
112     {GST_TAG_IMAGE_TYPE_FRONT_COVER, "GST_TAG_IMAGE_TYPE_FRONT_COVER",
113         "front-cover"},
114     {GST_TAG_IMAGE_TYPE_BACK_COVER, "GST_TAG_IMAGE_TYPE_BACK_COVER",
115         "back-cover"},
116     {GST_TAG_IMAGE_TYPE_LEAFLET_PAGE, "GST_TAG_IMAGE_TYPE_LEAFLET_PAGE",
117         "leaflet-page"},
118     {GST_TAG_IMAGE_TYPE_MEDIUM, "GST_TAG_IMAGE_TYPE_MEDIUM", "medium"},
119     {GST_TAG_IMAGE_TYPE_LEAD_ARTIST, "GST_TAG_IMAGE_TYPE_LEAD_ARTIST",
120         "lead-artist"},
121     {GST_TAG_IMAGE_TYPE_ARTIST, "GST_TAG_IMAGE_TYPE_ARTIST", "artist"},
122     {GST_TAG_IMAGE_TYPE_CONDUCTOR, "GST_TAG_IMAGE_TYPE_CONDUCTOR", "conductor"},
123     {GST_TAG_IMAGE_TYPE_BAND_ORCHESTRA, "GST_TAG_IMAGE_TYPE_BAND_ORCHESTRA",
124         "band-orchestra"},
125     {GST_TAG_IMAGE_TYPE_COMPOSER, "GST_TAG_IMAGE_TYPE_COMPOSER", "composer"},
126     {GST_TAG_IMAGE_TYPE_LYRICIST, "GST_TAG_IMAGE_TYPE_LYRICIST", "lyricist"},
127     {GST_TAG_IMAGE_TYPE_RECORDING_LOCATION,
128           "GST_TAG_IMAGE_TYPE_RECORDING_LOCATION",
129         "recording-location"},
130     {GST_TAG_IMAGE_TYPE_DURING_RECORDING, "GST_TAG_IMAGE_TYPE_DURING_RECORDING",
131         "during-recording"},
132     {GST_TAG_IMAGE_TYPE_DURING_PERFORMANCE,
133           "GST_TAG_IMAGE_TYPE_DURING_PERFORMANCE",
134         "during-performance"},
135     {GST_TAG_IMAGE_TYPE_VIDEO_CAPTURE, "GST_TAG_IMAGE_TYPE_VIDEO_CAPTURE",
136         "video-capture"},
137     {GST_TAG_IMAGE_TYPE_FISH, "GST_TAG_IMAGE_TYPE_FISH", "fish"},
138     {GST_TAG_IMAGE_TYPE_ILLUSTRATION, "GST_TAG_IMAGE_TYPE_ILLUSTRATION",
139         "illustration"},
140     {GST_TAG_IMAGE_TYPE_BAND_ARTIST_LOGO, "GST_TAG_IMAGE_TYPE_BAND_ARTIST_LOGO",
141         "artist-logo"},
142     {GST_TAG_IMAGE_TYPE_PUBLISHER_STUDIO_LOGO,
143           "GST_TAG_IMAGE_TYPE_PUBLISHER_STUDIO_LOGO",
144         "publisher-studio-logo"},
145     {0, NULL, NULL}
146   };
147
148   *id = g_enum_register_static ("GstTagImageType", image_types);
149
150   /* work around thread-safety issue with class creation in GLib */
151   g_type_class_ref (*id);
152 }
153
154 GType
155 gst_tag_image_type_get_type (void)
156 {
157   static GType id;
158
159   static GOnce once = G_ONCE_INIT;
160
161   g_once (&once, (GThreadFunc) register_tag_image_type_enum, &id);
162   return id;
163 }
164
165 static inline gboolean
166 gst_tag_image_type_is_valid (GstTagImageType type)
167 {
168   GEnumClass *klass;
169
170   gboolean res;
171
172   klass = g_type_class_ref (gst_tag_image_type_get_type ());
173   res = (g_enum_get_value (klass, type) != NULL);
174   g_type_class_unref (klass);
175
176   return res;
177 }
178
179 /**
180  * gst_tag_parse_extended_comment:
181  * @ext_comment: an extended comment string, see #GST_TAG_EXTENDED_COMMENT
182  * @key: return location for the comment description key, or NULL
183  * @lang: return location for the comment ISO-639 language code, or NULL
184  * @value: return location for the actual comment string, or NULL
185  * @fail_if_no_key: whether to fail if strings are not in key=value form
186  *
187  * Convenience function to parse a GST_TAG_EXTENDED_COMMENT string and
188  * separate it into its components.
189  *
190  * If successful, @key, @lang and/or @value will be set to newly allocated
191  * strings that you need to free with g_free() when done. @key and @lang
192  * may also be set to NULL by this function if there is no key or no language
193  * code in the extended comment string.
194  *
195  * Returns: TRUE if the string could be parsed, otherwise FALSE
196  *
197  * Since: 0.10.10
198  */
199 gboolean
200 gst_tag_parse_extended_comment (const gchar * ext_comment, gchar ** key,
201     gchar ** lang, gchar ** value, gboolean fail_if_no_key)
202 {
203   const gchar *div, *bop, *bcl;
204
205   g_return_val_if_fail (ext_comment != NULL, FALSE);
206   g_return_val_if_fail (g_utf8_validate (ext_comment, -1, NULL), FALSE);
207
208   if (key)
209     *key = NULL;
210   if (lang)
211     *lang = NULL;
212
213   div = strchr (ext_comment, '=');
214   bop = strchr (ext_comment, '[');
215   bcl = strchr (ext_comment, ']');
216
217   if (div == NULL) {
218     if (fail_if_no_key)
219       return FALSE;
220     if (value)
221       *value = g_strdup (ext_comment);
222     return TRUE;
223   }
224
225   if (bop != NULL && bop < div) {
226     if (bcl < bop || bcl > div)
227       return FALSE;
228     if (key)
229       *key = g_strndup (ext_comment, bop - ext_comment);
230     if (lang)
231       *lang = g_strndup (bop + 1, bcl - bop - 1);
232   } else {
233     if (key)
234       *key = g_strndup (ext_comment, div - ext_comment);
235   }
236
237   if (value)
238     *value = g_strdup (div + 1);
239
240   return TRUE;
241 }
242
243 /**
244  * gst_tag_freeform_string_to_utf8:
245  * @data: string data
246  * @size: length of string data, or -1 if the string is NUL-terminated
247  * @env_vars: a NULL-terminated string array of environment variable names,
248  *            or NULL
249  *
250  * Convenience function to read a string with unknown character encoding. If
251  * the string is already in UTF-8 encoding, it will be returned right away.
252  * Otherwise, the environment will be searched for a number of environment
253  * variables (whose names are specified in the NULL-terminated string array
254  * @env_vars) containing a list of character encodings to try/use. If none
255  * are specified, the current locale will be tried. If that also doesn't work,
256  * ISO-8859-1 is assumed (which will almost always succeed).
257  *
258  * Returns: a newly-allocated string in UTF-8 encoding, or NULL
259  *
260  * Since: 0.10.13
261  */
262 gchar *
263 gst_tag_freeform_string_to_utf8 (const gchar * data, gint size,
264     const gchar ** env_vars)
265 {
266   const gchar *cur_loc = NULL;
267
268   gsize bytes_read;
269
270   gchar *utf8 = NULL;
271
272   g_return_val_if_fail (data != NULL, NULL);
273
274   if (size < 0)
275     size = strlen (data);
276
277   /* chop off trailing string terminators to make sure utf8_validate doesn't
278    * get to see them (since that would make the utf8 check fail) */
279   while (size > 0 && data[size - 1] == '\0')
280     --size;
281
282   /* Should we try the charsets specified
283    * via environment variables FIRST ? */
284   if (g_utf8_validate (data, size, NULL)) {
285     utf8 = g_strndup (data, size);
286     GST_LOG ("String '%s' is valid UTF-8 already", utf8);
287     goto beach;
288   }
289
290   while (env_vars && *env_vars != NULL) {
291     const gchar *env = NULL;
292
293     /* Try charsets specified via the environment */
294     env = g_getenv (*env_vars);
295     if (env != NULL && *env != '\0') {
296       gchar **c, **csets;
297
298       csets = g_strsplit (env, G_SEARCHPATH_SEPARATOR_S, -1);
299
300       for (c = csets; c && *c; ++c) {
301         GST_LOG ("Trying to convert freeform string to UTF-8 from '%s'", *c);
302         if ((utf8 =
303                 g_convert (data, size, "UTF-8", *c, &bytes_read, NULL, NULL))) {
304           if (bytes_read == size) {
305             g_strfreev (csets);
306             goto beach;
307           }
308           g_free (utf8);
309           utf8 = NULL;
310         }
311       }
312
313       g_strfreev (csets);
314     }
315     ++env_vars;
316   }
317
318   /* Try current locale (if not UTF-8) */
319   if (!g_get_charset (&cur_loc)) {
320     GST_LOG ("Trying to convert freeform string using locale ('%s')", cur_loc);
321     if ((utf8 = g_locale_to_utf8 (data, size, &bytes_read, NULL, NULL))) {
322       if (bytes_read == size) {
323         goto beach;
324       }
325       g_free (utf8);
326       utf8 = NULL;
327     }
328   }
329
330   /* Try ISO-8859-1 */
331   GST_LOG ("Trying to convert freeform string using ISO-8859-1 fallback");
332   utf8 = g_convert (data, size, "UTF-8", "ISO-8859-1", &bytes_read, NULL, NULL);
333   if (utf8 != NULL && bytes_read == size) {
334     goto beach;
335   }
336
337   g_free (utf8);
338   return NULL;
339
340 beach:
341
342   g_strchomp (utf8);
343   if (utf8 && utf8[0] != '\0') {
344     GST_LOG ("Returning '%s'", utf8);
345     return utf8;
346   }
347
348   g_free (utf8);
349   return NULL;
350 }
351
352 /**
353  * gst_tag_image_data_to_image_buffer:
354  * @image_data: the (encoded) image
355  * @image_data_len: the length of the encoded image data at @image_data
356  * @image_type: type of the image, or #GST_TAG_IMAGE_TYPE_UNDEFINED. Pass
357  *     #GST_TAG_IMAGE_TYPE_NONE if no image type should be set at all (e.g.
358  *     for preview images)
359  *
360  * Helper function for tag-reading plugins to create a #GstBuffer suitable to
361  * add to a #GstTagList as an image tag (such as #GST_TAG_IMAGE or
362  * #GST_TAG_PREVIEW_IMAGE) from the encoded image data and an (optional) image
363  * type.
364  *
365  * Background: cover art and other images in tags are usually stored as a
366  * blob of binary image data, often accompanied by a MIME type or some other
367  * content type string (e.g. 'png', 'jpeg', 'jpg'). Sometimes there is also an
368  * 'image type' to indicate what kind of image this is (e.g. front cover,
369  * back cover, artist, etc.). The image data may also be an URI to the image
370  * rather than the image itself.
371  *
372  * In GStreamer, image tags are #GstBuffer<!-- -->s containing the raw image
373  * data, with the buffer caps describing the content type of the image
374  * (e.g. image/jpeg, image/png, text/uri-list). The buffer caps may contain
375  * an additional 'image-type' field of #GST_TYPE_TAG_IMAGE_TYPE to describe
376  * the type of image (front cover, back cover etc.). #GST_TAG_PREVIEW_IMAGE
377  * tags should not carry an image type, their type is already indicated via
378  * the special tag name.
379  *
380  * This function will do various checks and typefind the encoded image
381  * data (we can't trust the declared mime type).
382  *
383  * Returns: a newly-allocated image buffer for use in tag lists, or NULL
384  *
385  * Since: 0.10.20
386  */
387 GstBuffer *
388 gst_tag_image_data_to_image_buffer (const guint8 * image_data,
389     guint image_data_len, GstTagImageType image_type)
390 {
391   const gchar *name;
392
393   GstBuffer *image;
394
395   GstCaps *caps;
396
397   g_return_val_if_fail (image_data != NULL, NULL);
398   g_return_val_if_fail (image_data_len > 0, NULL);
399   g_return_val_if_fail (gst_tag_image_type_is_valid (image_type), NULL);
400
401   GST_DEBUG ("image data len: %u bytes", image_data_len);
402
403   /* allocate space for a NUL terminator for an uri too */
404   image = gst_buffer_try_new_and_alloc (image_data_len + 1);
405   if (image == NULL) {
406     GST_WARNING ("failed to allocate buffer of %d for image", image_data_len);
407     return NULL;
408   }
409
410   memcpy (GST_BUFFER_DATA (image), image_data, image_data_len);
411   GST_BUFFER_DATA (image)[image_data_len] = '\0';
412   GST_BUFFER_SIZE (image) = image_data_len;
413
414   /* Find GStreamer media type, can't trust declared type */
415   caps = gst_type_find_helper_for_buffer (NULL, image, NULL);
416
417   if (caps == NULL)
418     goto no_type;
419
420   GST_DEBUG ("Found GStreamer media type: %" GST_PTR_FORMAT, caps);
421
422   /* sanity check: make sure typefound/declared caps are either URI or image */
423   name = gst_structure_get_name (gst_caps_get_structure (caps, 0));
424
425   if (!g_str_has_prefix (name, "image/") &&
426       !g_str_has_prefix (name, "video/") &&
427       !g_str_equal (name, "text/uri-list")) {
428     GST_DEBUG ("Unexpected image type '%s', ignoring image frame", name);
429     goto error;
430   }
431
432   /* Decrease size by 1 if we don't have an URI list
433    * to keep the original size of the image
434    */
435   if (!g_str_equal (name, "text/uri-list"))
436     GST_BUFFER_SIZE (image) = GST_BUFFER_SIZE (image) - 1;
437
438   if (image_type != GST_TAG_IMAGE_TYPE_NONE) {
439     GST_LOG ("Setting image type: %d", image_type);
440     caps = gst_caps_make_writable (caps);
441     gst_caps_set_simple (caps, "image-type", GST_TYPE_TAG_IMAGE_TYPE,
442         image_type, NULL);
443   }
444
445   gst_buffer_set_caps (image, caps);
446   gst_caps_unref (caps);
447   return image;
448
449 /* ERRORS */
450 no_type:
451   {
452     GST_DEBUG ("Could not determine GStreamer media type, ignoring image");
453     /* fall through */
454   }
455 error:
456   {
457     if (image)
458       gst_buffer_unref (image);
459     if (caps)
460       gst_caps_unref (caps);
461     return NULL;
462   }
463 }