aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/ogg_extractor.c
blob: a9c4f0541488ed03c6fa92a5606c23629ad33f78 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
     This file is part of libextractor.
     (C) 2002, 2003, 2009 Vidyut Samanta and Christian Grothoff

     libextractor is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 2, or (at your
     option) any later version.

     libextractor 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
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with libextractor; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
 */

#include "platform.h"
#include "extractor.h"
#include "extractor_plugins.h"

#define DEBUG_EXTRACT_OGG 0
#define OGG_HEADER 0x4f676753

#if HAVE_VORBIS_VORBISFILE_H
#include <vorbis/vorbisfile.h>
#else
#error You must install the libvorbis header files!
#endif

static char *
get_comment (vorbis_comment * vc, char *label)
{
  char *tag;
  if (vc && (tag = vorbis_comment_query (vc, label, 0)) != NULL)
    return tag;
  return NULL;
}

static size_t
readOgg (void *ptr, size_t size, size_t nmemb, void *datasource)
{
  struct EXTRACTOR_PluginList *plugin = datasource;
  int64_t ret;
  unsigned char *read_data;

  ret = pl_read (plugin, &read_data, size*nmemb);
  if (ret <= 0)
  {
    if (ret < 0)
      errno = EIO;
    return 0;
  }
  memcpy (ptr, read_data, ret);
  return ret;
}

#define ADD(t,s) do { if (0 != (ret = proc (proc_cls, "ogg", t, EXTRACTOR_METAFORMAT_UTF8, "text/plain", s, strlen(s)+1))) goto FINISH; } while (0)

#define ADDG(t,d) do { m = get_comment (comments, d); if (m != NULL) ADD(t,m); } while (0)

/* mimetype = application/ogg */
int
EXTRACTOR_ogg_extract_method (struct EXTRACTOR_PluginList *plugin,
    EXTRACTOR_MetaDataProcessor proc, void *proc_cls)
{
  OggVorbis_File vf;
  vorbis_comment *comments;
  ov_callbacks callbacks;
  int ret;
  int64_t fsize;
  const char *m;

  fsize = pl_get_fsize (plugin);
  if (fsize > 0 && fsize < 2 * sizeof (int))
    return 1;
  if (fsize == 0)
    return 1;

  /* TODO: rewrite pl_seek() to be STDIO-compatible (SEEK_END) and enable seeking. */
  callbacks.read_func = &readOgg;
  callbacks.seek_func = NULL;
  callbacks.close_func = NULL;
  callbacks.tell_func = NULL;
  if (0 != ov_open_callbacks (plugin, &vf, NULL, 0, callbacks))
  {
    ov_clear (&vf);
    return 1;
  }
  comments = ov_comment (&vf, -1);
  if (NULL == comments)
  {
    ov_clear (&vf);
    return 1;
  }
  ret = 0;
  ADD (EXTRACTOR_METATYPE_MIMETYPE, "application/ogg");
  if ((comments->vendor != NULL) && (strlen (comments->vendor) > 0))
    ADD (EXTRACTOR_METATYPE_VENDOR, comments->vendor);
  ADDG (EXTRACTOR_METATYPE_TITLE, "title");
  ADDG (EXTRACTOR_METATYPE_ARTIST, "artist");
  ADDG (EXTRACTOR_METATYPE_PERFORMER, "performer");
  ADDG (EXTRACTOR_METATYPE_ALBUM, "album");
  ADDG (EXTRACTOR_METATYPE_TRACK_NUMBER, "tracknumber");
  ADDG (EXTRACTOR_METATYPE_DISC_NUMBER, "discnumber");
  ADDG (EXTRACTOR_METATYPE_CONTACT_INFORMATION, "contact");
  ADDG (EXTRACTOR_METATYPE_GENRE, "genre");
  ADDG (EXTRACTOR_METATYPE_CREATION_DATE, "date");
  ADDG (EXTRACTOR_METATYPE_COMMENT, "");
  ADDG (EXTRACTOR_METATYPE_LOCATION_SUBLOCATION, "location");
  ADDG (EXTRACTOR_METATYPE_DESCRIPTION, "description");
  ADDG (EXTRACTOR_METATYPE_ISRC, "isrc");
  ADDG (EXTRACTOR_METATYPE_ORGANIZATION, "organization");
  ADDG (EXTRACTOR_METATYPE_COPYRIGHT, "copyright");
  ADDG (EXTRACTOR_METATYPE_LICENSE, "license");
  ADDG (EXTRACTOR_METATYPE_SONG_VERSION, "version");
FINISH:
  ov_clear (&vf);
  return 1;
}