gstapedemux.c 13.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* GStreamer APEv1/2 tag reader
 * Copyright (C) 2004 Ronald Bultje <rbultje@ronald.bitfreak.net>
 * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
17 18
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
19 20 21 22 23 24 25 26
 */

/**
 * SECTION:element-apedemux
 *
 * apedemux accepts data streams with APE tags at the start or at the end
 * (or both). The mime type of the data between the tag blocks is detected
 * using typefind functions, and the appropriate output mime type set on
27 28
 * outgoing buffers.
 *
29 30 31 32 33 34
 * The element is only able to read APE tags at the end of a stream from
 * a seekable stream, ie. when get_range mode is supported by the upstream
 * elements. If get_range operation is available, apedemux makes it available
 * downstream. This means that elements which require get_range mode, such as
 * wavparse or musepackdec, can operate on files containing APE tag
 * 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 42 43 44 45 46 47 48 49 50
 * The contents of the file inside the APE tag regions should be detected, and
 * the appropriate mime type set on buffers produced from apedemux.
 * </refsect2>
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gst/gst.h>
#include <gst/gst-i18n-plugin.h>
51
#include <gst/pbutils/pbutils.h>
52 53 54

#include "gstapedemux.h"

55
#include <stdio.h>
56 57 58 59
#include <string.h>

#define APE_VERSION_MAJOR(ver)  ((ver)/1000)

60
GST_DEBUG_CATEGORY_STATIC (apedemux_debug);
61 62 63 64 65 66 67 68 69 70 71 72 73 74
#define GST_CAT_DEFAULT (apedemux_debug)

static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("application/x-apetag")
    );

static gboolean gst_ape_demux_identify_tag (GstTagDemux * demux,
    GstBuffer * buffer, gboolean start_tag, guint * tag_size);
static GstTagDemuxResult gst_ape_demux_parse_tag (GstTagDemux * demux,
    GstBuffer * buffer, gboolean start_tag, guint * tag_size,
    GstTagList ** tags);

Wim Taymans's avatar
Wim Taymans committed
75
G_DEFINE_TYPE (GstApeDemux, gst_ape_demux, GST_TYPE_TAG_DEMUX);
76

77
static void
Wim Taymans's avatar
Wim Taymans committed
78
gst_ape_demux_class_init (GstApeDemuxClass * klass)
79
{
Wim Taymans's avatar
Wim Taymans committed
80 81
  GstElementClass *element_class;
  GstTagDemuxClass *tagdemux_class;
82

Wim Taymans's avatar
Wim Taymans committed
83 84 85 86 87
  GST_DEBUG_CATEGORY_INIT (apedemux_debug, "apedemux", 0,
      "GStreamer APE tag demuxer");

  tagdemux_class = GST_TAG_DEMUX_CLASS (klass);
  element_class = GST_ELEMENT_CLASS (klass);
88

89
  gst_element_class_set_static_metadata (element_class, "APE tag demuxer",
90 91 92
      "Codec/Demuxer/Metadata",
      "Read and output APE tags while demuxing the contents",
      "Tim-Philipp Müller <tim centricular net>");
93

Wim Taymans's avatar
Wim Taymans committed
94 95
  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&sink_factory));
96 97 98

  tagdemux_class->identify_tag = GST_DEBUG_FUNCPTR (gst_ape_demux_identify_tag);
  tagdemux_class->parse_tag = GST_DEBUG_FUNCPTR (gst_ape_demux_parse_tag);
99 100 101 102 103 104 105

  /* no need for a merge function, the default behaviour to prefer start
   * tags (APEv2) over end tags (usually APEv1, but could theoretically also
   * be APEv2) is fine */

  tagdemux_class->min_start_size = 32;
  tagdemux_class->min_end_size = 32;
106 107 108
}

static void
Wim Taymans's avatar
Wim Taymans committed
109
gst_ape_demux_init (GstApeDemux * apedemux)
110
{
111
  /* nothing to do here */
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
}

