1 /* GStreamer
2 * Copyright (C) 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
3 *
4 * gstvorbistag.c: library for reading / modifying vorbis tags
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22 /**
23 * SECTION:gsttagvorbis
24 * @title: GstVorbisTag
25 * @short_description: tag mappings and support functions for plugins
26 * dealing with vorbiscomments
27 * @see_also: #GstTagList
28 *
29 * Contains various utility functions for plugins to parse or create
30 * vorbiscomments and map them to and from #GstTagList<!-- -->s.
31 *
32 */
33
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
37 #include <gst/gsttagsetter.h>
38 #include <gst/base/gstbytereader.h>
39 #include <gst/base/gstbytewriter.h>
40 #include "gsttageditingprivate.h"
41 #include <stdlib.h>
42 #include <string.h>
43
44 /*
45 * see http://xiph.org/ogg/vorbis/doc/v-comment.html
46 */
47 static const GstTagEntryMatch tag_matches[] = {
48 {GST_TAG_TITLE, "TITLE"},
49 {GST_TAG_VERSION, "VERSION"},
50 {GST_TAG_ALBUM, "ALBUM"},
51 {GST_TAG_TRACK_NUMBER, "TRACKNUMBER"},
52 {GST_TAG_ALBUM_VOLUME_NUMBER, "DISCNUMBER"},
53 {GST_TAG_TRACK_COUNT, "TRACKTOTAL"},
54 {GST_TAG_TRACK_COUNT, "TOTALTRACKS"}, /* old / non-standard */
55 {GST_TAG_ALBUM_VOLUME_COUNT, "DISCTOTAL"},
56 {GST_TAG_ALBUM_VOLUME_COUNT, "TOTALDISCS"}, /* old / non-standard */
57 {GST_TAG_ARTIST, "ARTIST"},
58 {GST_TAG_PERFORMER, "PERFORMER"},
59 {GST_TAG_COMPOSER, "COMPOSER"},
60 {GST_TAG_COPYRIGHT, "COPYRIGHT"},
61 {GST_TAG_LICENSE, "LICENSE"},
62 {GST_TAG_LICENSE_URI, "LICENSE"},
63 {GST_TAG_GEO_LOCATION_NAME, "LOCATION"},
64 {GST_TAG_ORGANIZATION, "ORGANIZATION"},
65 {GST_TAG_DESCRIPTION, "DESCRIPTION"},
66 {GST_TAG_GENRE, "GENRE"},
67 {GST_TAG_DATE_TIME, "DATE"},
68 {GST_TAG_CONTACT, "CONTACT"},
69 {GST_TAG_ISRC, "ISRC"},
70 {GST_TAG_COMMENT, "COMMENT"},
71 {GST_TAG_TRACK_GAIN, "REPLAYGAIN_TRACK_GAIN"},
72 {GST_TAG_TRACK_PEAK, "REPLAYGAIN_TRACK_PEAK"},
73 {GST_TAG_ALBUM_GAIN, "REPLAYGAIN_ALBUM_GAIN"},
74 {GST_TAG_ALBUM_PEAK, "REPLAYGAIN_ALBUM_PEAK"},
75 {GST_TAG_REFERENCE_LEVEL, "REPLAYGAIN_REFERENCE_LOUDNESS"},
76 {GST_TAG_MUSICBRAINZ_TRACKID, "MUSICBRAINZ_TRACKID"},
77 {GST_TAG_MUSICBRAINZ_ARTISTID, "MUSICBRAINZ_ARTISTID"},
78 {GST_TAG_MUSICBRAINZ_ALBUMID, "MUSICBRAINZ_ALBUMID"},
79 {GST_TAG_MUSICBRAINZ_ALBUMARTISTID, "MUSICBRAINZ_ALBUMARTISTID"},
80 {GST_TAG_MUSICBRAINZ_TRMID, "MUSICBRAINZ_TRMID"},
81 {GST_TAG_ARTIST_SORTNAME, "ARTISTSORT"},
82 {GST_TAG_ARTIST_SORTNAME, "ARTISTSORTORDER"},
83 {GST_TAG_ARTIST_SORTNAME, "MUSICBRAINZ_SORTNAME"},
84 {GST_TAG_ALBUM_SORTNAME, "ALBUMSORT"},
85 {GST_TAG_ALBUM_SORTNAME, "ALBUMSORTORDER"},
86 {GST_TAG_TITLE_SORTNAME, "TITLESORT"},
87 {GST_TAG_TITLE_SORTNAME, "TITLESORTORDER"},
88 {GST_TAG_ALBUM_ARTIST, "ALBUMARTIST"},
89 {GST_TAG_ALBUM_ARTIST, "ALBUM ARTIST"},
90 {GST_TAG_ALBUM_ARTIST_SORTNAME, "ALBUMARTISTSORT"},
91 {GST_TAG_ALBUM_ARTIST_SORTNAME, "ALBUMARTISTSORTORDER"},
92 {GST_TAG_LANGUAGE_CODE, "LANGUAGE"},
93 {GST_TAG_CDDA_MUSICBRAINZ_DISCID, "MUSICBRAINZ_DISCID"},
94 {GST_TAG_CDDA_CDDB_DISCID, "DISCID"},
95 /* For the apparent de-facto standard for coverart in vorbis comments, see:
96 * http://www.hydrogenaudio.org/forums/lofiversion/index.php/t48386.html */
97 {GST_TAG_PREVIEW_IMAGE, "COVERART"},
98 /* some evidence that "BPM" is used elsewhere:
99 * http://mail.kde.org/pipermail/amarok/2006-May/000090.html
100 */
101 {GST_TAG_BEATS_PER_MINUTE, "BPM"},
102 /* What GStreamer calls encoder ("encoder used to encode this stream") is
103 stored in the vendor string in Vorbis/Theora/Kate and possibly others.
104 The Vorbis comment packet used in those streams uses ENCODER as the name
105 of the encoding program, which GStreamer calls application-name. */
106 {GST_TAG_APPLICATION_NAME, "ENCODER"},
107 {NULL, NULL}
108 };
109
110 /**
111 * gst_tag_from_vorbis_tag:
112 * @vorbis_tag: vorbiscomment tag to convert to GStreamer tag
113 *
114 * Looks up the GStreamer tag for a vorbiscomment tag.
115 *
116 * Returns: The corresponding GStreamer tag or NULL if none exists.
117 */
118 const gchar *
gst_tag_from_vorbis_tag(const gchar * vorbis_tag)119 gst_tag_from_vorbis_tag (const gchar * vorbis_tag)
120 {
121 int i = 0;
122 gchar *real_vorbis_tag;
123
124 g_return_val_if_fail (vorbis_tag != NULL, NULL);
125
126 gst_tag_register_musicbrainz_tags ();
127
128 real_vorbis_tag = g_ascii_strup (vorbis_tag, -1);
129 while (tag_matches[i].gstreamer_tag != NULL) {
130 if (strcmp (real_vorbis_tag, tag_matches[i].original_tag) == 0) {
131 break;
132 }
133 i++;
134 }
135 g_free (real_vorbis_tag);
136 return tag_matches[i].gstreamer_tag;
137 }
138
139 /**
140 * gst_tag_to_vorbis_tag:
141 * @gst_tag: GStreamer tag to convert to vorbiscomment tag
142 *
143 * Looks up the vorbiscomment tag for a GStreamer tag.
144 *
145 * Returns: The corresponding vorbiscomment tag or NULL if none exists.
146 */
147 const gchar *
gst_tag_to_vorbis_tag(const gchar * gst_tag)148 gst_tag_to_vorbis_tag (const gchar * gst_tag)
149 {
150 int i = 0;
151
152 g_return_val_if_fail (gst_tag != NULL, NULL);
153
154 gst_tag_register_musicbrainz_tags ();
155
156 while (tag_matches[i].gstreamer_tag != NULL) {
157 if (strcmp (gst_tag, tag_matches[i].gstreamer_tag) == 0) {
158 return tag_matches[i].original_tag;
159 }
160 i++;
161 }
162 return NULL;
163 }
164
165
166 /**
167 * gst_vorbis_tag_add:
168 * @list: a #GstTagList
169 * @tag: a vorbiscomment tag string (key in key=value), must be valid UTF-8
170 * @value: a vorbiscomment value string (value in key=value), must be valid UTF-8
171 *
172 * Convenience function using gst_tag_from_vorbis_tag(), parsing
173 * a vorbis comment string into the right type and adding it to the
174 * given taglist @list.
175 *
176 * Unknown vorbiscomment tags will be added to the tag list in form
177 * of a #GST_TAG_EXTENDED_COMMENT.
178 */
179 void
gst_vorbis_tag_add(GstTagList * list,const gchar * tag,const gchar * value)180 gst_vorbis_tag_add (GstTagList * list, const gchar * tag, const gchar * value)
181 {
182 const gchar *gst_tag;
183 GType tag_type;
184
185 g_return_if_fail (list != NULL);
186 g_return_if_fail (tag != NULL);
187 g_return_if_fail (value != NULL);
188
189 g_return_if_fail (g_utf8_validate (tag, -1, NULL));
190 g_return_if_fail (g_utf8_validate (value, -1, NULL));
191 g_return_if_fail (strchr (tag, '=') == NULL);
192
193 gst_tag = gst_tag_from_vorbis_tag (tag);
194 if (gst_tag == NULL) {
195 gchar *ext_comment;
196
197 ext_comment = g_strdup_printf ("%s=%s", tag, value);
198 gst_tag_list_add (list, GST_TAG_MERGE_APPEND, GST_TAG_EXTENDED_COMMENT,
199 ext_comment, NULL);
200 g_free (ext_comment);
201 return;
202 }
203
204 tag_type = gst_tag_get_type (gst_tag);
205 switch (tag_type) {
206 case G_TYPE_UINT:{
207 guint tmp;
208 gchar *check;
209 gboolean is_track_number_tag;
210 gboolean is_disc_number_tag;
211
212 is_track_number_tag = (strcmp (gst_tag, GST_TAG_TRACK_NUMBER) == 0);
213 is_disc_number_tag = (strcmp (gst_tag, GST_TAG_ALBUM_VOLUME_NUMBER) == 0);
214 tmp = strtoul (value, &check, 10);
215 if (*check == '/' && (is_track_number_tag || is_disc_number_tag)) {
216 guint count;
217
218 check++;
219 count = strtoul (check, &check, 10);
220 if (*check != '\0' || count == 0)
221 break;
222 if (is_track_number_tag) {
223 gst_tag_list_add (list, GST_TAG_MERGE_APPEND, GST_TAG_TRACK_COUNT,
224 count, NULL);
225 } else {
226 gst_tag_list_add (list, GST_TAG_MERGE_APPEND,
227 GST_TAG_ALBUM_VOLUME_COUNT, count, NULL);
228 }
229 }
230 if (*check == '\0') {
231 gst_tag_list_add (list, GST_TAG_MERGE_APPEND, gst_tag, tmp, NULL);
232 }
233 break;
234 }
235 case G_TYPE_STRING:{
236 gchar *valid = NULL;
237
238 /* specialcase for language code */
239 if (strcmp (tag, "LANGUAGE") == 0) {
240 const gchar *s = strchr (value, '[');
241
242 /* Accept both ISO-639-1 and ISO-639-2 codes */
243 if (s && strchr (s, ']') == s + 4) {
244 valid = g_strndup (s + 1, 3);
245 } else if (s && strchr (s, ']') == s + 3) {
246 valid = g_strndup (s + 1, 2);
247 } else if (strlen (value) != 2 && strlen (value) != 3) {
248 GST_WARNING ("doesn't contain an ISO-639 language code: %s", value);
249 }
250 } else if (strcmp (tag, "LICENSE") == 0) {
251 /* license tags in vorbis comments must contain an URI representing
252 * the license and nothing more, at least according to:
253 * http://wiki.xiph.org/index.php/LICENSE_and_COPYRIGHT_tags_on_Vorbis_Comments */
254 if (value && gst_uri_is_valid (value))
255 gst_tag = GST_TAG_LICENSE_URI;
256 }
257
258 if (!valid) {
259 valid = g_strdup (value);
260 }
261 gst_tag_list_add (list, GST_TAG_MERGE_APPEND, gst_tag, valid, NULL);
262 g_free (valid);
263 break;
264 }
265 case G_TYPE_DOUBLE:{
266 gchar *c;
267
268 c = g_strdup (value);
269 g_strdelimit (c, ",", '.');
270 gst_tag_list_add (list, GST_TAG_MERGE_APPEND, gst_tag,
271 g_strtod (c, NULL), NULL);
272 g_free (c);
273 break;
274 }
275 default:{
276 if (tag_type == GST_TYPE_DATE_TIME) {
277 GstDateTime *datetime;
278
279 datetime = gst_date_time_new_from_iso8601_string (value);
280
281 if (datetime) {
282 gst_tag_list_add (list, GST_TAG_MERGE_APPEND, gst_tag, datetime,
283 NULL);
284 gst_date_time_unref (datetime);
285 } else {
286 GST_WARNING ("could not parse datetime string '%s'", value);
287 }
288 } else {
289 GST_WARNING ("Unhandled tag of type '%s' (%d)",
290 g_type_name (tag_type), (gint) tag_type);
291 }
292 break;
293 }
294 }
295 }
296
297 static void
gst_vorbis_tag_add_coverart(GstTagList * tags,gchar * img_data_base64,gint base64_len)298 gst_vorbis_tag_add_coverart (GstTagList * tags, gchar * img_data_base64,
299 gint base64_len)
300 {
301 GstSample *img;
302 gsize img_len;
303
304 if (base64_len < 2)
305 goto not_enough_data;
306
307 /* img_data_base64 points to a temporary copy of the base64 encoded data, so
308 * it's safe to do inpace decoding here
309 */
310 g_base64_decode_inplace (img_data_base64, &img_len);
311 if (img_len == 0)
312 goto decode_failed;
313
314 img =
315 gst_tag_image_data_to_image_sample ((const guint8 *) img_data_base64,
316 img_len, GST_TAG_IMAGE_TYPE_NONE);
317
318 if (img == NULL)
319 goto convert_failed;
320
321 gst_tag_list_add (tags, GST_TAG_MERGE_APPEND,
322 GST_TAG_PREVIEW_IMAGE, img, NULL);
323
324 gst_sample_unref (img);
325 return;
326
327 /* ERRORS */
328 not_enough_data:
329 {
330 GST_WARNING ("COVERART tag with too little base64-encoded data");
331 return;
332 }
333 decode_failed:
334 {
335 GST_WARNING ("Couldn't decode base64 image data from COVERART tag");
336 return;
337 }
338 convert_failed:
339 {
340 GST_WARNING ("Couldn't extract image or image type from COVERART tag");
341 return;
342 }
343 }
344
345 /* Standardized way of adding pictures to vorbiscomments:
346 * http://wiki.xiph.org/VorbisComment#METADATA_BLOCK_PICTURE
347 */
348 static void
gst_vorbis_tag_add_metadata_block_picture(GstTagList * tags,gchar * value,gint value_len)349 gst_vorbis_tag_add_metadata_block_picture (GstTagList * tags,
350 gchar * value, gint value_len)
351 {
352 GstByteReader reader;
353 guint32 img_len = 0, img_type = 0;
354 guint32 img_mimetype_len = 0, img_description_len = 0;
355 gsize decoded_len;
356 const guint8 *data = NULL;
357
358 /* img_data_base64 points to a temporary copy of the base64 encoded data, so
359 * it's safe to do inpace decoding here
360 */
361 g_base64_decode_inplace (value, &decoded_len);
362 if (decoded_len == 0)
363 goto decode_failed;
364
365 gst_byte_reader_init (&reader, (guint8 *) value, decoded_len);
366
367 if (!gst_byte_reader_get_uint32_be (&reader, &img_type))
368 goto error;
369
370 if (!gst_byte_reader_get_uint32_be (&reader, &img_mimetype_len))
371 goto error;
372 if (!gst_byte_reader_skip (&reader, img_mimetype_len))
373 goto error;
374
375 if (!gst_byte_reader_get_uint32_be (&reader, &img_description_len))
376 goto error;
377 if (!gst_byte_reader_skip (&reader, img_description_len))
378 goto error;
379
380 /* Skip width, height, color depth and number of colors for
381 * indexed formats */
382 if (!gst_byte_reader_skip (&reader, 4 * 4))
383 goto error;
384
385 if (!gst_byte_reader_get_uint32_be (&reader, &img_len))
386 goto error;
387
388 if (!gst_byte_reader_get_data (&reader, img_len, &data))
389 goto error;
390
391 gst_tag_list_add_id3_image (tags, data, img_len, img_type);
392
393 return;
394
395 error:
396 GST_WARNING
397 ("Couldn't extract image or image type from METADATA_BLOCK_PICTURE tag");
398 return;
399 decode_failed:
400 GST_WARNING ("Failed to decode Base64 data from METADATA_BLOCK_PICTURE tag");
401 return;
402 }
403
404 /**
405 * gst_tag_list_from_vorbiscomment:
406 * @data: (array length=size): data to convert
407 * @size: size of @data
408 * @id_data: (array length=id_data_length): identification data at start of stream
409 * @id_data_length: length of identification data
410 * @vendor_string: (out) (optional): pointer to a string that should take the
411 * vendor string of this vorbis comment or NULL if you don't need it.
412 *
413 * Creates a new tag list that contains the information parsed out of a
414 * vorbiscomment packet.
415 *
416 * Returns: A new #GstTagList with all tags that could be extracted from the
417 * given vorbiscomment buffer or NULL on error.
418 */
419 GstTagList *
gst_tag_list_from_vorbiscomment(const guint8 * data,gsize size,const guint8 * id_data,const guint id_data_length,gchar ** vendor_string)420 gst_tag_list_from_vorbiscomment (const guint8 * data, gsize size,
421 const guint8 * id_data, const guint id_data_length, gchar ** vendor_string)
422 {
423 #define ADVANCE(x) G_STMT_START{ \
424 data += x; \
425 size -= x; \
426 if (size < 4) goto error; \
427 cur_size = GST_READ_UINT32_LE (data); \
428 data += 4; \
429 size -= 4; \
430 if (cur_size > size) goto error; \
431 cur = (gchar*)data; \
432 }G_STMT_END
433 gchar *cur, *value;
434 guint cur_size;
435 guint iterations;
436 guint value_len;
437 GstTagList *list;
438
439 g_return_val_if_fail (data != NULL, NULL);
440 g_return_val_if_fail (id_data != NULL || id_data_length == 0, NULL);
441
442 list = gst_tag_list_new_empty ();
443
444 if (size < 11 || size <= id_data_length + 4)
445 goto error;
446
447 if (id_data_length > 0 && memcmp (data, id_data, id_data_length) != 0)
448 goto error;
449
450 ADVANCE (id_data_length);
451
452 if (vendor_string)
453 *vendor_string = g_strndup (cur, cur_size);
454
455 ADVANCE (cur_size);
456 iterations = cur_size;
457 cur_size = 0;
458
459 while (iterations) {
460 ADVANCE (cur_size);
461 iterations--;
462 cur = g_strndup (cur, cur_size);
463 value = strchr (cur, '=');
464 if (value == NULL) {
465 g_free (cur);
466 continue;
467 }
468 *value = '\0';
469 value++;
470 value_len = strlen (value);
471 if (value_len == 0 || !g_utf8_validate (value, value_len, NULL)) {
472 g_free (cur);
473 continue;
474 }
475 /* we'll just ignore COVERARTMIME and typefind the image data */
476 if (g_ascii_strcasecmp (cur, "COVERARTMIME") == 0) {
477 g_free (cur);
478 continue;
479 } else if (g_ascii_strcasecmp (cur, "COVERART") == 0) {
480 gst_vorbis_tag_add_coverart (list, value, value_len);
481 } else if (g_ascii_strcasecmp (cur, "METADATA_BLOCK_PICTURE") == 0) {
482 gst_vorbis_tag_add_metadata_block_picture (list, value, value_len);
483 } else if (g_utf8_validate (cur, -1, NULL)) {
484 gst_vorbis_tag_add (list, cur, value);
485 }
486 g_free (cur);
487 }
488
489 return list;
490
491 error:
492 if (vendor_string && *vendor_string) {
493 g_free (*vendor_string);
494 *vendor_string = NULL;
495 }
496 gst_tag_list_unref (list);
497 return NULL;
498 #undef ADVANCE
499 }
500
501 /**
502 * gst_tag_list_from_vorbiscomment_buffer:
503 * @buffer: buffer to convert
504 * @id_data: (array length=id_data_length): identification data at start of stream
505 * @id_data_length: length of identification data
506 * @vendor_string: (out) (optional): pointer to a string that should take the
507 * vendor string of this vorbis comment or NULL if you don't need it.
508 *
509 * Creates a new tag list that contains the information parsed out of a
510 * vorbiscomment packet.
511 *
512 * Returns: A new #GstTagList with all tags that could be extracted from the
513 * given vorbiscomment buffer or NULL on error.
514 */
515 GstTagList *
gst_tag_list_from_vorbiscomment_buffer(GstBuffer * buffer,const guint8 * id_data,const guint id_data_length,gchar ** vendor_string)516 gst_tag_list_from_vorbiscomment_buffer (GstBuffer * buffer,
517 const guint8 * id_data, const guint id_data_length, gchar ** vendor_string)
518 {
519 GstTagList *res;
520 GstMapInfo info;
521
522 if (!gst_buffer_map (buffer, &info, GST_MAP_READ))
523 g_return_val_if_reached (NULL);
524
525 res =
526 gst_tag_list_from_vorbiscomment (info.data, info.size, id_data,
527 id_data_length, vendor_string);
528 gst_buffer_unmap (buffer, &info);
529
530 return res;
531 }
532
533 typedef struct
534 {
535 guint count;
536 guint data_count;
537 GList *entries;
538 }
539 MyForEach;
540
541 static GList *
gst_tag_to_metadata_block_picture(const gchar * tag,const GValue * image_value)542 gst_tag_to_metadata_block_picture (const gchar * tag,
543 const GValue * image_value)
544 {
545 gchar *comment_data, *data_result;
546 const gchar *mime_type;
547 guint mime_type_len;
548 GstStructure *mime_struct;
549 GstSample *sample;
550 GstBuffer *buffer;
551 GstCaps *caps;
552 GList *l = NULL;
553 GstMapInfo mapinfo = { 0, };
554 GstByteWriter writer;
555 GstTagImageType image_type = GST_TAG_IMAGE_TYPE_NONE;
556 gint width = 0, height = 0;
557 guint8 *metadata_block;
558 guint metadata_block_len;
559
560 g_return_val_if_fail (image_value != NULL, NULL);
561
562 sample = gst_value_get_sample (image_value);
563 buffer = gst_sample_get_buffer (sample);
564 caps = gst_sample_get_caps (sample);
565 g_return_val_if_fail (gst_caps_is_fixed (caps), NULL);
566 mime_struct = gst_caps_get_structure (caps, 0);
567
568 mime_type = gst_structure_get_name (mime_struct);
569 if (strcmp (mime_type, "text/uri-list") == 0)
570 mime_type = "-->";
571 mime_type_len = strlen (mime_type);
572
573 /* FIXME 2.0: Remove the image-type reading from the caps, this was
574 * a bug until 1.2.2. The image-type is only supposed to be in the
575 * info structure */
576 gst_structure_get (mime_struct, "image-type", GST_TYPE_TAG_IMAGE_TYPE,
577 &image_type, "width", G_TYPE_INT, &width, "height", G_TYPE_INT, &height,
578 NULL);
579
580 if (image_type == GST_TAG_IMAGE_TYPE_NONE) {
581 const GstStructure *info_struct;
582
583 info_struct = gst_sample_get_info (sample);
584 if (info_struct && gst_structure_has_name (info_struct, "GstTagImageInfo")) {
585 gst_structure_get (info_struct, "image-type", GST_TYPE_TAG_IMAGE_TYPE,
586 &image_type, NULL);
587 }
588 }
589
590 metadata_block_len = 32 + mime_type_len + gst_buffer_get_size (buffer);
591 gst_byte_writer_init_with_size (&writer, metadata_block_len, TRUE);
592
593 if (image_type == GST_TAG_IMAGE_TYPE_NONE
594 && strcmp (tag, GST_TAG_PREVIEW_IMAGE) == 0) {
595 gst_byte_writer_put_uint32_be_unchecked (&writer, 0x01);
596 } else {
597 /* Convert to ID3v2 APIC image type */
598 if (image_type == GST_TAG_IMAGE_TYPE_NONE)
599 image_type = GST_TAG_IMAGE_TYPE_UNDEFINED;
600 else
601 image_type = image_type + 2;
602 gst_byte_writer_put_uint32_be_unchecked (&writer, image_type);
603 }
604
605 gst_byte_writer_put_uint32_be_unchecked (&writer, mime_type_len);
606 gst_byte_writer_put_data_unchecked (&writer, (guint8 *) mime_type,
607 mime_type_len);
608 /* description length */
609 gst_byte_writer_put_uint32_be_unchecked (&writer, 0);
610 gst_byte_writer_put_uint32_be_unchecked (&writer, width);
611 gst_byte_writer_put_uint32_be_unchecked (&writer, height);
612 /* color depth */
613 gst_byte_writer_put_uint32_be_unchecked (&writer, 0);
614 /* for indexed formats the number of colors */
615 gst_byte_writer_put_uint32_be_unchecked (&writer, 0);
616
617 if (gst_buffer_map (buffer, &mapinfo, GST_MAP_READ)) {
618 gst_byte_writer_put_uint32_be_unchecked (&writer, mapinfo.size);
619 gst_byte_writer_put_data_unchecked (&writer, mapinfo.data, mapinfo.size);
620 gst_buffer_unmap (buffer, &mapinfo);
621 } else {
622 GST_WARNING ("Failed to map vorbistag image buffer");
623 gst_byte_writer_reset (&writer);
624 return NULL; /* List is always null up to here */
625 }
626
627 g_assert (gst_byte_writer_get_pos (&writer) == metadata_block_len);
628
629 metadata_block = gst_byte_writer_reset_and_get_data (&writer);
630 comment_data = g_base64_encode (metadata_block, metadata_block_len);
631 g_free (metadata_block);
632 data_result = g_strdup_printf ("METADATA_BLOCK_PICTURE=%s", comment_data);
633 g_free (comment_data);
634
635 l = g_list_append (l, data_result);
636
637 return l;
638 }
639
640 /**
641 * gst_tag_to_vorbis_comments:
642 * @list: a #GstTagList
643 * @tag: a GStreamer tag identifier, such as #GST_TAG_ARTIST
644 *
645 * Creates a new tag list that contains the information parsed out of a
646 * vorbiscomment packet.
647 *
648 * Returns: (element-type utf8) (transfer full): A #GList of newly-allocated
649 * key=value strings. Free with g_list_foreach (list, (GFunc) g_free, NULL)
650 * plus g_list_free (list)
651 */
652 GList *
gst_tag_to_vorbis_comments(const GstTagList * list,const gchar * tag)653 gst_tag_to_vorbis_comments (const GstTagList * list, const gchar * tag)
654 {
655 const gchar *vorbis_tag = NULL;
656 GList *l = NULL;
657 guint i;
658
659 g_return_val_if_fail (list != NULL, NULL);
660 g_return_val_if_fail (tag != NULL, NULL);
661
662 /* Special case: cover art is split into two tags to store data and
663 * MIME-type. Even if the tag list contains multiple entries, there is
664 * no reasonable way to save more than one.
665 * If both, preview image and image, are present we prefer the
666 * image tag.
667 */
668 if ((strcmp (tag, GST_TAG_PREVIEW_IMAGE) == 0 &&
669 gst_tag_list_get_tag_size (list, GST_TAG_IMAGE) == 0) ||
670 strcmp (tag, GST_TAG_IMAGE) == 0) {
671 return gst_tag_to_metadata_block_picture (tag,
672 gst_tag_list_get_value_index (list, tag, 0));
673 }
674
675 if (strcmp (tag, GST_TAG_EXTENDED_COMMENT) != 0) {
676 vorbis_tag = gst_tag_to_vorbis_tag (tag);
677 if (!vorbis_tag)
678 return NULL;
679 }
680
681 /* FIXME: for tags that can map to multiple vorbis comment keys, add all
682 * of the possible keys */
683 for (i = 0; i < gst_tag_list_get_tag_size (list, tag); i++) {
684 GType tag_type = gst_tag_get_type (tag);
685 gchar *result = NULL;
686
687 switch (tag_type) {
688 case G_TYPE_UINT:{
689 guint u;
690
691 if (!gst_tag_list_get_uint_index (list, tag, i, &u))
692 g_return_val_if_reached (NULL);
693 result = g_strdup_printf ("%s=%u", vorbis_tag, u);
694 break;
695 }
696 case G_TYPE_STRING:{
697 const gchar *str = NULL;
698
699 if (!gst_tag_list_peek_string_index (list, tag, i, &str))
700 g_return_val_if_reached (NULL);
701
702 /* special case: GST_TAG_EXTENDED_COMMENT */
703 if (vorbis_tag == NULL) {
704 gchar *key = NULL, *val = NULL;
705
706 if (gst_tag_parse_extended_comment (str, &key, NULL, &val, TRUE)) {
707 result = g_strdup_printf ("%s=%s", key, val);
708 g_free (key);
709 g_free (val);
710 } else {
711 GST_WARNING ("Not a valid extended comment string: %s", str);
712 continue;
713 }
714 } else {
715 result = g_strdup_printf ("%s=%s", vorbis_tag, str);
716 }
717 break;
718 }
719 case G_TYPE_DOUBLE:{
720 gdouble value;
721 gchar buf[G_ASCII_DTOSTR_BUF_SIZE];
722
723 if (!gst_tag_list_get_double_index (list, tag, i, &value))
724 g_return_val_if_reached (NULL);
725 g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, "%f", value);
726 result = g_strconcat (vorbis_tag, "=", buf, NULL);
727 break;
728 }
729 default:{
730 if (tag_type == GST_TYPE_DATE_TIME) {
731 GstDateTime *datetime;
732
733 if (gst_tag_list_get_date_time_index (list, tag, i, &datetime)) {
734 gchar *string;
735
736 /* vorbis suggests using ISO date formats:
737 * http://wiki.xiph.org/VorbisComment#Date_and_time */
738 string = gst_date_time_to_iso8601_string (datetime);
739 result = g_strdup_printf ("%s=%s", vorbis_tag, string);
740 g_free (string);
741
742 gst_date_time_unref (datetime);
743 }
744 } else {
745 GST_DEBUG ("Couldn't write tag %s", tag);
746 continue;
747 }
748 break;
749 }
750 }
751 l = g_list_prepend (l, result);
752 }
753
754 return g_list_reverse (l);
755 }
756
757 static void
write_one_tag(const GstTagList * list,const gchar * tag,gpointer user_data)758 write_one_tag (const GstTagList * list, const gchar * tag, gpointer user_data)
759 {
760 MyForEach *data = (MyForEach *) user_data;
761 GList *comments;
762 GList *it;
763
764 comments = gst_tag_to_vorbis_comments (list, tag);
765
766 for (it = comments; it != NULL; it = it->next) {
767 gchar *result = it->data;
768
769 data->count++;
770 data->data_count += strlen (result);
771 data->entries = g_list_prepend (data->entries, result);
772 }
773
774 g_list_free (comments);
775 }
776
777 /**
778 * gst_tag_list_to_vorbiscomment_buffer:
779 * @list: tag list to convert
780 * @id_data: (array length=id_data_length): identification data at start of stream
781 * @id_data_length: length of identification data, may be 0 if @id_data is NULL
782 * @vendor_string: (nullable): string that describes the vendor string or NULL
783 *
784 * Creates a new vorbiscomment buffer from a tag list.
785 *
786 * Returns: A new #GstBuffer containing a vorbiscomment buffer with all tags
787 * that could be converted from the given tag list.
788 */
789 GstBuffer *
gst_tag_list_to_vorbiscomment_buffer(const GstTagList * list,const guint8 * id_data,const guint id_data_length,const gchar * vendor_string)790 gst_tag_list_to_vorbiscomment_buffer (const GstTagList * list,
791 const guint8 * id_data, const guint id_data_length,
792 const gchar * vendor_string)
793 {
794 GstBuffer *buffer;
795 GstMapInfo info;
796 guint8 *data;
797 guint i;
798 GList *l;
799 MyForEach my_data = { 0, 0, NULL };
800 guint vendor_len;
801 int required_size;
802
803 g_return_val_if_fail (GST_IS_TAG_LIST (list), NULL);
804 g_return_val_if_fail (id_data != NULL || id_data_length == 0, NULL);
805
806 if (vendor_string == NULL)
807 vendor_string = "GStreamer encoded vorbiscomment";
808 vendor_len = strlen (vendor_string);
809 required_size = id_data_length + 4 + vendor_len + 4 + 1;
810 gst_tag_list_foreach ((GstTagList *) list, write_one_tag, &my_data);
811 required_size += 4 * my_data.count + my_data.data_count;
812
813 buffer = gst_buffer_new_and_alloc (required_size);
814 gst_buffer_map (buffer, &info, GST_MAP_WRITE);
815 data = info.data;
816 if (id_data_length > 0) {
817 memcpy (data, id_data, id_data_length);
818 data += id_data_length;
819 }
820 GST_WRITE_UINT32_LE (data, vendor_len);
821 data += 4;
822 memcpy (data, vendor_string, vendor_len);
823 data += vendor_len;
824 l = my_data.entries = g_list_reverse (my_data.entries);
825 GST_WRITE_UINT32_LE (data, my_data.count);
826 data += 4;
827 for (i = 0; i < my_data.count; i++) {
828 guint size;
829 gchar *cur;
830
831 g_assert (l != NULL);
832 cur = l->data;
833 l = g_list_next (l);
834 size = strlen (cur);
835 GST_WRITE_UINT32_LE (data, size);
836 data += 4;
837 memcpy (data, cur, size);
838 data += size;
839 }
840 g_list_foreach (my_data.entries, (GFunc) g_free, NULL);
841 g_list_free (my_data.entries);
842 *data = 1;
843 gst_buffer_unmap (buffer, &info);
844
845 return buffer;
846 }
847