tags: try ISO-8859-1 as second fallback in case WINDOWS-1252 is not supported
[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   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
55 #endif
56
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,
65       G_TYPE_STRING,
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);
69
70   /* CDDA tags */
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);
74
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);
78
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);
82
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);
87
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);
92
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);
96
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);
100
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);
104
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);
108
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);
112
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);
116
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);
120
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);
124
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);
128
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);
133
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);
138
139   gst_tag_register (GST_TAG_CAPTURING_FLASH_FIRED, GST_TAG_FLAG_META,
140       G_TYPE_BOOLEAN, _("capturing flash fired"),
141       _("If the flash fired while capturing and image"), NULL);
142
143   gst_tag_register (GST_TAG_CAPTURING_FLASH_MODE, GST_TAG_FLAG_META,
144       G_TYPE_STRING, _("capturing flash mode"),
145       _("The selected flash mode while capturing and image"), NULL);
146
147   gst_tag_register (GST_TAG_IMAGE_HORIZONTAL_PPI, GST_TAG_FLAG_META,
148       G_TYPE_DOUBLE, _("image horizontal ppi"),
149       _("Media (image/video) intended horizontal pixel density in ppi"), NULL);
150
151   gst_tag_register (GST_TAG_IMAGE_VERTICAL_PPI, GST_TAG_FLAG_META,
152       G_TYPE_DOUBLE, _("image vertical ppi"),
153       _("Media (image/video) intended vertical pixel density in ppi"), NULL);
154
155   return NULL;
156 }
157
158 /* FIXME 0.11: rename this to gst_tag_init() or gst_tag_register_tags() */
159 /**
160  * gst_tag_register_musicbrainz_tags
161  *
162  * Registers additional musicbrainz-specific tags with the GStreamer tag
163  * system. Plugins and applications that use these tags should call this
164  * function before using them. Can be called multiple times.
165  */
166 void
167 gst_tag_register_musicbrainz_tags (void)
168 {
169   static GOnce mb_once = G_ONCE_INIT;
170
171   g_once (&mb_once, gst_tag_register_tags_internal, NULL);
172 }
173
174 static void
175 register_tag_image_type_enum (GType * id)
176 {
177   static const GEnumValue image_types[] = {
178     {GST_TAG_IMAGE_TYPE_NONE, "GST_TAG_IMAGE_TYPE_NONE", "none"},
179     {GST_TAG_IMAGE_TYPE_UNDEFINED, "GST_TAG_IMAGE_TYPE_UNDEFINED", "undefined"},
180     {GST_TAG_IMAGE_TYPE_FRONT_COVER, "GST_TAG_IMAGE_TYPE_FRONT_COVER",
181         "front-cover"},
182     {GST_TAG_IMAGE_TYPE_BACK_COVER, "GST_TAG_IMAGE_TYPE_BACK_COVER",
183         "back-cover"},
184     {GST_TAG_IMAGE_TYPE_LEAFLET_PAGE, "GST_TAG_IMAGE_TYPE_LEAFLET_PAGE",
185         "leaflet-page"},
186     {GST_TAG_IMAGE_TYPE_MEDIUM, "GST_TAG_IMAGE_TYPE_MEDIUM", "medium"},
187     {GST_TAG_IMAGE_TYPE_LEAD_ARTIST, "GST_TAG_IMAGE_TYPE_LEAD_ARTIST",
188         "lead-artist"},
189     {GST_TAG_IMAGE_TYPE_ARTIST, "GST_TAG_IMAGE_TYPE_ARTIST", "artist"},
190     {GST_TAG_IMAGE_TYPE_CONDUCTOR, "GST_TAG_IMAGE_TYPE_CONDUCTOR", "conductor"},
191     {GST_TAG_IMAGE_TYPE_BAND_ORCHESTRA, "GST_TAG_IMAGE_TYPE_BAND_ORCHESTRA",
192         "band-orchestra"},
193     {GST_TAG_IMAGE_TYPE_COMPOSER, "GST_TAG_IMAGE_TYPE_COMPOSER", "composer"},
194     {GST_TAG_IMAGE_TYPE_LYRICIST, "GST_TAG_IMAGE_TYPE_LYRICIST", "lyricist"},
195     {GST_TAG_IMAGE_TYPE_RECORDING_LOCATION,
196           "GST_TAG_IMAGE_TYPE_RECORDING_LOCATION",
197         "recording-location"},
198     {GST_TAG_IMAGE_TYPE_DURING_RECORDING, "GST_TAG_IMAGE_TYPE_DURING_RECORDING",
199         "during-recording"},
200     {GST_TAG_IMAGE_TYPE_DURING_PERFORMANCE,
201           "GST_TAG_IMAGE_TYPE_DURING_PERFORMANCE",
202         "during-performance"},
203     {GST_TAG_IMAGE_TYPE_VIDEO_CAPTURE, "GST_TAG_IMAGE_TYPE_VIDEO_CAPTURE",
204         "video-capture"},
205     {GST_TAG_IMAGE_TYPE_FISH, "GST_TAG_IMAGE_TYPE_FISH", "fish"},
206     {GST_TAG_IMAGE_TYPE_ILLUSTRATION, "GST_TAG_IMAGE_TYPE_ILLUSTRATION",
207         "illustration"},
208     {GST_TAG_IMAGE_TYPE_BAND_ARTIST_LOGO, "GST_TAG_IMAGE_TYPE_BAND_ARTIST_LOGO",
209         "artist-logo"},
210     {GST_TAG_IMAGE_TYPE_PUBLISHER_STUDIO_LOGO,
211           "GST_TAG_IMAGE_TYPE_PUBLISHER_STUDIO_LOGO",
212         "publisher-studio-logo"},
213     {0, NULL, NULL}
214   };
215
216   *id = g_enum_register_static ("GstTagImageType", image_types);
217
218   /* work around thread-safety issue with class creation in GLib */
219   g_type_class_ref (*id);
220 }
221
222 GType
223 gst_tag_image_type_get_type (void)
224 {
225   static GType id;
226
227   static GOnce once = G_ONCE_INIT;
228
229   g_once (&once, (GThreadFunc) register_tag_image_type_enum, &id);
230   return id;
231 }
232
233 static inline gboolean
234 gst_tag_image_type_is_valid (GstTagImageType type)
235 {
236   GEnumClass *klass;
237
238   gboolean res;
239
240   klass = g_type_class_ref (gst_tag_image_type_get_type ());
241   res = (g_enum_get_value (klass, type) != NULL);
242   g_type_class_unref (klass);
243
244   return res;
245 }
246
247 /**
248  * gst_tag_parse_extended_comment:
249  * @ext_comment: an extended comment string, see #GST_TAG_EXTENDED_COMMENT
250  * @key: return location for the comment description key, or NULL
251  * @lang: return location for the comment ISO-639 language code, or NULL
252  * @value: return location for the actual comment string, or NULL
253  * @fail_if_no_key: whether to fail if strings are not in key=value form
254  *
255  * Convenience function to parse a GST_TAG_EXTENDED_COMMENT string and
256  * separate it into its components.
257  *
258  * If successful, @key, @lang and/or @value will be set to newly allocated
259  * strings that you need to free with g_free() when done. @key and @lang
260  * may also be set to NULL by this function if there is no key or no language
261  * code in the extended comment string.
262  *
263  * Returns: TRUE if the string could be parsed, otherwise FALSE
264  *
265  * Since: 0.10.10
266  */
267 gboolean
268 gst_tag_parse_extended_comment (const gchar * ext_comment, gchar ** key,
269     gchar ** lang, gchar ** value, gboolean fail_if_no_key)
270 {
271   const gchar *div, *bop, *bcl;
272
273   g_return_val_if_fail (ext_comment != NULL, FALSE);
274   g_return_val_if_fail (g_utf8_validate (ext_comment, -1, NULL), FALSE);
275
276   if (key)
277     *key = NULL;
278   if (lang)
279     *lang = NULL;
280
281   div = strchr (ext_comment, '=');
282   bop = strchr (ext_comment, '[');
283   bcl = strchr (ext_comment, ']');
284
285   if (div == NULL) {
286     if (fail_if_no_key)
287       return FALSE;
288     if (value)
289       *value = g_strdup (ext_comment);
290     return TRUE;
291   }
292
293   if (bop != NULL && bop < div) {
294     if (bcl < bop || bcl > div)
295       return FALSE;
296     if (key)
297       *key = g_strndup (ext_comment, bop - ext_comment);
298     if (lang)
299       *lang = g_strndup (bop + 1, bcl - bop - 1);
300   } else {
301     if (key)
302       *key = g_strndup (ext_comment, div - ext_comment);
303   }
304
305   if (value)
306     *value = g_strdup (div + 1);
307
308   return TRUE;
309 }
310
311 /**
312  * gst_tag_freeform_string_to_utf8:
313  * @data: string data
314  * @size: length of string data, or -1 if the string is NUL-terminated
315  * @env_vars: a NULL-terminated string array of environment variable names,
316  *            or NULL
317  *
318  * Convenience function to read a string with unknown character encoding. If
319  * the string is already in UTF-8 encoding, it will be returned right away.
320  * If not it tries to detect byte-order-mark for UTF-16/32 cases and use that.
321  * Otherwise, the environment will be searched for a number of environment
322  * variables (whose names are specified in the NULL-terminated string array
323  * @env_vars) containing a list of character encodings to try/use. If none
324  * are specified, the current locale will be tried. If that also doesn't work,
325  * WINDOWS-1252/ISO-8859-1 is assumed (which will almost always succeed).
326  *
327  * Returns: a newly-allocated string in UTF-8 encoding, or NULL
328  *
329  * Since: 0.10.13
330  */
331 gchar *
332 gst_tag_freeform_string_to_utf8 (const gchar * data, gint size,
333     const gchar ** env_vars)
334 {
335   const gchar *cur_loc = NULL;
336
337   gsize bytes_read;
338
339   gchar *utf8 = NULL;
340
341   g_return_val_if_fail (data != NULL, NULL);
342
343   if (size < 0)
344     size = strlen (data);
345
346   /* chop off trailing string terminators to make sure utf8_validate doesn't
347    * get to see them (since that would make the utf8 check fail) */
348   while (size > 0 && data[size - 1] == '\0')
349     --size;
350
351   /* Should we try the charsets specified
352    * via environment variables FIRST ? */
353   if (g_utf8_validate (data, size, NULL)) {
354     utf8 = g_strndup (data, size);
355     GST_LOG ("String '%s' is valid UTF-8 already", utf8);
356     goto beach;
357   }
358
359   /* check for and use byte-order-mark for UTF-16/32 cases */
360   if (size >= 2) {
361     const gchar *c = NULL;
362     gint prefix, ssize;
363
364     if (size >= 4) {
365       prefix = 4;
366       ssize = GST_ROUND_DOWN_4 (size - 4);
367       switch (GST_READ_UINT32_BE (data)) {
368         case 0x0000FEFF:
369           c = "UTF-32BE";
370           break;
371         case 0xFFFE0000:
372           c = "UTF-32LE";
373           break;
374         default:
375           break;
376       }
377     }
378     if (!c) {
379       prefix = 2;
380       ssize = GST_ROUND_DOWN_2 (size - 2);
381       switch (GST_READ_UINT16_BE (data)) {
382         case 0xFEFF:
383           c = "UTF-16BE";
384           break;
385         case 0xFFFE:
386           c = "UTF-16LE";
387           break;
388         default:
389           break;
390       }
391     }
392     if (c) {
393       GST_LOG ("Trying to convert freeform string to UTF-8 from '%s'", c);
394       if ((utf8 =
395               g_convert (data + prefix, ssize, "UTF-8", c, &bytes_read, NULL,
396                   NULL))) {
397         if (bytes_read == ssize)
398           goto beach;
399         g_free (utf8);
400         utf8 = NULL;
401       }
402     }
403   }
404
405   while (env_vars && *env_vars != NULL) {
406     const gchar *env = NULL;
407
408     /* Try charsets specified via the environment */
409     env = g_getenv (*env_vars);
410     if (env != NULL && *env != '\0') {
411       gchar **c, **csets;
412
413       csets = g_strsplit (env, G_SEARCHPATH_SEPARATOR_S, -1);
414
415       for (c = csets; c && *c; ++c) {
416         GST_LOG ("Trying to convert freeform string to UTF-8 from '%s'", *c);
417         if ((utf8 =
418                 g_convert (data, size, "UTF-8", *c, &bytes_read, NULL, NULL))) {
419           if (bytes_read == size) {
420             g_strfreev (csets);
421             goto beach;
422           }
423           g_free (utf8);
424           utf8 = NULL;
425         }
426       }
427
428       g_strfreev (csets);
429     }
430     ++env_vars;
431   }
432
433   /* Try current locale (if not UTF-8) */
434   if (!g_get_charset (&cur_loc)) {
435     GST_LOG ("Trying to convert freeform string using locale ('%s')", cur_loc);
436     if ((utf8 = g_locale_to_utf8 (data, size, &bytes_read, NULL, NULL))) {
437       if (bytes_read == size) {
438         goto beach;
439       }
440       g_free (utf8);
441       utf8 = NULL;
442     }
443   }
444
445   /* Try Windows-1252 (which is a superset of ISO 8859-1 that uses a control
446    * character range in ISO 8859-1 for more printable characters) */
447   {
448     GError *err = NULL;
449
450     GST_LOG ("Trying to convert freeform string using Windows-1252/ISO-8859-1 "
451         "fallback");
452     utf8 = g_convert (data, size, "UTF-8", "WINDOWS-1252", &bytes_read, NULL,
453         &err);
454     if (err != NULL) {
455       /* fallback in case iconv implementation doesn't support windows-1252
456        * for some reason */
457       if (err->code == G_CONVERT_ERROR_NO_CONVERSION) {
458         utf8 = g_convert (data, size, "UTF-8", "ISO-8859-1", &bytes_read,
459             NULL, NULL);
460       }
461       g_error_free (err);
462     }
463
464     if (utf8 != NULL && bytes_read == size)
465       goto beach;
466   }
467
468   g_free (utf8);
469   return NULL;
470
471 beach:
472
473   g_strchomp (utf8);
474   if (utf8 && utf8[0] != '\0') {
475     GST_LOG ("Returning '%s'", utf8);
476     return utf8;
477   }
478
479   g_free (utf8);
480   return NULL;
481 }
482
483 /**
484  * gst_tag_image_data_to_image_buffer:
485  * @image_data: the (encoded) image
486  * @image_data_len: the length of the encoded image data at @image_data
487  * @image_type: type of the image, or #GST_TAG_IMAGE_TYPE_UNDEFINED. Pass
488  *     #GST_TAG_IMAGE_TYPE_NONE if no image type should be set at all (e.g.
489  *     for preview images)
490  *
491  * Helper function for tag-reading plugins to create a #GstBuffer suitable to
492  * add to a #GstTagList as an image tag (such as #GST_TAG_IMAGE or
493  * #GST_TAG_PREVIEW_IMAGE) from the encoded image data and an (optional) image
494  * type.
495  *
496  * Background: cover art and other images in tags are usually stored as a
497  * blob of binary image data, often accompanied by a MIME type or some other
498  * content type string (e.g. 'png', 'jpeg', 'jpg'). Sometimes there is also an
499  * 'image type' to indicate what kind of image this is (e.g. front cover,
500  * back cover, artist, etc.). The image data may also be an URI to the image
501  * rather than the image itself.
502  *
503  * In GStreamer, image tags are #GstBuffer<!-- -->s containing the raw image
504  * data, with the buffer caps describing the content type of the image
505  * (e.g. image/jpeg, image/png, text/uri-list). The buffer caps may contain
506  * an additional 'image-type' field of #GST_TYPE_TAG_IMAGE_TYPE to describe
507  * the type of image (front cover, back cover etc.). #GST_TAG_PREVIEW_IMAGE
508  * tags should not carry an image type, their type is already indicated via
509  * the special tag name.
510  *
511  * This function will do various checks and typefind the encoded image
512  * data (we can't trust the declared mime type).
513  *
514  * Returns: a newly-allocated image buffer for use in tag lists, or NULL
515  *
516  * Since: 0.10.20
517  */
518 GstBuffer *
519 gst_tag_image_data_to_image_buffer (const guint8 * image_data,
520     guint image_data_len, GstTagImageType image_type)
521 {
522   const gchar *name;
523
524   GstBuffer *image;
525
526   GstCaps *caps;
527
528   g_return_val_if_fail (image_data != NULL, NULL);
529   g_return_val_if_fail (image_data_len > 0, NULL);
530   g_return_val_if_fail (gst_tag_image_type_is_valid (image_type), NULL);
531
532   GST_DEBUG ("image data len: %u bytes", image_data_len);
533
534   /* allocate space for a NUL terminator for an uri too */
535   image = gst_buffer_try_new_and_alloc (image_data_len + 1);
536   if (image == NULL) {
537     GST_WARNING ("failed to allocate buffer of %d for image", image_data_len);
538     return NULL;
539   }
540
541   memcpy (GST_BUFFER_DATA (image), image_data, image_data_len);
542   GST_BUFFER_DATA (image)[image_data_len] = '\0';
543
544   /* Find GStreamer media type, can't trust declared type */
545   caps = gst_type_find_helper_for_buffer (NULL, image, NULL);
546
547   if (caps == NULL)
548     goto no_type;
549
550   GST_DEBUG ("Found GStreamer media type: %" GST_PTR_FORMAT, caps);
551
552   /* sanity check: make sure typefound/declared caps are either URI or image */
553   name = gst_structure_get_name (gst_caps_get_structure (caps, 0));
554
555   if (!g_str_has_prefix (name, "image/") &&
556       !g_str_has_prefix (name, "video/") &&
557       !g_str_equal (name, "text/uri-list")) {
558     GST_DEBUG ("Unexpected image type '%s', ignoring image frame", name);
559     goto error;
560   }
561
562   /* Decrease size by 1 if we don't have an URI list
563    * to keep the original size of the image
564    */
565   if (!g_str_equal (name, "text/uri-list"))
566     GST_BUFFER_SIZE (image) = image_data_len;
567
568   if (image_type != GST_TAG_IMAGE_TYPE_NONE) {
569     GST_LOG ("Setting image type: %d", image_type);
570     caps = gst_caps_make_writable (caps);
571     gst_caps_set_simple (caps, "image-type", GST_TYPE_TAG_IMAGE_TYPE,
572         image_type, NULL);
573   }
574
575   gst_buffer_set_caps (image, caps);
576   gst_caps_unref (caps);
577   return image;
578
579 /* ERRORS */
580 no_type:
581   {
582     GST_DEBUG ("Could not determine GStreamer media type, ignoring image");
583     /* fall through */
584   }
585 error:
586   {
587     if (image)
588       gst_buffer_unref (image);
589     if (caps)
590       gst_caps_unref (caps);
591     return NULL;
592   }
593 }