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