aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/ogg_extractor.c
blob: d7005ef4c0e47e460d75052576a98306c9af81e1 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
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
196
197
198
199
200
201
202
203
/*
     This file is part of libextractor.
     Copyright (C) 2002, 2003, 2009, 2012 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 3, 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., 51 Franklin Street, Fifth Floor,
     Boston, MA 02110-1301, USA.
 */
/**
 * @file plugins/ogg_extractor.c
 * @brief plugin to support OGG files
 * @author Christian Grothoff
 */
#include "platform.h"
#include "extractor.h"
#include <vorbis/vorbisfile.h>

/**
 * Bytes each ogg file must begin with (not used, but we might
 * choose to add this back in the future to improve performance
 * for non-ogg files).
 */
#define OGG_HEADER 0x4f676753


/**
 * Custom read function for ogg.
 *
 * @param ptr where to write the data
 * @param size number of bytes to read per member
 * @param nmemb number of members to read
 * @param datasource the 'struct EXTRACTOR_ExtractContext'
 * @return 0 on end-of-data, 0 with errno set to indicate read error
 */
static size_t
read_ogg (void *ptr, size_t size, size_t nmemb, void *datasource)
{
  struct EXTRACTOR_ExtractContext *ec = datasource;
  void *data;
  ssize_t ret;

  data = NULL;
  ret = ec->read (ec->cls,
		  &data,
		  size * nmemb);
  if (-1 == ret)
    return 0;
  if (0 == ret)
    {
      errno = 0;
      return 0;
    }
  memcpy (ptr, data, ret);
  errno = 0;
  return ret;
}


/**
 * Seek to a particular position in the file.
 *
 * @param datasource  the 'struct EXTRACTOR_ExtractContext'
 * @param offset where to seek
 * @param whence how to seek
 * @return -1 on error, new position on success
 */
static int
seek_ogg (void *datasource,
	  ogg_int64_t offset,
	  int whence)
{
  struct EXTRACTOR_ExtractContext *ec = datasource;
  int64_t new_position;

  new_position = ec->seek (ec->cls, (int64_t) offset, whence);
  return (long) new_position;
}


/**
 * Tell ogg where we are in the file
 *
 * @param datasource  the 'struct EXTRACTOR_ExtractContext'
 * @return
 */
static long
tell_ogg (void *datasource)
{
  struct EXTRACTOR_ExtractContext *ec = datasource;

  return (long) ec->seek (ec->cls,
			  0,
			  SEEK_CUR);
}



/**
 * Extract the associated meta data for a given label from vorbis.
 *
 * @param vc vorbis comment data
 * @param label label marking the desired entry
 * @return NULL on error, otherwise the meta data
 */
static char *
get_comment (vorbis_comment *vc,
	     const char *label)
{
  if (NULL == vc)
    return NULL;
  return vorbis_comment_query (vc, label, 0);
}


/**
 * Extract meta data from vorbis using the given LE type and value.
 *
 * @param t LE meta data type
 * @param s meta data to add
 */
#define ADD(t,s) do { if (0 != (ret = ec->proc (ec->cls, "ogg", t, EXTRACTOR_METAFORMAT_UTF8, "text/plain", s, strlen(s)+1))) goto FINISH; } while (0)


/**
 * Extract meta data from vorbis using the given LE type and label.
 *
 * @param t LE meta data type
 * @param d vorbis meta data label
 */
#define ADDG(t,d) do { m = get_comment (comments, d); if (NULL != m) ADD(t,m); } while (0)


/**
 * Main entry method for the 'application/ogg' extraction plugin.
 *
 * @param ec extraction context provided to the plugin
 */
void
EXTRACTOR_ogg_extract_method (struct EXTRACTOR_ExtractContext *ec)
{
  uint64_t fsize;
  ov_callbacks callbacks;
  OggVorbis_File vf;
  vorbis_comment *comments;
  int ret;
  const char *m;

  fsize = ec->get_size (ec->cls);
  if (fsize < 8)
    return;

  callbacks.read_func = &read_ogg;
  callbacks.seek_func = &seek_ogg;
  callbacks.close_func = NULL;
  callbacks.tell_func = &tell_ogg;
  ret = ov_open_callbacks (ec, &vf, NULL, 0, callbacks);
  if (0 != ret)
  {
    ov_clear (&vf);
    return;
  }
  comments = ov_comment (&vf, -1);
  if (NULL == comments)
  {
    ov_clear (&vf);
    return;
  }
  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);
}

/* end of ogg_extractor.c */