Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.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 GST_BOILERPLATE (GstApev2Mux, gst_apev2_mux, GstTagLibMux,
68     GST_TYPE_TAG_LIB_MUX);
69
70 static GstBuffer *gst_apev2_mux_render_tag (GstTagLibMux * mux,
71     GstTagList * taglist);
72
73 static void
74 gst_apev2_mux_base_init (gpointer g_class)
75 {
76   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
77
78   gst_element_class_add_static_pad_template (element_class, &src_template);
79
80   gst_element_class_set_details_simple (element_class,
81       "TagLib-based APEv2 Muxer", "Formatter/Metadata",
82       "Adds an APEv2 header to the beginning of files using taglib",
83       "Sebastian Dröge <slomo@circular-chaos.org>");
84
85   GST_DEBUG_CATEGORY_INIT (gst_apev2_mux_debug, "apev2mux", 0,
86       "taglib-based APEv2 tag muxer");
87 }
88
89 static void
90 gst_apev2_mux_class_init (GstApev2MuxClass * klass)
91 {
92   GST_TAG_LIB_MUX_CLASS (klass)->render_tag =
93       GST_DEBUG_FUNCPTR (gst_apev2_mux_render_tag);
94 }
95
96 static void
97 gst_apev2_mux_init (GstApev2Mux * apev2mux, GstApev2MuxClass * apev2mux_class)
98 {
99   /* nothing to do */
100 }
101
102 static void
103 add_one_tag (const GstTagList * list, const gchar * tag, gpointer user_data)
104 {
105   APE::Tag * apev2tag = (APE::Tag *) user_data;
106   gboolean result;
107
108   /* FIXME: if there are several values set for the same tag, this won't
109    * work, only the first value will be taken into account
110    */
111   if (strcmp (tag, GST_TAG_TITLE) == 0) {
112     const char *title;
113
114     result = gst_tag_list_peek_string_index (list, tag, 0, &title);
115     if (result != FALSE) {
116       GST_DEBUG ("Setting title to %s", title);
117       apev2tag->setTitle (String (title, String::UTF8));
118     }
119   } else if (strcmp (tag, GST_TAG_ALBUM) == 0) {
120     const char *album;
121
122     result = gst_tag_list_peek_string_index (list, tag, 0, &album);
123     if (result != FALSE) {
124       GST_DEBUG ("Setting album to %s", album);
125       apev2tag->setAlbum (String (album, String::UTF8));
126     }
127   } else if (strcmp (tag, GST_TAG_ARTIST) == 0) {
128     const char *artist;
129
130     result = gst_tag_list_peek_string_index (list, tag, 0, &artist);
131     if (result != FALSE) {
132       GST_DEBUG ("Setting artist to %s", artist);
133       apev2tag->setArtist (String (artist, String::UTF8));
134     }
135   } else if (strcmp (tag, GST_TAG_COMPOSER) == 0) {
136     const char *composer;
137
138     result = gst_tag_list_peek_string_index (list, tag, 0, &composer);
139     if (result != FALSE) {
140       GST_DEBUG ("Setting composer to %s", composer);
141       apev2tag->addValue (String ("COMPOSER", String::UTF8),
142           String (composer, String::UTF8));
143     }
144   } else if (strcmp (tag, GST_TAG_GENRE) == 0) {
145     const char *genre;
146
147     result = gst_tag_list_peek_string_index (list, tag, 0, &genre);
148     if (result != FALSE) {
149       GST_DEBUG ("Setting genre to %s", genre);
150       apev2tag->setGenre (String (genre, String::UTF8));
151     }
152   } else if (strcmp (tag, GST_TAG_COMMENT) == 0) {
153     const char *comment;
154
155     result = gst_tag_list_peek_string_index (list, tag, 0, &comment);
156     if (result != FALSE) {
157       GST_DEBUG ("Setting comment to %s", comment);
158       apev2tag->setComment (String (comment, String::UTF8));
159     }
160   } else if (strcmp (tag, GST_TAG_DATE) == 0) {
161     GDate *date;
162
163     result = gst_tag_list_get_date_index (list, tag, 0, &date);
164     if (result != FALSE) {
165       GDateYear year;
166
167       year = g_date_get_year (date);
168       GST_DEBUG ("Setting track year to %d", year);
169       apev2tag->setYear (year);
170       g_date_free (date);
171     }
172   } else if (strcmp (tag, GST_TAG_TRACK_NUMBER) == 0) {
173     guint track_number;
174
175     result = gst_tag_list_get_uint_index (list, tag, 0, &track_number);
176     if (result != FALSE) {
177       guint total_tracks;
178
179       result = gst_tag_list_get_uint_index (list, GST_TAG_TRACK_COUNT,
180           0, &total_tracks);
181       if (result) {
182         gchar *tag_str;
183
184         tag_str = g_strdup_printf ("%d/%d", track_number, total_tracks);
185         GST_DEBUG ("Setting track number to %s", tag_str);
186         apev2tag->addValue (String ("TRACK", String::UTF8),
187             String (tag_str, String::UTF8), true);
188         g_free (tag_str);
189       } else {
190         GST_DEBUG ("Setting track number to %d", track_number);
191         apev2tag->setTrack (track_number);
192       }
193     }
194   } else if (strcmp (tag, GST_TAG_TRACK_COUNT) == 0) {
195     guint n;
196
197     if (gst_tag_list_get_uint_index (list, GST_TAG_TRACK_NUMBER, 0, &n)) {
198       GST_DEBUG ("track-count handled with track-number, skipping");
199     } else if (gst_tag_list_get_uint_index (list, GST_TAG_TRACK_COUNT, 0, &n)) {
200       gchar *tag_str;
201
202       tag_str = g_strdup_printf ("0/%d", n);
203       GST_DEBUG ("Setting track number to %s", tag_str);
204       apev2tag->addValue (String ("TRACK", String::UTF8),
205           String (tag_str, String::UTF8), true);
206       g_free (tag_str);
207     }
208 #if 0
209   } else if (strcmp (tag, GST_TAG_ALBUM_VOLUME_NUMBER) == 0) {
210     guint volume_number;
211
212     result = gst_tag_list_get_uint_index (list, tag, 0, &volume_number);
213
214     if (result != FALSE) {
215       guint volume_count;
216       gchar *tag_str;
217
218       result = gst_tag_list_get_uint_index (list, GST_TAG_ALBUM_VOLUME_COUNT,
219           0, &volume_count);
220
221       if (result) {
222         tag_str = g_strdup_printf ("CD %d/%d", volume_number, volume_count);
223       } else {
224         tag_str = g_strdup_printf ("CD %d", volume_number);
225       }
226
227       GST_DEBUG ("Setting album number to %s", tag_str);
228
229       apev2tag->addValue (String ("MEDIA", String::UTF8),
230           String (tag_str, String::UTF8), true);
231       g_free (tag_str);
232     }
233   } else if (strcmp (tag, GST_TAG_ALBUM_VOLUME_COUNT) == 0) {
234     guint n;
235
236     if (gst_tag_list_get_uint_index (list, GST_TAG_ALBUM_VOLUME_NUMBER, 0, &n)) {
237       GST_DEBUG ("volume-count handled with volume-number, skipping");
238     } else if (gst_tag_list_get_uint_index (list, GST_TAG_ALBUM_VOLUME_COUNT,
239             0, &n)) {
240       gchar *tag_str;
241
242       tag_str = g_strdup_printf ("CD 0/%u", n);
243       GST_DEBUG ("Setting album volume number/count to %s", tag_str);
244
245       apev2tag->addValue (String ("MEDIA", String::UTF8),
246           String (tag_str, String::UTF8), true);
247       g_free (tag_str);
248     }
249 #endif
250   } else if (strcmp (tag, GST_TAG_COPYRIGHT) == 0) {
251     const gchar *copyright;
252
253     result = gst_tag_list_peek_string_index (list, tag, 0, &copyright);
254
255     if (result != FALSE) {
256       GST_DEBUG ("Setting copyright to %s", copyright);
257       apev2tag->addValue (String ("COPYRIGHT", String::UTF8),
258           String (copyright, String::UTF8), true);
259     }
260   } else if (strcmp (tag, GST_TAG_LOCATION) == 0) {
261     const gchar *location;
262
263     result = gst_tag_list_peek_string_index (list, tag, 0, &location);
264
265     if (result != FALSE) {
266       GST_DEBUG ("Setting location to %s", location);
267       apev2tag->addValue (String ("FILE", String::UTF8),
268           String (location, String::UTF8), true);
269     }
270   } else if (strcmp (tag, GST_TAG_ISRC) == 0) {
271     const gchar *isrc;
272
273     result = gst_tag_list_peek_string_index (list, tag, 0, &isrc);
274
275     if (result != FALSE) {
276       GST_DEBUG ("Setting ISRC to %s", isrc);
277       apev2tag->addValue (String ("ISRC", String::UTF8),
278           String (isrc, String::UTF8), true);
279     }
280   } else if (strcmp (tag, GST_TAG_TRACK_GAIN) == 0) {
281     gdouble value;
282
283     result = gst_tag_list_get_double_index (list, tag, 0, &value);
284
285     if (result != FALSE) {
286       gchar *track_gain = (gchar *) g_malloc0 (G_ASCII_DTOSTR_BUF_SIZE);
287
288       track_gain = g_ascii_dtostr (track_gain, G_ASCII_DTOSTR_BUF_SIZE, value);
289       GST_DEBUG ("Setting track gain to %s", track_gain);
290       apev2tag->addValue (String ("REPLAYGAIN_TRACK_GAIN",
291               String::UTF8), String (track_gain, String::UTF8), true);
292       g_free (track_gain);
293     }
294   } else if (strcmp (tag, GST_TAG_TRACK_PEAK) == 0) {
295     gdouble value;
296
297     result = gst_tag_list_get_double_index (list, tag, 0, &value);
298
299     if (result != FALSE) {
300       gchar *track_peak = (gchar *) g_malloc0 (G_ASCII_DTOSTR_BUF_SIZE);
301
302       track_peak = g_ascii_dtostr (track_peak, G_ASCII_DTOSTR_BUF_SIZE, value);
303       GST_DEBUG ("Setting track peak to %s", track_peak);
304       apev2tag->addValue (String ("REPLAYGAIN_TRACK_PEAK",
305               String::UTF8), String (track_peak, String::UTF8), true);
306       g_free (track_peak);
307     }
308   } else if (strcmp (tag, GST_TAG_ALBUM_GAIN) == 0) {
309     gdouble value;
310
311     result = gst_tag_list_get_double_index (list, tag, 0, &value);
312
313     if (result != FALSE) {
314       gchar *album_gain = (gchar *) g_malloc0 (G_ASCII_DTOSTR_BUF_SIZE);
315
316       album_gain = g_ascii_dtostr (album_gain, G_ASCII_DTOSTR_BUF_SIZE, value);
317       GST_DEBUG ("Setting album gain to %s", album_gain);
318       apev2tag->addValue (String ("REPLAYGAIN_ALBUM_GAIN",
319               String::UTF8), String (album_gain, String::UTF8), true);
320       g_free (album_gain);
321     }
322   } else if (strcmp (tag, GST_TAG_ALBUM_PEAK) == 0) {
323     gdouble value;
324
325     result = gst_tag_list_get_double_index (list, tag, 0, &value);
326
327     if (result != FALSE) {
328       gchar *album_peak = (gchar *) g_malloc0 (G_ASCII_DTOSTR_BUF_SIZE);
329
330       album_peak = g_ascii_dtostr (album_peak, G_ASCII_DTOSTR_BUF_SIZE, value);
331       GST_DEBUG ("Setting album peak to %s", album_peak);
332       apev2tag->addValue (String ("REPLAYGAIN_ALBUM_PEAK",
333               String::UTF8), String (album_peak, String::UTF8), true);
334       g_free (album_peak);
335     }
336   } else {
337     GST_WARNING ("Unsupported tag: %s", tag);
338   }
339 }
340
341 static GstBuffer *
342 gst_apev2_mux_render_tag (GstTagLibMux * mux, GstTagList * taglist)
343 {
344   APE::Tag apev2tag;
345   ByteVector rendered_tag;
346   GstBuffer *buf;
347   guint tag_size;
348
349   /* Render the tag */
350   gst_tag_list_foreach (taglist, add_one_tag, &apev2tag);
351
352   rendered_tag = apev2tag.render ();
353   tag_size = rendered_tag.size ();
354
355   GST_LOG_OBJECT (mux, "tag size = %d bytes", tag_size);
356
357   /* Create buffer with tag */
358   buf = gst_buffer_new_and_alloc (tag_size);
359   memcpy (GST_BUFFER_DATA (buf), rendered_tag.data (), tag_size);
360   gst_buffer_set_caps (buf, GST_PAD_CAPS (mux->srcpad));
361
362   return buf;
363 }
364
365 gboolean
366 gst_apev2_mux_plugin_init (GstPlugin * plugin)
367 {
368   if (!gst_element_register (plugin, "apev2mux", GST_RANK_NONE,
369           GST_TYPE_APEV2_MUX))
370     return FALSE;
371
372   return TRUE;
373 }