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