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