Merge branch 'master' into 0.11
[platform/upstream/gst-plugins-good.git] / ext / taglib / gstapev2mux.cc
1 /* GStreamer taglib-based APEv2 muxer
2  * Copyright (C) 2006 Christophe Fergeau <teuf@gnome.org>
3  * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
4  * Copyright (C) 2006 Sebastian Dröge <slomo@circular-chaos.org>
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., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 /**
23  * SECTION:element-apev2mux
24  * @see_also: #GstTagSetter
25  *
26  * This element adds APEv2 tags to the beginning of a stream using the taglib
27  * library.
28  *
29  * Applications can set the tags to write using the #GstTagSetter interface.
30  * Tags sent by upstream elements will be picked up automatically (and merged
31  * according to the merge mode set via the tag setter interface).
32  *
33  * <refsect2>
34  * <title>Example pipelines</title>
35  * |[
36  * gst-launch -v filesrc location=foo.ogg ! decodebin ! audioconvert ! lame ! apev2mux ! filesink location=foo.mp3
37  * ]| A pipeline that transcodes a file from Ogg/Vorbis to mp3 format with an
38  * APEv2 that contains the same as the the Ogg/Vorbis file. Make sure the
39  * Ogg/Vorbis file actually has comments to preserve.
40  * |[
41  * gst-launch -m filesrc location=foo.mp3 ! apedemux ! fakesink silent=TRUE 2&gt; /dev/null | grep taglist
42  * ]| Verify that tags have been written.
43  * </refsect2>
44  */
45
46 #ifdef HAVE_CONFIG_H
47 #include <config.h>
48 #endif
49
50 #include "gstapev2mux.h"
51
52 #include <string.h>
53
54 #include <apetag.h>
55 #include <gst/tag/tag.h>
56
57 using namespace TagLib;
58
59 GST_DEBUG_CATEGORY_STATIC (gst_apev2_mux_debug);
60 #define GST_CAT_DEFAULT gst_apev2_mux_debug
61
62 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
63     GST_PAD_SRC,
64     GST_PAD_ALWAYS,
65     GST_STATIC_CAPS ("application/x-apetag"));
66
67 G_DEFINE_TYPE (GstApev2Mux, gst_apev2_mux, GST_TYPE_TAG_LIB_MUX);
68
69 static GstBuffer *gst_apev2_mux_render_tag (GstTagLibMux * mux,
70     GstTagList * taglist);
71
72 static void
73 gst_apev2_mux_class_init (GstApev2MuxClass * klass)
74 {
75   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
76
77   GST_TAG_LIB_MUX_CLASS (klass)->render_tag =
78       GST_DEBUG_FUNCPTR (gst_apev2_mux_render_tag);
79
80   gst_element_class_add_pad_template (element_class,
81       gst_static_pad_template_get (&src_template));
82
83   gst_element_class_set_details_simple (element_class,
84       "TagLib-based APEv2 Muxer", "Formatter/Metadata",
85       "Adds an APEv2 header to the beginning of files using taglib",
86       "Sebastian Dröge <slomo@circular-chaos.org>");
87
88   GST_DEBUG_CATEGORY_INIT (gst_apev2_mux_debug, "apev2mux", 0,
89       "taglib-based APEv2 tag muxer");
90 }
91
92 static void
93 gst_apev2_mux_init (GstApev2Mux * apev2mux)
94 {
95   /* nothing to do */
96 }
97
98 static void
99 add_one_tag (const GstTagList * list, const gchar * tag, gpointer user_data)
100 {
101   APE::Tag * apev2tag = (APE::Tag *) user_data;
102   gboolean result;
103
104   /* FIXME: if there are several values set for the same tag, this won't
105    * work, only the first value will be taken into account
106    */
107   if (strcmp (tag, GST_TAG_TITLE) == 0) {
108     const char *title;
109
110     result = gst_tag_list_peek_string_index (list, tag, 0, &title);
111     if (result != FALSE) {
112       GST_DEBUG ("Setting title to %s", title);
113       apev2tag->setTitle (String (title, String::UTF8));
114     }
115   } else if (strcmp (tag, GST_TAG_ALBUM) == 0) {
116     const char *album;
117
118     result = gst_tag_list_peek_string_index (list, tag, 0, &album);
119     if (result != FALSE) {
120       GST_DEBUG ("Setting album to %s", album);
121       apev2tag->setAlbum (String (album, String::UTF8));
122     }
123   } else if (strcmp (tag, GST_TAG_ARTIST) == 0) {
124     const char *artist;
125
126     result = gst_tag_list_peek_string_index (list, tag, 0, &artist);
127     if (result != FALSE) {
128       GST_DEBUG ("Setting artist to %s", artist);
129       apev2tag->setArtist (String (artist, String::UTF8));
130     }
131   } else if (strcmp (tag, GST_TAG_COMPOSER) == 0) {
132     const char *composer;
133
134     result = gst_tag_list_peek_string_index (list, tag, 0, &composer);
135     if (result != FALSE) {
136       GST_DEBUG ("Setting composer to %s", composer);
137       apev2tag->addValue (String ("COMPOSER", String::UTF8),
138           String (composer, String::UTF8));
139     }
140   } else if (strcmp (tag, GST_TAG_GENRE) == 0) {
141     const char *genre;
142
143     result = gst_tag_list_peek_string_index (list, tag, 0, &genre);
144     if (result != FALSE) {
145       GST_DEBUG ("Setting genre to %s", genre);
146       apev2tag->setGenre (String (genre, String::UTF8));
147     }
148   } else if (strcmp (tag, GST_TAG_COMMENT) == 0) {
149     const char *comment;
150
151     result = gst_tag_list_peek_string_index (list, tag, 0, &comment);
152     if (result != FALSE) {
153       GST_DEBUG ("Setting comment to %s", comment);
154       apev2tag->setComment (String (comment, String::UTF8));
155     }
156   } else if (strcmp (tag, GST_TAG_DATE) == 0) {
157     GDate *date;
158
159     result = gst_tag_list_get_date_index (list, tag, 0, &date);
160     if (result != FALSE) {
161       GDateYear year;
162
163       year = g_date_get_year (date);
164       GST_DEBUG ("Setting track year to %d", year);
165       apev2tag->setYear (year);
166       g_date_free (date);
167     }
168   } else if (strcmp (tag, GST_TAG_TRACK_NUMBER) == 0) {
169     guint track_number;
170
171     result = gst_tag_list_get_uint_index (list, tag, 0, &track_number);
172     if (result != FALSE) {
173       guint total_tracks;
174
175       result = gst_tag_list_get_uint_index (list, GST_TAG_TRACK_COUNT,
176           0, &total_tracks);
177       if (result) {
178         gchar *tag_str;
179
180         tag_str = g_strdup_printf ("%d/%d", track_number, total_tracks);
181         GST_DEBUG ("Setting track number to %s", tag_str);
182         apev2tag->addValue (String ("TRACK", String::UTF8),
183             String (tag_str, String::UTF8), true);
184         g_free (tag_str);
185       } else {
186         GST_DEBUG ("Setting track number to %d", track_number);
187         apev2tag->setTrack (track_number);
188       }
189     }
190   } else if (strcmp (tag, GST_TAG_TRACK_COUNT) == 0) {
191     guint n;
192
193     if (gst_tag_list_get_uint_index (list, GST_TAG_TRACK_NUMBER, 0, &n)) {
194       GST_DEBUG ("track-count handled with track-number, skipping");
195     } else if (gst_tag_list_get_uint_index (list, GST_TAG_TRACK_COUNT, 0, &n)) {
196       gchar *tag_str;
197
198       tag_str = g_strdup_printf ("0/%d", n);
199       GST_DEBUG ("Setting track number to %s", tag_str);
200       apev2tag->addValue (String ("TRACK", String::UTF8),
201           String (tag_str, String::UTF8), true);
202       g_free (tag_str);
203     }
204 #if 0
205   } else if (strcmp (tag, GST_TAG_ALBUM_VOLUME_NUMBER) == 0) {
206     guint volume_number;
207
208     result = gst_tag_list_get_uint_index (list, tag, 0, &volume_number);
209
210     if (result != FALSE) {
211       guint volume_count;
212       gchar *tag_str;
213
214       result = gst_tag_list_get_uint_index (list, GST_TAG_ALBUM_VOLUME_COUNT,
215           0, &volume_count);
216
217       if (result) {
218         tag_str = g_strdup_printf ("CD %d/%d", volume_number, volume_count);
219       } else {
220         tag_str = g_strdup_printf ("CD %d", volume_number);
221       }
222
223       GST_DEBUG ("Setting album number to %s", tag_str);
224
225       apev2tag->addValue (String ("MEDIA", String::UTF8),
226           String (tag_str, String::UTF8), true);
227       g_free (tag_str);
228     }
229   } else if (strcmp (tag, GST_TAG_ALBUM_VOLUME_COUNT) == 0) {
230     guint n;
231
232     if (gst_tag_list_get_uint_index (list, GST_TAG_ALBUM_VOLUME_NUMBER, 0, &n)) {
233       GST_DEBUG ("volume-count handled with volume-number, skipping");
234     } else if (gst_tag_list_get_uint_index (list, GST_TAG_ALBUM_VOLUME_COUNT,
235             0, &n)) {
236       gchar *tag_str;
237
238       tag_str = g_strdup_printf ("CD 0/%u", n);
239       GST_DEBUG ("Setting album volume number/count to %s", tag_str);
240
241       apev2tag->addValue (String ("MEDIA", String::UTF8),
242           String (tag_str, String::UTF8), true);
243       g_free (tag_str);
244     }
245 #endif
246   } else if (strcmp (tag, GST_TAG_COPYRIGHT) == 0) {
247     const gchar *copyright;
248
249     result = gst_tag_list_peek_string_index (list, tag, 0, &copyright);
250
251     if (result != FALSE) {
252       GST_DEBUG ("Setting copyright to %s", copyright);
253       apev2tag->addValue (String ("COPYRIGHT", String::UTF8),
254           String (copyright, String::UTF8), true);
255     }
256   } else if (strcmp (tag, GST_TAG_LOCATION) == 0) {
257     const gchar *location;
258
259     result = gst_tag_list_peek_string_index (list, tag, 0, &location);
260
261     if (result != FALSE) {
262       GST_DEBUG ("Setting location to %s", location);
263       apev2tag->addValue (String ("FILE", String::UTF8),
264           String (location, String::UTF8), true);
265     }
266   } else if (strcmp (tag, GST_TAG_ISRC) == 0) {
267     const gchar *isrc;
268
269     result = gst_tag_list_peek_string_index (list, tag, 0, &isrc);
270
271     if (result != FALSE) {
272       GST_DEBUG ("Setting ISRC to %s", isrc);
273       apev2tag->addValue (String ("ISRC", String::UTF8),
274           String (isrc, String::UTF8), true);
275     }
276   } else if (strcmp (tag, GST_TAG_TRACK_GAIN) == 0) {
277     gdouble value;
278
279     result = gst_tag_list_get_double_index (list, tag, 0, &value);
280
281     if (result != FALSE) {
282       gchar *track_gain = (gchar *) g_malloc0 (G_ASCII_DTOSTR_BUF_SIZE);
283
284       track_gain = g_ascii_dtostr (track_gain, G_ASCII_DTOSTR_BUF_SIZE, value);
285       GST_DEBUG ("Setting track gain to %s", track_gain);
286       apev2tag->addValue (String ("REPLAYGAIN_TRACK_GAIN",
287               String::UTF8), String (track_gain, String::UTF8), true);
288       g_free (track_gain);
289     }
290   } else if (strcmp (tag, GST_TAG_TRACK_PEAK) == 0) {
291     gdouble value;
292
293     result = gst_tag_list_get_double_index (list, tag, 0, &value);
294
295     if (result != FALSE) {
296       gchar *track_peak = (gchar *) g_malloc0 (G_ASCII_DTOSTR_BUF_SIZE);
297
298       track_peak = g_ascii_dtostr (track_peak, G_ASCII_DTOSTR_BUF_SIZE, value);
299       GST_DEBUG ("Setting track peak to %s", track_peak);
300       apev2tag->addValue (String ("REPLAYGAIN_TRACK_PEAK",
301               String::UTF8), String (track_peak, String::UTF8), true);
302       g_free (track_peak);
303     }
304   } else if (strcmp (tag, GST_TAG_ALBUM_GAIN) == 0) {
305     gdouble value;
306
307     result = gst_tag_list_get_double_index (list, tag, 0, &value);
308
309     if (result != FALSE) {
310       gchar *album_gain = (gchar *) g_malloc0 (G_ASCII_DTOSTR_BUF_SIZE);
311
312       album_gain = g_ascii_dtostr (album_gain, G_ASCII_DTOSTR_BUF_SIZE, value);
313       GST_DEBUG ("Setting album gain to %s", album_gain);
314       apev2tag->addValue (String ("REPLAYGAIN_ALBUM_GAIN",
315               String::UTF8), String (album_gain, String::UTF8), true);
316       g_free (album_gain);
317     }
318   } else if (strcmp (tag, GST_TAG_ALBUM_PEAK) == 0) {
319     gdouble value;
320
321     result = gst_tag_list_get_double_index (list, tag, 0, &value);
322
323     if (result != FALSE) {
324       gchar *album_peak = (gchar *) g_malloc0 (G_ASCII_DTOSTR_BUF_SIZE);
325
326       album_peak = g_ascii_dtostr (album_peak, G_ASCII_DTOSTR_BUF_SIZE, value);
327       GST_DEBUG ("Setting album peak to %s", album_peak);
328       apev2tag->addValue (String ("REPLAYGAIN_ALBUM_PEAK",
329               String::UTF8), String (album_peak, String::UTF8), true);
330       g_free (album_peak);
331     }
332   } else {
333     GST_WARNING ("Unsupported tag: %s", tag);
334   }
335 }
336
337 static GstBuffer *
338 gst_apev2_mux_render_tag (GstTagLibMux * mux, GstTagList * taglist)
339 {
340   APE::Tag apev2tag;
341   ByteVector rendered_tag;
342   GstBuffer *buf;
343   guint tag_size;
344
345   /* Render the tag */
346   gst_tag_list_foreach (taglist, add_one_tag, &apev2tag);
347
348   rendered_tag = apev2tag.render ();
349   tag_size = rendered_tag.size ();
350
351   GST_LOG_OBJECT (mux, "tag size = %d bytes", tag_size);
352
353   /* Create buffer with tag */
354   buf = gst_buffer_new_and_alloc (tag_size);
355   gst_buffer_fill (buf, 0, rendered_tag.data (), tag_size);
356
357   return buf;
358 }
359
360 gboolean
361 gst_apev2_mux_plugin_init (GstPlugin * plugin)
362 {
363   if (!gst_element_register (plugin, "apev2mux", GST_RANK_NONE,
364           GST_TYPE_APEV2_MUX))
365     return FALSE;
366
367   return TRUE;
368 }