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);
169 lms_string_size_strip_and_free(&info->title);
172 tag = vorbis_comment_query(&vc, "ARTIST", 0);
173 if (tag && (size = strlen(tag)) > 0) {
174 info->artist.len = size;
175 info->artist.str = malloc(size * sizeof(char));
176 memcpy(info->artist.str, tag, size);
177 lms_string_size_strip_and_free(&info->artist);
180 tag = vorbis_comment_query(&vc, "ALBUM", 0);
181 if (tag && (size = strlen(tag)) > 0) {
182 info->album.len = size;
183 info->album.str = malloc(size * sizeof(char));
184 memcpy(info->album.str, tag, size);
185 lms_string_size_strip_and_free(&info->album);
188 tag = vorbis_comment_query(&vc, "TRACKNUMBER", 0);
189 if (tag && (size = strlen(tag)) > 0) {
190 info->trackno = atoi(tag);
193 tag = vorbis_comment_query(&vc, "GENRE", 0);
194 if (tag && (size = strlen(tag)) > 0) {
195 info->genre.len = size;
196 info->genre.str = malloc(size * sizeof(char));
197 memcpy(info->genre.str, tag, size);
198 lms_string_size_strip_and_free(&info->genre);
202 vorbis_comment_clear(&vc);
208 static const char _name[] = "ogg";
209 static const struct lms_string_size _exts[] = {
210 LMS_STATIC_STRING_SIZE(".ogg")
214 struct lms_plugin plugin;
215 lms_db_audio_t *audio_db;
219 _match(struct plugin *p, const char *path, int len, int base)
223 i = lms_which_extension(path, len, _exts, LMS_ARRAY_SIZE(_exts));
227 return (void*)(i + 1);
231 _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_info *finfo, void *match)
233 struct lms_audio_info info = {0};
236 r = _parse_ogg(finfo->path, &info);
240 if (!info.title.str) {
243 ext_idx = ((int)match) - 1;
244 info.title.len = finfo->path_len - finfo->base - _exts[ext_idx].len;
245 info.title.str = (char *)malloc((info.title.len + 1) * sizeof(char));
246 memcpy(info.title.str, finfo->path + finfo->base, info.title.len);
247 info.title.str[info.title.len] = '\0';
251 lms_charset_conv(ctxt->cs_conv, &info.title.str, &info.title.len);
253 lms_charset_conv(ctxt->cs_conv, &info.artist.str, &info.artist.len);
255 lms_charset_conv(ctxt->cs_conv, &info.album.str, &info.album.len);
257 lms_charset_conv(ctxt->cs_conv, &info.genre.str, &info.genre.len);
260 r = lms_db_audio_add(plugin->audio_db, &info);
264 free(info.title.str);
266 free(info.artist.str);
268 free(info.album.str);
270 free(info.genre.str);
276 _setup(struct plugin *plugin, struct lms_context *ctxt)
278 plugin->audio_db = lms_db_audio_new(ctxt->db);
279 if (!plugin->audio_db)
286 _start(struct plugin *plugin, struct lms_context *ctxt)
288 return lms_db_audio_start(plugin->audio_db);
292 _finish(struct plugin *plugin, struct lms_context *ctxt)
294 if (plugin->audio_db)
295 return lms_db_audio_free(plugin->audio_db);
301 _close(struct plugin *plugin)
307 API struct lms_plugin *
308 lms_plugin_open(void)
310 struct plugin *plugin;
312 plugin = (struct plugin *)malloc(sizeof(*plugin));
313 plugin->plugin.name = _name;
314 plugin->plugin.match = (lms_plugin_match_fn_t)_match;
315 plugin->plugin.parse = (lms_plugin_parse_fn_t)_parse;
316 plugin->plugin.close = (lms_plugin_close_fn_t)_close;
317 plugin->plugin.setup = (lms_plugin_setup_fn_t)_setup;
318 plugin->plugin.start = (lms_plugin_start_fn_t)_start;
319 plugin->plugin.finish = (lms_plugin_finish_fn_t)_finish;
321 return (struct lms_plugin *)plugin;