static const struct _GstApeDemuxTagTableEntry
{
  const gchar *ape_tag;
  const gchar *gst_tag;
} tag_table[] = {
  {
  "replaygain_track_gain", GST_TAG_TRACK_GAIN}, {
  "replaygain_track_peak", GST_TAG_TRACK_PEAK}, {
  "replaygain_album_gain", GST_TAG_ALBUM_GAIN}, {
  "replaygain_album_peak", GST_TAG_ALBUM_PEAK}, {
  "title", GST_TAG_TITLE}, {
  "artist", GST_TAG_ARTIST}, {
  "album", GST_TAG_ALBUM}, {
127
  "composer", GST_TAG_COMPOSER}, {
128 129 130 131 132
  "comment", GST_TAG_COMMENT}, {
  "comments", GST_TAG_COMMENT}, {
  "copyright", GST_TAG_COPYRIGHT}, {
  "genre", GST_TAG_GENRE}, {
  "isrc", GST_TAG_ISRC}, {
133 134 135 136
  "disc", GST_TAG_ALBUM_VOLUME_NUMBER}, {
  "disk", GST_TAG_ALBUM_VOLUME_NUMBER}, {
  "discnumber", GST_TAG_ALBUM_VOLUME_NUMBER}, {
  "disknumber", GST_TAG_ALBUM_VOLUME_NUMBER}, {
137
  "track", GST_TAG_TRACK_NUMBER}, {
138
  "tracknumber", GST_TAG_TRACK_NUMBER}, {
139 140
  "year", GST_TAG_DATE}, {
  "file", GST_TAG_LOCATION}
141 142 143 144 145 146 147 148 149 150 151 152
};

static gboolean
ape_demux_get_gst_tag_from_tag (const gchar * ape_tag,
    const gchar ** gst_tag, GType * gst_tag_type)
{
  gint i;

  for (i = 0; i < G_N_ELEMENTS (tag_table); ++i) {
    if (g_ascii_strcasecmp (tag_table[i].ape_tag, ape_tag) == 0) {
      *gst_tag = tag_table[i].gst_tag;
      *gst_tag_type = gst_tag_get_type (tag_table[i].gst_tag);
153
      GST_LOG ("Mapped APE tag '%s' to GStreamer tag '%s'", ape_tag, *gst_tag);
154 155 156 157 158 159 160 161 162 163 164
      return TRUE;
    }
  }

  GST_WARNING ("Could not map APE tag '%s' to a GStreamer tag", ape_tag);
  return FALSE;
}

