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