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