static GstTagList *
ape_demux_parse_tags (const guint8 * data, gint size)
{
Edward Hervey's avatar
Edward Hervey committed
165
  GstTagList *taglist = gst_tag_list_new_empty ();
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195

  GST_LOG ("Reading tags from chunk of size %u bytes", size);

  /* get rid of header/footer */
  if (size >= 32 && memcmp (data, "APETAGEX", 8) == 0) {
    data += 32;
    size -= 32;
  }
  if (size > 32 && memcmp (data + size - 32, "APETAGEX", 8) == 0) {
    size -= 32;
  }

  /* read actual tags - at least 10 bytes for tag header */
  while (size >= 10) {
    guint len, n = 8;
    gchar *tag, *val;
    const gchar *gst_tag;
    GType gst_tag_type;

    /* find tag type and size */
    len = GST_READ_UINT32_LE (data);
    while (n < size && data[n] != 0x0)
      n++;
    if (n == size)
      break;
    g_assert (data[n] == 0x0);
    n++;
    if (size - n < len)
      break;

Edward Hervey's avatar
Edward Hervey committed
196 197 198 199
    /* If the tag is empty, skip to the next one */
    if (len == 0)
      goto next_tag;

200 201 202 203
    /* read */
    tag = g_strndup ((gchar *) data + 8, n - 9);
    val = g_strndup ((gchar *) data + n, len);

Edward Hervey's avatar
Edward Hervey committed
204 205
    GST_LOG ("tag [%s], val[%s]", tag, val);

206 207 208 209 210 211 212 213 214 215 216
    /* special-case 'media' tag, could be e.g. "CD 1/2" */
    if (g_ascii_strcasecmp (tag, "media") == 0) {
      gchar *sp, *sp2;

      g_free (tag);
      tag = g_strdup ("discnumber");
      /* get rid of the medium in front */
      sp = strchr (val, ' ');
      while (sp != NULL && (sp2 = strchr (sp + 1, ' ')) != NULL)
        sp = sp2;
      if (sp) {
217
        memmove (val, sp + 1, strlen (sp + 1) + 1);
218 219 220
      }
    }

221 222 223 224
    if (ape_demux_get_gst_tag_from_tag (tag, &gst_tag, &gst_tag_type)) {
      GValue v = { 0, };

      switch (gst_tag_type) {
225 226 227 228 229 230 231
        case G_TYPE_INT:{
          gint v_int;

          if (sscanf (val, "%d", &v_int) == 1) {
            g_value_init (&v, G_TYPE_INT);
            g_value_set_int (&v, v_int);
          }
232
          break;
233 234 235 236 237 238 239 240 241 242 243
        }
        case G_TYPE_UINT:{
          guint v_uint, count;

          if (strcmp (gst_tag, GST_TAG_TRACK_NUMBER) == 0) {
            gint dummy;

            if (sscanf (val, "%u", &v_uint) == 1 && v_uint > 0) {
              g_value_init (&v, G_TYPE_UINT);
              g_value_set_uint (&v, v_uint);
            }
244
            GST_LOG ("checking for track count: %s", val);
245 246 247 248 249
            /* might be 0/N or -1/N to specify that there is only a count */
            if (sscanf (val, "%d/%u", &dummy, &count) == 2 && count > 0) {
              gst_tag_list_add (taglist, GST_TAG_MERGE_APPEND,
                  GST_TAG_TRACK_COUNT, count, NULL);
            }
250 251 252 253 254 255 256 257 258 259 260 261 262
          } else if (strcmp (gst_tag, GST_TAG_ALBUM_VOLUME_NUMBER) == 0) {
            gint dummy;

            if (sscanf (val, "%u", &v_uint) == 1 && v_uint > 0) {
              g_value_init (&v, G_TYPE_UINT);
              g_value_set_uint (&v, v_uint);
            }
            GST_LOG ("checking for volume count: %s", val);
            /* might be 0/N or -1/N to specify that there is only a count */
            if (sscanf (val, "%d/%u", &dummy, &count) == 2 && count > 0) {
              gst_tag_list_add (taglist, GST_TAG_MERGE_APPEND,
                  GST_TAG_ALBUM_VOLUME_COUNT, count, NULL);
            }
263 264 265 266
          } else if (sscanf (val, "%u", &v_uint) == 1) {
            g_value_init (&v, G_TYPE_UINT);
            g_value_set_uint (&v, v_uint);
          }
267
          break;
268 269
        }
        case G_TYPE_STRING:{
270 271 272
          g_value_init (&v, G_TYPE_STRING);
          g_value_set_string (&v, val);
          break;
273 274 275
        }
        case G_TYPE_DOUBLE:{
          gdouble v_double;
276
          gchar *endptr;
277

278 279 280 281 282
          /* floating point strings can be "4,123" or "4.123" depending on
           * the locale. We need to be able to parse and read either version
           * no matter what our current locale is */
          g_strdelimit (val, ",", '.');
          v_double = g_ascii_strtod (val, &endptr);
283
          if (endptr != val) {
284 285 286
            g_value_init (&v, G_TYPE_DOUBLE);
            g_value_set_double (&v, v_double);
          }
287

288
          break;
289
        }
290
        default:{
291
          if (gst_tag_type == G_TYPE_DATE) {
292 293 294 295
            gint v_int;

            if (sscanf (val, "%d", &v_int) == 1) {
              GDate *date = g_date_new_dmy (1, 1, v_int);
296

297 298
              g_value_init (&v, G_TYPE_DATE);
              g_value_take_boxed (&v, date);
299
            }
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
          } else {
            GST_WARNING ("Unhandled tag type '%s' for tag '%s'",
                g_type_name (gst_tag_type), gst_tag);
          }
          break;
        }
      }
      if (G_VALUE_TYPE (&v) != 0) {
        gst_tag_list_add_values (taglist, GST_TAG_MERGE_APPEND,
            gst_tag, &v, NULL);
        g_value_unset (&v);
      }
    }
    GST_DEBUG ("Read tag %s: %s", tag, val);
    g_free (tag);
    g_free (val);

    /* move data pointer */
Edward Hervey's avatar
Edward Hervey committed
318
  next_tag:
319 320 321 322 323 324 325 326 327 328 329 330
    size -= len + n;
    data += len + n;
  }

  GST_DEBUG ("Taglist: %" GST_PTR_FORMAT, taglist);
  return taglist;
}

static gboolean
gst_ape_demux_identify_tag (GstTagDemux * demux, GstBuffer * buffer,
    gboolean start_tag, guint * tag_size)
{
Wim Taymans's avatar
Wim Taymans committed
331
  GstMapInfo map;
Wim Taymans's avatar
Wim Taymans committed
332

Wim Taymans's avatar
Wim Taymans committed
333
  gst_buffer_map (buffer, &map, GST_MAP_READ);
Wim Taymans's avatar
Wim Taymans committed
334

Wim Taymans's avatar
Wim Taymans committed
335
  if (memcmp (map.data, "APETAGEX", 8) != 0) {
336 337
    GST_DEBUG_OBJECT (demux, "No APETAGEX marker at %s - not an APE file",
        (start_tag) ? "start" : "end");
Wim Taymans's avatar
Wim Taymans committed
338
    gst_buffer_unmap (buffer, &map);
339 340 341
    return FALSE;
  }

Wim Taymans's avatar
Wim Taymans committed
342
  *tag_size = GST_READ_UINT32_LE (map.data + 12);
343 344 345 346

  /* size is without header, so add 32 to account for that */
  *tag_size += 32;

Wim Taymans's avatar
Wim Taymans committed
347
  gst_buffer_unmap (buffer, &map);
Wim Taymans's avatar
Wim Taymans committed
348

349 350 351 352 353 354 355
  return TRUE;
}

