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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, 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-1.0 -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-1.0 -m filesrc location=foo.mp3 ! apedemux ! fakesink silent=TRUE 2> /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 G_DEFINE_TYPE (GstApev2Mux, gst_apev2_mux, GST_TYPE_TAG_MUX);
73
74 static GstBuffer *gst_apev2_mux_render_start_tag (GstTagMux * mux,
75 const GstTagList * taglist);
76 static GstBuffer *gst_apev2_mux_render_end_tag (GstTagMux * mux,
77 const GstTagList * taglist);
78
79 static void
gst_apev2_mux_class_init(GstApev2MuxClass * klass)80 gst_apev2_mux_class_init (GstApev2MuxClass * klass)
81 {
82 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
83
84 GST_TAG_MUX_CLASS (klass)->render_start_tag =
85 GST_DEBUG_FUNCPTR (gst_apev2_mux_render_start_tag);
86 GST_TAG_MUX_CLASS (klass)->render_end_tag =
87 GST_DEBUG_FUNCPTR (gst_apev2_mux_render_end_tag);
88
89 gst_element_class_add_static_pad_template (element_class, &sink_template);
90 gst_element_class_add_static_pad_template (element_class, &src_template);
91
92 gst_element_class_set_static_metadata (element_class,
93 "TagLib-based APEv2 Muxer", "Formatter/Metadata",
94 "Adds an APEv2 header to the beginning of files using taglib",
95 "Sebastian Dröge <slomo@circular-chaos.org>");
96
97 GST_DEBUG_CATEGORY_INIT (gst_apev2_mux_debug, "apev2mux", 0,
98 "taglib-based APEv2 tag muxer");
99 }
100
101 static void
gst_apev2_mux_init(GstApev2Mux * apev2mux)102 gst_apev2_mux_init (GstApev2Mux * apev2mux)
103 {
104 /* nothing to do */
105 }
106
107 static gboolean
gst_apev2_mux_have_wavpack(GstApev2Mux * apev2mux)108 gst_apev2_mux_have_wavpack (GstApev2Mux * apev2mux)
109 {
110 const GstStructure *s;
111 gboolean ret;
112 GstCaps *caps;
113 GstPad *sink;
114
115 sink = gst_element_get_static_pad (GST_ELEMENT_CAST (apev2mux), "sink");
116 caps = gst_pad_get_current_caps (sink);
117 gst_object_unref (sink);
118
119 if (caps == NULL)
120 return FALSE;
121
122 s = gst_caps_get_structure (caps, 0);
123 ret = gst_structure_has_name (s, "audio/x-wavpack");
124 gst_caps_unref (caps);
125
126 GST_LOG_OBJECT (apev2mux, "got wavpack input: %s", ret ? "yes" : "no");
127 return ret;
128 }
129
130 static void
add_one_tag(const GstTagList * list,const gchar * tag,gpointer user_data)131 add_one_tag (const GstTagList * list, const gchar * tag, gpointer user_data)
132 {
133 APE::Tag * apev2tag = (APE::Tag *) user_data;
134 gboolean result;
135
136 /* FIXME: if there are several values set for the same tag, this won't
137 * work, only the first value will be taken into account
138 */
139 if (strcmp (tag, GST_TAG_TITLE) == 0) {
140 const char *title;
141
142 result = gst_tag_list_peek_string_index (list, tag, 0, &title);
143 if (result != FALSE) {
144 GST_DEBUG ("Setting title to %s", title);
145 apev2tag->setTitle (String (title, String::UTF8));
146 }
147 } else if (strcmp (tag, GST_TAG_ALBUM) == 0) {
148 const char *album;
149
150 result = gst_tag_list_peek_string_index (list, tag, 0, &album);
151 if (result != FALSE) {
152 GST_DEBUG ("Setting album to %s", album);
153 apev2tag->setAlbum (String (album, String::UTF8));
154 }
155 } else if (strcmp (tag, GST_TAG_ARTIST) == 0) {
156 const char *artist;
157
158 result = gst_tag_list_peek_string_index (list, tag, 0, &artist);
159 if (result != FALSE) {
160 GST_DEBUG ("Setting artist to %s", artist);
161 apev2tag->setArtist (String (artist, String::UTF8));
162 }
163 } else if (strcmp (tag, GST_TAG_COMPOSER) == 0) {
164 const char *composer;
165
166 result = gst_tag_list_peek_string_index (list, tag, 0, &composer);
167 if (result != FALSE) {
168 GST_DEBUG ("Setting composer to %s", composer);
169 apev2tag->addValue (String ("COMPOSER", String::UTF8),
170 String (composer, String::UTF8));
171 }
172 } else if (strcmp (tag, GST_TAG_GENRE) == 0) {
173 const char *genre;
174
175 result = gst_tag_list_peek_string_index (list, tag, 0, &genre);
176 if (result != FALSE) {
177 GST_DEBUG ("Setting genre to %s", genre);
178 apev2tag->setGenre (String (genre, String::UTF8));
179 }
180 } else if (strcmp (tag, GST_TAG_COMMENT) == 0) {
181 const char *comment;
182
183 result = gst_tag_list_peek_string_index (list, tag, 0, &comment);
184 if (result != FALSE) {
185 GST_DEBUG ("Setting comment to %s", comment);
186 apev2tag->setComment (String (comment, String::UTF8));
187 }
188 } else if (strcmp (tag, GST_TAG_DATE) == 0) {
189 GDate *date;
190
191 result = gst_tag_list_get_date_index (list, tag, 0, &date);
192 if (result != FALSE) {
193 GDateYear year;
194
195 year = g_date_get_year (date);
196 GST_DEBUG ("Setting track year to %d", year);
197 apev2tag->setYear (year);
198 g_date_free (date);
199 }
200 } else if (strcmp (tag, GST_TAG_TRACK_NUMBER) == 0) {
201 guint track_number;
202
203 result = gst_tag_list_get_uint_index (list, tag, 0, &track_number);
204 if (result != FALSE) {
205 guint total_tracks;
206
207 result = gst_tag_list_get_uint_index (list, GST_TAG_TRACK_COUNT,
208 0, &total_tracks);
209 if (result) {
210 gchar *tag_str;
211
212 tag_str = g_strdup_printf ("%d/%d", track_number, total_tracks);
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 } else {
218 GST_DEBUG ("Setting track number to %d", track_number);
219 apev2tag->setTrack (track_number);
220 }
221 }
222 } else if (strcmp (tag, GST_TAG_TRACK_COUNT) == 0) {
223 guint n;
224
225 if (gst_tag_list_get_uint_index (list, GST_TAG_TRACK_NUMBER, 0, &n)) {
226 GST_DEBUG ("track-count handled with track-number, skipping");
227 } else if (gst_tag_list_get_uint_index (list, GST_TAG_TRACK_COUNT, 0, &n)) {
228 gchar *tag_str;
229
230 tag_str = g_strdup_printf ("0/%d", n);
231 GST_DEBUG ("Setting track number to %s", tag_str);
232 apev2tag->addValue (String ("TRACK", String::UTF8),
233 String (tag_str, String::UTF8), true);
234 g_free (tag_str);
235 }
236 #if 0
237 } else if (strcmp (tag, GST_TAG_ALBUM_VOLUME_NUMBER) == 0) {
238 guint volume_number;
239
240 result = gst_tag_list_get_uint_index (list, tag, 0, &volume_number);
241
242 if (result != FALSE) {
243 guint volume_count;
244 gchar *tag_str;
245
246 result = gst_tag_list_get_uint_index (list, GST_TAG_ALBUM_VOLUME_COUNT,
247 0, &volume_count);
248
249 if (result) {
250 tag_str = g_strdup_printf ("CD %d/%d", volume_number, volume_count);
251 } else {
252 tag_str = g_strdup_printf ("CD %d", volume_number);
253 }
254
255 GST_DEBUG ("Setting album number to %s", tag_str);
256
257 apev2tag->addValue (String ("MEDIA", String::UTF8),
258 String (tag_str, String::UTF8), true);
259 g_free (tag_str);
260 }
261 } else if (strcmp (tag, GST_TAG_ALBUM_VOLUME_COUNT) == 0) {
262 guint n;
263
264 if (gst_tag_list_get_uint_index (list, GST_TAG_ALBUM_VOLUME_NUMBER, 0, &n)) {
265 GST_DEBUG ("volume-count handled with volume-number, skipping");
266 } else if (gst_tag_list_get_uint_index (list, GST_TAG_ALBUM_VOLUME_COUNT,
267 0, &n)) {
268 gchar *tag_str;
269
270 tag_str = g_strdup_printf ("CD 0/%u", n);
271 GST_DEBUG ("Setting album volume number/count to %s", tag_str);
272
273 apev2tag->addValue (String ("MEDIA", String::UTF8),
274 String (tag_str, String::UTF8), true);
275 g_free (tag_str);
276 }
277 #endif
278 } else if (strcmp (tag, GST_TAG_COPYRIGHT) == 0) {
279 const gchar *copyright;
280
281 result = gst_tag_list_peek_string_index (list, tag, 0, ©right);
282
283 if (result != FALSE) {
284 GST_DEBUG ("Setting copyright to %s", copyright);
285 apev2tag->addValue (String ("COPYRIGHT", String::UTF8),
286 String (copyright, String::UTF8), true);
287 }
288 } else if (strcmp (tag, GST_TAG_LOCATION) == 0) {
289 const gchar *location;
290
291 result = gst_tag_list_peek_string_index (list, tag, 0, &location);
292
293 if (result != FALSE) {
294 GST_DEBUG ("Setting location to %s", location);
295 apev2tag->addValue (String ("FILE", String::UTF8),
296 String (location, String::UTF8), true);
297 }
298 } else if (strcmp (tag, GST_TAG_ISRC) == 0) {
299 const gchar *isrc;
300
301 result = gst_tag_list_peek_string_index (list, tag, 0, &isrc);
302
303 if (result != FALSE) {
304 GST_DEBUG ("Setting ISRC to %s", isrc);
305 apev2tag->addValue (String ("ISRC", String::UTF8),
306 String (isrc, String::UTF8), true);
307 }
308 } else if (strcmp (tag, GST_TAG_TRACK_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 *track_gain = (gchar *) g_malloc0 (G_ASCII_DTOSTR_BUF_SIZE);
315
316 track_gain = g_ascii_dtostr (track_gain, G_ASCII_DTOSTR_BUF_SIZE, value);
317 GST_DEBUG ("Setting track gain to %s", track_gain);
318 apev2tag->addValue (String ("REPLAYGAIN_TRACK_GAIN",
319 String::UTF8), String (track_gain, String::UTF8), true);
320 g_free (track_gain);
321 }
322 } else if (strcmp (tag, GST_TAG_TRACK_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 *track_peak = (gchar *) g_malloc0 (G_ASCII_DTOSTR_BUF_SIZE);
329
330 track_peak = g_ascii_dtostr (track_peak, G_ASCII_DTOSTR_BUF_SIZE, value);
331 GST_DEBUG ("Setting track peak to %s", track_peak);
332 apev2tag->addValue (String ("REPLAYGAIN_TRACK_PEAK",
333 String::UTF8), String (track_peak, String::UTF8), true);
334 g_free (track_peak);
335 }
336 } else if (strcmp (tag, GST_TAG_ALBUM_GAIN) == 0) {
337 gdouble value;
338
339 result = gst_tag_list_get_double_index (list, tag, 0, &value);
340
341 if (result != FALSE) {
342 gchar *album_gain = (gchar *) g_malloc0 (G_ASCII_DTOSTR_BUF_SIZE);
343
344 album_gain = g_ascii_dtostr (album_gain, G_ASCII_DTOSTR_BUF_SIZE, value);
345 GST_DEBUG ("Setting album gain to %s", album_gain);
346 apev2tag->addValue (String ("REPLAYGAIN_ALBUM_GAIN",
347 String::UTF8), String (album_gain, String::UTF8), true);
348 g_free (album_gain);
349 }
350 } else if (strcmp (tag, GST_TAG_ALBUM_PEAK) == 0) {
351 gdouble value;
352
353 result = gst_tag_list_get_double_index (list, tag, 0, &value);
354
355 if (result != FALSE) {
356 gchar *album_peak = (gchar *) g_malloc0 (G_ASCII_DTOSTR_BUF_SIZE);
357
358 album_peak = g_ascii_dtostr (album_peak, G_ASCII_DTOSTR_BUF_SIZE, value);
359 GST_DEBUG ("Setting album peak to %s", album_peak);
360 apev2tag->addValue (String ("REPLAYGAIN_ALBUM_PEAK",
361 String::UTF8), String (album_peak, String::UTF8), true);
362 g_free (album_peak);
363 }
364 } else {
365 GST_WARNING ("Unsupported tag: %s", tag);
366 }
367 }
368
369 static GstBuffer *
gst_apev2_mux_render_tag(GstTagMux * mux,const GstTagList * taglist)370 gst_apev2_mux_render_tag (GstTagMux * mux, const GstTagList * taglist)
371 {
372 APE::Tag apev2tag;
373 ByteVector rendered_tag;
374 GstBuffer *buf;
375 guint tag_size;
376
377 /* Render the tag */
378 gst_tag_list_foreach (taglist, add_one_tag, &apev2tag);
379
380 rendered_tag = apev2tag.render ();
381 tag_size = rendered_tag.size ();
382
383 GST_LOG_OBJECT (mux, "tag size = %d bytes", tag_size);
384
385 /* Create buffer with tag */
386 buf = gst_buffer_new_and_alloc (tag_size);
387 gst_buffer_fill (buf, 0, rendered_tag.data (), tag_size);
388
389 return buf;
390 }
391
392 static GstBuffer *
gst_apev2_mux_render_start_tag(GstTagMux * mux,const GstTagList * taglist)393 gst_apev2_mux_render_start_tag (GstTagMux * mux, const GstTagList * taglist)
394 {
395 if (gst_apev2_mux_have_wavpack (GST_APEV2_MUX (mux)))
396 return NULL;
397
398 return gst_apev2_mux_render_tag (mux, taglist);
399 }
400
401 static GstBuffer *
gst_apev2_mux_render_end_tag(GstTagMux * mux,const GstTagList * taglist)402 gst_apev2_mux_render_end_tag (GstTagMux * mux, const GstTagList * taglist)
403 {
404 if (gst_apev2_mux_have_wavpack (GST_APEV2_MUX (mux)))
405 return gst_apev2_mux_render_tag (mux, taglist);
406
407 return NULL;
408 }
409