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