1 /* GStreamer ID3v2 tag writer
3 * Copyright (C) 2006 Christophe Fergeau <teuf@gnome.org>
4 * Copyright (C) 2006-2009 Tim-Philipp Müller <tim centricular net>
5 * Copyright (C) 2009 Pioneers of the Inevitable <songbird@songbirdnest.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
26 #include <gst/tag/tag.h>
28 GST_DEBUG_CATEGORY_EXTERN (gst_id3_mux_debug);
29 #define GST_CAT_DEFAULT gst_id3_mux_debug
31 #define ID3V2_APIC_PICTURE_OTHER 0
32 #define ID3V2_APIC_PICTURE_FILE_ICON 1
34 /* ======================================================================== */
36 typedef GString GstByteWriter;
38 static inline GstByteWriter *
39 gst_byte_writer_new (guint size)
41 return (GstByteWriter *) g_string_sized_new (size);
45 gst_byte_writer_get_length (GstByteWriter * w)
47 return ((GString *) w)->len;
51 gst_byte_writer_write_bytes (GstByteWriter * w, const guint8 * data, guint len)
53 g_string_append_len ((GString *) w, (const gchar *) data, len);
57 gst_byte_writer_write_uint8 (GstByteWriter * w, guint8 val)
61 GST_WRITE_UINT8 (data, val);
62 gst_byte_writer_write_bytes (w, data, 1);
66 gst_byte_writer_write_uint16 (GstByteWriter * w, guint16 val)
70 GST_WRITE_UINT16_BE (data, val);
71 gst_byte_writer_write_bytes (w, data, 2);
75 gst_byte_writer_write_uint32 (GstByteWriter * w, guint32 val)
79 GST_WRITE_UINT32_BE (data, val);
80 gst_byte_writer_write_bytes (w, data, 4);
84 gst_byte_writer_write_uint32_syncsafe (GstByteWriter * w, guint32 val)
88 data[0] = (guint8) ((val >> 21) & 0x7f);
89 data[1] = (guint8) ((val >> 14) & 0x7f);
90 data[2] = (guint8) ((val >> 7) & 0x7f);
91 data[3] = (guint8) ((val >> 0) & 0x7f);
92 gst_byte_writer_write_bytes (w, data, 4);
96 gst_byte_writer_copy_bytes (GstByteWriter * w, guint8 * dest, guint offset,
101 length = gst_byte_writer_get_length (w);
104 size = length - offset;
106 g_warn_if_fail (length >= (offset + size));
108 memcpy (dest, w->str + offset, MIN (size, length - offset));
112 gst_byte_writer_free (GstByteWriter * w)
114 g_string_free (w, TRUE);
117 /* ======================================================================== */
121 GST_ID3V2_FRAME_FLAG_NONE = 0,
122 GST_ID3V2_FRAME_FLAG_
123 } GstID3v2FrameMsgFlags;
129 guint32 len; /* Length encoded in the header; this is the
130 total length - header size */
132 GstByteWriter *writer;
133 gboolean dirty; /* TRUE if frame header needs updating */
139 guint major_version; /* The 3 in v2.3.0 */
142 typedef void (*GstId3v2AddTagFunc) (GstId3v2Tag * tag, const GstTagList * list,
143 const gchar * gst_tag, guint num_tags, const gchar * data);
145 #define ID3V2_ENCODING_ISO_8859_1 0x00
146 #define ID3V2_ENCODING_UTF16_BOM 0x01
147 #define ID3V2_ENCODING_UTF8 0x03
149 static gboolean id3v2_tag_init (GstId3v2Tag * tag, guint major_version);
150 static void id3v2_tag_unset (GstId3v2Tag * tag);
152 static void id3v2_frame_init (GstId3v2Frame * frame,
153 const gchar * frame_id, guint16 flags);
154 static void id3v2_frame_unset (GstId3v2Frame * frame);
155 static void id3v2_frame_finish (GstId3v2Tag * tag, GstId3v2Frame * frame);
156 static guint id3v2_frame_get_size (GstId3v2Tag * tag, GstId3v2Frame * frame);
158 static void id3v2_tag_add_text_frame (GstId3v2Tag * tag,
159 const gchar * frame_id, const gchar ** strings, int num_strings);
160 static void id3v2_tag_add_simple_text_frame (GstId3v2Tag * tag,
161 const gchar * frame_id, const gchar * string);
164 id3v2_tag_init (GstId3v2Tag * tag, guint major_version)
166 if (major_version != 3 && major_version != 4)
169 tag->major_version = major_version;
170 tag->frames = g_array_new (TRUE, TRUE, sizeof (GstId3v2Frame));
176 id3v2_tag_unset (GstId3v2Tag * tag)
180 for (i = 0; i < tag->frames->len; ++i)
181 id3v2_frame_unset (&g_array_index (tag->frames, GstId3v2Frame, i));
183 g_array_free (tag->frames, TRUE);
184 memset (tag, 0, sizeof (GstId3v2Tag));
187 #ifndef GST_ROUND_UP_1024
188 #define GST_ROUND_UP_1024(num) (((num)+1023)&~1023)
192 id3v2_tag_to_buffer (GstId3v2Tag * tag)
198 guint i, size, offset, size_frames = 0;
200 GST_DEBUG ("Creating buffer for ID3v2 tag containing %d frames",
203 for (i = 0; i < tag->frames->len; ++i) {
204 GstId3v2Frame *frame = &g_array_index (tag->frames, GstId3v2Frame, i);
206 id3v2_frame_finish (tag, frame);
207 size_frames += id3v2_frame_get_size (tag, frame);
210 size = GST_ROUND_UP_1024 (10 + size_frames);
212 w = gst_byte_writer_new (10);
213 gst_byte_writer_write_uint8 (w, 'I');
214 gst_byte_writer_write_uint8 (w, 'D');
215 gst_byte_writer_write_uint8 (w, '3');
216 gst_byte_writer_write_uint8 (w, tag->major_version);
217 gst_byte_writer_write_uint8 (w, 0); /* micro version */
218 gst_byte_writer_write_uint8 (w, 0); /* flags */
219 gst_byte_writer_write_uint32_syncsafe (w, size - 10);
221 buf = gst_buffer_new_allocate (NULL, size, NULL);
222 gst_buffer_map (buf, &info, GST_MAP_WRITE);
224 gst_byte_writer_copy_bytes (w, dest, 0, 10);
227 for (i = 0; i < tag->frames->len; ++i) {
228 GstId3v2Frame *frame = &g_array_index (tag->frames, GstId3v2Frame, i);
230 gst_byte_writer_copy_bytes (frame->writer, dest + offset, 0, -1);
231 offset += id3v2_frame_get_size (tag, frame);
234 /* Zero out any additional space in our buffer as padding. */
235 memset (dest + offset, 0, size - offset);
237 gst_byte_writer_free (w);
238 gst_buffer_unmap (buf, &info);
244 id3v2_frame_write_bytes (GstId3v2Frame * frame, const guint8 * data, guint len)
246 gst_byte_writer_write_bytes (frame->writer, data, len);
251 id3v2_frame_write_uint8 (GstId3v2Frame * frame, guint8 val)
253 gst_byte_writer_write_uint8 (frame->writer, val);
258 id3v2_frame_write_uint16 (GstId3v2Frame * frame, guint16 val)
260 gst_byte_writer_write_uint16 (frame->writer, val);
265 id3v2_frame_write_uint32 (GstId3v2Frame * frame, guint32 val)
267 gst_byte_writer_write_uint32 (frame->writer, val);
272 id3v2_frame_init (GstId3v2Frame * frame, const gchar * frame_id, guint16 flags)
274 g_assert (strlen (frame_id) == 4); /* we only handle 2.3.0/2.4.0 */
275 memcpy (frame->id, frame_id, 4 + 1);
276 frame->flags = flags;
278 frame->writer = gst_byte_writer_new (64);
279 id3v2_frame_write_bytes (frame, (const guint8 *) frame->id, 4);
280 id3v2_frame_write_uint32 (frame, 0); /* size, set later */
281 id3v2_frame_write_uint16 (frame, frame->flags);
285 id3v2_frame_finish (GstId3v2Tag * tag, GstId3v2Frame * frame)
288 frame->len = frame->writer->len - 10;
289 GST_LOG ("[%s] %u bytes", frame->id, frame->len);
290 if (tag->major_version == 3) {
291 GST_WRITE_UINT32_BE (frame->writer->str + 4, frame->len);
293 /* Version 4 uses a syncsafe int here */
294 GST_WRITE_UINT8 (frame->writer->str + 4, (frame->len >> 21) & 0x7f);
295 GST_WRITE_UINT8 (frame->writer->str + 5, (frame->len >> 14) & 0x7f);
296 GST_WRITE_UINT8 (frame->writer->str + 6, (frame->len >> 7) & 0x7f);
297 GST_WRITE_UINT8 (frame->writer->str + 7, (frame->len >> 0) & 0x7f);
299 frame->dirty = FALSE;
304 id3v2_frame_get_size (GstId3v2Tag * tag, GstId3v2Frame * frame)
306 id3v2_frame_finish (tag, frame);
307 return gst_byte_writer_get_length (frame->writer);
311 id3v2_frame_unset (GstId3v2Frame * frame)
313 gst_byte_writer_free (frame->writer);
314 memset (frame, 0, sizeof (GstId3v2Frame));
318 id3v2_string_is_ascii (const gchar * string)
321 if (!g_ascii_isprint (*string++))
329 id3v2_tag_string_encoding (GstId3v2Tag * tag, const gchar * string)
332 if (tag->major_version == 4) {
333 /* ID3v2.4 supports UTF8, use it unconditionally as it's really the only
334 sensible encoding. */
335 encoding = ID3V2_ENCODING_UTF8;
337 /* If we're not writing v2.4, then check to see if it's ASCII.
338 If it is, write ISO-8859-1 (compatible with ASCII).
339 Otherwise, write UTF-16-LE with a byte order marker.
340 Note that we don't write arbitrary ISO-8859-1 as ISO-8859-1, because much
341 software misuses this - and non-ASCII might confuse it. */
342 if (id3v2_string_is_ascii (string))
343 encoding = ID3V2_ENCODING_ISO_8859_1;
345 encoding = ID3V2_ENCODING_UTF16_BOM;
352 id3v2_frame_write_string (GstId3v2Frame * frame, int encoding,
353 const gchar * string, gboolean null_terminate)
355 int terminator_length;
356 if (encoding == ID3V2_ENCODING_UTF16_BOM) {
358 const guint8 bom[] = { 0xFF, 0xFE };
359 /* This converts to little-endian UTF-16 */
360 gchar *utf16 = g_convert (string, -1, "UTF-16LE", "UTF-8",
361 NULL, &utf16len, NULL);
363 GST_WARNING ("Failed to convert UTF-8 to UTF-16LE");
368 id3v2_frame_write_bytes (frame, (const guint8 *) bom, 2);
369 id3v2_frame_write_bytes (frame, (const guint8 *) utf16, utf16len);
370 if (null_terminate) {
371 /* NUL terminator is 2 bytes, if present. */
372 id3v2_frame_write_uint16 (frame, 0);
377 /* write NUL terminator as well if requested */
378 terminator_length = null_terminate ? 1 : 0;
379 id3v2_frame_write_bytes (frame, (const guint8 *) string,
380 strlen (string) + terminator_length);
385 id3v2_tag_add_text_frame (GstId3v2Tag * tag, const gchar * frame_id,
386 const gchar ** strings_utf8, int num_strings)
392 if (num_strings < 1 || strings_utf8 == NULL || strings_utf8[0] == NULL) {
393 GST_LOG ("Not adding text frame, no strings");
397 id3v2_frame_init (&frame, frame_id, 0);
399 encoding = id3v2_tag_string_encoding (tag, strings_utf8[0]);
400 id3v2_frame_write_uint8 (&frame, encoding);
402 GST_LOG ("Adding text frame %s with %d strings", frame_id, num_strings);
404 for (i = 0; i < num_strings; ++i) {
405 len = strlen (strings_utf8[i]);
406 g_return_if_fail (g_utf8_validate (strings_utf8[i], len, NULL));
408 id3v2_frame_write_string (&frame, encoding, strings_utf8[i],
409 i != num_strings - 1);
411 /* only v2.4.0 supports multiple strings per frame (according to the
412 * earlier specs tag readers should just ignore everything after the first
413 * string, but we probably shouldn't write anything there, just in case
414 * tag readers that only support the old version are not expecting
415 * more data after the first string) */
416 if (tag->major_version < 4)
420 if (i < num_strings - 1) {
421 GST_WARNING ("Only wrote one of multiple string values for text frame %s "
422 "- ID3v2 supports multiple string values only since v2.4.0, but writing"
423 "v2.%u.0 tag", frame_id, tag->major_version);
426 g_array_append_val (tag->frames, frame);
430 id3v2_tag_add_simple_text_frame (GstId3v2Tag * tag, const gchar * frame_id,
431 const gchar * string)
433 id3v2_tag_add_text_frame (tag, frame_id, (const gchar **) &string, 1);
436 /* ====================================================================== */
439 add_text_tag (GstId3v2Tag * id3v2tag, const GstTagList * list,
440 const gchar * tag, guint num_tags, const gchar * frame_id)
442 const gchar **strings;
445 GST_LOG ("Adding '%s' frame", frame_id);
447 strings = g_new0 (const gchar *, num_tags + 1);
448 for (n = 0, i = 0; n < num_tags; ++n) {
449 if (gst_tag_list_peek_string_index (list, tag, n, &strings[i]) &&
450 strings[i] != NULL) {
451 GST_LOG ("%s: %s[%u] = '%s'", frame_id, tag, i, strings[i]);
456 if (strings[0] != NULL) {
457 id3v2_tag_add_text_frame (id3v2tag, frame_id, strings, i);
459 GST_WARNING ("Empty list for tag %s, skipping", tag);
462 g_free ((gchar **) strings);
466 add_private_data_tag (GstId3v2Tag * id3v2tag, const GstTagList * list,
467 const gchar * tag, guint num_tags, const gchar * frame_id)
471 for (n = 0; n < num_tags; ++n) {
473 GstSample *sample = NULL;
474 const GstStructure *structure = NULL;
475 GstBuffer *binary = NULL;
476 GstBuffer *priv_frame = NULL;
477 const gchar *owner_str = NULL;
481 if (!gst_tag_list_get_sample_index (list, tag, n, &sample))
484 structure = gst_sample_get_info (sample);
485 if (structure != NULL
486 && !strcmp (gst_structure_get_name (structure), "ID3PrivateFrame")) {
487 owner_str = gst_structure_get_string (structure, "owner");
489 if (owner_str != NULL) {
490 owner_len = strlen (owner_str) + 1;
491 priv_frame = gst_buffer_new_and_alloc (owner_len);
492 gst_buffer_fill (priv_frame, 0, owner_str, owner_len);
494 binary = gst_buffer_ref (gst_sample_get_buffer (sample));
495 priv_frame = gst_buffer_append (priv_frame, binary);
497 id3v2_frame_init (&frame, frame_id, 0);
499 if (gst_buffer_map (priv_frame, &mapinfo, GST_MAP_READ)) {
500 id3v2_frame_write_bytes (&frame, mapinfo.data, mapinfo.size);
501 g_array_append_val (id3v2tag->frames, frame);
502 gst_buffer_unmap (priv_frame, &mapinfo);
504 GST_WARNING ("Couldn't map priv frame tag buffer");
505 id3v2_frame_unset (&frame);
508 gst_buffer_unref (priv_frame);
509 gst_sample_unref (sample);
512 GST_WARNING ("Couldn't find ID3PrivateFrame structure");
518 add_id3v2frame_tag (GstId3v2Tag * id3v2tag, const GstTagList * list,
519 const gchar * tag, guint num_tags, const gchar * unused)
523 for (i = 0; i < num_tags; ++i) {
528 if (!gst_tag_list_get_sample_index (list, tag, i, &sample))
531 buf = gst_sample_get_buffer (sample);
533 /* FIXME: should use auxiliary sample struct instead of caps for this */
534 caps = gst_sample_get_caps (sample);
540 s = gst_caps_get_structure (caps, 0);
541 /* We can only add it if this private buffer is for the same ID3 version,
542 because we don't understand the contents at all. */
543 if (s && gst_structure_get_int (s, "version", &version) &&
544 version == id3v2tag->major_version) {
552 if (!gst_buffer_map (buf, &mapinfo, GST_MAP_READ)) {
553 gst_sample_unref (sample);
560 if (size >= 10) { /* header size */
561 /* We only get here if the frame version matches the muxer. Since the
562 * muxer only does v2.3 or v2.4, the frame must be one of those - and
563 * so the frame header is the same format */
564 memcpy (frame_id, data, 4);
566 flags = GST_READ_UINT16_BE (data + 8);
568 id3v2_frame_init (&frame, frame_id, flags);
569 id3v2_frame_write_bytes (&frame, data + 10, size - 10);
571 g_array_append_val (id3v2tag->frames, frame);
572 GST_DEBUG ("Added unparsed tag with %d bytes", size);
573 gst_buffer_unmap (buf, &mapinfo);
575 GST_WARNING ("Short ID3v2 frame");
578 GST_WARNING ("Discarding unrecognised ID3 tag for different ID3 "
582 gst_sample_unref (sample);
587 add_text_tag_v4 (GstId3v2Tag * id3v2tag, const GstTagList * list,
588 const gchar * tag, guint num_tags, const gchar * frame_id)
590 if (id3v2tag->major_version == 4)
591 add_text_tag (id3v2tag, list, tag, num_tags, frame_id);
593 GST_WARNING ("Cannot serialise tag '%s' in ID3v2.%d", frame_id,
594 id3v2tag->major_version);
599 add_count_or_num_tag (GstId3v2Tag * id3v2tag, const GstTagList * list,
600 const gchar * tag, guint num_tags, const gchar * frame_id)
604 const gchar *gst_tag;
605 const gchar *corr_count; /* corresponding COUNT tag (if number) */
606 const gchar *corr_num; /* corresponding NUMBER tag (if count) */
609 GST_TAG_TRACK_NUMBER, GST_TAG_TRACK_COUNT, NULL}, {
610 GST_TAG_TRACK_COUNT, NULL, GST_TAG_TRACK_NUMBER}, {
611 GST_TAG_ALBUM_VOLUME_NUMBER, GST_TAG_ALBUM_VOLUME_COUNT, NULL}, {
612 GST_TAG_ALBUM_VOLUME_COUNT, NULL, GST_TAG_ALBUM_VOLUME_NUMBER}
616 for (idx = 0; idx < G_N_ELEMENTS (corr); ++idx) {
617 if (strcmp (corr[idx].gst_tag, tag) == 0)
621 g_assert (idx < G_N_ELEMENTS (corr));
622 g_assert (frame_id && strlen (frame_id) == 4);
624 if (corr[idx].corr_num == NULL) {
628 if (gst_tag_list_get_uint_index (list, tag, 0, &number)) {
632 if (gst_tag_list_get_uint_index (list, corr[idx].corr_count, 0, &count))
633 tag_str = g_strdup_printf ("%u/%u", number, count);
635 tag_str = g_strdup_printf ("%u", number);
637 GST_DEBUG ("Setting %s to %s (frame_id = %s)", tag, tag_str, frame_id);
639 id3v2_tag_add_simple_text_frame (id3v2tag, frame_id, tag_str);
642 } else if (corr[idx].corr_count == NULL) {
646 if (gst_tag_list_get_uint_index (list, corr[idx].corr_num, 0, &count)) {
647 GST_DEBUG ("%s handled with %s, skipping", tag, corr[idx].corr_num);
648 } else if (gst_tag_list_get_uint_index (list, tag, 0, &count)) {
649 gchar *tag_str = g_strdup_printf ("0/%u", count);
650 GST_DEBUG ("Setting %s to %s (frame_id = %s)", tag, tag_str, frame_id);
652 id3v2_tag_add_simple_text_frame (id3v2tag, frame_id, tag_str);
658 GST_WARNING ("more than one %s, can only handle one", tag);
663 add_bpm_tag (GstId3v2Tag * id3v2tag, const GstTagList * list,
664 const gchar * tag, guint num_tags, const gchar * unused)
668 GST_LOG ("Adding BPM frame");
670 if (gst_tag_list_get_double (list, tag, &bpm)) {
673 /* bpm is stored as an integer in id3 tags, but is a double in
676 tag_str = g_strdup_printf ("%u", (guint) bpm);
677 GST_DEBUG ("Setting %s to %s", tag, tag_str);
678 id3v2_tag_add_simple_text_frame (id3v2tag, "TBPM", tag_str);
683 GST_WARNING ("more than one %s, can only handle one", tag);
688 add_comment_tag (GstId3v2Tag * id3v2tag, const GstTagList * list,
689 const gchar * tag, guint num_tags, const gchar * unused)
693 GST_LOG ("Adding comment frames");
694 for (n = 0; n < num_tags; ++n) {
695 const gchar *s = NULL;
697 if (gst_tag_list_peek_string_index (list, tag, n, &s) && s != NULL) {
698 gchar *desc = NULL, *val = NULL, *lang = NULL;
699 int desclen, vallen, encoding1, encoding2, encoding;
702 id3v2_frame_init (&frame, "COMM", 0);
704 if (strcmp (tag, GST_TAG_COMMENT) == 0 ||
705 !gst_tag_parse_extended_comment (s, &desc, &lang, &val, TRUE)) {
706 /* create dummy description fields */
707 desc = g_strdup ("Comment");
711 /* If we don't have a valid language, match what taglib does for
713 if (!lang || strlen (lang) < 3)
714 lang = g_strdup ("XXX");
716 desclen = strlen (desc);
717 g_return_if_fail (g_utf8_validate (desc, desclen, NULL));
718 vallen = strlen (val);
719 g_return_if_fail (g_utf8_validate (val, vallen, NULL));
721 GST_LOG ("%s[%u] = '%s' (%s|%s|%s)", tag, n, s, GST_STR_NULL (desc),
722 GST_STR_NULL (lang), GST_STR_NULL (val));
724 encoding1 = id3v2_tag_string_encoding (id3v2tag, desc);
725 encoding2 = id3v2_tag_string_encoding (id3v2tag, val);
726 encoding = MAX (encoding1, encoding2);
728 id3v2_frame_write_uint8 (&frame, encoding);
729 id3v2_frame_write_bytes (&frame, (const guint8 *) lang, 3);
730 /* write description and value */
731 id3v2_frame_write_string (&frame, encoding, desc, TRUE);
732 id3v2_frame_write_string (&frame, encoding, val, FALSE);
738 g_array_append_val (id3v2tag->frames, frame);
744 add_image_tag (GstId3v2Tag * id3v2tag, const GstTagList * list,
745 const gchar * tag, guint num_tags, const gchar * unused)
749 for (n = 0; n < num_tags; ++n) {
754 GST_DEBUG ("image %u/%u", n + 1, num_tags);
756 if (!gst_tag_list_get_sample_index (list, tag, n, &sample))
759 image = gst_sample_get_buffer (sample);
760 caps = gst_sample_get_caps (sample);
762 if (image != NULL && gst_buffer_get_size (image) > 0 &&
763 caps != NULL && !gst_caps_is_empty (caps)) {
764 const gchar *mime_type;
767 s = gst_caps_get_structure (caps, 0);
768 mime_type = gst_structure_get_name (s);
769 if (mime_type != NULL) {
770 const gchar *desc = NULL;
774 const GstStructure *info_struct;
776 info_struct = gst_sample_get_info (sample);
778 || !gst_structure_has_name (info_struct, "GstTagImageInfo"))
781 /* APIC frame specifies "-->" if we're providing a URL to the image
782 rather than directly embedding it */
783 if (strcmp (mime_type, "text/uri-list") == 0)
786 GST_DEBUG ("Attaching picture of %" G_GSIZE_FORMAT " bytes and "
787 "mime type %s", gst_buffer_get_size (image), mime_type);
789 id3v2_frame_init (&frame, "APIC", 0);
792 desc = gst_structure_get_string (info_struct, "image-description");
795 encoding = id3v2_tag_string_encoding (id3v2tag, desc);
796 id3v2_frame_write_uint8 (&frame, encoding);
798 id3v2_frame_write_string (&frame, encoding, mime_type, TRUE);
800 if (strcmp (tag, GST_TAG_PREVIEW_IMAGE) == 0) {
801 id3v2_frame_write_uint8 (&frame, ID3V2_APIC_PICTURE_FILE_ICON);
803 int image_type = ID3V2_APIC_PICTURE_OTHER;
806 if (gst_structure_get (info_struct, "image-type",
807 GST_TYPE_TAG_IMAGE_TYPE, &image_type, NULL)) {
808 if (image_type > 0 && image_type <= 18) {
811 image_type = ID3V2_APIC_PICTURE_OTHER;
814 image_type = ID3V2_APIC_PICTURE_OTHER;
817 id3v2_frame_write_uint8 (&frame, image_type);
820 id3v2_frame_write_string (&frame, encoding, desc, TRUE);
822 if (gst_buffer_map (image, &mapinfo, GST_MAP_READ)) {
823 id3v2_frame_write_bytes (&frame, mapinfo.data, mapinfo.size);
824 g_array_append_val (id3v2tag->frames, frame);
825 gst_buffer_unmap (image, &mapinfo);
827 GST_WARNING ("Couldn't map image tag buffer");
828 id3v2_frame_unset (&frame);
832 GST_WARNING ("no image or caps: %p, caps=%" GST_PTR_FORMAT, image, caps);
834 gst_sample_unref (sample);
839 add_musicbrainz_tag (GstId3v2Tag * id3v2tag, const GstTagList * list,
840 const gchar * tag, guint num_tags, const gchar * data)
844 const gchar gst_tag[28];
845 const gchar spec_id[28];
846 const gchar realworld_id[28];
849 GST_TAG_MUSICBRAINZ_ARTISTID, "MusicBrainz Artist Id",
850 "musicbrainz_artistid"}, {
851 GST_TAG_MUSICBRAINZ_ALBUMID, "MusicBrainz Album Id", "musicbrainz_albumid"}, {
852 GST_TAG_MUSICBRAINZ_ALBUMARTISTID, "MusicBrainz Album Artist Id",
853 "musicbrainz_albumartistid"}, {
854 GST_TAG_MUSICBRAINZ_TRMID, "MusicBrainz TRM Id", "musicbrainz_trmid"}, {
855 GST_TAG_CDDA_MUSICBRAINZ_DISCID, "MusicBrainz DiscID",
856 "musicbrainz_discid"}, {
857 /* the following one is more or less made up, there seems to be little
858 * evidence that any popular application is actually putting this info
859 * into TXXX frames; the first one comes from a musicbrainz wiki 'proposed
860 * tags' page, the second one is analogue to the vorbis/ape/flac tag. */
861 GST_TAG_CDDA_CDDB_DISCID, "CDDB DiscID", "discid"}
865 idx = (guint8) data[0];
866 g_assert (idx < G_N_ELEMENTS (mb_ids));
868 for (i = 0; i < num_tags; ++i) {
871 if (gst_tag_list_peek_string_index (list, tag, 0, &id_str) && id_str) {
872 /* add two frames, one with the ID the musicbrainz.org spec mentions
873 * and one with the ID that applications use in the real world */
874 GstId3v2Frame frame1, frame2;
877 GST_DEBUG ("Setting '%s' to '%s'", mb_ids[idx].spec_id, id_str);
878 encoding = id3v2_tag_string_encoding (id3v2tag, id_str);
880 id3v2_frame_init (&frame1, "TXXX", 0);
881 id3v2_frame_write_uint8 (&frame1, encoding);
882 id3v2_frame_write_string (&frame1, encoding, mb_ids[idx].spec_id, TRUE);
883 id3v2_frame_write_string (&frame1, encoding, id_str, FALSE);
884 g_array_append_val (id3v2tag->frames, frame1);
886 id3v2_frame_init (&frame2, "TXXX", 0);
887 id3v2_frame_write_uint8 (&frame2, encoding);
888 id3v2_frame_write_string (&frame2, encoding,
889 mb_ids[idx].realworld_id, TRUE);
890 id3v2_frame_write_string (&frame2, encoding, id_str, FALSE);
891 g_array_append_val (id3v2tag->frames, frame2);
897 add_unique_file_id_tag (GstId3v2Tag * id3v2tag, const GstTagList * list,
898 const gchar * tag, guint num_tags, const gchar * unused)
900 const gchar *origin = "http://musicbrainz.org";
901 const gchar *id_str = NULL;
903 if (gst_tag_list_peek_string_index (list, tag, 0, &id_str) && id_str) {
906 GST_LOG ("Adding %s (%s): %s", tag, origin, id_str);
908 id3v2_frame_init (&frame, "UFID", 0);
909 id3v2_frame_write_bytes (&frame, (const guint8 *) origin,
910 strlen (origin) + 1);
911 id3v2_frame_write_bytes (&frame, (const guint8 *) id_str,
912 strlen (id_str) + 1);
913 g_array_append_val (id3v2tag->frames, frame);
918 add_date_tag (GstId3v2Tag * id3v2tag, const GstTagList * list,
919 const gchar * tag, guint num_tags, const gchar * unused)
923 const gchar *frame_id;
926 if (id3v2tag->major_version == 3)
931 GST_LOG ("Adding date time frame");
933 strings = g_new0 (gchar *, num_tags + 1);
934 for (n = 0; n < num_tags; ++n) {
935 GstDateTime *dt = NULL;
939 if (!gst_tag_list_get_date_time_index (list, tag, n, &dt) || dt == NULL)
942 year = gst_date_time_get_year (dt);
943 if (year > 500 && year < 2100) {
944 s = g_strdup_printf ("%u", year);
945 GST_LOG ("%s[%u] = '%s'", tag, n, s);
949 GST_WARNING ("invalid year %u, skipping", year);
952 if (gst_date_time_has_month (dt)) {
953 if (id3v2tag->major_version == 3)
954 GST_FIXME ("write TDAT and possibly also TIME frame");
956 gst_date_time_unref (dt);
959 if (strings[0] != NULL) {
960 id3v2_tag_add_text_frame (id3v2tag, frame_id, (const gchar **) strings, i);
962 GST_WARNING ("Empty list for tag %s, skipping", tag);
965 g_strfreev (strings);
969 add_encoder_tag (GstId3v2Tag * id3v2tag, const GstTagList * list,
970 const gchar * tag, guint num_tags, const gchar * unused)
976 /* ENCODER_VERSION is either handled with the ENCODER tag or not at all */
977 if (strcmp (tag, GST_TAG_ENCODER_VERSION) == 0)
980 strings = g_new0 (gchar *, num_tags + 1);
981 for (n = 0; n < num_tags; ++n) {
982 const gchar *encoder = NULL;
984 if (gst_tag_list_peek_string_index (list, tag, n, &encoder) && encoder) {
985 guint encoder_version;
988 if (gst_tag_list_get_uint_index (list, GST_TAG_ENCODER_VERSION, n,
989 &encoder_version) && encoder_version > 0) {
990 s = g_strdup_printf ("%s %u", encoder, encoder_version);
992 s = g_strdup (encoder);
995 GST_LOG ("encoder[%u] = '%s'", n, s);
1001 if (strings[0] != NULL) {
1002 id3v2_tag_add_text_frame (id3v2tag, "TSSE", (const gchar **) strings, i);
1004 GST_WARNING ("Empty list for tag %s, skipping", tag);
1007 g_strfreev (strings);
1011 add_uri_tag (GstId3v2Tag * id3v2tag, const GstTagList * list,
1012 const gchar * tag, guint num_tags, const gchar * frame_id)
1014 const gchar *url = NULL;
1016 g_assert (frame_id != NULL);
1018 /* URI tags are limited to one of each per taglist */
1019 if (gst_tag_list_peek_string_index (list, tag, 0, &url) && url != NULL) {
1022 url_len = strlen (url);
1023 if (url_len > 0 && gst_uri_is_valid (url)) {
1024 GstId3v2Frame frame;
1026 id3v2_frame_init (&frame, frame_id, 0);
1027 id3v2_frame_write_bytes (&frame, (const guint8 *) url, strlen (url) + 1);
1028 g_array_append_val (id3v2tag->frames, frame);
1030 GST_WARNING ("Tag %s does not contain a valid URI (%s)", tag, url);
1036 add_relative_volume_tag (GstId3v2Tag * id3v2tag, const GstTagList * list,
1037 const gchar * tag, guint num_tags, const gchar * unused)
1039 const char *gain_tag_name;
1040 const char *peak_tag_name;
1043 const char *identification;
1047 GstId3v2Frame frame;
1048 const gchar *frame_id;
1050 /* figure out tag names and the identification string to use */
1051 if (strcmp (tag, GST_TAG_TRACK_PEAK) == 0 ||
1052 strcmp (tag, GST_TAG_TRACK_GAIN) == 0) {
1053 gain_tag_name = GST_TAG_TRACK_GAIN;
1054 peak_tag_name = GST_TAG_TRACK_PEAK;
1055 identification = "track";
1056 GST_DEBUG ("adding track relative-volume frame");
1058 gain_tag_name = GST_TAG_ALBUM_GAIN;
1059 peak_tag_name = GST_TAG_ALBUM_PEAK;
1060 identification = "album";
1062 if (id3v2tag->major_version == 3) {
1063 GST_WARNING ("Cannot store replaygain album gain data in ID3v2.3");
1066 GST_DEBUG ("adding album relative-volume frame");
1069 /* find the value for the paired tag (gain, if this is peak, and
1070 * vice versa). if both tags exist, only write the frame when
1071 * we're processing the peak tag.
1073 if (strcmp (tag, GST_TAG_TRACK_PEAK) == 0 ||
1074 strcmp (tag, GST_TAG_ALBUM_PEAK) == 0) {
1076 gst_tag_list_get_double (list, tag, &peak_val);
1078 if (gst_tag_list_get_tag_size (list, gain_tag_name) > 0) {
1079 gst_tag_list_get_double (list, gain_tag_name, &gain_val);
1080 GST_DEBUG ("setting volume adjustment %g", gain_val);
1081 gain_int = (gint16) (gain_val * 512.0);
1085 /* copying mutagen: always write as 16 bits for sanity. */
1086 peak_int = (short) (peak_val * G_MAXSHORT);
1089 gst_tag_list_get_double (list, tag, &gain_val);
1090 GST_DEBUG ("setting volume adjustment %g", gain_val);
1092 gain_int = (gint16) (gain_val * 512.0);
1096 if (gst_tag_list_get_tag_size (list, peak_tag_name) != 0) {
1098 ("both gain and peak tags exist, not adding frame this time around");
1103 if (id3v2tag->major_version == 4) {
1104 /* 2.4: Use RVA2 tag */
1107 /* 2.3: Use XRVA tag - this is experimental, but useful in the real world.
1108 This version only officially supports the 'RVAD' tag, but that appears
1109 to not be widely implemented in reality. */
1113 id3v2_frame_init (&frame, frame_id, 0);
1114 id3v2_frame_write_bytes (&frame, (const guint8 *) identification,
1115 strlen (identification) + 1);
1116 id3v2_frame_write_uint8 (&frame, 0x01); /* Master volume */
1117 id3v2_frame_write_uint16 (&frame, gain_int);
1118 id3v2_frame_write_uint8 (&frame, peak_bits);
1120 id3v2_frame_write_uint16 (&frame, peak_int);
1122 g_array_append_val (id3v2tag->frames, frame);
1125 /* id3demux produces these for frames it cannot parse */
1126 #define GST_ID3_DEMUX_TAG_ID3V2_FRAME "private-id3v2-frame"
1130 const gchar *gst_tag;
1131 const GstId3v2AddTagFunc func;
1135 /* Simple text tags */
1136 GST_TAG_ARTIST, add_text_tag, "TPE1"}, {
1137 GST_TAG_ALBUM_ARTIST, add_text_tag, "TPE2"}, {
1138 GST_TAG_TITLE, add_text_tag, "TIT2"}, {
1139 GST_TAG_ALBUM, add_text_tag, "TALB"}, {
1140 GST_TAG_COPYRIGHT, add_text_tag, "TCOP"}, {
1141 GST_TAG_COMPOSER, add_text_tag, "TCOM"}, {
1142 GST_TAG_GENRE, add_text_tag, "TCON"}, {
1143 GST_TAG_ENCODED_BY, add_text_tag, "TENC"}, {
1144 GST_TAG_PUBLISHER, add_text_tag, "TPUB"}, {
1145 GST_TAG_INTERPRETED_BY, add_text_tag, "TPE4"}, {
1146 GST_TAG_MUSICAL_KEY, add_text_tag, "TKEY"}, {
1148 /* Private frames */
1149 GST_TAG_PRIVATE_DATA, add_private_data_tag, "PRIV"}, {
1150 GST_ID3_DEMUX_TAG_ID3V2_FRAME, add_id3v2frame_tag, NULL}, {
1152 /* Track and album numbers */
1153 GST_TAG_TRACK_NUMBER, add_count_or_num_tag, "TRCK"}, {
1154 GST_TAG_TRACK_COUNT, add_count_or_num_tag, "TRCK"}, {
1155 GST_TAG_ALBUM_VOLUME_NUMBER, add_count_or_num_tag, "TPOS"}, {
1156 GST_TAG_ALBUM_VOLUME_COUNT, add_count_or_num_tag, "TPOS"}, {
1159 GST_TAG_COMMENT, add_comment_tag, NULL}, {
1160 GST_TAG_EXTENDED_COMMENT, add_comment_tag, NULL}, {
1163 GST_TAG_BEATS_PER_MINUTE, add_bpm_tag, NULL}, {
1166 GST_TAG_IMAGE, add_image_tag, NULL}, {
1167 GST_TAG_PREVIEW_IMAGE, add_image_tag, NULL}, {
1169 /* Misc user-defined text tags for IDs (and UFID frame) */
1170 GST_TAG_MUSICBRAINZ_ARTISTID, add_musicbrainz_tag, "\000"}, {
1171 GST_TAG_MUSICBRAINZ_ALBUMID, add_musicbrainz_tag, "\001"}, {
1172 GST_TAG_MUSICBRAINZ_ALBUMARTISTID, add_musicbrainz_tag, "\002"}, {
1173 GST_TAG_MUSICBRAINZ_TRMID, add_musicbrainz_tag, "\003"}, {
1174 GST_TAG_CDDA_MUSICBRAINZ_DISCID, add_musicbrainz_tag, "\004"}, {
1175 GST_TAG_CDDA_CDDB_DISCID, add_musicbrainz_tag, "\005"}, {
1176 GST_TAG_MUSICBRAINZ_TRACKID, add_unique_file_id_tag, NULL}, {
1178 /* Info about encoder */
1179 GST_TAG_ENCODER, add_encoder_tag, NULL}, {
1180 GST_TAG_ENCODER_VERSION, add_encoder_tag, NULL}, {
1183 GST_TAG_COPYRIGHT_URI, add_uri_tag, "WCOP"}, {
1184 GST_TAG_LICENSE_URI, add_uri_tag, "WCOP"}, {
1186 /* Up to here, all the frame ids and contents have been the same between
1187 versions 2.3 and 2.4. The rest of them differ... */
1188 /* Date (in ID3v2.3, this is a TYER tag. In v2.4, it's a TDRC tag */
1189 GST_TAG_DATE_TIME, add_date_tag, NULL}, {
1191 /* Replaygain data (not really supported in 2.3, we use an experimental
1193 GST_TAG_TRACK_PEAK, add_relative_volume_tag, NULL}, {
1194 GST_TAG_TRACK_GAIN, add_relative_volume_tag, NULL}, {
1195 GST_TAG_ALBUM_PEAK, add_relative_volume_tag, NULL}, {
1196 GST_TAG_ALBUM_GAIN, add_relative_volume_tag, NULL}, {
1198 /* Sortable version of various tags. These are all v2.4 ONLY */
1199 GST_TAG_ARTIST_SORTNAME, add_text_tag_v4, "TSOP"}, {
1200 GST_TAG_ALBUM_SORTNAME, add_text_tag_v4, "TSOA"}, {
1201 GST_TAG_TITLE_SORTNAME, add_text_tag_v4, "TSOT"}
1205 foreach_add_tag (const GstTagList * list, const gchar * tag, gpointer userdata)
1207 GstId3v2Tag *id3v2tag = (GstId3v2Tag *) userdata;
1210 num_tags = gst_tag_list_get_tag_size (list, tag);
1212 GST_LOG ("Processing tag %s (num=%u)", tag, num_tags);
1214 if (num_tags > 1 && gst_tag_is_fixed (tag)) {
1215 GST_WARNING ("Multiple occurrences of fixed tag '%s', ignoring some", tag);
1219 for (i = 0; i < G_N_ELEMENTS (add_funcs); ++i) {
1220 if (strcmp (add_funcs[i].gst_tag, tag) == 0) {
1221 add_funcs[i].func (id3v2tag, list, tag, num_tags, add_funcs[i].data);
1226 if (i == G_N_ELEMENTS (add_funcs)) {
1227 GST_WARNING ("Unsupported tag '%s' - not written", tag);
1232 id3_mux_render_v2_tag (GstTagMux * mux, const GstTagList * taglist, int version)
1237 if (!id3v2_tag_init (&tag, version)) {
1238 GST_WARNING_OBJECT (mux, "Unsupported version %d", version);
1242 /* Render the tag */
1243 gst_tag_list_foreach (taglist, foreach_add_tag, &tag);
1246 /* Do we want to add our own signature to the tag somewhere? */
1248 gchar *tag_producer_str;
1250 tag_producer_str = g_strdup_printf ("(GStreamer id3v2mux %s, using "
1251 "taglib %u.%u)", VERSION, TAGLIB_MAJOR_VERSION, TAGLIB_MINOR_VERSION);
1252 add_one_txxx_tag (id3v2tag, "tag_encoder", tag_producer_str);
1253 g_free (tag_producer_str);
1257 /* Create buffer with tag */
1258 buf = id3v2_tag_to_buffer (&tag);
1259 GST_LOG_OBJECT (mux, "tag size = %d bytes", (int) gst_buffer_get_size (buf));
1261 id3v2_tag_unset (&tag);
1266 #define ID3_V1_TAG_SIZE 128
1268 typedef void (*GstId3v1WriteFunc) (const GstTagList * list,
1269 const gchar * gst_tag, guint8 * dst, int len, gboolean * wrote_tag);
1272 latin1_convert (const GstTagList * list, const gchar * tag,
1273 guint8 * dst, int maxlen, gboolean * wrote_tag)
1279 if (!gst_tag_list_get_string (list, tag, &str) || str == NULL)
1282 /* Convert to Latin-1 (ISO-8859-1), replacing unrepresentable characters
1285 g_convert_with_fallback (str, -1, "ISO-8859-1", "UTF-8", (char *) "?",
1288 if (latin1 != NULL && *latin1 != '\0') {
1289 len = MIN (len, maxlen);
1290 memcpy (dst, latin1, len);
1299 date_v1_convert (const GstTagList * list, const gchar * tag,
1300 guint8 * dst, int maxlen, gboolean * wrote_tag)
1304 /* Only one date supported */
1305 if (gst_tag_list_get_date_time_index (list, tag, 0, &dt)) {
1306 guint year = gst_date_time_get_year (dt);
1307 /* Check for plausible year */
1308 if (year > 500 && year < 2100) {
1310 g_snprintf (str, 5, "%.4u", year);
1312 memcpy (dst, str, 4);
1314 GST_WARNING ("invalid year %u, skipping", year);
1317 gst_date_time_unref (dt);
1322 genre_v1_convert (const GstTagList * list, const gchar * tag,
1323 guint8 * dst, int maxlen, gboolean * wrote_tag)
1329 /* We only support one genre */
1330 if (!gst_tag_list_peek_string_index (list, tag, 0, &str) || str == NULL)
1333 max = gst_tag_id3_genre_count ();
1335 for (i = 0; i < max; i++) {
1336 const gchar *genre = gst_tag_id3_genre_get (i);
1337 if (g_str_equal (str, genre)) {
1343 if (genreidx >= 0 && genreidx <= 127) {
1344 *dst = (guint8) genreidx;
1350 track_number_convert (const GstTagList * list, const gchar * tag,
1351 guint8 * dst, int maxlen, gboolean * wrote_tag)
1355 /* We only support one track number */
1356 if (!gst_tag_list_get_uint_index (list, tag, 0, &tracknum))
1359 if (tracknum <= 127) {
1360 *dst = (guint8) tracknum;
1365 /* FIXME: get rid of silly table */
1368 const gchar *gst_tag;
1371 const GstId3v1WriteFunc func;
1374 GST_TAG_TITLE, 3, 30, latin1_convert}, {
1375 GST_TAG_ARTIST, 33, 30, latin1_convert}, {
1376 GST_TAG_ALBUM, 63, 30, latin1_convert}, {
1377 GST_TAG_DATE_TIME, 93, 4, date_v1_convert}, {
1378 GST_TAG_COMMENT, 97, 28, latin1_convert}, {
1379 /* Note: one-byte gap here */
1380 GST_TAG_TRACK_NUMBER, 126, 1, track_number_convert}, {
1381 GST_TAG_GENRE, 127, 1, genre_v1_convert}
1385 id3_mux_render_v1_tag (GstTagMux * mux, const GstTagList * taglist)
1390 gboolean wrote_tag = FALSE;
1393 buf = gst_buffer_new_allocate (NULL, ID3_V1_TAG_SIZE, NULL);
1394 gst_buffer_map (buf, &info, GST_MAP_WRITE);
1396 memset (data, 0, ID3_V1_TAG_SIZE);
1402 /* Genre #0 stands for 'Blues', so init genre field to an invalid number */
1405 for (i = 0; i < G_N_ELEMENTS (v1_funcs); i++) {
1406 v1_funcs[i].func (taglist, v1_funcs[i].gst_tag, data + v1_funcs[i].offset,
1407 v1_funcs[i].length, &wrote_tag);
1410 gst_buffer_unmap (buf, &info);
1413 GST_WARNING_OBJECT (mux, "no ID3v1 tag written (no suitable tags found)");
1414 gst_buffer_unref (buf);