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
46 #ifndef GST_DISABLE_GST_DEBUG
47 #define GST_CAT_DEFAULT gst_tag_ensure_debug_category()
49 static GstDebugCategory *
50 gst_tag_ensure_debug_category (void)
52 static gsize cat_gonce = 0;
54 if (g_once_init_enter (&cat_gonce)) {
55 GstDebugCategory *cat = NULL;
57 GST_DEBUG_CATEGORY_INIT (cat, "tag-tags", 0, "GstTag helper functions");
59 g_once_init_leave (&cat_gonce, (gsize) cat);
62 return (GstDebugCategory *) cat_gonce;
64 #endif /* GST_DISABLE_GST_DEBUG */
67 gst_tag_register_tags_internal (gpointer unused)
70 GST_DEBUG ("binding text domain %s to locale dir %s", GETTEXT_PACKAGE,
72 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
73 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
76 /* musicbrainz tags */
77 gst_tag_register (GST_TAG_MUSICBRAINZ_TRACKID, GST_TAG_FLAG_META,
78 G_TYPE_STRING, _("track ID"), _("MusicBrainz track ID"), NULL);
79 gst_tag_register (GST_TAG_MUSICBRAINZ_ARTISTID, GST_TAG_FLAG_META,
80 G_TYPE_STRING, _("artist ID"), _("MusicBrainz artist ID"), NULL);
81 gst_tag_register (GST_TAG_MUSICBRAINZ_ALBUMID, GST_TAG_FLAG_META,
82 G_TYPE_STRING, _("album ID"), _("MusicBrainz album ID"), NULL);
83 gst_tag_register (GST_TAG_MUSICBRAINZ_ALBUMARTISTID, GST_TAG_FLAG_META,
85 _("album artist ID"), _("MusicBrainz album artist ID"), NULL);
86 gst_tag_register (GST_TAG_MUSICBRAINZ_TRMID, GST_TAG_FLAG_META,
87 G_TYPE_STRING, _("track TRM ID"), _("MusicBrainz TRM ID"), NULL);
90 gst_tag_register (GST_TAG_CDDA_CDDB_DISCID, GST_TAG_FLAG_META,
91 G_TYPE_STRING, "discid", "CDDB discid for metadata retrieval",
92 gst_tag_merge_use_first);
94 gst_tag_register (GST_TAG_CDDA_CDDB_DISCID_FULL, GST_TAG_FLAG_META,
95 G_TYPE_STRING, "discid full",
96 "CDDB discid for metadata retrieval (full)", gst_tag_merge_use_first);
98 gst_tag_register (GST_TAG_CDDA_MUSICBRAINZ_DISCID, GST_TAG_FLAG_META,
99 G_TYPE_STRING, "musicbrainz-discid",
100 "Musicbrainz discid for metadata retrieval", gst_tag_merge_use_first);
102 gst_tag_register (GST_TAG_CDDA_MUSICBRAINZ_DISCID_FULL, GST_TAG_FLAG_META,
103 G_TYPE_STRING, "musicbrainz-discid-full",
104 "Musicbrainz discid for metadata retrieval (full)",
105 gst_tag_merge_use_first);
107 /* photography tags */
108 gst_tag_register (GST_TAG_CAPTURING_SHUTTER_SPEED, GST_TAG_FLAG_META,
109 GST_TYPE_FRACTION, _("capturing shutter speed"),
110 _("Shutter speed used when capturing an image, in seconds"), NULL);
112 gst_tag_register (GST_TAG_CAPTURING_FOCAL_RATIO, GST_TAG_FLAG_META,
113 G_TYPE_DOUBLE, _("capturing focal ratio"),
114 _("Focal ratio (f-number) used when capturing the image"), NULL);
116 gst_tag_register (GST_TAG_CAPTURING_FOCAL_LENGTH, GST_TAG_FLAG_META,
117 G_TYPE_DOUBLE, _("capturing focal length"),
118 _("Focal length of the lens used capturing the image, in mm"), NULL);
120 gst_tag_register (GST_TAG_CAPTURING_DIGITAL_ZOOM_RATIO, GST_TAG_FLAG_META,
121 G_TYPE_DOUBLE, _("capturing digital zoom ratio"),
122 _("Digital zoom ratio used when capturing an image"), NULL);
124 gst_tag_register (GST_TAG_CAPTURING_ISO_SPEED, GST_TAG_FLAG_META,
125 G_TYPE_INT, _("capturing iso speed"),
126 _("The ISO speed used when capturing an image"), NULL);
128 gst_tag_register (GST_TAG_CAPTURING_EXPOSURE_PROGRAM, GST_TAG_FLAG_META,
129 G_TYPE_STRING, _("capturing exposure program"),
130 _("The exposure program used when capturing an image"), NULL);
132 gst_tag_register (GST_TAG_CAPTURING_EXPOSURE_MODE, GST_TAG_FLAG_META,
133 G_TYPE_STRING, _("capturing exposure mode"),
134 _("The exposure mode used when capturing an image"), NULL);
136 gst_tag_register (GST_TAG_CAPTURING_SCENE_CAPTURE_TYPE, GST_TAG_FLAG_META,
137 G_TYPE_STRING, _("capturing scene capture type"),
138 _("The scene capture mode used when capturing an image"), NULL);
140 gst_tag_register (GST_TAG_CAPTURING_GAIN_ADJUSTMENT, GST_TAG_FLAG_META,
141 G_TYPE_STRING, _("capturing gain adjustment"),
142 _("The overall gain adjustment applied on an image"), NULL);
144 gst_tag_register (GST_TAG_CAPTURING_WHITE_BALANCE, GST_TAG_FLAG_META,
145 G_TYPE_STRING, _("capturing white balance"),
146 _("The white balance mode set when capturing an image"), NULL);
148 gst_tag_register (GST_TAG_CAPTURING_CONTRAST, GST_TAG_FLAG_META,
149 G_TYPE_STRING, _("capturing contrast"),
150 _("The direction of contrast processing applied "
151 "when capturing an image"), NULL);
153 gst_tag_register (GST_TAG_CAPTURING_SATURATION, GST_TAG_FLAG_META,
154 G_TYPE_STRING, _("capturing saturation"),
155 _("The direction of saturation processing applied when "
156 "capturing an image"), NULL);
158 gst_tag_register (GST_TAG_CAPTURING_SHARPNESS, GST_TAG_FLAG_META,
159 G_TYPE_STRING, _("capturing sharpness"),
160 _("The direction of sharpness processing applied "
161 "when capturing an image"), NULL);
163 gst_tag_register (GST_TAG_CAPTURING_FLASH_FIRED, GST_TAG_FLAG_META,
164 G_TYPE_BOOLEAN, _("capturing flash fired"),
165 _("If the flash fired while capturing an image"), NULL);
167 gst_tag_register (GST_TAG_CAPTURING_FLASH_MODE, GST_TAG_FLAG_META,
168 G_TYPE_STRING, _("capturing flash mode"),
169 _("The selected flash mode while capturing an image"), NULL);
171 gst_tag_register (GST_TAG_CAPTURING_METERING_MODE, GST_TAG_FLAG_META,
172 G_TYPE_STRING, _("capturing metering mode"),
173 _("The metering mode used while determining exposure for capturing an"
176 gst_tag_register (GST_TAG_IMAGE_HORIZONTAL_PPI, GST_TAG_FLAG_META,
177 G_TYPE_DOUBLE, _("image horizontal ppi"),
178 _("Media (image/video) intended horizontal pixel density in ppi"), NULL);
180 gst_tag_register (GST_TAG_IMAGE_VERTICAL_PPI, GST_TAG_FLAG_META,
181 G_TYPE_DOUBLE, _("image vertical ppi"),
182 _("Media (image/video) intended vertical pixel density in ppi"), NULL);
187 /* FIXME 0.11: rename this to gst_tag_init() or gst_tag_register_tags() */
189 * gst_tag_register_musicbrainz_tags
191 * Registers additional musicbrainz-specific tags with the GStreamer tag
192 * system. Plugins and applications that use these tags should call this
193 * function before using them. Can be called multiple times.
196 gst_tag_register_musicbrainz_tags (void)
198 static GOnce mb_once = G_ONCE_INIT;
200 g_once (&mb_once, gst_tag_register_tags_internal, NULL);
204 register_tag_image_type_enum (GType * id)
206 static const GEnumValue image_types[] = {
207 {GST_TAG_IMAGE_TYPE_NONE, "GST_TAG_IMAGE_TYPE_NONE", "none"},
208 {GST_TAG_IMAGE_TYPE_UNDEFINED, "GST_TAG_IMAGE_TYPE_UNDEFINED", "undefined"},
209 {GST_TAG_IMAGE_TYPE_FRONT_COVER, "GST_TAG_IMAGE_TYPE_FRONT_COVER",
211 {GST_TAG_IMAGE_TYPE_BACK_COVER, "GST_TAG_IMAGE_TYPE_BACK_COVER",
213 {GST_TAG_IMAGE_TYPE_LEAFLET_PAGE, "GST_TAG_IMAGE_TYPE_LEAFLET_PAGE",
215 {GST_TAG_IMAGE_TYPE_MEDIUM, "GST_TAG_IMAGE_TYPE_MEDIUM", "medium"},
216 {GST_TAG_IMAGE_TYPE_LEAD_ARTIST, "GST_TAG_IMAGE_TYPE_LEAD_ARTIST",
218 {GST_TAG_IMAGE_TYPE_ARTIST, "GST_TAG_IMAGE_TYPE_ARTIST", "artist"},
219 {GST_TAG_IMAGE_TYPE_CONDUCTOR, "GST_TAG_IMAGE_TYPE_CONDUCTOR", "conductor"},
220 {GST_TAG_IMAGE_TYPE_BAND_ORCHESTRA, "GST_TAG_IMAGE_TYPE_BAND_ORCHESTRA",
222 {GST_TAG_IMAGE_TYPE_COMPOSER, "GST_TAG_IMAGE_TYPE_COMPOSER", "composer"},
223 {GST_TAG_IMAGE_TYPE_LYRICIST, "GST_TAG_IMAGE_TYPE_LYRICIST", "lyricist"},
224 {GST_TAG_IMAGE_TYPE_RECORDING_LOCATION,
225 "GST_TAG_IMAGE_TYPE_RECORDING_LOCATION",
226 "recording-location"},
227 {GST_TAG_IMAGE_TYPE_DURING_RECORDING, "GST_TAG_IMAGE_TYPE_DURING_RECORDING",
229 {GST_TAG_IMAGE_TYPE_DURING_PERFORMANCE,
230 "GST_TAG_IMAGE_TYPE_DURING_PERFORMANCE",
231 "during-performance"},
232 {GST_TAG_IMAGE_TYPE_VIDEO_CAPTURE, "GST_TAG_IMAGE_TYPE_VIDEO_CAPTURE",
234 {GST_TAG_IMAGE_TYPE_FISH, "GST_TAG_IMAGE_TYPE_FISH", "fish"},
235 {GST_TAG_IMAGE_TYPE_ILLUSTRATION, "GST_TAG_IMAGE_TYPE_ILLUSTRATION",
237 {GST_TAG_IMAGE_TYPE_BAND_ARTIST_LOGO, "GST_TAG_IMAGE_TYPE_BAND_ARTIST_LOGO",
239 {GST_TAG_IMAGE_TYPE_PUBLISHER_STUDIO_LOGO,
240 "GST_TAG_IMAGE_TYPE_PUBLISHER_STUDIO_LOGO",
241 "publisher-studio-logo"},
245 *id = g_enum_register_static ("GstTagImageType", image_types);
247 /* work around thread-safety issue with class creation in GLib */
248 g_type_class_ref (*id);
252 gst_tag_image_type_get_type (void)
256 static GOnce once = G_ONCE_INIT;
258 g_once (&once, (GThreadFunc) register_tag_image_type_enum, &id);
262 static inline gboolean
263 gst_tag_image_type_is_valid (GstTagImageType type)
269 klass = g_type_class_ref (gst_tag_image_type_get_type ());
270 res = (g_enum_get_value (klass, type) != NULL);
271 g_type_class_unref (klass);
277 * gst_tag_parse_extended_comment:
278 * @ext_comment: an extended comment string, see #GST_TAG_EXTENDED_COMMENT
279 * @key: return location for the comment description key, or NULL
280 * @lang: return location for the comment ISO-639 language code, or NULL
281 * @value: return location for the actual comment string, or NULL
282 * @fail_if_no_key: whether to fail if strings are not in key=value form
284 * Convenience function to parse a GST_TAG_EXTENDED_COMMENT string and
285 * separate it into its components.
287 * If successful, @key, @lang and/or @value will be set to newly allocated
288 * strings that you need to free with g_free() when done. @key and @lang
289 * may also be set to NULL by this function if there is no key or no language
290 * code in the extended comment string.
292 * Returns: TRUE if the string could be parsed, otherwise FALSE
297 gst_tag_parse_extended_comment (const gchar * ext_comment, gchar ** key,
298 gchar ** lang, gchar ** value, gboolean fail_if_no_key)
300 const gchar *div, *bop, *bcl;
302 g_return_val_if_fail (ext_comment != NULL, FALSE);
303 g_return_val_if_fail (g_utf8_validate (ext_comment, -1, NULL), FALSE);
310 div = strchr (ext_comment, '=');
311 bop = strchr (ext_comment, '[');
312 bcl = strchr (ext_comment, ']');
318 *value = g_strdup (ext_comment);
322 if (bop != NULL && bop < div) {
323 if (bcl < bop || bcl > div)
326 *key = g_strndup (ext_comment, bop - ext_comment);
328 *lang = g_strndup (bop + 1, bcl - bop - 1);
331 *key = g_strndup (ext_comment, div - ext_comment);
335 *value = g_strdup (div + 1);
341 * gst_tag_freeform_string_to_utf8:
343 * @size: length of string data, or -1 if the string is NUL-terminated
344 * @env_vars: a NULL-terminated string array of environment variable names,
347 * Convenience function to read a string with unknown character encoding. If
348 * the string is already in UTF-8 encoding, it will be returned right away.
349 * If not it tries to detect byte-order-mark for UTF-16/32 cases and use that.
350 * Otherwise, the environment will be searched for a number of environment
351 * variables (whose names are specified in the NULL-terminated string array
352 * @env_vars) containing a list of character encodings to try/use. If none
353 * are specified, the current locale will be tried. If that also doesn't work,
354 * WINDOWS-1252/ISO-8859-1 is assumed (which will almost always succeed).
356 * Returns: a newly-allocated string in UTF-8 encoding, or NULL
361 gst_tag_freeform_string_to_utf8 (const gchar * data, gint size,
362 const gchar ** env_vars)
364 const gchar *cur_loc = NULL;
370 g_return_val_if_fail (data != NULL, NULL);
373 size = strlen (data);
375 /* chop off trailing string terminators to make sure utf8_validate doesn't
376 * get to see them (since that would make the utf8 check fail) */
377 while (size > 0 && data[size - 1] == '\0')
380 /* Should we try the charsets specified
381 * via environment variables FIRST ? */
382 if (g_utf8_validate (data, size, NULL)) {
383 utf8 = g_strndup (data, size);
384 GST_LOG ("String '%s' is valid UTF-8 already", utf8);
388 /* check for and use byte-order-mark for UTF-16/32 cases */
390 const gchar *c = NULL;
395 ssize = GST_ROUND_DOWN_4 (size - 4);
396 switch (GST_READ_UINT32_BE (data)) {
409 ssize = GST_ROUND_DOWN_2 (size - 2);
410 switch (GST_READ_UINT16_BE (data)) {
422 GST_LOG ("Trying to convert freeform string to UTF-8 from '%s'", c);
424 g_convert (data + prefix, ssize, "UTF-8", c, &bytes_read, NULL,
426 if (bytes_read == ssize)
434 while (env_vars && *env_vars != NULL) {
435 const gchar *env = NULL;
437 /* Try charsets specified via the environment */
438 env = g_getenv (*env_vars);
439 if (env != NULL && *env != '\0') {
442 csets = g_strsplit (env, G_SEARCHPATH_SEPARATOR_S, -1);
444 for (c = csets; c && *c; ++c) {
445 GST_LOG ("Trying to convert freeform string to UTF-8 from '%s'", *c);
447 g_convert (data, size, "UTF-8", *c, &bytes_read, NULL, NULL))) {
448 if (bytes_read == size) {
462 /* Try current locale (if not UTF-8) */
463 if (!g_get_charset (&cur_loc)) {
464 GST_LOG ("Trying to convert freeform string using locale ('%s')", cur_loc);
465 if ((utf8 = g_locale_to_utf8 (data, size, &bytes_read, NULL, NULL))) {
466 if (bytes_read == size) {
474 /* Try Windows-1252 (which is a superset of ISO 8859-1 that uses a control
475 * character range in ISO 8859-1 for more printable characters) */
479 GST_LOG ("Trying to convert freeform string using Windows-1252/ISO-8859-1 "
481 utf8 = g_convert (data, size, "UTF-8", "WINDOWS-1252", &bytes_read, NULL,
484 /* fallback in case iconv implementation doesn't support windows-1252
486 if (err->code == G_CONVERT_ERROR_NO_CONVERSION) {
487 utf8 = g_convert (data, size, "UTF-8", "ISO-8859-1", &bytes_read,
493 if (utf8 != NULL && bytes_read == size)
503 if (utf8 && utf8[0] != '\0') {
504 GST_LOG ("Returning '%s'", utf8);
513 * gst_tag_image_data_to_image_buffer:
514 * @image_data: the (encoded) image
515 * @image_data_len: the length of the encoded image data at @image_data
516 * @image_type: type of the image, or #GST_TAG_IMAGE_TYPE_UNDEFINED. Pass
517 * #GST_TAG_IMAGE_TYPE_NONE if no image type should be set at all (e.g.
518 * for preview images)
520 * Helper function for tag-reading plugins to create a #GstBuffer suitable to
521 * add to a #GstTagList as an image tag (such as #GST_TAG_IMAGE or
522 * #GST_TAG_PREVIEW_IMAGE) from the encoded image data and an (optional) image
525 * Background: cover art and other images in tags are usually stored as a
526 * blob of binary image data, often accompanied by a MIME type or some other
527 * content type string (e.g. 'png', 'jpeg', 'jpg'). Sometimes there is also an
528 * 'image type' to indicate what kind of image this is (e.g. front cover,
529 * back cover, artist, etc.). The image data may also be an URI to the image
530 * rather than the image itself.
532 * In GStreamer, image tags are #GstBuffer<!-- -->s containing the raw image
533 * data, with the buffer caps describing the content type of the image
534 * (e.g. image/jpeg, image/png, text/uri-list). The buffer caps may contain
535 * an additional 'image-type' field of #GST_TYPE_TAG_IMAGE_TYPE to describe
536 * the type of image (front cover, back cover etc.). #GST_TAG_PREVIEW_IMAGE
537 * tags should not carry an image type, their type is already indicated via
538 * the special tag name.
540 * This function will do various checks and typefind the encoded image
541 * data (we can't trust the declared mime type).
543 * Returns: a newly-allocated image buffer for use in tag lists, or NULL
548 gst_tag_image_data_to_image_buffer (const guint8 * image_data,
549 guint image_data_len, GstTagImageType image_type)
557 g_return_val_if_fail (image_data != NULL, NULL);
558 g_return_val_if_fail (image_data_len > 0, NULL);
559 g_return_val_if_fail (gst_tag_image_type_is_valid (image_type), NULL);
561 GST_DEBUG ("image data len: %u bytes", image_data_len);
563 /* allocate space for a NUL terminator for an uri too */
564 image = gst_buffer_try_new_and_alloc (image_data_len + 1);
566 GST_WARNING ("failed to allocate buffer of %d for image", image_data_len);
570 memcpy (GST_BUFFER_DATA (image), image_data, image_data_len);
571 GST_BUFFER_DATA (image)[image_data_len] = '\0';
573 /* Find GStreamer media type, can't trust declared type */
574 caps = gst_type_find_helper_for_buffer (NULL, image, NULL);
579 GST_DEBUG ("Found GStreamer media type: %" GST_PTR_FORMAT, caps);
581 /* sanity check: make sure typefound/declared caps are either URI or image */
582 name = gst_structure_get_name (gst_caps_get_structure (caps, 0));
584 if (!g_str_has_prefix (name, "image/") &&
585 !g_str_has_prefix (name, "video/") &&
586 !g_str_equal (name, "text/uri-list")) {
587 GST_DEBUG ("Unexpected image type '%s', ignoring image frame", name);
591 /* Decrease size by 1 if we don't have an URI list
592 * to keep the original size of the image
594 if (!g_str_equal (name, "text/uri-list"))
595 GST_BUFFER_SIZE (image) = image_data_len;
597 if (image_type != GST_TAG_IMAGE_TYPE_NONE) {
598 GST_LOG ("Setting image type: %d", image_type);
599 caps = gst_caps_make_writable (caps);
600 gst_caps_set_simple (caps, "image-type", GST_TYPE_TAG_IMAGE_TYPE,
604 gst_buffer_set_caps (image, caps);
605 gst_caps_unref (caps);
611 GST_DEBUG ("Could not determine GStreamer media type, ignoring image");
617 gst_buffer_unref (image);
619 gst_caps_unref (caps);