static GstTagDemuxResult
gst_ape_demux_parse_tag (GstTagDemux * demux, GstBuffer * buffer,
    gboolean start_tag, guint * tag_size, GstTagList ** tags)
{
Wim Taymans's avatar
Wim Taymans committed
356
  guint8 *data;
Sebastian Dröge's avatar
Sebastian Dröge committed
357
  guint8 *footer;
358 359
  gboolean have_header;
  gboolean end_tag = !start_tag;
360
  GstCaps *sink_caps;
361
  guint version, footer_size;
Wim Taymans's avatar
Wim Taymans committed
362
  GstMapInfo map;
Sebastian Dröge's avatar
Sebastian Dröge committed
363 364
  gsize size;

Wim Taymans's avatar
Wim Taymans committed
365 366 367
  gst_buffer_map (buffer, &map, GST_MAP_READ);
  data = map.data;
  size = map.size;
368

Sebastian Dröge's avatar
Sebastian Dröge committed
369
  GST_LOG_OBJECT (demux, "Parsing buffer of size %" G_GSIZE_FORMAT, size);
370

Sebastian Dröge's avatar
Sebastian Dröge committed
371
  footer = data + size - 32;
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424

  GST_LOG_OBJECT (demux, "Checking for footer at offset 0x%04x",
      (guint) (footer - data));
  if (footer > data && memcmp (footer, "APETAGEX", 8) == 0) {
    GST_DEBUG_OBJECT (demux, "Found footer");
    footer_size = 32;
  } else {
    GST_DEBUG_OBJECT (demux, "No footer");
    footer_size = 0;
  }

  /* APE tags at the end must have a footer */
  if (end_tag && footer_size == 0) {
    GST_WARNING_OBJECT (demux, "Tag at end of file without footer!");
    return GST_TAG_DEMUX_RESULT_BROKEN_TAG;
  }

  /* don't trust the header/footer flags, better detect them ourselves */
  have_header = (memcmp (data, "APETAGEX", 8) == 0);

  if (start_tag && !have_header) {
    GST_DEBUG_OBJECT (demux, "Tag at beginning of file without header!");
    return GST_TAG_DEMUX_RESULT_BROKEN_TAG;
  }

  if (end_tag && !have_header) {
    GST_DEBUG_OBJECT (demux, "Tag at end of file has no header (APEv1)");
    *tag_size -= 32;            /* adjust tag size */
  }

  if (have_header) {
    version = GST_READ_UINT32_LE (data + 8);
  } else {
    version = GST_READ_UINT32_LE (footer + 8);
  }

  /* skip header */
  if (have_header) {
    data += 32;
  }

  GST_DEBUG_OBJECT (demux, "APE tag with version %u, size %u at offset 0x%08"
      G_GINT64_MODIFIER "x", version, *tag_size,
      GST_BUFFER_OFFSET (buffer) + ((have_header) ? 0 : 32));

  if (APE_VERSION_MAJOR (version) != 1 && APE_VERSION_MAJOR (version) != 2) {
    GST_WARNING ("APE tag is version %u.%03u, but decoder only supports "
        "v1 or v2. Ignoring.", APE_VERSION_MAJOR (version), version % 1000);
    return GST_TAG_DEMUX_RESULT_OK;
  }

  *tags = ape_demux_parse_tags (data, *tag_size - footer_size);

425 426 427 428 429
  sink_caps = gst_static_pad_template_get_caps (&sink_factory);
  gst_pb_utils_add_codec_description_to_tag_list (*tags,
      GST_TAG_CONTAINER_FORMAT, sink_caps);
  gst_caps_unref (sink_caps);

Wim Taymans's avatar
Wim Taymans committed
430
  gst_buffer_unmap (buffer, &map);
Sebastian Dröge's avatar
Sebastian Dröge committed
431

432 433 434 435 436 437 438 439 440 441 442 443
  return GST_TAG_DEMUX_RESULT_OK;
}

static gboolean
plugin_init (GstPlugin * plugin)
{
  return gst_element_register (plugin, "apedemux",
      GST_RANK_PRIMARY, GST_TYPE_APE_DEMUX);
}

GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
    GST_VERSION_MINOR,
444
    apetag,
445 446
    "APEv1/2 tag reader",
    plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)