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