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