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>
4  *
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.
9  *
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.
14  *
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., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 
21 /**
22  * SECTION:element-apedemux
23  *
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
27  * outgoing buffers.
28  *
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
34  * information.
35  *
36  * <refsect2>
37  * <title>Example launch line</title>
38  * |[
39  * gst-launch-1.0 -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.
43  * </refsect2>
44  */
45 #ifdef HAVE_CONFIG_H
46 #include "config.h"
47 #endif
48 
49 #include <gst/gst.h>
50 #include <gst/gst-i18n-plugin.h>
51 #include <gst/pbutils/pbutils.h>
52 
53 #include "gstapedemux.h"
54 
55 #include <stdio.h>
56 #include <string.h>
57 
58 #define APE_VERSION_MAJOR(ver)  ((ver)/1000)
59 
60 GST_DEBUG_CATEGORY_STATIC (apedemux_debug);
61 #define GST_CAT_DEFAULT (apedemux_debug)
62 
63 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
64     GST_PAD_SINK,
65     GST_PAD_ALWAYS,
66     GST_STATIC_CAPS ("application/x-apetag")
67     );
68 
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,
73     GstTagList ** tags);
74 
75 G_DEFINE_TYPE (GstApeDemux, gst_ape_demux, GST_TYPE_TAG_DEMUX);
76 
77 static void
gst_ape_demux_class_init(GstApeDemuxClass * klass)78 gst_ape_demux_class_init (GstApeDemuxClass * klass)
79 {
80   GstElementClass *element_class;
81   GstTagDemuxClass *tagdemux_class;
82 
83   GST_DEBUG_CATEGORY_INIT (apedemux_debug, "apedemux", 0,
84       "GStreamer APE tag demuxer");
85 
86   tagdemux_class = GST_TAG_DEMUX_CLASS (klass);
87   element_class = GST_ELEMENT_CLASS (klass);
88 
89   gst_element_class_set_static_metadata (element_class, "APE tag demuxer",
90       "Codec/Demuxer/Metadata",
91       "Read and output APE tags while demuxing the contents",
92       "Tim-Philipp Müller <tim centricular net>");
93 
94   gst_element_class_add_static_pad_template (element_class, &sink_factory);
95 
96   tagdemux_class->identify_tag = GST_DEBUG_FUNCPTR (gst_ape_demux_identify_tag);
97   tagdemux_class->parse_tag = GST_DEBUG_FUNCPTR (gst_ape_demux_parse_tag);
98 
99   /* no need for a merge function, the default behaviour to prefer start
100    * tags (APEv2) over end tags (usually APEv1, but could theoretically also
101    * be APEv2) is fine */
102 
103   tagdemux_class->min_start_size = 32;
104   tagdemux_class->min_end_size = 32;
105 }
106 
107 static void
gst_ape_demux_init(GstApeDemux * apedemux)108 gst_ape_demux_init (GstApeDemux * apedemux)
109 {
110   /* nothing to do here */
111 }
112 
113 static const struct _GstApeDemuxTagTableEntry
114 {
115   const gchar *ape_tag;
116   const gchar *gst_tag;
117 } tag_table[] = {
118   {
119   "replaygain_track_gain", GST_TAG_TRACK_GAIN}, {
120   "replaygain_track_peak", GST_TAG_TRACK_PEAK}, {
121   "replaygain_album_gain", GST_TAG_ALBUM_GAIN}, {
122   "replaygain_album_peak", GST_TAG_ALBUM_PEAK}, {
123   "title", GST_TAG_TITLE}, {
124   "artist", GST_TAG_ARTIST}, {
125   "album", GST_TAG_ALBUM}, {
126   "composer", GST_TAG_COMPOSER}, {
127   "comment", GST_TAG_COMMENT}, {
128   "comments", GST_TAG_COMMENT}, {
129   "copyright", GST_TAG_COPYRIGHT}, {
130   "genre", GST_TAG_GENRE}, {
131   "isrc", GST_TAG_ISRC}, {
132   "disc", GST_TAG_ALBUM_VOLUME_NUMBER}, {
133   "disk", GST_TAG_ALBUM_VOLUME_NUMBER}, {
134   "discnumber", GST_TAG_ALBUM_VOLUME_NUMBER}, {
135   "disknumber", GST_TAG_ALBUM_VOLUME_NUMBER}, {
136   "track", GST_TAG_TRACK_NUMBER}, {
137   "tracknumber", GST_TAG_TRACK_NUMBER}, {
138   "year", GST_TAG_DATE}, {
139   "file", GST_TAG_LOCATION}
140 };
141 
142 static gboolean
ape_demux_get_gst_tag_from_tag(const gchar * ape_tag,const gchar ** gst_tag,GType * gst_tag_type)143 ape_demux_get_gst_tag_from_tag (const gchar * ape_tag,
144     const gchar ** gst_tag, GType * gst_tag_type)
145 {
146   gint i;
147 
148   for (i = 0; i < G_N_ELEMENTS (tag_table); ++i) {
149     if (g_ascii_strcasecmp (tag_table[i].ape_tag, ape_tag) == 0) {
150       *gst_tag = tag_table[i].gst_tag;
151       *gst_tag_type = gst_tag_get_type (tag_table[i].gst_tag);
152       GST_LOG ("Mapped APE tag '%s' to GStreamer tag '%s'", ape_tag, *gst_tag);
153       return TRUE;
154     }
155   }
156 
157   GST_WARNING ("Could not map APE tag '%s' to a GStreamer tag", ape_tag);
158   return FALSE;
159 }
160 
161 static GstTagList *
ape_demux_parse_tags(const guint8 * data,gint size)162 ape_demux_parse_tags (const guint8 * data, gint size)
163 {
164   GstTagList *taglist = gst_tag_list_new_empty ();
165 
166   GST_LOG ("Reading tags from chunk of size %u bytes", size);
167 
168   /* get rid of header/footer */
169   if (size >= 32 && memcmp (data, "APETAGEX", 8) == 0) {
170     data += 32;
171     size -= 32;
172   }
173   if (size > 32 && memcmp (data + size - 32, "APETAGEX", 8) == 0) {
174     size -= 32;
175   }
176 
177   /* read actual tags - at least 10 bytes for tag header */
178   while (size >= 10) {
179     guint len, n = 8;
180     gchar *tag, *val;
181     const gchar *gst_tag;
182     GType gst_tag_type;
183 
184     /* find tag type and size */
185     len = GST_READ_UINT32_LE (data);
186     while (n < size && data[n] != 0x0)
187       n++;
188     if (n == size)
189       break;
190     g_assert (data[n] == 0x0);
191     n++;
192     if (size - n < len)
193       break;
194 
195     /* If the tag is empty, skip to the next one */
196     if (len == 0)
197       goto next_tag;
198 
199     /* read */
200     tag = g_strndup ((gchar *) data + 8, n - 9);
201     val = g_strndup ((gchar *) data + n, len);
202 
203     GST_LOG ("tag [%s], val[%s]", tag, val);
204 
205     /* special-case 'media' tag, could be e.g. "CD 1/2" */
206     if (g_ascii_strcasecmp (tag, "media") == 0) {
207       gchar *sp, *sp2;
208 
209       g_free (tag);
210       tag = g_strdup ("discnumber");
211       /* get rid of the medium in front */
212       sp = strchr (val, ' ');
213       while (sp != NULL && (sp2 = strchr (sp + 1, ' ')) != NULL)
214         sp = sp2;
215       if (sp) {
216         memmove (val, sp + 1, strlen (sp + 1) + 1);
217       }
218     }
219 
220     if (ape_demux_get_gst_tag_from_tag (tag, &gst_tag, &gst_tag_type)) {
221       GValue v = { 0, };
222 
223       switch (gst_tag_type) {
224         case G_TYPE_INT:{
225           gint v_int;
226 
227           if (sscanf (val, "%d", &v_int) == 1) {
228             g_value_init (&v, G_TYPE_INT);
229             g_value_set_int (&v, v_int);
230           }
231           break;
232         }
233         case G_TYPE_UINT:{
234           guint v_uint, count;
235 
236           if (strcmp (gst_tag, GST_TAG_TRACK_NUMBER) == 0) {
237             gint dummy;
238 
239             if (sscanf (val, "%u", &v_uint) == 1 && v_uint > 0) {
240               g_value_init (&v, G_TYPE_UINT);
241               g_value_set_uint (&v, v_uint);
242             }
243             GST_LOG ("checking for track count: %s", val);
244             /* might be 0/N or -1/N to specify that there is only a count */
245             if (sscanf (val, "%d/%u", &dummy, &count) == 2 && count > 0) {
246               gst_tag_list_add (taglist, GST_TAG_MERGE_APPEND,
247                   GST_TAG_TRACK_COUNT, count, NULL);
248             }
249           } else if (strcmp (gst_tag, GST_TAG_ALBUM_VOLUME_NUMBER) == 0) {
250             gint dummy;
251 
252             if (sscanf (val, "%u", &v_uint) == 1 && v_uint > 0) {
253               g_value_init (&v, G_TYPE_UINT);
254               g_value_set_uint (&v, v_uint);
255             }
256             GST_LOG ("checking for volume count: %s", val);
257             /* might be 0/N or -1/N to specify that there is only a count */
258             if (sscanf (val, "%d/%u", &dummy, &count) == 2 && count > 0) {
259               gst_tag_list_add (taglist, GST_TAG_MERGE_APPEND,
260                   GST_TAG_ALBUM_VOLUME_COUNT, count, NULL);
261             }
262           } else if (sscanf (val, "%u", &v_uint) == 1) {
263             g_value_init (&v, G_TYPE_UINT);
264             g_value_set_uint (&v, v_uint);
265           }
266           break;
267         }
268         case G_TYPE_STRING:{
269           g_value_init (&v, G_TYPE_STRING);
270           g_value_set_string (&v, val);
271           break;
272         }
273         case G_TYPE_DOUBLE:{
274           gdouble v_double;
275           gchar *endptr;
276 
277           /* floating point strings can be "4,123" or "4.123" depending on
278            * the locale. We need to be able to parse and read either version
279            * no matter what our current locale is */
280           g_strdelimit (val, ",", '.');
281           v_double = g_ascii_strtod (val, &endptr);
282           if (endptr != val) {
283             g_value_init (&v, G_TYPE_DOUBLE);
284             g_value_set_double (&v, v_double);
285           }
286 
287           break;
288         }
289         default:{
290           if (gst_tag_type == G_TYPE_DATE) {
291             gint v_int;
292 
293             if (sscanf (val, "%d", &v_int) == 1) {
294               GDate *date = g_date_new_dmy (1, 1, v_int);
295 
296               g_value_init (&v, G_TYPE_DATE);
297               g_value_take_boxed (&v, date);
298             }
299           } else {
300             GST_WARNING ("Unhandled tag type '%s' for tag '%s'",
301                 g_type_name (gst_tag_type), gst_tag);
302           }
303           break;
304         }
305       }
306       if (G_VALUE_TYPE (&v) != 0) {
307         gst_tag_list_add_values (taglist, GST_TAG_MERGE_APPEND,
308             gst_tag, &v, NULL);
309         g_value_unset (&v);
310       }
311     }
312     GST_DEBUG ("Read tag %s: %s", tag, val);
313     g_free (tag);
314     g_free (val);
315 
316     /* move data pointer */
317   next_tag:
318     size -= len + n;
319     data += len + n;
320   }
321 
322   GST_DEBUG ("Taglist: %" GST_PTR_FORMAT, taglist);
323   return taglist;
324 }
325 
326 static gboolean
gst_ape_demux_identify_tag(GstTagDemux * demux,GstBuffer * buffer,gboolean start_tag,guint * tag_size)327 gst_ape_demux_identify_tag (GstTagDemux * demux, GstBuffer * buffer,
328     gboolean start_tag, guint * tag_size)
329 {
330   GstMapInfo map;
331 
332   gst_buffer_map (buffer, &map, GST_MAP_READ);
333 
334   if (memcmp (map.data, "APETAGEX", 8) != 0) {
335     GST_DEBUG_OBJECT (demux, "No APETAGEX marker at %s - not an APE file",
336         (start_tag) ? "start" : "end");
337     gst_buffer_unmap (buffer, &map);
338     return FALSE;
339   }
340 
341   *tag_size = GST_READ_UINT32_LE (map.data + 12);
342 
343   /* size is without header, so add 32 to account for that */
344   *tag_size += 32;
345 
346   gst_buffer_unmap (buffer, &map);
347 
348   return TRUE;
349 }
350 
351 static GstTagDemuxResult
gst_ape_demux_parse_tag(GstTagDemux * demux,GstBuffer * buffer,gboolean start_tag,guint * tag_size,GstTagList ** tags)352 gst_ape_demux_parse_tag (GstTagDemux * demux, GstBuffer * buffer,
353     gboolean start_tag, guint * tag_size, GstTagList ** tags)
354 {
355   guint8 *data;
356   guint8 *footer;
357   gboolean have_header;
358   gboolean end_tag = !start_tag;
359   GstCaps *sink_caps;
360   guint version, footer_size;
361   GstMapInfo map;
362   gsize size;
363 
364   gst_buffer_map (buffer, &map, GST_MAP_READ);
365   data = map.data;
366   size = map.size;
367 
368   GST_LOG_OBJECT (demux, "Parsing buffer of size %" G_GSIZE_FORMAT, size);
369 
370   footer = data + size - 32;
371 
372   GST_LOG_OBJECT (demux, "Checking for footer at offset 0x%04x",
373       (guint) (footer - data));
374   if (footer > data && memcmp (footer, "APETAGEX", 8) == 0) {
375     GST_DEBUG_OBJECT (demux, "Found footer");
376     footer_size = 32;
377   } else {
378     GST_DEBUG_OBJECT (demux, "No footer");
379     footer_size = 0;
380   }
381 
382   /* APE tags at the end must have a footer */
383   if (end_tag && footer_size == 0) {
384     GST_WARNING_OBJECT (demux, "Tag at end of file without footer!");
385     return GST_TAG_DEMUX_RESULT_BROKEN_TAG;
386   }
387 
388   /* don't trust the header/footer flags, better detect them ourselves */
389   have_header = (memcmp (data, "APETAGEX", 8) == 0);
390 
391   if (start_tag && !have_header) {
392     GST_DEBUG_OBJECT (demux, "Tag at beginning of file without header!");
393     return GST_TAG_DEMUX_RESULT_BROKEN_TAG;
394   }
395 
396   if (end_tag && !have_header) {
397     GST_DEBUG_OBJECT (demux, "Tag at end of file has no header (APEv1)");
398     *tag_size -= 32;            /* adjust tag size */
399   }
400 
401   if (have_header) {
402     version = GST_READ_UINT32_LE (data + 8);
403   } else {
404     version = GST_READ_UINT32_LE (footer + 8);
405   }
406 
407   /* skip header */
408   if (have_header) {
409     data += 32;
410   }
411 
412   GST_DEBUG_OBJECT (demux, "APE tag with version %u, size %u at offset 0x%08"
413       G_GINT64_MODIFIER "x", version, *tag_size,
414       GST_BUFFER_OFFSET (buffer) + ((have_header) ? 0 : 32));
415 
416   if (APE_VERSION_MAJOR (version) != 1 && APE_VERSION_MAJOR (version) != 2) {
417     GST_WARNING ("APE tag is version %u.%03u, but decoder only supports "
418         "v1 or v2. Ignoring.", APE_VERSION_MAJOR (version), version % 1000);
419     return GST_TAG_DEMUX_RESULT_OK;
420   }
421 
422   *tags = ape_demux_parse_tags (data, *tag_size - footer_size);
423 
424   sink_caps = gst_static_pad_template_get_caps (&sink_factory);
425   gst_pb_utils_add_codec_description_to_tag_list (*tags,
426       GST_TAG_CONTAINER_FORMAT, sink_caps);
427   gst_caps_unref (sink_caps);
428 
429   gst_buffer_unmap (buffer, &map);
430 
431   return GST_TAG_DEMUX_RESULT_OK;
432 }
433 
434 static gboolean
plugin_init(GstPlugin * plugin)435 plugin_init (GstPlugin * plugin)
436 {
437   return gst_element_register (plugin, "apedemux",
438       GST_RANK_PRIMARY, GST_TYPE_APE_DEMUX);
439 }
440 
441 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
442     GST_VERSION_MINOR,
443     apetag,
444     "APEv1/2 tag reader",
445     plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)
446