1 /* GStreamer APEv1/2 tag reader
2 * Copyright (C) 2004 Ronald Bultje <rbultje@ronald.bitfreak.net>
3 * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * SECTION:element-apedemux
24 * apedemux accepts data streams with APE tags at the start or at the end
25 * (or both). The mime type of the data between the tag blocks is detected
26 * using typefind functions, and the appropriate output mime type set on
29 * The element is only able to read APE tags at the end of a stream from
30 * a seekable stream, ie. when get_range mode is supported by the upstream
31 * elements. If get_range operation is available, apedemux makes it available
32 * downstream. This means that elements which require get_range mode, such as
33 * wavparse or musepackdec, can operate on files containing APE tag
37 * <title>Example launch line</title>
39 * gst-launch -t filesrc location=file.mpc ! apedemux ! fakesink
40 * ]| This pipeline should read any available APE tag information and output it.
41 * The contents of the file inside the APE tag regions should be detected, and
42 * the appropriate mime type set on buffers produced from apedemux.
50 #include <gst/gst-i18n-plugin.h>
51 #include <gst/pbutils/pbutils.h>
53 #include "gstapedemux.h"
58 #define APE_VERSION_MAJOR(ver) ((ver)/1000)
60 GST_DEBUG_CATEGORY_STATIC (apedemux_debug);
61 #define GST_CAT_DEFAULT (apedemux_debug)
63 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
66 GST_STATIC_CAPS ("application/x-apetag")
69 static gboolean gst_ape_demux_identify_tag (GstTagDemux * demux,
70 GstBuffer * buffer, gboolean start_tag, guint * tag_size);
71 static GstTagDemuxResult gst_ape_demux_parse_tag (GstTagDemux * demux,
72 GstBuffer * buffer, gboolean start_tag, guint * tag_size,
75 GST_BOILERPLATE (GstApeDemux, gst_ape_demux, GstTagDemux, GST_TYPE_TAG_DEMUX);
78 gst_ape_demux_base_init (gpointer klass)
80 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
82 gst_element_class_add_static_pad_template (element_class, &sink_factory);
84 gst_element_class_set_details_simple (element_class, "APE tag demuxer",
85 "Codec/Demuxer/Metadata",
86 "Read and output APE tags while demuxing the contents",
87 "Tim-Philipp Müller <tim centricular net>");
89 GST_DEBUG_CATEGORY_INIT (apedemux_debug, "apedemux", 0,
90 "GStreamer APE tag demuxer");
94 gst_ape_demux_class_init (GstApeDemuxClass * klass)
96 GstTagDemuxClass *tagdemux_class;
98 tagdemux_class = GST_TAG_DEMUX_CLASS (klass);
100 tagdemux_class->identify_tag = GST_DEBUG_FUNCPTR (gst_ape_demux_identify_tag);
101 tagdemux_class->parse_tag = GST_DEBUG_FUNCPTR (gst_ape_demux_parse_tag);
103 /* no need for a merge function, the default behaviour to prefer start
104 * tags (APEv2) over end tags (usually APEv1, but could theoretically also
105 * be APEv2) is fine */
107 tagdemux_class->min_start_size = 32;
108 tagdemux_class->min_end_size = 32;
112 gst_ape_demux_init (GstApeDemux * apedemux, GstApeDemuxClass * gclass)
114 /* nothing to do here */
117 static const struct _GstApeDemuxTagTableEntry
119 const gchar *ape_tag;
120 const gchar *gst_tag;
123 "replaygain_track_gain", GST_TAG_TRACK_GAIN}, {
124 "replaygain_track_peak", GST_TAG_TRACK_PEAK}, {
125 "replaygain_album_gain", GST_TAG_ALBUM_GAIN}, {
126 "replaygain_album_peak", GST_TAG_ALBUM_PEAK}, {
127 "title", GST_TAG_TITLE}, {
128 "artist", GST_TAG_ARTIST}, {
129 "album", GST_TAG_ALBUM}, {
130 "composer", GST_TAG_COMPOSER}, {
131 "comment", GST_TAG_COMMENT}, {
132 "comments", GST_TAG_COMMENT}, {
133 "copyright", GST_TAG_COPYRIGHT}, {
134 "genre", GST_TAG_GENRE}, {
135 "isrc", GST_TAG_ISRC}, {
136 "disc", GST_TAG_ALBUM_VOLUME_NUMBER}, {
137 "disk", GST_TAG_ALBUM_VOLUME_NUMBER}, {
138 "discnumber", GST_TAG_ALBUM_VOLUME_NUMBER}, {
139 "disknumber", GST_TAG_ALBUM_VOLUME_NUMBER}, {
140 "track", GST_TAG_TRACK_NUMBER}, {
141 "tracknumber", GST_TAG_TRACK_NUMBER}, {
142 "year", GST_TAG_DATE}, {
143 "file", GST_TAG_LOCATION}
147 ape_demux_get_gst_tag_from_tag (const gchar * ape_tag,
148 const gchar ** gst_tag, GType * gst_tag_type)
152 for (i = 0; i < G_N_ELEMENTS (tag_table); ++i) {
153 if (g_ascii_strcasecmp (tag_table[i].ape_tag, ape_tag) == 0) {
154 *gst_tag = tag_table[i].gst_tag;
155 *gst_tag_type = gst_tag_get_type (tag_table[i].gst_tag);
156 GST_LOG ("Mapped APE tag '%s' to GStreamer tag '%s'", ape_tag, *gst_tag);
161 GST_WARNING ("Could not map APE tag '%s' to a GStreamer tag", ape_tag);
166 ape_demux_parse_tags (const guint8 * data, gint size)
168 GstTagList *taglist = gst_tag_list_new ();
170 GST_LOG ("Reading tags from chunk of size %u bytes", size);
172 /* get rid of header/footer */
173 if (size >= 32 && memcmp (data, "APETAGEX", 8) == 0) {
177 if (size > 32 && memcmp (data + size - 32, "APETAGEX", 8) == 0) {
181 /* read actual tags - at least 10 bytes for tag header */
185 const gchar *gst_tag;
188 /* find tag type and size */
189 len = GST_READ_UINT32_LE (data);
190 while (n < size && data[n] != 0x0)
194 g_assert (data[n] == 0x0);
199 /* If the tag is empty, skip to the next one */
204 tag = g_strndup ((gchar *) data + 8, n - 9);
205 val = g_strndup ((gchar *) data + n, len);
207 GST_LOG ("tag [%s], val[%s]", tag, val);
209 /* special-case 'media' tag, could be e.g. "CD 1/2" */
210 if (g_ascii_strcasecmp (tag, "media") == 0) {
214 tag = g_strdup ("discnumber");
215 /* get rid of the medium in front */
216 sp = strchr (val, ' ');
217 while (sp != NULL && (sp2 = strchr (sp + 1, ' ')) != NULL)
220 g_memmove (val, sp + 1, strlen (sp + 1) + 1);
224 if (ape_demux_get_gst_tag_from_tag (tag, &gst_tag, &gst_tag_type)) {
227 switch (gst_tag_type) {
231 if (sscanf (val, "%d", &v_int) == 1) {
232 g_value_init (&v, G_TYPE_INT);
233 g_value_set_int (&v, v_int);
240 if (strcmp (gst_tag, GST_TAG_TRACK_NUMBER) == 0) {
243 if (sscanf (val, "%u", &v_uint) == 1 && v_uint > 0) {
244 g_value_init (&v, G_TYPE_UINT);
245 g_value_set_uint (&v, v_uint);
247 GST_LOG ("checking for track count: %s", val);
248 /* might be 0/N or -1/N to specify that there is only a count */
249 if (sscanf (val, "%d/%u", &dummy, &count) == 2 && count > 0) {
250 gst_tag_list_add (taglist, GST_TAG_MERGE_APPEND,
251 GST_TAG_TRACK_COUNT, count, NULL);
253 } else if (strcmp (gst_tag, GST_TAG_ALBUM_VOLUME_NUMBER) == 0) {
256 if (sscanf (val, "%u", &v_uint) == 1 && v_uint > 0) {
257 g_value_init (&v, G_TYPE_UINT);
258 g_value_set_uint (&v, v_uint);
260 GST_LOG ("checking for volume count: %s", val);
261 /* might be 0/N or -1/N to specify that there is only a count */
262 if (sscanf (val, "%d/%u", &dummy, &count) == 2 && count > 0) {
263 gst_tag_list_add (taglist, GST_TAG_MERGE_APPEND,
264 GST_TAG_ALBUM_VOLUME_COUNT, count, NULL);
266 } else if (sscanf (val, "%u", &v_uint) == 1) {
267 g_value_init (&v, G_TYPE_UINT);
268 g_value_set_uint (&v, v_uint);
273 g_value_init (&v, G_TYPE_STRING);
274 g_value_set_string (&v, val);
281 /* floating point strings can be "4,123" or "4.123" depending on
282 * the locale. We need to be able to parse and read either version
283 * no matter what our current locale is */
284 g_strdelimit (val, ",", '.');
285 v_double = g_ascii_strtod (val, &endptr);
287 g_value_init (&v, G_TYPE_DOUBLE);
288 g_value_set_double (&v, v_double);
294 if (gst_tag_type == GST_TYPE_DATE) {
297 if (sscanf (val, "%d", &v_int) == 1) {
298 GDate *date = g_date_new_dmy (1, 1, v_int);
300 g_value_init (&v, GST_TYPE_DATE);
301 gst_value_set_date (&v, date);
305 GST_WARNING ("Unhandled tag type '%s' for tag '%s'",
306 g_type_name (gst_tag_type), gst_tag);
311 if (G_VALUE_TYPE (&v) != 0) {
312 gst_tag_list_add_values (taglist, GST_TAG_MERGE_APPEND,
317 GST_DEBUG ("Read tag %s: %s", tag, val);
321 /* move data pointer */
327 GST_DEBUG ("Taglist: %" GST_PTR_FORMAT, taglist);
332 gst_ape_demux_identify_tag (GstTagDemux * demux, GstBuffer * buffer,
333 gboolean start_tag, guint * tag_size)
335 if (memcmp (GST_BUFFER_DATA (buffer), "APETAGEX", 8) != 0) {
336 GST_DEBUG_OBJECT (demux, "No APETAGEX marker at %s - not an APE file",
337 (start_tag) ? "start" : "end");
341 *tag_size = GST_READ_UINT32_LE (GST_BUFFER_DATA (buffer) + 12);
343 /* size is without header, so add 32 to account for that */
349 static GstTagDemuxResult
350 gst_ape_demux_parse_tag (GstTagDemux * demux, GstBuffer * buffer,
351 gboolean start_tag, guint * tag_size, GstTagList ** tags)
354 const guint8 *footer;
355 gboolean have_header;
356 gboolean end_tag = !start_tag;
358 guint version, footer_size;
360 GST_LOG_OBJECT (demux, "Parsing buffer of size %u", GST_BUFFER_SIZE (buffer));
362 data = GST_BUFFER_DATA (buffer);
363 footer = GST_BUFFER_DATA (buffer) + GST_BUFFER_SIZE (buffer) - 32;
365 GST_LOG_OBJECT (demux, "Checking for footer at offset 0x%04x",
366 (guint) (footer - data));
367 if (footer > data && memcmp (footer, "APETAGEX", 8) == 0) {
368 GST_DEBUG_OBJECT (demux, "Found footer");
371 GST_DEBUG_OBJECT (demux, "No footer");
375 /* APE tags at the end must have a footer */
376 if (end_tag && footer_size == 0) {
377 GST_WARNING_OBJECT (demux, "Tag at end of file without footer!");
378 return GST_TAG_DEMUX_RESULT_BROKEN_TAG;
381 /* don't trust the header/footer flags, better detect them ourselves */
382 have_header = (memcmp (data, "APETAGEX", 8) == 0);
384 if (start_tag && !have_header) {
385 GST_DEBUG_OBJECT (demux, "Tag at beginning of file without header!");
386 return GST_TAG_DEMUX_RESULT_BROKEN_TAG;
389 if (end_tag && !have_header) {
390 GST_DEBUG_OBJECT (demux, "Tag at end of file has no header (APEv1)");
391 *tag_size -= 32; /* adjust tag size */
395 version = GST_READ_UINT32_LE (data + 8);
397 version = GST_READ_UINT32_LE (footer + 8);
405 GST_DEBUG_OBJECT (demux, "APE tag with version %u, size %u at offset 0x%08"
406 G_GINT64_MODIFIER "x", version, *tag_size,
407 GST_BUFFER_OFFSET (buffer) + ((have_header) ? 0 : 32));
409 if (APE_VERSION_MAJOR (version) != 1 && APE_VERSION_MAJOR (version) != 2) {
410 GST_WARNING ("APE tag is version %u.%03u, but decoder only supports "
411 "v1 or v2. Ignoring.", APE_VERSION_MAJOR (version), version % 1000);
412 return GST_TAG_DEMUX_RESULT_OK;
415 *tags = ape_demux_parse_tags (data, *tag_size - footer_size);
417 sink_caps = gst_static_pad_template_get_caps (&sink_factory);
418 gst_pb_utils_add_codec_description_to_tag_list (*tags,
419 GST_TAG_CONTAINER_FORMAT, sink_caps);
420 gst_caps_unref (sink_caps);
422 return GST_TAG_DEMUX_RESULT_OK;
426 plugin_init (GstPlugin * plugin)
428 return gst_element_register (plugin, "apedemux",
429 GST_RANK_PRIMARY, GST_TYPE_APE_DEMUX);
432 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
435 "APEv1/2 tag reader",
436 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)