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>
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.
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.
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.
23 * SECTION:element-apev2mux
24 * @see_also: #GstTagSetter
26 * This element adds APEv2 tags to the beginning of a stream using the taglib
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).
34 * <title>Example pipelines</title>
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.
41 * gst-launch -m filesrc location=foo.mp3 ! apedemux ! fakesink silent=TRUE 2> /dev/null | grep taglist
42 * ]| Verify that tags have been written.
50 #include "gstapev2mux.h"
55 #include <gst/tag/tag.h>
57 using namespace TagLib;
59 GST_DEBUG_CATEGORY_STATIC (gst_apev2_mux_debug);
60 #define GST_CAT_DEFAULT gst_apev2_mux_debug
62 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
65 GST_STATIC_CAPS ("application/x-apetag"));
67 GST_BOILERPLATE (GstApev2Mux, gst_apev2_mux, GstTagLibMux,
68 GST_TYPE_TAG_LIB_MUX);
70 static GstBuffer *gst_apev2_mux_render_tag (GstTagLibMux * mux,
71 GstTagList * taglist);
74 gst_apev2_mux_base_init (gpointer g_class)
76 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
78 gst_element_class_add_pad_template (element_class,
79 gst_static_pad_template_get (&src_template));
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>");
86 GST_DEBUG_CATEGORY_INIT (gst_apev2_mux_debug, "apev2mux", 0,
87 "taglib-based APEv2 tag muxer");
91 gst_apev2_mux_class_init (GstApev2MuxClass * klass)
93 GST_TAG_LIB_MUX_CLASS (klass)->render_tag =
94 GST_DEBUG_FUNCPTR (gst_apev2_mux_render_tag);
98 gst_apev2_mux_init (GstApev2Mux * apev2mux, GstApev2MuxClass * apev2mux_class)
104 add_one_tag (const GstTagList * list, const gchar * tag, gpointer user_data)
106 APE::Tag * apev2tag = (APE::Tag *) user_data;
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
112 if (strcmp (tag, GST_TAG_TITLE) == 0) {
115 result = gst_tag_list_get_string_index (list, tag, 0, &title);
116 if (result != FALSE) {
117 GST_DEBUG ("Setting title to %s", title);
118 apev2tag->setTitle (String::String (title, String::UTF8));
121 } else if (strcmp (tag, GST_TAG_ALBUM) == 0) {
124 result = gst_tag_list_get_string_index (list, tag, 0, &album);
125 if (result != FALSE) {
126 GST_DEBUG ("Setting album to %s", album);
127 apev2tag->setAlbum (String::String (album, String::UTF8));
130 } else if (strcmp (tag, GST_TAG_ARTIST) == 0) {
133 result = gst_tag_list_get_string_index (list, tag, 0, &artist);
134 if (result != FALSE) {
135 GST_DEBUG ("Setting artist to %s", artist);
136 apev2tag->setArtist (String::String (artist, String::UTF8));
139 } else if (strcmp (tag, GST_TAG_COMPOSER) == 0) {
142 result = gst_tag_list_get_string_index (list, tag, 0, &composer);
143 if (result != FALSE) {
144 GST_DEBUG ("Setting composer to %s", composer);
145 apev2tag->addValue (String::String ("COMPOSER", String::UTF8),
146 String::String (composer, String::UTF8));
149 } else if (strcmp (tag, GST_TAG_GENRE) == 0) {
152 result = gst_tag_list_get_string_index (list, tag, 0, &genre);
153 if (result != FALSE) {
154 GST_DEBUG ("Setting genre to %s", genre);
155 apev2tag->setGenre (String::String (genre, String::UTF8));
158 } else if (strcmp (tag, GST_TAG_COMMENT) == 0) {
161 result = gst_tag_list_get_string_index (list, tag, 0, &comment);
162 if (result != FALSE) {
163 GST_DEBUG ("Setting comment to %s", comment);
164 apev2tag->setComment (String::String (comment, String::UTF8));
167 } else if (strcmp (tag, GST_TAG_DATE) == 0) {
170 result = gst_tag_list_get_date_index (list, tag, 0, &date);
171 if (result != FALSE) {
174 year = g_date_get_year (date);
175 GST_DEBUG ("Setting track year to %d", year);
176 apev2tag->setYear (year);
179 } else if (strcmp (tag, GST_TAG_TRACK_NUMBER) == 0) {
182 result = gst_tag_list_get_uint_index (list, tag, 0, &track_number);
183 if (result != FALSE) {
186 result = gst_tag_list_get_uint_index (list, GST_TAG_TRACK_COUNT,
191 tag_str = g_strdup_printf ("%d/%d", track_number, total_tracks);
192 GST_DEBUG ("Setting track number to %s", tag_str);
193 apev2tag->addValue (String::String ("TRACK", String::UTF8),
194 String::String (tag_str, String::UTF8), true);
197 GST_DEBUG ("Setting track number to %d", track_number);
198 apev2tag->setTrack (track_number);
201 } else if (strcmp (tag, GST_TAG_TRACK_COUNT) == 0) {
204 if (gst_tag_list_get_uint_index (list, GST_TAG_TRACK_NUMBER, 0, &n)) {
205 GST_DEBUG ("track-count handled with track-number, skipping");
206 } else if (gst_tag_list_get_uint_index (list, GST_TAG_TRACK_COUNT, 0, &n)) {
209 tag_str = g_strdup_printf ("0/%d", n);
210 GST_DEBUG ("Setting track number to %s", tag_str);
211 apev2tag->addValue (String::String ("TRACK", String::UTF8),
212 String::String (tag_str, String::UTF8), true);
216 } else if (strcmp (tag, GST_TAG_ALBUM_VOLUME_NUMBER) == 0) {
219 result = gst_tag_list_get_uint_index (list, tag, 0, &volume_number);
221 if (result != FALSE) {
225 result = gst_tag_list_get_uint_index (list, GST_TAG_ALBUM_VOLUME_COUNT,
229 tag_str = g_strdup_printf ("CD %d/%d", volume_number, volume_count);
231 tag_str = g_strdup_printf ("CD %d", volume_number);
234 GST_DEBUG ("Setting album number to %s", tag_str);
236 apev2tag->addValue (String::String ("MEDIA", String::UTF8),
237 String::String (tag_str, String::UTF8), true);
240 } else if (strcmp (tag, GST_TAG_ALBUM_VOLUME_COUNT) == 0) {
243 if (gst_tag_list_get_uint_index (list, GST_TAG_ALBUM_VOLUME_NUMBER, 0, &n)) {
244 GST_DEBUG ("volume-count handled with volume-number, skipping");
245 } else if (gst_tag_list_get_uint_index (list, GST_TAG_ALBUM_VOLUME_COUNT,
249 tag_str = g_strdup_printf ("CD 0/%u", n);
250 GST_DEBUG ("Setting album volume number/count to %s", tag_str);
252 apev2tag->addValue (String::String ("MEDIA", String::UTF8),
253 String::String (tag_str, String::UTF8), true);
257 } else if (strcmp (tag, GST_TAG_COPYRIGHT) == 0) {
260 result = gst_tag_list_get_string_index (list, tag, 0, ©right);
262 if (result != FALSE) {
263 GST_DEBUG ("Setting copyright to %s", copyright);
264 apev2tag->addValue (String::String ("COPYRIGHT", String::UTF8),
265 String::String (copyright, String::UTF8), true);
268 } else if (strcmp (tag, GST_TAG_LOCATION) == 0) {
271 result = gst_tag_list_get_string_index (list, tag, 0, &location);
273 if (result != FALSE) {
274 GST_DEBUG ("Setting location to %s", location);
275 apev2tag->addValue (String::String ("FILE", String::UTF8),
276 String::String (location, String::UTF8), true);
279 } else if (strcmp (tag, GST_TAG_ISRC) == 0) {
282 result = gst_tag_list_get_string_index (list, tag, 0, &isrc);
284 if (result != FALSE) {
285 GST_DEBUG ("Setting ISRC to %s", isrc);
286 apev2tag->addValue (String::String ("ISRC", String::UTF8),
287 String::String (isrc, String::UTF8), true);
290 } else if (strcmp (tag, GST_TAG_TRACK_GAIN) == 0) {
293 result = gst_tag_list_get_double_index (list, tag, 0, &value);
295 if (result != FALSE) {
296 gchar *track_gain = (gchar *) g_malloc0 (G_ASCII_DTOSTR_BUF_SIZE);
298 track_gain = g_ascii_dtostr (track_gain, G_ASCII_DTOSTR_BUF_SIZE, value);
299 GST_DEBUG ("Setting track gain to %s", track_gain);
300 apev2tag->addValue (String::String ("REPLAYGAIN_TRACK_GAIN",
301 String::UTF8), String::String (track_gain, String::UTF8), true);
304 } else if (strcmp (tag, GST_TAG_TRACK_PEAK) == 0) {
307 result = gst_tag_list_get_double_index (list, tag, 0, &value);
309 if (result != FALSE) {
310 gchar *track_peak = (gchar *) g_malloc0 (G_ASCII_DTOSTR_BUF_SIZE);
312 track_peak = g_ascii_dtostr (track_peak, G_ASCII_DTOSTR_BUF_SIZE, value);
313 GST_DEBUG ("Setting track peak to %s", track_peak);
314 apev2tag->addValue (String::String ("REPLAYGAIN_TRACK_PEAK",
315 String::UTF8), String::String (track_peak, String::UTF8), true);
318 } else if (strcmp (tag, GST_TAG_ALBUM_GAIN) == 0) {
321 result = gst_tag_list_get_double_index (list, tag, 0, &value);
323 if (result != FALSE) {
324 gchar *album_gain = (gchar *) g_malloc0 (G_ASCII_DTOSTR_BUF_SIZE);
326 album_gain = g_ascii_dtostr (album_gain, G_ASCII_DTOSTR_BUF_SIZE, value);
327 GST_DEBUG ("Setting album gain to %s", album_gain);
328 apev2tag->addValue (String::String ("REPLAYGAIN_ALBUM_GAIN",
329 String::UTF8), String::String (album_gain, String::UTF8), true);
332 } else if (strcmp (tag, GST_TAG_ALBUM_PEAK) == 0) {
335 result = gst_tag_list_get_double_index (list, tag, 0, &value);
337 if (result != FALSE) {
338 gchar *album_peak = (gchar *) g_malloc0 (G_ASCII_DTOSTR_BUF_SIZE);
340 album_peak = g_ascii_dtostr (album_peak, G_ASCII_DTOSTR_BUF_SIZE, value);
341 GST_DEBUG ("Setting album peak to %s", album_peak);
342 apev2tag->addValue (String::String ("REPLAYGAIN_ALBUM_PEAK",
343 String::UTF8), String::String (album_peak, String::UTF8), true);
347 GST_WARNING ("Unsupported tag: %s", tag);
352 gst_apev2_mux_render_tag (GstTagLibMux * mux, GstTagList * taglist)
355 ByteVector rendered_tag;
360 gst_tag_list_foreach (taglist, add_one_tag, &apev2tag);
362 rendered_tag = apev2tag.render ();
363 tag_size = rendered_tag.size ();
365 GST_LOG_OBJECT (mux, "tag size = %d bytes", tag_size);
367 /* Create buffer with tag */
368 buf = gst_buffer_new_and_alloc (tag_size);
369 memcpy (GST_BUFFER_DATA (buf), rendered_tag.data (), tag_size);
370 gst_buffer_set_caps (buf, GST_PAD_CAPS (mux->srcpad));
376 gst_apev2_mux_plugin_init (GstPlugin * plugin)
378 if (!gst_element_register (plugin, "apev2mux", GST_RANK_NONE,