2 * Copyright (C) 2007 by INdT
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * @author Renato Chencarek <renato.chencarek@openbossa.org>
31 #include <lightmediascanner_plugin.h>
32 #include <lightmediascanner_db.h>
35 #include <vorbis/codec.h>
36 #include <vorbis/vorbisfile.h>
39 _id3_tag_size (FILE *file)
44 if (fread(tmp, 1, 4, file) == 4) {
45 if (tmp[0] == 'I' && tmp[1] == 'D' &&
46 tmp[2] == '3' && tmp[3] < 0xFF) {
47 fseek(file, 2, SEEK_CUR);
48 if (fread(tmp, 1, 4, file) == 4) {
49 size = 10 + ( (long)(tmp[3])
50 | ((long)(tmp[2]) << 7)
51 | ((long)(tmp[1]) << 14)
52 | ((long)(tmp[0]) << 21) );
63 _get_vorbis_comment (FILE *file, vorbis_comment *vc)
66 int bytes, i, chunks = 0;
78 ogg_sync_init(&osync);
81 buffer = ogg_sync_buffer(&osync, CHUNKSIZE);
82 bytes = fread(buffer, 1, CHUNKSIZE, file);
84 ogg_sync_wrote(&osync, bytes);
86 if (ogg_sync_pageout(&osync, &og) == 1)
93 serial = ogg_page_serialno(&og);
95 ogg_stream_init(&os, serial);
96 vorbis_info_init(&vi);
97 vorbis_comment_init(vc);
99 if (ogg_stream_pagein(&os, &og) < 0)
101 if (ogg_stream_packetout(&os, &header) != 1)
103 if (vorbis_synthesis_headerin(&vi, vc, &header) != 0)
108 while (i < nheaders) {
109 while (i < nheaders) {
110 int result = ogg_sync_pageout(&osync, &og);
113 else if (result == 1) {
114 ogg_stream_pagein(&os, &og);
115 while (i < nheaders) {
116 result = ogg_stream_packetout(&os, &header);
122 vorbis_synthesis_headerin(&vi, vc, &header);
128 buffer = ogg_sync_buffer(&osync, CHUNKSIZE);
129 bytes = fread(buffer, 1, CHUNKSIZE, file);
131 if (bytes == 0 && i < 2)
134 ogg_sync_wrote(&osync, bytes);
137 ogg_stream_clear(&os);
138 ogg_sync_clear(&osync);
139 vorbis_info_clear(&vi);
145 _parse_ogg (const char *filename, struct lms_audio_info *info)
155 file = fopen(filename, "rb");
159 fseek(file, _id3_tag_size(file), SEEK_SET);
161 if (_get_vorbis_comment(file, &vc) != 0)
164 tag = vorbis_comment_query(&vc, "TITLE", 0);
165 if (tag && (size = strlen(tag)) > 0) {
166 info->title.len = size;
167 info->title.str = malloc(size * sizeof(char));
168 memcpy(info->title.str, tag, size);
171 tag = vorbis_comment_query(&vc, "ARTIST", 0);
172 if (tag && (size = strlen(tag)) > 0) {
173 info->artist.len = size;
174 info->artist.str = malloc(size * sizeof(char));
175 memcpy(info->artist.str, tag, size);
178 tag = vorbis_comment_query(&vc, "ALBUM", 0);
179 if (tag && (size = strlen(tag)) > 0) {
180 info->album.len = size;
181 info->album.str = malloc(size * sizeof(char));
182 memcpy(info->album.str, tag, size);
185 tag = vorbis_comment_query(&vc, "TRACKNUMBER", 0);
186 if (tag && (size = strlen(tag)) > 0) {
187 info->trackno = atoi(tag);
190 tag = vorbis_comment_query(&vc, "GENRE", 0);
191 if (tag && (size = strlen(tag)) > 0) {
192 info->genre.len = size;
193 info->genre.str = malloc(size * sizeof(char));
194 memcpy(info->genre.str, tag, size);
198 vorbis_comment_clear(&vc);
204 static const char _name[] = "ogg";
205 static const struct lms_string_size _exts[] = {
206 LMS_STATIC_STRING_SIZE(".ogg")
210 struct lms_plugin plugin;
211 lms_db_audio_t *audio_db;
215 _match(struct plugin *p, const char *path, int len, int base)
219 i = lms_which_extension(path, len, _exts, LMS_ARRAY_SIZE(_exts));
223 return (void*)(i + 1);
227 _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_info *finfo, void *match)
229 struct lms_audio_info info = {0};
232 r = _parse_ogg(finfo->path, &info);
236 if (!info.title.str) {
239 ext_idx = ((int)match) - 1;
240 info.title.len = finfo->path_len - finfo->base - _exts[ext_idx].len;
241 info.title.str = (char *)malloc((info.title.len + 1) * sizeof(char));
242 memcpy(info.title.str, finfo->path + finfo->base, info.title.len);
243 info.title.str[info.title.len] = '\0';
247 lms_charset_conv(ctxt->cs_conv, &info.title.str, &info.title.len);
249 lms_charset_conv(ctxt->cs_conv, &info.artist.str, &info.artist.len);
251 lms_charset_conv(ctxt->cs_conv, &info.album.str, &info.album.len);
253 lms_charset_conv(ctxt->cs_conv, &info.genre.str, &info.genre.len);
256 r = lms_db_audio_add(plugin->audio_db, &info);
260 free(info.title.str);
262 free(info.artist.str);
264 free(info.album.str);
266 free(info.genre.str);
272 _setup(struct plugin *plugin, struct lms_context *ctxt)
274 plugin->audio_db = lms_db_audio_new(ctxt->db);
275 if (!plugin->audio_db)
282 _start(struct plugin *plugin, struct lms_context *ctxt)
284 return lms_db_audio_start(plugin->audio_db);
288 _finish(struct plugin *plugin, struct lms_context *ctxt)
290 if (plugin->audio_db)
291 return lms_db_audio_free(plugin->audio_db);
297 _close(struct plugin *plugin)
303 API struct lms_plugin *
304 lms_plugin_open(void)
306 struct plugin *plugin;
308 plugin = (struct plugin *)malloc(sizeof(*plugin));
309 plugin->plugin.name = _name;
310 plugin->plugin.match = (lms_plugin_match_fn_t)_match;
311 plugin->plugin.parse = (lms_plugin_parse_fn_t)_parse;
312 plugin->plugin.close = (lms_plugin_close_fn_t)_close;
313 plugin->plugin.setup = (lms_plugin_setup_fn_t)_setup;
314 plugin->plugin.start = (lms_plugin_start_fn_t)_start;
315 plugin->plugin.finish = (lms_plugin_finish_fn_t)_finish;
317 return (struct lms_plugin *)plugin;