Fix FSF address
[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., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, 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 #include "id3v2.h"
30
31 #include <string.h>
32
33 /**
34  * SECTION:gsttag
35  * @short_description: additional tag definitions for plugins and applications
36  * @see_also: #GstTagList
37  * 
38  * <refsect2>
39  * <para>
40  * Contains additional standardized GStreamer tag definitions for plugins
41  * and applications, and functions to register them with the GStreamer
42  * tag system.
43  * </para>
44  * </refsect2>
45  */
46
47 #ifndef GST_DISABLE_GST_DEBUG
48 #define GST_CAT_DEFAULT gst_tag_ensure_debug_category()
49
50 static GstDebugCategory *
51 gst_tag_ensure_debug_category (void)
52 {
53   static gsize cat_gonce = 0;
54
55   if (g_once_init_enter (&cat_gonce)) {
56     GstDebugCategory *cat = NULL;
57
58     GST_DEBUG_CATEGORY_INIT (cat, "tag-tags", 0, "GstTag helper functions");
59
60     g_once_init_leave (&cat_gonce, (gsize) cat);
61   }
62
63   return (GstDebugCategory *) cat_gonce;
64 }
65 #endif /* GST_DISABLE_GST_DEBUG */
66
67 static gpointer
68 gst_tag_register_tags_internal (gpointer unused)
69 {
70 #ifdef ENABLE_NLS
71   GST_DEBUG ("binding text domain %s to locale dir %s", GETTEXT_PACKAGE,
72       LOCALEDIR);
73   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
74   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
75 #endif
76
77   /* musicbrainz tags */
78   gst_tag_register_static (GST_TAG_MUSICBRAINZ_TRACKID, GST_TAG_FLAG_META,
79       G_TYPE_STRING, _("track ID"), _("MusicBrainz track ID"), NULL);
80   gst_tag_register_static (GST_TAG_MUSICBRAINZ_ARTISTID, GST_TAG_FLAG_META,
81       G_TYPE_STRING, _("artist ID"), _("MusicBrainz artist ID"), NULL);
82   gst_tag_register_static (GST_TAG_MUSICBRAINZ_ALBUMID, GST_TAG_FLAG_META,
83       G_TYPE_STRING, _("album ID"), _("MusicBrainz album ID"), NULL);
84   gst_tag_register_static (GST_TAG_MUSICBRAINZ_ALBUMARTISTID, GST_TAG_FLAG_META,
85       G_TYPE_STRING,
86       _("album artist ID"), _("MusicBrainz album artist ID"), NULL);
87   gst_tag_register_static (GST_TAG_MUSICBRAINZ_TRMID, GST_TAG_FLAG_META,
88       G_TYPE_STRING, _("track TRM ID"), _("MusicBrainz TRM ID"), NULL);
89
90   /* CDDA tags */
91   gst_tag_register_static (GST_TAG_CDDA_CDDB_DISCID, GST_TAG_FLAG_META,
92       G_TYPE_STRING, "discid", "CDDB discid for metadata retrieval",
93       gst_tag_merge_use_first);
94
95   gst_tag_register_static (GST_TAG_CDDA_CDDB_DISCID_FULL, GST_TAG_FLAG_META,
96       G_TYPE_STRING, "discid full",
97       "CDDB discid for metadata retrieval (full)", gst_tag_merge_use_first);
98
99   gst_tag_register_static (GST_TAG_CDDA_MUSICBRAINZ_DISCID, GST_TAG_FLAG_META,
100       G_TYPE_STRING, "musicbrainz-discid",
101       "Musicbrainz discid for metadata retrieval", gst_tag_merge_use_first);
102
103   gst_tag_register_static (GST_TAG_CDDA_MUSICBRAINZ_DISCID_FULL,
104       GST_TAG_FLAG_META, G_TYPE_STRING, "musicbrainz-discid-full",
105       "Musicbrainz discid for metadata retrieval (full)",
106       gst_tag_merge_use_first);
107
108   /* photography tags */
109   gst_tag_register_static (GST_TAG_CAPTURING_SHUTTER_SPEED, GST_TAG_FLAG_META,
110       GST_TYPE_FRACTION, _("capturing shutter speed"),
111       _("Shutter speed used when capturing an image, in seconds"), NULL);
112
113   gst_tag_register_static (GST_TAG_CAPTURING_FOCAL_RATIO, GST_TAG_FLAG_META,
114       G_TYPE_DOUBLE, _("capturing focal ratio"),
115       _("Focal ratio (f-number) used when capturing the image"), NULL);
116
117   gst_tag_register_static (GST_TAG_CAPTURING_FOCAL_LENGTH, GST_TAG_FLAG_META,
118       G_TYPE_DOUBLE, _("capturing focal length"),
119       _("Focal length of the lens used capturing the image, in mm"), NULL);
120
121   gst_tag_register_static (GST_TAG_CAPTURING_DIGITAL_ZOOM_RATIO,
122       GST_TAG_FLAG_META, G_TYPE_DOUBLE, _("capturing digital zoom ratio"),
123       _("Digital zoom ratio used when capturing an image"), NULL);
124
125   gst_tag_register_static (GST_TAG_CAPTURING_ISO_SPEED, GST_TAG_FLAG_META,
126       G_TYPE_INT, _("capturing iso speed"),
127       _("The ISO speed used when capturing an image"), NULL);
128
129   gst_tag_register_static (GST_TAG_CAPTURING_EXPOSURE_PROGRAM,
130       GST_TAG_FLAG_META, G_TYPE_STRING, _("capturing exposure program"),
131       _("The exposure program used when capturing an image"), NULL);
132
133   gst_tag_register_static (GST_TAG_CAPTURING_EXPOSURE_MODE, GST_TAG_FLAG_META,
134       G_TYPE_STRING, _("capturing exposure mode"),
135       _("The exposure mode used when capturing an image"), NULL);
136
137   gst_tag_register_static (GST_TAG_CAPTURING_EXPOSURE_COMPENSATION,
138       GST_TAG_FLAG_META, G_TYPE_DOUBLE, _("capturing exposure compensation"),
139       _("The exposure compensation used when capturing an image"), NULL);
140
141   gst_tag_register_static (GST_TAG_CAPTURING_SCENE_CAPTURE_TYPE,
142       GST_TAG_FLAG_META, G_TYPE_STRING, _("capturing scene capture type"),
143       _("The scene capture mode used when capturing an image"), NULL);
144
145   gst_tag_register_static (GST_TAG_CAPTURING_GAIN_ADJUSTMENT, GST_TAG_FLAG_META,
146       G_TYPE_STRING, _("capturing gain adjustment"),
147       _("The overall gain adjustment applied on an image"), NULL);
148
149   gst_tag_register_static (GST_TAG_CAPTURING_WHITE_BALANCE, GST_TAG_FLAG_META,
150       G_TYPE_STRING, _("capturing white balance"),
151       _("The white balance mode set when capturing an image"), NULL);
152
153   gst_tag_register_static (GST_TAG_CAPTURING_CONTRAST, GST_TAG_FLAG_META,
154       G_TYPE_STRING, _("capturing contrast"),
155       _("The direction of contrast processing applied "
156           "when capturing an image"), NULL);
157
158   gst_tag_register_static (GST_TAG_CAPTURING_SATURATION, GST_TAG_FLAG_META,
159       G_TYPE_STRING, _("capturing saturation"),
160       _("The direction of saturation processing applied when "
161           "capturing an image"), NULL);
162
163   gst_tag_register_static (GST_TAG_CAPTURING_SHARPNESS, GST_TAG_FLAG_META,
164       G_TYPE_STRING, _("capturing sharpness"),
165       _("The direction of sharpness processing applied "
166           "when capturing an image"), NULL);
167
168   gst_tag_register_static (GST_TAG_CAPTURING_FLASH_FIRED, GST_TAG_FLAG_META,
169       G_TYPE_BOOLEAN, _("capturing flash fired"),
170       _("If the flash fired while capturing an image"), NULL);
171
172   gst_tag_register_static (GST_TAG_CAPTURING_FLASH_MODE, GST_TAG_FLAG_META,
173       G_TYPE_STRING, _("capturing flash mode"),
174       _("The selected flash mode while capturing an image"), NULL);
175
176   gst_tag_register_static (GST_TAG_CAPTURING_METERING_MODE, GST_TAG_FLAG_META,
177       G_TYPE_STRING, _("capturing metering mode"),
178       _("The metering mode used while determining exposure for capturing an"
179           " image"), NULL);
180
181   gst_tag_register_static (GST_TAG_CAPTURING_SOURCE, GST_TAG_FLAG_META,
182       G_TYPE_STRING, _("capturing source"),
183       _("The source or type of device used for the capture"), NULL);
184
185   gst_tag_register_static (GST_TAG_IMAGE_HORIZONTAL_PPI, GST_TAG_FLAG_META,
186       G_TYPE_DOUBLE, _("image horizontal ppi"),
187       _("Media (image/video) intended horizontal pixel density in ppi"), NULL);
188
189   gst_tag_register_static (GST_TAG_IMAGE_VERTICAL_PPI, GST_TAG_FLAG_META,
190       G_TYPE_DOUBLE, _("image vertical ppi"),
191       _("Media (image/video) intended vertical pixel density in ppi"), NULL);
192
193   gst_tag_register_static (GST_TAG_ID3V2_FRAME, GST_TAG_FLAG_META,
194       GST_TYPE_BUFFER, _("ID3v2 frame"), _("unparsed id3v2 tag frame"),
195       gst_tag_merge_use_first);
196
197   return NULL;
198 }
199
200 /* FIXME 0.11: rename this to gst_tag_init() or gst_tag_register_tags() or
201  * even better: make tags auto-register themselves, either by defining them
202  * to a wrapper func that does the initing, or by adding tag factories so
203  * that the core can load+register tags automatically when needed. */
204
205 /**
206  * gst_tag_register_musicbrainz_tags:
207  *
208  * Registers additional musicbrainz-specific tags with the GStreamer tag
209  * system. Plugins and applications that use these tags should call this
210  * function before using them. Can be called multiple times.
211  */
212 void
213 gst_tag_register_musicbrainz_tags (void)
214 {
215   static GOnce mb_once = G_ONCE_INIT;
216
217   g_once (&mb_once, gst_tag_register_tags_internal, NULL);
218 }
219
220 static void
221 register_tag_image_type_enum (GType * id)
222 {
223   static const GEnumValue image_types[] = {
224     {GST_TAG_IMAGE_TYPE_NONE, "GST_TAG_IMAGE_TYPE_NONE", "none"},
225     {GST_TAG_IMAGE_TYPE_UNDEFINED, "GST_TAG_IMAGE_TYPE_UNDEFINED", "undefined"},
226     {GST_TAG_IMAGE_TYPE_FRONT_COVER, "GST_TAG_IMAGE_TYPE_FRONT_COVER",
227         "front-cover"},
228     {GST_TAG_IMAGE_TYPE_BACK_COVER, "GST_TAG_IMAGE_TYPE_BACK_COVER",
229         "back-cover"},
230     {GST_TAG_IMAGE_TYPE_LEAFLET_PAGE, "GST_TAG_IMAGE_TYPE_LEAFLET_PAGE",
231         "leaflet-page"},
232     {GST_TAG_IMAGE_TYPE_MEDIUM, "GST_TAG_IMAGE_TYPE_MEDIUM", "medium"},
233     {GST_TAG_IMAGE_TYPE_LEAD_ARTIST, "GST_TAG_IMAGE_TYPE_LEAD_ARTIST",
234         "lead-artist"},
235     {GST_TAG_IMAGE_TYPE_ARTIST, "GST_TAG_IMAGE_TYPE_ARTIST", "artist"},
236     {GST_TAG_IMAGE_TYPE_CONDUCTOR, "GST_TAG_IMAGE_TYPE_CONDUCTOR", "conductor"},
237     {GST_TAG_IMAGE_TYPE_BAND_ORCHESTRA, "GST_TAG_IMAGE_TYPE_BAND_ORCHESTRA",
238         "band-orchestra"},
239     {GST_TAG_IMAGE_TYPE_COMPOSER, "GST_TAG_IMAGE_TYPE_COMPOSER", "composer"},
240     {GST_TAG_IMAGE_TYPE_LYRICIST, "GST_TAG_IMAGE_TYPE_LYRICIST", "lyricist"},
241     {GST_TAG_IMAGE_TYPE_RECORDING_LOCATION,
242           "GST_TAG_IMAGE_TYPE_RECORDING_LOCATION",
243         "recording-location"},
244     {GST_TAG_IMAGE_TYPE_DURING_RECORDING, "GST_TAG_IMAGE_TYPE_DURING_RECORDING",
245         "during-recording"},
246     {GST_TAG_IMAGE_TYPE_DURING_PERFORMANCE,
247           "GST_TAG_IMAGE_TYPE_DURING_PERFORMANCE",
248         "during-performance"},
249     {GST_TAG_IMAGE_TYPE_VIDEO_CAPTURE, "GST_TAG_IMAGE_TYPE_VIDEO_CAPTURE",
250         "video-capture"},
251     {GST_TAG_IMAGE_TYPE_FISH, "GST_TAG_IMAGE_TYPE_FISH", "fish"},
252     {GST_TAG_IMAGE_TYPE_ILLUSTRATION, "GST_TAG_IMAGE_TYPE_ILLUSTRATION",
253         "illustration"},
254     {GST_TAG_IMAGE_TYPE_BAND_ARTIST_LOGO, "GST_TAG_IMAGE_TYPE_BAND_ARTIST_LOGO",
255         "artist-logo"},
256     {GST_TAG_IMAGE_TYPE_PUBLISHER_STUDIO_LOGO,
257           "GST_TAG_IMAGE_TYPE_PUBLISHER_STUDIO_LOGO",
258         "publisher-studio-logo"},
259     {0, NULL, NULL}
260   };
261
262   *id = g_enum_register_static ("GstTagImageType", image_types);
263
264   /* work around thread-safety issue with class creation in GLib */
265   g_type_class_ref (*id);
266 }
267
268 GType
269 gst_tag_image_type_get_type (void)
270 {
271   static GType id;
272
273   static GOnce once = G_ONCE_INIT;
274
275   g_once (&once, (GThreadFunc) register_tag_image_type_enum, &id);
276   return id;
277 }
278
279 static inline gboolean
280 gst_tag_image_type_is_valid (GstTagImageType type)
281 {
282   GEnumClass *klass;
283
284   gboolean res;
285
286   klass = g_type_class_ref (gst_tag_image_type_get_type ());
287   res = (g_enum_get_value (klass, type) != NULL);
288   g_type_class_unref (klass);
289
290   return res;
291 }
292
293 /**
294  * gst_tag_parse_extended_comment:
295  * @ext_comment: an extended comment string, see #GST_TAG_EXTENDED_COMMENT
296  * @key: return location for the comment description key, or NULL
297  * @lang: return location for the comment ISO-639 language code, or NULL
298  * @value: return location for the actual comment string, or NULL
299  * @fail_if_no_key: whether to fail if strings are not in key=value form
300  *
301  * Convenience function to parse a GST_TAG_EXTENDED_COMMENT string and
302  * separate it into its components.
303  *
304  * If successful, @key, @lang and/or @value will be set to newly allocated
305  * strings that you need to free with g_free() when done. @key and @lang
306  * may also be set to NULL by this function if there is no key or no language
307  * code in the extended comment string.
308  *
309  * Returns: TRUE if the string could be parsed, otherwise FALSE
310  */
311 gboolean
312 gst_tag_parse_extended_comment (const gchar * ext_comment, gchar ** key,
313     gchar ** lang, gchar ** value, gboolean fail_if_no_key)
314 {
315   const gchar *div, *bop, *bcl;
316
317   g_return_val_if_fail (ext_comment != NULL, FALSE);
318   g_return_val_if_fail (g_utf8_validate (ext_comment, -1, NULL), FALSE);
319
320   if (key)
321     *key = NULL;
322   if (lang)
323     *lang = NULL;
324
325   div = strchr (ext_comment, '=');
326   bop = strchr (ext_comment, '[');
327   bcl = strchr (ext_comment, ']');
328
329   if (div == NULL) {
330     if (fail_if_no_key)
331       return FALSE;
332     if (value)
333       *value = g_strdup (ext_comment);
334     return TRUE;
335   }
336
337   if (bop != NULL && bop < div) {
338     if (bcl < bop || bcl > div)
339       return FALSE;
340     if (key)
341       *key = g_strndup (ext_comment, bop - ext_comment);
342     if (lang)
343       *lang = g_strndup (bop + 1, bcl - bop - 1);
344   } else {
345     if (key)
346       *key = g_strndup (ext_comment, div - ext_comment);
347   }
348
349   if (value)
350     *value = g_strdup (div + 1);
351
352   return TRUE;
353 }
354
355 /**
356  * gst_tag_freeform_string_to_utf8:
357  * @data: string data
358  * @size: length of string data, or -1 if the string is NUL-terminated
359  * @env_vars: a NULL-terminated string array of environment variable names,
360  *            or NULL
361  *
362  * Convenience function to read a string with unknown character encoding. If
363  * the string is already in UTF-8 encoding, it will be returned right away.
364  * If not it tries to detect byte-order-mark for UTF-16/32 cases and use that.
365  * Otherwise, the environment will be searched for a number of environment
366  * variables (whose names are specified in the NULL-terminated string array
367  * @env_vars) containing a list of character encodings to try/use. If none
368  * are specified, the current locale will be tried. If that also doesn't work,
369  * WINDOWS-1252/ISO-8859-1 is assumed (which will almost always succeed).
370  *
371  * Returns: a newly-allocated string in UTF-8 encoding, or NULL
372  */
373 gchar *
374 gst_tag_freeform_string_to_utf8 (const gchar * data, gint size,
375     const gchar ** env_vars)
376 {
377   const gchar *cur_loc = NULL;
378
379   gsize bytes_read;
380
381   gchar *utf8 = NULL;
382
383   g_return_val_if_fail (data != NULL, NULL);
384
385   if (size < 0)
386     size = strlen (data);
387
388   /* chop off trailing string terminators to make sure utf8_validate doesn't
389    * get to see them (since that would make the utf8 check fail) */
390   while (size > 0 && data[size - 1] == '\0')
391     --size;
392
393   /* Should we try the charsets specified
394    * via environment variables FIRST ? */
395   if (g_utf8_validate (data, size, NULL)) {
396     utf8 = g_strndup (data, size);
397     GST_LOG ("String '%s' is valid UTF-8 already", utf8);
398     goto beach;
399   }
400
401   /* check for and use byte-order-mark for UTF-16/32 cases */
402   if (size >= 2) {
403     const gchar *c = NULL;
404     gint prefix, ssize;
405
406     if (size >= 4) {
407       prefix = 4;
408       ssize = GST_ROUND_DOWN_4 (size - 4);
409       switch (GST_READ_UINT32_BE (data)) {
410         case 0x0000FEFF:
411           c = "UTF-32BE";
412           break;
413         case 0xFFFE0000:
414           c = "UTF-32LE";
415           break;
416         default:
417           break;
418       }
419     }
420     if (!c) {
421       prefix = 2;
422       ssize = GST_ROUND_DOWN_2 (size - 2);
423       switch (GST_READ_UINT16_BE (data)) {
424         case 0xFEFF:
425           c = "UTF-16BE";
426           break;
427         case 0xFFFE:
428           c = "UTF-16LE";
429           break;
430         default:
431           break;
432       }
433     }
434     if (c) {
435       GST_LOG ("Trying to convert freeform string to UTF-8 from '%s'", c);
436       if ((utf8 =
437               g_convert (data + prefix, ssize, "UTF-8", c, &bytes_read, NULL,
438                   NULL))) {
439         if (bytes_read == ssize)
440           goto beach;
441         g_free (utf8);
442         utf8 = NULL;
443       }
444     }
445   }
446
447   while (env_vars && *env_vars != NULL) {
448     const gchar *env = NULL;
449
450     /* Try charsets specified via the environment */
451     env = g_getenv (*env_vars);
452     if (env != NULL && *env != '\0') {
453       gchar **c, **csets;
454
455       csets = g_strsplit (env, G_SEARCHPATH_SEPARATOR_S, -1);
456
457       for (c = csets; c && *c; ++c) {
458         GST_LOG ("Trying to convert freeform string to UTF-8 from '%s'", *c);
459         if ((utf8 =
460                 g_convert (data, size, "UTF-8", *c, &bytes_read, NULL, NULL))) {
461           if (bytes_read == size) {
462             g_strfreev (csets);
463             goto beach;
464           }
465           g_free (utf8);
466           utf8 = NULL;
467         }
468       }
469
470       g_strfreev (csets);
471     }
472     ++env_vars;
473   }
474
475   /* Try current locale (if not UTF-8) */
476   if (!g_get_charset (&cur_loc)) {
477     GST_LOG ("Trying to convert freeform string using locale ('%s')", cur_loc);
478     if ((utf8 = g_locale_to_utf8 (data, size, &bytes_read, NULL, NULL))) {
479       if (bytes_read == size) {
480         goto beach;
481       }
482       g_free (utf8);
483       utf8 = NULL;
484     }
485   }
486
487   /* Try Windows-1252 (which is a superset of ISO 8859-1 that uses a control
488    * character range in ISO 8859-1 for more printable characters) */
489   {
490     GError *err = NULL;
491
492     GST_LOG ("Trying to convert freeform string using Windows-1252/ISO-8859-1 "
493         "fallback");
494     utf8 = g_convert (data, size, "UTF-8", "WINDOWS-1252", &bytes_read, NULL,
495         &err);
496     if (err != NULL) {
497       /* fallback in case iconv implementation doesn't support windows-1252
498        * for some reason */
499       if (err->code == G_CONVERT_ERROR_NO_CONVERSION) {
500         g_free (utf8);
501         utf8 = g_convert (data, size, "UTF-8", "ISO-8859-1", &bytes_read,
502             NULL, NULL);
503       }
504       g_error_free (err);
505     }
506
507     if (utf8 != NULL && bytes_read == size)
508       goto beach;
509   }
510
511   g_free (utf8);
512   return NULL;
513
514 beach:
515
516   g_strchomp (utf8);
517   if (utf8 && utf8[0] != '\0') {
518     GST_LOG ("Returning '%s'", utf8);
519     return utf8;
520   }
521
522   g_free (utf8);
523   return NULL;
524 }
525
526 /**
527  * gst_tag_image_data_to_image_sample:
528  * @image_data: the (encoded) image
529  * @image_data_len: the length of the encoded image data at @image_data
530  * @image_type: type of the image, or #GST_TAG_IMAGE_TYPE_UNDEFINED. Pass
531  *     #GST_TAG_IMAGE_TYPE_NONE if no image type should be set at all (e.g.
532  *     for preview images)
533  *
534  * Helper function for tag-reading plugins to create a #GstSample suitable to
535  * add to a #GstTagList as an image tag (such as #GST_TAG_IMAGE or
536  * #GST_TAG_PREVIEW_IMAGE) from the encoded image data and an (optional) image
537  * type.
538  *
539  * Background: cover art and other images in tags are usually stored as a
540  * blob of binary image data, often accompanied by a MIME type or some other
541  * content type string (e.g. 'png', 'jpeg', 'jpg'). Sometimes there is also an
542  * 'image type' to indicate what kind of image this is (e.g. front cover,
543  * back cover, artist, etc.). The image data may also be an URI to the image
544  * rather than the image itself.
545  *
546  * In GStreamer, image tags are #GstSample<!-- -->s containing the raw image
547  * data, with the sample caps describing the content type of the image
548  * (e.g. image/jpeg, image/png, text/uri-list). The sample info may contain
549  * an additional 'image-type' field of #GST_TYPE_TAG_IMAGE_TYPE to describe
550  * the type of image (front cover, back cover etc.). #GST_TAG_PREVIEW_IMAGE
551  * tags should not carry an image type, their type is already indicated via
552  * the special tag name.
553  *
554  * This function will do various checks and typefind the encoded image
555  * data (we can't trust the declared mime type).
556  *
557  * Returns: a newly-allocated image sample for use in tag lists, or NULL
558  */
559 GstSample *
560 gst_tag_image_data_to_image_sample (const guint8 * image_data,
561     guint image_data_len, GstTagImageType image_type)
562 {
563   const gchar *name;
564   GstBuffer *image;
565   GstSample *sample;
566   GstCaps *caps;
567   GstMapInfo info;
568   GstStructure *image_info = NULL;
569
570   g_return_val_if_fail (image_data != NULL, NULL);
571   g_return_val_if_fail (image_data_len > 0, NULL);
572   g_return_val_if_fail (gst_tag_image_type_is_valid (image_type), NULL);
573
574   GST_DEBUG ("image data len: %u bytes", image_data_len);
575
576   /* allocate space for a NUL terminator for an uri too */
577   image = gst_buffer_new_and_alloc (image_data_len + 1);
578   if (image == NULL)
579     goto alloc_failed;
580
581   gst_buffer_map (image, &info, GST_MAP_WRITE);
582   memcpy (info.data, image_data, image_data_len);
583   info.data[image_data_len] = '\0';
584   gst_buffer_unmap (image, &info);
585
586   /* Find GStreamer media type, can't trust declared type */
587   caps = gst_type_find_helper_for_buffer (NULL, image, NULL);
588
589   if (caps == NULL)
590     goto no_type;
591
592   GST_DEBUG ("Found GStreamer media type: %" GST_PTR_FORMAT, caps);
593
594   /* sanity check: make sure typefound/declared caps are either URI or image */
595   name = gst_structure_get_name (gst_caps_get_structure (caps, 0));
596
597   if (!g_str_has_prefix (name, "image/") &&
598       !g_str_has_prefix (name, "video/") &&
599       !g_str_equal (name, "text/uri-list")) {
600     GST_DEBUG ("Unexpected image type '%s', ignoring image frame", name);
601     goto error;
602   }
603
604   /* Decrease size by 1 if we don't have an URI list
605    * to keep the original size of the image
606    */
607   if (!g_str_equal (name, "text/uri-list"))
608     gst_buffer_set_size (image, image_data_len);
609
610   if (image_type != GST_TAG_IMAGE_TYPE_NONE) {
611     GST_LOG ("Setting image type: %d", image_type);
612     image_info = gst_structure_new ("GstTagImageInfo",
613         "image-type", GST_TYPE_TAG_IMAGE_TYPE, image_type, NULL);
614   }
615   sample = gst_sample_new (image, caps, NULL, image_info);
616   gst_buffer_unref (image);
617   gst_caps_unref (caps);
618
619   return sample;
620
621 /* ERRORS */
622 no_type:
623   {
624     GST_DEBUG ("Could not determine GStreamer media type, ignoring image");
625     /* fall through */
626   }
627 error:
628   {
629     if (image)
630       gst_buffer_unref (image);
631     if (caps)
632       gst_caps_unref (caps);
633     return NULL;
634   }
635 alloc_failed:
636   {
637     GST_WARNING ("failed to allocate buffer of %d for image", image_data_len);
638     gst_buffer_unref (image);
639     return NULL;
640   }
641
642 }