tizen 2.0 init
[framework/multimedia/gst-plugins-good0.10.git] / ext / taglib / gstid3v2mux.cc
1 /* GStreamer taglib-based ID3v2 muxer
2  * Copyright (C) 2006 Christophe Fergeau <teuf@gnome.org>
3  * Copyright (C) 2006 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 /**
22  * SECTION:element-id3v2mux
23  * @see_also: #GstID3Demux, #GstTagSetter
24  *
25  * This element adds ID3v2 tags to the beginning of a stream using the taglib
26  * library. More precisely, the tags written are ID3 version 2.4.0 tags (which
27  * means in practice that some hardware players or outdated programs might not
28  * be able to read them properly).
29  *
30  * Applications can set the tags to write using the #GstTagSetter interface.
31  * Tags sent by upstream elements will be picked up automatically (and merged
32  * according to the merge mode set via the tag setter interface).
33  *
34  * <refsect2>
35  * <title>Example pipelines</title>
36  * |[
37  * gst-launch -v filesrc location=foo.ogg ! decodebin ! audioconvert ! lame ! id3v2mux ! filesink location=foo.mp3
38  * ]| A pipeline that transcodes a file from Ogg/Vorbis to mp3 format with an
39  * ID3v2 that contains the same as the the Ogg/Vorbis file. Make sure the
40  * Ogg/Vorbis file actually has comments to preserve.
41  * |[
42  * gst-launch -m filesrc location=foo.mp3 ! id3demux ! fakesink silent=TRUE 2&gt; /dev/null | grep taglist
43  * ]| Verify that tags have been written.
44  * </refsect2>
45  */
46
47 #ifdef HAVE_CONFIG_H
48 #include <config.h>
49 #endif
50
51 #include "gstid3v2mux.h"
52
53 #include <string.h>
54
55 #include <textidentificationframe.h>
56 #include <uniquefileidentifierframe.h>
57 #include <attachedpictureframe.h>
58 #include <relativevolumeframe.h>
59 #include <commentsframe.h>
60 #include <unknownframe.h>
61 #include <id3v2synchdata.h>
62 #include <id3v2tag.h>
63 #include <gst/tag/tag.h>
64
65 using namespace TagLib;
66
67 GST_DEBUG_CATEGORY_STATIC (gst_id3v2_mux_debug);
68 #define GST_CAT_DEFAULT gst_id3v2_mux_debug
69
70 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
71     GST_PAD_SRC,
72     GST_PAD_ALWAYS,
73     GST_STATIC_CAPS ("application/x-id3"));
74
75
76 GST_BOILERPLATE (GstId3v2Mux, gst_id3v2_mux, GstTagLibMux,
77     GST_TYPE_TAG_LIB_MUX);
78
79 static GstBuffer *gst_id3v2_mux_render_tag (GstTagLibMux * mux,
80     GstTagList * taglist);
81
82 static void
83 gst_id3v2_mux_base_init (gpointer g_class)
84 {
85   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
86
87   gst_element_class_add_static_pad_template (element_class, &src_template);
88
89   gst_element_class_set_details_simple (element_class,
90       "TagLib-based ID3v2 Muxer", "Formatter/Metadata",
91       "Adds an ID3v2 header to the beginning of MP3 files using taglib",
92       "Christophe Fergeau <teuf@gnome.org>");
93
94   GST_DEBUG_CATEGORY_INIT (gst_id3v2_mux_debug, "id3v2mux", 0,
95       "taglib-based ID3v2 tag muxer");
96 }
97
98 static void
99 gst_id3v2_mux_class_init (GstId3v2MuxClass * klass)
100 {
101   GST_TAG_LIB_MUX_CLASS (klass)->render_tag =
102       GST_DEBUG_FUNCPTR (gst_id3v2_mux_render_tag);
103 }
104
105 static void
106 gst_id3v2_mux_init (GstId3v2Mux * id3v2mux, GstId3v2MuxClass * id3v2mux_class)
107 {
108   /* nothing to do */
109 }
110
111 #if 0
112 static void
113 add_one_txxx_tag (ID3v2::Tag * id3v2tag, const gchar * key, const gchar * val)
114 {
115   ID3v2::UserTextIdentificationFrame * frame;
116
117   if (val == NULL)
118     return;
119
120   GST_DEBUG ("Setting %s to %s", key, val);
121   frame = new ID3v2::UserTextIdentificationFrame (String::UTF8);
122   id3v2tag->addFrame (frame);
123   frame->setDescription (key);
124   frame->setText (val);
125 }
126 #endif
127
128 typedef void (*GstId3v2MuxAddTagFunc) (ID3v2::Tag * id3v2tag,
129     const GstTagList * list,
130     const gchar * tag, guint num_tags, const gchar * data);
131
132 static void
133 add_encoder_tag (ID3v2::Tag * id3v2tag, const GstTagList * list,
134     const gchar * tag, guint num_tags, const gchar * unused)
135 {
136   TagLib::StringList string_list;
137   guint n;
138
139   /* ENCODER_VERSION is either handled with the ENCODER tag or not at all */
140   if (strcmp (tag, GST_TAG_ENCODER_VERSION) == 0)
141     return;
142
143   for (n = 0; n < num_tags; ++n) {
144     gchar *encoder = NULL;
145
146     if (gst_tag_list_get_string_index (list, tag, n, &encoder) && encoder) {
147       guint encoder_version;
148       gchar *s;
149
150       if (gst_tag_list_get_uint_index (list, GST_TAG_ENCODER_VERSION, n,
151               &encoder_version) && encoder_version > 0) {
152         s = g_strdup_printf ("%s %u", encoder, encoder_version);
153       } else {
154         s = g_strdup (encoder);
155       }
156
157       GST_LOG ("encoder[%u] = '%s'", n, s);
158       string_list.append (String (s, String::UTF8));
159       g_free (s);
160       g_free (encoder);
161     }
162   }
163
164   if (!string_list.isEmpty ()) {
165     ID3v2::TextIdentificationFrame * f;
166
167     f = new ID3v2::TextIdentificationFrame ("TSSE", String::UTF8);
168     id3v2tag->addFrame (f);
169     f->setText (string_list);
170   } else {
171     GST_WARNING ("Empty list for tag %s, skipping", tag);
172   }
173 }
174
175 static void
176 add_date_tag (ID3v2::Tag * id3v2tag, const GstTagList * list,
177     const gchar * tag, guint num_tags, const gchar * unused)
178 {
179   TagLib::StringList string_list;
180   guint n;
181
182   GST_LOG ("Adding date frame");
183
184   for (n = 0; n < num_tags; ++n) {
185     GDate *date = NULL;
186
187     if (gst_tag_list_get_date_index (list, tag, n, &date) && date != NULL) {
188       GDateYear year;
189       gchar *s;
190
191       year = g_date_get_year (date);
192       if (year > 500 && year < 2100) {
193         s = g_strdup_printf ("%u", year);
194         GST_LOG ("%s[%u] = '%s'", tag, n, s);
195         string_list.append (String (s, String::UTF8));
196         g_free (s);
197       } else {
198         GST_WARNING ("invalid year %u, skipping", year);
199       }
200
201       g_date_free (date);
202     }
203   }
204
205   if (!string_list.isEmpty ()) {
206     ID3v2::TextIdentificationFrame * f;
207
208     f = new ID3v2::TextIdentificationFrame ("TDRC", String::UTF8);
209     id3v2tag->addFrame (f);
210     f->setText (string_list);
211   } else {
212     GST_WARNING ("Empty list for tag %s, skipping", tag);
213   }
214 }
215
216 static void
217 add_count_or_num_tag (ID3v2::Tag * id3v2tag, const GstTagList * list,
218     const gchar * tag, guint num_tags, const gchar * frame_id)
219 {
220   static const struct
221   {
222     const gchar *gst_tag;
223     const gchar *corr_count;    /* corresponding COUNT tag (if number) */
224     const gchar *corr_num;      /* corresponding NUMBER tag (if count) */
225   } corr[] = {
226     {
227     GST_TAG_TRACK_NUMBER, GST_TAG_TRACK_COUNT, NULL}, {
228     GST_TAG_TRACK_COUNT, NULL, GST_TAG_TRACK_NUMBER}, {
229     GST_TAG_ALBUM_VOLUME_NUMBER, GST_TAG_ALBUM_VOLUME_COUNT, NULL}, {
230     GST_TAG_ALBUM_VOLUME_COUNT, NULL, GST_TAG_ALBUM_VOLUME_NUMBER}
231   };
232   guint idx;
233
234   for (idx = 0; idx < G_N_ELEMENTS (corr); ++idx) {
235     if (strcmp (corr[idx].gst_tag, tag) == 0)
236       break;
237   }
238
239   g_assert (idx < G_N_ELEMENTS (corr));
240   g_assert (frame_id && strlen (frame_id) == 4);
241
242   if (corr[idx].corr_num == NULL) {
243     guint number;
244
245     /* number tag */
246     if (gst_tag_list_get_uint_index (list, tag, 0, &number)) {
247       ID3v2::TextIdentificationFrame * frame;
248       gchar *tag_str;
249       guint count;
250
251       if (gst_tag_list_get_uint_index (list, corr[idx].corr_count, 0, &count))
252         tag_str = g_strdup_printf ("%u/%u", number, count);
253       else
254         tag_str = g_strdup_printf ("%u", number);
255
256       GST_DEBUG ("Setting %s to %s (frame_id = %s)", tag, tag_str, frame_id);
257       frame = new ID3v2::TextIdentificationFrame (frame_id, String::UTF8);
258       id3v2tag->addFrame (frame);
259       frame->setText (tag_str);
260       g_free (tag_str);
261     }
262   } else if (corr[idx].corr_count == NULL) {
263     guint count;
264
265     /* count tag */
266     if (gst_tag_list_get_uint_index (list, corr[idx].corr_num, 0, &count)) {
267       GST_DEBUG ("%s handled with %s, skipping", tag, corr[idx].corr_num);
268     } else if (gst_tag_list_get_uint_index (list, tag, 0, &count)) {
269       ID3v2::TextIdentificationFrame * frame;
270       gchar *tag_str;
271
272       tag_str = g_strdup_printf ("0/%u", count);
273       GST_DEBUG ("Setting %s to %s (frame_id = %s)", tag, tag_str, frame_id);
274       frame = new ID3v2::TextIdentificationFrame (frame_id, String::UTF8);
275       id3v2tag->addFrame (frame);
276       frame->setText (tag_str);
277       g_free (tag_str);
278     }
279   }
280
281   if (num_tags > 1) {
282     GST_WARNING ("more than one %s, can only handle one", tag);
283   }
284 }
285
286 static void
287 add_unique_file_id_tag (ID3v2::Tag * id3v2tag, const GstTagList * list,
288     const gchar * tag, guint num_tags, const gchar * unused)
289 {
290   const gchar *origin = "http://musicbrainz.org";
291   gchar *id_str = NULL;
292
293   if (gst_tag_list_get_string_index (list, tag, 0, &id_str) && id_str) {
294     ID3v2::UniqueFileIdentifierFrame * frame;
295
296     GST_LOG ("Adding %s (%s): %s", tag, origin, id_str);
297     frame = new ID3v2::UniqueFileIdentifierFrame (origin, id_str);
298     id3v2tag->addFrame (frame);
299     g_free (id_str);
300   }
301 }
302
303 static void
304 add_musicbrainz_tag (ID3v2::Tag * id3v2tag, const GstTagList * list,
305     const gchar * tag, guint num_tags, const gchar * data)
306 {
307   static const struct
308   {
309     const gchar gst_tag[28];
310     const gchar spec_id[28];
311     const gchar realworld_id[28];
312   } mb_ids[] = {
313     {
314     GST_TAG_MUSICBRAINZ_ARTISTID, "MusicBrainz Artist Id",
315           "musicbrainz_artistid"}, {
316     GST_TAG_MUSICBRAINZ_ALBUMID, "MusicBrainz Album Id", "musicbrainz_albumid"}, {
317     GST_TAG_MUSICBRAINZ_ALBUMARTISTID, "MusicBrainz Album Artist Id",
318           "musicbrainz_albumartistid"}, {
319     GST_TAG_MUSICBRAINZ_TRMID, "MusicBrainz TRM Id", "musicbrainz_trmid"}, {
320     GST_TAG_CDDA_MUSICBRAINZ_DISCID, "MusicBrainz DiscID",
321           "musicbrainz_discid"}, {
322       /* the following one is more or less made up, there seems to be little
323        * evidence that any popular application is actually putting this info
324        * into TXXX frames; the first one comes from a musicbrainz wiki 'proposed
325        * tags' page, the second one is analogue to the vorbis/ape/flac tag. */
326     GST_TAG_CDDA_CDDB_DISCID, "CDDB DiscID", "discid"}
327   };
328   guint i, idx;
329
330   idx = (guint8) data[0];
331   g_assert (idx < G_N_ELEMENTS (mb_ids));
332
333   for (i = 0; i < num_tags; ++i) {
334     ID3v2::UserTextIdentificationFrame * frame;
335     gchar *id_str;
336
337     if (gst_tag_list_get_string_index (list, tag, 0, &id_str) && id_str) {
338       GST_DEBUG ("Setting '%s' to '%s'", mb_ids[idx].spec_id, id_str);
339
340       /* add two frames, one with the ID the musicbrainz.org spec mentions
341        * and one with the ID that applications use in the real world */
342       frame = new ID3v2::UserTextIdentificationFrame (String::Latin1);
343       id3v2tag->addFrame (frame);
344       frame->setDescription (mb_ids[idx].spec_id);
345       frame->setText (id_str);
346
347       frame = new ID3v2::UserTextIdentificationFrame (String::Latin1);
348       id3v2tag->addFrame (frame);
349       frame->setDescription (mb_ids[idx].realworld_id);
350       frame->setText (id_str);
351
352       g_free (id_str);
353     }
354   }
355 }
356
357 static void
358 add_id3v2frame_tag (ID3v2::Tag * id3v2tag, const GstTagList * list,
359     const gchar * tag, guint num_tags, const gchar * frame_id)
360 {
361   ID3v2::FrameFactory * factory = ID3v2::FrameFactory::instance ();
362   guint i;
363
364   for (i = 0; i < num_tags; ++i) {
365     ID3v2::Frame * frame;
366     const GValue *val;
367     GstBuffer *buf;
368
369     val = gst_tag_list_get_value_index (list, tag, i);
370     buf = (GstBuffer *) gst_value_get_mini_object (val);
371
372     if (buf && GST_BUFFER_CAPS (buf)) {
373       GstStructure *s;
374       gint version = 0;
375
376       s = gst_caps_get_structure (GST_BUFFER_CAPS (buf), 0);
377       if (s && gst_structure_get_int (s, "version", &version) && version > 0) {
378         ByteVector bytes ((char *) GST_BUFFER_DATA (buf),
379             GST_BUFFER_SIZE (buf));
380
381         GST_DEBUG ("Injecting ID3v2.%u frame %u/%u of length %u and type %"
382             GST_PTR_FORMAT, version, i, num_tags, GST_BUFFER_SIZE (buf), s);
383
384         frame = factory->createFrame (bytes, (TagLib::uint) version);
385         if (frame)
386           id3v2tag->addFrame (frame);
387       }
388     }
389   }
390 }
391
392 static void
393 add_image_tag (ID3v2::Tag * id3v2tag, const GstTagList * list,
394     const gchar * tag, guint num_tags, const gchar * unused)
395 {
396   guint n;
397
398   for (n = 0; n < num_tags; ++n) {
399     const GValue *val;
400     GstBuffer *image;
401
402     GST_DEBUG ("image %u/%u", n + 1, num_tags);
403
404     val = gst_tag_list_get_value_index (list, tag, n);
405     image = (GstBuffer *) gst_value_get_mini_object (val);
406
407     if (GST_IS_BUFFER (image) && GST_BUFFER_SIZE (image) > 0 &&
408         GST_BUFFER_CAPS (image) != NULL &&
409         !gst_caps_is_empty (GST_BUFFER_CAPS (image))) {
410       const gchar *mime_type;
411       GstStructure *s;
412
413       s = gst_caps_get_structure (GST_BUFFER_CAPS (image), 0);
414       mime_type = gst_structure_get_name (s);
415       if (mime_type != NULL) {
416         ID3v2::AttachedPictureFrame * frame;
417         const gchar *desc;
418
419         if (strcmp (mime_type, "text/uri-list") == 0)
420           mime_type = "-->";
421
422         frame = new ID3v2::AttachedPictureFrame ();
423
424         GST_DEBUG ("Attaching picture of %u bytes and mime type %s",
425             GST_BUFFER_SIZE (image), mime_type);
426
427         id3v2tag->addFrame (frame);
428         frame->setPicture (ByteVector ((const char *) GST_BUFFER_DATA (image),
429                 GST_BUFFER_SIZE (image)));
430         frame->setTextEncoding (String::UTF8);
431         frame->setMimeType (mime_type);
432
433         desc = gst_structure_get_string (s, "image-description");
434         frame->setDescription ((desc) ? desc : "");
435
436         /* FIXME set image type properly from caps */
437         if (strcmp (tag, GST_TAG_PREVIEW_IMAGE) == 0) {
438           frame->setType (ID3v2::AttachedPictureFrame::FileIcon);
439         } else {
440           frame->setType (ID3v2::AttachedPictureFrame::Other);
441         }
442       }
443     } else {
444       GST_WARNING ("NULL image or no caps on image buffer (%p, caps=%"
445           GST_PTR_FORMAT ")", image, (image) ? GST_BUFFER_CAPS (image) : NULL);
446     }
447   }
448 }
449
450 static void
451 add_comment_tag (ID3v2::Tag * id3v2tag, const GstTagList * list,
452     const gchar * tag, guint num_tags, const gchar * unused)
453 {
454   TagLib::StringList string_list;
455   guint n;
456
457   GST_LOG ("Adding comment frames");
458   for (n = 0; n < num_tags; ++n) {
459     gchar *s = NULL;
460
461     if (gst_tag_list_get_string_index (list, tag, n, &s) && s != NULL) {
462       ID3v2::CommentsFrame * f;
463       gchar *desc = NULL, *val = NULL, *lang = NULL;
464
465       f = new ID3v2::CommentsFrame (String::UTF8);
466
467       if (strcmp (tag, GST_TAG_COMMENT) == 0 ||
468           !gst_tag_parse_extended_comment (s, &desc, &lang, &val, TRUE)) {
469         /* create dummy description to allow for multiple comment frames
470          * (taglib will drop comment frames if descriptions are not unique) */
471         desc = g_strdup_printf ("c%u", n);
472         val = g_strdup (s);
473       }
474
475       GST_LOG ("%s[%u] = '%s' (%s|%s|%s)", tag, n, s, GST_STR_NULL (desc),
476           GST_STR_NULL (lang), GST_STR_NULL (val));
477
478       f->setDescription (desc);
479       f->setText (val);
480       if (lang) {
481         f->setLanguage (lang);
482       }
483
484       g_free (lang);
485       g_free (desc);
486       g_free (val);
487
488       id3v2tag->addFrame (f);
489     }
490     g_free (s);
491   }
492 }
493
494 static void
495 add_text_tag (ID3v2::Tag * id3v2tag, const GstTagList * list,
496     const gchar * tag, guint num_tags, const gchar * frame_id)
497 {
498   ID3v2::TextIdentificationFrame * f;
499   TagLib::StringList string_list;
500   guint n;
501
502   GST_LOG ("Adding '%s' frame", frame_id);
503   for (n = 0; n < num_tags; ++n) {
504     gchar *s = NULL;
505
506     if (gst_tag_list_get_string_index (list, tag, n, &s) && s != NULL) {
507       GST_LOG ("%s: %s[%u] = '%s'", frame_id, tag, n, s);
508       string_list.append (String (s, String::UTF8));
509       g_free (s);
510     }
511   }
512
513   if (!string_list.isEmpty ()) {
514     f = new ID3v2::TextIdentificationFrame (frame_id, String::UTF8);
515     id3v2tag->addFrame (f);
516     f->setText (string_list);
517   } else {
518     GST_WARNING ("Empty list for tag %s, skipping", tag);
519   }
520 }
521
522 static void
523 add_uri_tag (ID3v2::Tag * id3v2tag, const GstTagList * list,
524     const gchar * tag, guint num_tags, const gchar * frame_id)
525 {
526   gchar *url = NULL;
527
528   g_assert (frame_id != NULL);
529
530   /* URI tags are limited to one of each per taglist */
531   if (gst_tag_list_get_string_index (list, tag, 0, &url) && url != NULL) {
532     guint url_len;
533
534     url_len = strlen (url);
535     if (url_len > 0 && gst_uri_is_valid (url)) {
536       ID3v2::FrameFactory * factory = ID3v2::FrameFactory::instance ();
537       ID3v2::Frame * frame;
538       char *data;
539
540       data = g_new0 (char, 10 + url_len);
541
542       memcpy (data, frame_id, 4);
543       memcpy (data + 4, ID3v2::SynchData::fromUInt (url_len).data (), 4);
544       memcpy (data + 10, url, url_len);
545       ByteVector bytes (data, 10 + url_len);
546
547       g_free (data);
548
549       frame = factory->createFrame (bytes, (TagLib::uint) 4);
550       if (frame) {
551         id3v2tag->addFrame (frame);
552
553         GST_LOG ("%s: %s = '%s'", frame_id, tag, url);
554       }
555     } else {
556       GST_WARNING ("Tag %s does not contain a valid URI (%s)", tag, url);
557     }
558
559     g_free (url);
560   }
561 }
562
563 static void
564 add_relative_volume_tag (ID3v2::Tag * id3v2tag, const GstTagList * list,
565     const gchar * tag, guint num_tags, const gchar * frame_id)
566 {
567   const char *gain_tag_name;
568   const char *peak_tag_name;
569   gdouble peak_val;
570   gdouble gain_val;
571   ID3v2::RelativeVolumeFrame * frame;
572
573   frame = new ID3v2::RelativeVolumeFrame ();
574
575   /* figure out tag names and the identification string to use */
576   if (strcmp (tag, GST_TAG_TRACK_PEAK) == 0 ||
577       strcmp (tag, GST_TAG_TRACK_GAIN) == 0) {
578     gain_tag_name = GST_TAG_TRACK_GAIN;
579     peak_tag_name = GST_TAG_TRACK_PEAK;
580     frame->setIdentification ("track");
581     GST_DEBUG ("adding track relative-volume frame");
582   } else {
583     gain_tag_name = GST_TAG_ALBUM_GAIN;
584     peak_tag_name = GST_TAG_ALBUM_PEAK;
585     frame->setIdentification ("album");
586     GST_DEBUG ("adding album relative-volume frame");
587   }
588   
589   /* find the value for the paired tag (gain, if this is peak, and
590    * vice versa).  if both tags exist, only write the frame when
591    * we're processing the peak tag.
592    */
593   if (strcmp (tag, GST_TAG_TRACK_PEAK) == 0 ||
594       strcmp (tag, GST_TAG_ALBUM_PEAK) == 0) {
595     ID3v2::RelativeVolumeFrame::PeakVolume encoded_peak;
596     short peak_int;
597
598     gst_tag_list_get_double (list, tag, &peak_val);
599
600     if (gst_tag_list_get_tag_size (list, gain_tag_name) > 0) {
601       gst_tag_list_get_double (list, gain_tag_name, &gain_val);
602       GST_DEBUG ("setting volume adjustment %g", gain_val);
603       frame->setVolumeAdjustment (gain_val);
604     }
605
606     /* copying mutagen: always write as 16 bits for sanity. */
607     peak_int = (short)(peak_val * G_MAXSHORT);
608     encoded_peak.bitsRepresentingPeak = 16;
609     encoded_peak.peakVolume = ByteVector::fromShort(peak_int, true);
610     GST_DEBUG ("setting peak value %g", peak_val);
611     frame->setPeakVolume(encoded_peak);
612
613   } else {
614     gst_tag_list_get_double (list, tag, &gain_val);
615     GST_DEBUG ("setting volume adjustment %g", gain_val);
616     frame->setVolumeAdjustment (gain_val);
617
618     if (gst_tag_list_get_tag_size (list, peak_tag_name) != 0) {
619       GST_DEBUG ("both gain and peak tags exist, not adding frame this time around");
620       delete frame;
621       return;
622     }
623   }
624
625   id3v2tag->addFrame (frame);
626 }
627
628 static void
629 add_bpm_tag (ID3v2::Tag * id3v2tag, const GstTagList * list,
630     const gchar * tag, guint num_tags, const gchar * frame_id)
631 {
632   gdouble bpm;
633
634   if (gst_tag_list_get_double_index (list, tag, 0, &bpm)) {
635     ID3v2::TextIdentificationFrame * frame;
636     gchar *tag_str;
637
638     tag_str = g_strdup_printf ("%u", (guint)bpm);
639
640     GST_DEBUG ("Setting %s to %s", tag, tag_str);
641     frame = new ID3v2::TextIdentificationFrame ("TBPM", String::UTF8);
642     id3v2tag->addFrame (frame);
643     frame->setText (tag_str);
644     g_free (tag_str);
645   }
646 }
647
648 /* id3demux produces these for frames it cannot parse */
649 #define GST_ID3_DEMUX_TAG_ID3V2_FRAME "private-id3v2-frame"
650
651 static const struct
652 {
653   const gchar *gst_tag;
654   const GstId3v2MuxAddTagFunc func;
655   const gchar data[5];
656 } add_funcs[] = {
657   {
658   GST_TAG_ARTIST, add_text_tag, "TPE1"}, {
659   GST_TAG_ALBUM_ARTIST, add_text_tag, "TPE2"}, {
660   GST_TAG_TITLE, add_text_tag, "TIT2"}, {
661   GST_TAG_ALBUM, add_text_tag, "TALB"}, {
662   GST_TAG_COPYRIGHT, add_text_tag, "TCOP"}, {
663   GST_TAG_COMPOSER, add_text_tag, "TCOM"}, {
664   GST_TAG_GENRE, add_text_tag, "TCON"}, {
665   GST_TAG_COMMENT, add_comment_tag, ""}, {
666   GST_TAG_EXTENDED_COMMENT, add_comment_tag, ""}, {
667   GST_TAG_DATE, add_date_tag, ""}, {
668   GST_TAG_IMAGE, add_image_tag, ""}, {
669   GST_TAG_PREVIEW_IMAGE, add_image_tag, ""}, {
670   GST_ID3_DEMUX_TAG_ID3V2_FRAME, add_id3v2frame_tag, ""}, {
671   GST_TAG_MUSICBRAINZ_ARTISTID, add_musicbrainz_tag, "\000"}, {
672   GST_TAG_MUSICBRAINZ_ALBUMID, add_musicbrainz_tag, "\001"}, {
673   GST_TAG_MUSICBRAINZ_ALBUMARTISTID, add_musicbrainz_tag, "\002"}, {
674   GST_TAG_MUSICBRAINZ_TRMID, add_musicbrainz_tag, "\003"}, {
675   GST_TAG_CDDA_MUSICBRAINZ_DISCID, add_musicbrainz_tag, "\004"}, {
676   GST_TAG_CDDA_CDDB_DISCID, add_musicbrainz_tag, "\005"}, {
677   GST_TAG_MUSICBRAINZ_TRACKID, add_unique_file_id_tag, ""}, {
678   GST_TAG_ARTIST_SORTNAME, add_text_tag, "TSOP"}, {
679   GST_TAG_ALBUM_SORTNAME, add_text_tag, "TSOA"}, {
680   GST_TAG_TITLE_SORTNAME, add_text_tag, "TSOT"}, {
681   GST_TAG_TRACK_NUMBER, add_count_or_num_tag, "TRCK"}, {
682   GST_TAG_TRACK_COUNT, add_count_or_num_tag, "TRCK"}, {
683   GST_TAG_ALBUM_VOLUME_NUMBER, add_count_or_num_tag, "TPOS"}, {
684   GST_TAG_ALBUM_VOLUME_COUNT, add_count_or_num_tag, "TPOS"}, {
685   GST_TAG_ENCODER, add_encoder_tag, ""}, {
686   GST_TAG_ENCODER_VERSION, add_encoder_tag, ""}, {
687   GST_TAG_COPYRIGHT_URI, add_uri_tag, "WCOP"}, {
688   GST_TAG_LICENSE_URI, add_uri_tag, "WCOP"}, {
689   GST_TAG_TRACK_PEAK, add_relative_volume_tag, ""}, {
690   GST_TAG_TRACK_GAIN, add_relative_volume_tag, ""}, {
691   GST_TAG_ALBUM_PEAK, add_relative_volume_tag, ""}, {
692   GST_TAG_ALBUM_GAIN, add_relative_volume_tag, ""}, {
693   GST_TAG_BEATS_PER_MINUTE, add_bpm_tag, ""}
694 };
695
696
697 static void
698 foreach_add_tag (const GstTagList * list, const gchar * tag, gpointer userdata)
699 {
700   ID3v2::Tag * id3v2tag = (ID3v2::Tag *) userdata;
701   TagLib::StringList string_list;
702   guint num_tags, i;
703
704   num_tags = gst_tag_list_get_tag_size (list, tag);
705
706   GST_LOG ("Processing tag %s (num=%u)", tag, num_tags);
707
708   if (num_tags > 1 && gst_tag_is_fixed (tag)) {
709     GST_WARNING ("Multiple occurences of fixed tag '%s', ignoring some", tag);
710     num_tags = 1;
711   }
712
713   for (i = 0; i < G_N_ELEMENTS (add_funcs); ++i) {
714     if (strcmp (add_funcs[i].gst_tag, tag) == 0) {
715       add_funcs[i].func (id3v2tag, list, tag, num_tags, add_funcs[i].data);
716       break;
717     }
718   }
719
720   if (i == G_N_ELEMENTS (add_funcs)) {
721     GST_WARNING ("Unsupported tag '%s' - not written", tag);
722   }
723 }
724
725 static GstBuffer *
726 gst_id3v2_mux_render_tag (GstTagLibMux * mux, GstTagList * taglist)
727 {
728   ID3v2::Tag id3v2tag;
729   ByteVector rendered_tag;
730   GstBuffer *buf;
731   guint tag_size;
732
733   /* write all strings as UTF-8 by default */
734   TagLib::ID3v2::FrameFactory::instance ()->
735       setDefaultTextEncoding (TagLib::String::UTF8);
736
737   /* Render the tag */
738   gst_tag_list_foreach (taglist, foreach_add_tag, &id3v2tag);
739
740 #if 0
741   /* Do we want to add our own signature to the tag somewhere? */
742   {
743     gchar *tag_producer_str;
744
745     tag_producer_str = g_strdup_printf ("(GStreamer id3v2mux %s, using "
746         "taglib %u.%u)", VERSION, TAGLIB_MAJOR_VERSION, TAGLIB_MINOR_VERSION);
747     add_one_txxx_tag (id3v2tag, "tag_encoder", tag_producer_str);
748     g_free (tag_producer_str);
749   }
750 #endif
751
752   rendered_tag = id3v2tag.render ();
753   tag_size = rendered_tag.size ();
754
755   GST_LOG_OBJECT (mux, "tag size = %d bytes", tag_size);
756
757   /* Create buffer with tag */
758   buf = gst_buffer_new_and_alloc (tag_size);
759   memcpy (GST_BUFFER_DATA (buf), rendered_tag.data (), tag_size);
760   gst_buffer_set_caps (buf, GST_PAD_CAPS (mux->srcpad));
761
762   return buf;
763 }
764
765 gboolean
766 gst_id3v2_mux_plugin_init (GstPlugin * plugin)
767 {
768   if (!gst_element_register (plugin, "id3v2mux", GST_RANK_NONE,
769           GST_TYPE_ID3V2_MUX))
770     return FALSE;
771
772   gst_tag_register_musicbrainz_tags ();
773
774   return TRUE;
775 }