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>
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.
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.
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.
25 #include <gst/gst-i18n-plugin.h>
26 #include <gst/base/gsttypefindhelper.h>
34 * @short_description: additional tag definitions for plugins and applications
35 * @see_also: #GstTagList
39 * Contains additional standardized GStreamer tag definitions for plugins
40 * and applications, and functions to register them with the GStreamer
48 gst_tag_register_tags_internal (gpointer unused)
51 GST_DEBUG ("binding text domain %s to locale dir %s", GETTEXT_PACKAGE,
53 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
54 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
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,
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);
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);
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);
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);
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);
88 /* photography tags */
89 gst_tag_register (GST_TAG_CAPTURING_SHUTTER_SPEED, GST_TAG_FLAG_META,
90 GST_TYPE_FRACTION, _("capturing shutter speed"),
91 _("Shutter speed used when capturing an image, in seconds"), NULL);
93 gst_tag_register (GST_TAG_CAPTURING_FOCAL_RATIO, GST_TAG_FLAG_META,
94 G_TYPE_DOUBLE, _("capturing focal ratio"),
95 _("Focal ratio (f-number) used when capturing the image"), NULL);
97 gst_tag_register (GST_TAG_CAPTURING_FOCAL_LENGTH, GST_TAG_FLAG_META,
98 G_TYPE_DOUBLE, _("capturing focal length"),
99 _("Focal length of the lens used capturing the image, in mm"), NULL);
101 gst_tag_register (GST_TAG_CAPTURING_DIGITAL_ZOOM_RATIO, GST_TAG_FLAG_META,
102 G_TYPE_DOUBLE, _("capturing digital zoom ratio"),
103 _("Digital zoom ratio used when capturing an image"), NULL);
105 gst_tag_register (GST_TAG_CAPTURING_ISO_SPEED, GST_TAG_FLAG_META,
106 G_TYPE_INT, _("capturing iso speed"),
107 _("The ISO speed used when capturing an image"), NULL);
109 gst_tag_register (GST_TAG_CAPTURING_EXPOSURE_PROGRAM, GST_TAG_FLAG_META,
110 G_TYPE_STRING, _("capturing exposure program"),
111 _("The exposure program used when capturing an image"), NULL);
113 gst_tag_register (GST_TAG_CAPTURING_EXPOSURE_MODE, GST_TAG_FLAG_META,
114 G_TYPE_STRING, _("capturing exposure mode"),
115 _("The exposure mode used when capturing an image"), NULL);
117 gst_tag_register (GST_TAG_CAPTURING_SCENE_CAPTURE_TYPE, GST_TAG_FLAG_META,
118 G_TYPE_STRING, _("capturing scene capture type"),
119 _("The scene capture mode used when capturing an image"), NULL);
121 gst_tag_register (GST_TAG_CAPTURING_GAIN_ADJUSTMENT, GST_TAG_FLAG_META,
122 G_TYPE_STRING, _("capturing gain adjustment"),
123 _("The overall gain adjustment applied on an image"), NULL);
125 gst_tag_register (GST_TAG_CAPTURING_WHITE_BALANCE, GST_TAG_FLAG_META,
126 G_TYPE_STRING, _("capturing white balance"),
127 _("The white balance mode set when capturing an image"), NULL);
129 gst_tag_register (GST_TAG_CAPTURING_CONTRAST, GST_TAG_FLAG_META,
130 G_TYPE_STRING, _("capturing contrast"),
131 _("The direction of contrast processing applied "
132 "when capturing an image"), NULL);
134 gst_tag_register (GST_TAG_CAPTURING_SATURATION, GST_TAG_FLAG_META,
135 G_TYPE_STRING, _("capturing saturation"),
136 _("The direction of saturation processing applied when "
137 "capturing an image"), NULL);
142 /* FIXME 0.11: rename this to gst_tag_init() or gst_tag_register_tags() */
144 * gst_tag_register_musicbrainz_tags
146 * Registers additional musicbrainz-specific tags with the GStreamer tag
147 * system. Plugins and applications that use these tags should call this
148 * function before using them. Can be called multiple times.
151 gst_tag_register_musicbrainz_tags (void)
153 static GOnce mb_once = G_ONCE_INIT;
155 g_once (&mb_once, gst_tag_register_tags_internal, NULL);
159 register_tag_image_type_enum (GType * id)
161 static const GEnumValue image_types[] = {
162 {GST_TAG_IMAGE_TYPE_NONE, "GST_TAG_IMAGE_TYPE_NONE", "none"},
163 {GST_TAG_IMAGE_TYPE_UNDEFINED, "GST_TAG_IMAGE_TYPE_UNDEFINED", "undefined"},
164 {GST_TAG_IMAGE_TYPE_FRONT_COVER, "GST_TAG_IMAGE_TYPE_FRONT_COVER",
166 {GST_TAG_IMAGE_TYPE_BACK_COVER, "GST_TAG_IMAGE_TYPE_BACK_COVER",
168 {GST_TAG_IMAGE_TYPE_LEAFLET_PAGE, "GST_TAG_IMAGE_TYPE_LEAFLET_PAGE",
170 {GST_TAG_IMAGE_TYPE_MEDIUM, "GST_TAG_IMAGE_TYPE_MEDIUM", "medium"},
171 {GST_TAG_IMAGE_TYPE_LEAD_ARTIST, "GST_TAG_IMAGE_TYPE_LEAD_ARTIST",
173 {GST_TAG_IMAGE_TYPE_ARTIST, "GST_TAG_IMAGE_TYPE_ARTIST", "artist"},
174 {GST_TAG_IMAGE_TYPE_CONDUCTOR, "GST_TAG_IMAGE_TYPE_CONDUCTOR", "conductor"},
175 {GST_TAG_IMAGE_TYPE_BAND_ORCHESTRA, "GST_TAG_IMAGE_TYPE_BAND_ORCHESTRA",
177 {GST_TAG_IMAGE_TYPE_COMPOSER, "GST_TAG_IMAGE_TYPE_COMPOSER", "composer"},
178 {GST_TAG_IMAGE_TYPE_LYRICIST, "GST_TAG_IMAGE_TYPE_LYRICIST", "lyricist"},
179 {GST_TAG_IMAGE_TYPE_RECORDING_LOCATION,
180 "GST_TAG_IMAGE_TYPE_RECORDING_LOCATION",
181 "recording-location"},
182 {GST_TAG_IMAGE_TYPE_DURING_RECORDING, "GST_TAG_IMAGE_TYPE_DURING_RECORDING",
184 {GST_TAG_IMAGE_TYPE_DURING_PERFORMANCE,
185 "GST_TAG_IMAGE_TYPE_DURING_PERFORMANCE",
186 "during-performance"},
187 {GST_TAG_IMAGE_TYPE_VIDEO_CAPTURE, "GST_TAG_IMAGE_TYPE_VIDEO_CAPTURE",
189 {GST_TAG_IMAGE_TYPE_FISH, "GST_TAG_IMAGE_TYPE_FISH", "fish"},
190 {GST_TAG_IMAGE_TYPE_ILLUSTRATION, "GST_TAG_IMAGE_TYPE_ILLUSTRATION",
192 {GST_TAG_IMAGE_TYPE_BAND_ARTIST_LOGO, "GST_TAG_IMAGE_TYPE_BAND_ARTIST_LOGO",
194 {GST_TAG_IMAGE_TYPE_PUBLISHER_STUDIO_LOGO,
195 "GST_TAG_IMAGE_TYPE_PUBLISHER_STUDIO_LOGO",
196 "publisher-studio-logo"},
200 *id = g_enum_register_static ("GstTagImageType", image_types);
202 /* work around thread-safety issue with class creation in GLib */
203 g_type_class_ref (*id);
207 gst_tag_image_type_get_type (void)
211 static GOnce once = G_ONCE_INIT;
213 g_once (&once, (GThreadFunc) register_tag_image_type_enum, &id);
217 static inline gboolean
218 gst_tag_image_type_is_valid (GstTagImageType type)
224 klass = g_type_class_ref (gst_tag_image_type_get_type ());
225 res = (g_enum_get_value (klass, type) != NULL);
226 g_type_class_unref (klass);
232 * gst_tag_parse_extended_comment:
233 * @ext_comment: an extended comment string, see #GST_TAG_EXTENDED_COMMENT
234 * @key: return location for the comment description key, or NULL
235 * @lang: return location for the comment ISO-639 language code, or NULL
236 * @value: return location for the actual comment string, or NULL
237 * @fail_if_no_key: whether to fail if strings are not in key=value form
239 * Convenience function to parse a GST_TAG_EXTENDED_COMMENT string and
240 * separate it into its components.
242 * If successful, @key, @lang and/or @value will be set to newly allocated
243 * strings that you need to free with g_free() when done. @key and @lang
244 * may also be set to NULL by this function if there is no key or no language
245 * code in the extended comment string.
247 * Returns: TRUE if the string could be parsed, otherwise FALSE
252 gst_tag_parse_extended_comment (const gchar * ext_comment, gchar ** key,
253 gchar ** lang, gchar ** value, gboolean fail_if_no_key)
255 const gchar *div, *bop, *bcl;
257 g_return_val_if_fail (ext_comment != NULL, FALSE);
258 g_return_val_if_fail (g_utf8_validate (ext_comment, -1, NULL), FALSE);
265 div = strchr (ext_comment, '=');
266 bop = strchr (ext_comment, '[');
267 bcl = strchr (ext_comment, ']');
273 *value = g_strdup (ext_comment);
277 if (bop != NULL && bop < div) {
278 if (bcl < bop || bcl > div)
281 *key = g_strndup (ext_comment, bop - ext_comment);
283 *lang = g_strndup (bop + 1, bcl - bop - 1);
286 *key = g_strndup (ext_comment, div - ext_comment);
290 *value = g_strdup (div + 1);
296 * gst_tag_freeform_string_to_utf8:
298 * @size: length of string data, or -1 if the string is NUL-terminated
299 * @env_vars: a NULL-terminated string array of environment variable names,
302 * Convenience function to read a string with unknown character encoding. If
303 * the string is already in UTF-8 encoding, it will be returned right away.
304 * If not it tries to detect byte-order-mark for UTF-16/32 cases and use that.
305 * Otherwise, the environment will be searched for a number of environment
306 * variables (whose names are specified in the NULL-terminated string array
307 * @env_vars) containing a list of character encodings to try/use. If none
308 * are specified, the current locale will be tried. If that also doesn't work,
309 * ISO-8859-1 is assumed (which will almost always succeed).
311 * Returns: a newly-allocated string in UTF-8 encoding, or NULL
316 gst_tag_freeform_string_to_utf8 (const gchar * data, gint size,
317 const gchar ** env_vars)
319 const gchar *cur_loc = NULL;
325 g_return_val_if_fail (data != NULL, NULL);
328 size = strlen (data);
330 /* chop off trailing string terminators to make sure utf8_validate doesn't
331 * get to see them (since that would make the utf8 check fail) */
332 while (size > 0 && data[size - 1] == '\0')
335 /* Should we try the charsets specified
336 * via environment variables FIRST ? */
337 if (g_utf8_validate (data, size, NULL)) {
338 utf8 = g_strndup (data, size);
339 GST_LOG ("String '%s' is valid UTF-8 already", utf8);
343 /* check for and use byte-order-mark for UTF-16/32 cases */
345 const gchar *c = NULL;
350 ssize = GST_ROUND_DOWN_4 (size - 4);
351 switch (GST_READ_UINT32_BE (data)) {
364 ssize = GST_ROUND_DOWN_2 (size - 2);
365 switch (GST_READ_UINT16_BE (data)) {
377 GST_LOG ("Trying to convert freeform string to UTF-8 from '%s'", c);
379 g_convert (data + prefix, ssize, "UTF-8", c, &bytes_read, NULL,
381 if (bytes_read == ssize)
389 while (env_vars && *env_vars != NULL) {
390 const gchar *env = NULL;
392 /* Try charsets specified via the environment */
393 env = g_getenv (*env_vars);
394 if (env != NULL && *env != '\0') {
397 csets = g_strsplit (env, G_SEARCHPATH_SEPARATOR_S, -1);
399 for (c = csets; c && *c; ++c) {
400 GST_LOG ("Trying to convert freeform string to UTF-8 from '%s'", *c);
402 g_convert (data, size, "UTF-8", *c, &bytes_read, NULL, NULL))) {
403 if (bytes_read == size) {
417 /* Try current locale (if not UTF-8) */
418 if (!g_get_charset (&cur_loc)) {
419 GST_LOG ("Trying to convert freeform string using locale ('%s')", cur_loc);
420 if ((utf8 = g_locale_to_utf8 (data, size, &bytes_read, NULL, NULL))) {
421 if (bytes_read == size) {
430 GST_LOG ("Trying to convert freeform string using ISO-8859-1 fallback");
431 utf8 = g_convert (data, size, "UTF-8", "ISO-8859-1", &bytes_read, NULL, NULL);
432 if (utf8 != NULL && bytes_read == size) {
442 if (utf8 && utf8[0] != '\0') {
443 GST_LOG ("Returning '%s'", utf8);
452 * gst_tag_image_data_to_image_buffer:
453 * @image_data: the (encoded) image
454 * @image_data_len: the length of the encoded image data at @image_data
455 * @image_type: type of the image, or #GST_TAG_IMAGE_TYPE_UNDEFINED. Pass
456 * #GST_TAG_IMAGE_TYPE_NONE if no image type should be set at all (e.g.
457 * for preview images)
459 * Helper function for tag-reading plugins to create a #GstBuffer suitable to
460 * add to a #GstTagList as an image tag (such as #GST_TAG_IMAGE or
461 * #GST_TAG_PREVIEW_IMAGE) from the encoded image data and an (optional) image
464 * Background: cover art and other images in tags are usually stored as a
465 * blob of binary image data, often accompanied by a MIME type or some other
466 * content type string (e.g. 'png', 'jpeg', 'jpg'). Sometimes there is also an
467 * 'image type' to indicate what kind of image this is (e.g. front cover,
468 * back cover, artist, etc.). The image data may also be an URI to the image
469 * rather than the image itself.
471 * In GStreamer, image tags are #GstBuffer<!-- -->s containing the raw image
472 * data, with the buffer caps describing the content type of the image
473 * (e.g. image/jpeg, image/png, text/uri-list). The buffer caps may contain
474 * an additional 'image-type' field of #GST_TYPE_TAG_IMAGE_TYPE to describe
475 * the type of image (front cover, back cover etc.). #GST_TAG_PREVIEW_IMAGE
476 * tags should not carry an image type, their type is already indicated via
477 * the special tag name.
479 * This function will do various checks and typefind the encoded image
480 * data (we can't trust the declared mime type).
482 * Returns: a newly-allocated image buffer for use in tag lists, or NULL
487 gst_tag_image_data_to_image_buffer (const guint8 * image_data,
488 guint image_data_len, GstTagImageType image_type)
496 g_return_val_if_fail (image_data != NULL, NULL);
497 g_return_val_if_fail (image_data_len > 0, NULL);
498 g_return_val_if_fail (gst_tag_image_type_is_valid (image_type), NULL);
500 GST_DEBUG ("image data len: %u bytes", image_data_len);
502 /* allocate space for a NUL terminator for an uri too */
503 image = gst_buffer_try_new_and_alloc (image_data_len + 1);
505 GST_WARNING ("failed to allocate buffer of %d for image", image_data_len);
509 memcpy (GST_BUFFER_DATA (image), image_data, image_data_len);
510 GST_BUFFER_DATA (image)[image_data_len] = '\0';
512 /* Find GStreamer media type, can't trust declared type */
513 caps = gst_type_find_helper_for_buffer (NULL, image, NULL);
518 GST_DEBUG ("Found GStreamer media type: %" GST_PTR_FORMAT, caps);
520 /* sanity check: make sure typefound/declared caps are either URI or image */
521 name = gst_structure_get_name (gst_caps_get_structure (caps, 0));
523 if (!g_str_has_prefix (name, "image/") &&
524 !g_str_has_prefix (name, "video/") &&
525 !g_str_equal (name, "text/uri-list")) {
526 GST_DEBUG ("Unexpected image type '%s', ignoring image frame", name);
530 /* Decrease size by 1 if we don't have an URI list
531 * to keep the original size of the image
533 if (!g_str_equal (name, "text/uri-list"))
534 GST_BUFFER_SIZE (image) = image_data_len;
536 if (image_type != GST_TAG_IMAGE_TYPE_NONE) {
537 GST_LOG ("Setting image type: %d", image_type);
538 caps = gst_caps_make_writable (caps);
539 gst_caps_set_simple (caps, "image-type", GST_TYPE_TAG_IMAGE_TYPE,
543 gst_buffer_set_caps (image, caps);
544 gst_caps_unref (caps);
550 GST_DEBUG ("Could not determine GStreamer media type, ignoring image");
556 gst_buffer_unref (image);
558 gst_caps_unref (caps);