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