3425370afa1c5843f76c78d1a57f97aaa8371c57
[lms] / lightmediascanner / src / plugins / ogg / ogg.c
1 /**
2  * Copyright (C) 2007 by INdT
3  *
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.
8  *
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.
13  *
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.
17  *
18  * @author Renato Chencarek <renato.chencarek@openbossa.org>
19  */
20
21 /**
22  * @brief
23  *
24  * ogg file parser.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <lightmediascanner_plugin.h>
32 #include <lightmediascanner_db.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <vorbis/codec.h>
36 #include <vorbis/vorbisfile.h>
37
38 static long int
39 _id3_tag_size (FILE *file)
40 {
41     unsigned char tmp[4];
42     long int size;
43
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) );
53
54                 return size;
55             }
56         }
57     }
58     return 0L;
59 }
60
61
62 static int
63 _get_vorbis_comment (FILE *file, vorbis_comment *vc)
64 {
65     char *buffer;
66     int bytes, i, chunks = 0;
67     ogg_packet header;
68
69     ogg_page og;
70     ogg_sync_state osync;
71     ogg_stream_state os;
72     vorbis_info vi;
73
74     int serial;
75     int CHUNKSIZE = 4096;
76     int nheaders = 0;
77
78     ogg_sync_init(&osync);
79
80     while (1) {
81         buffer = ogg_sync_buffer(&osync, CHUNKSIZE);
82         bytes = fread(buffer, 1, CHUNKSIZE, file);
83
84         ogg_sync_wrote(&osync, bytes);
85
86         if (ogg_sync_pageout(&osync, &og) == 1)
87             break;
88
89         if (chunks++ >= 10)
90             return -1;
91     }
92
93     serial = ogg_page_serialno(&og);
94
95     ogg_stream_init(&os, serial);
96     vorbis_info_init(&vi);
97     vorbis_comment_init(vc);
98
99     if (ogg_stream_pagein(&os, &og) < 0)
100         return -1;
101     if (ogg_stream_packetout(&os, &header) != 1)
102         return -1;
103     if (vorbis_synthesis_headerin(&vi, vc, &header) != 0)
104         return -1;
105
106     i = 1;
107     nheaders = 3;
108     while (i < nheaders) {
109         while (i < nheaders) {
110             int result = ogg_sync_pageout(&osync, &og);
111             if (result == 0)
112                 break;
113             else if (result == 1) {
114                 ogg_stream_pagein(&os, &og);
115                 while (i < nheaders) {
116                     result = ogg_stream_packetout(&os, &header);
117                     if (result == 0)
118                         break;
119                     if (result == -1)
120                         return -1;
121
122                     vorbis_synthesis_headerin(&vi, vc, &header);
123                     i++;
124                 }
125             }
126         }
127
128         buffer = ogg_sync_buffer(&osync, CHUNKSIZE);
129         bytes = fread(buffer, 1, CHUNKSIZE, file);
130
131         if (bytes == 0 && i < 2)
132             return -1;
133
134         ogg_sync_wrote(&osync, bytes);
135     }
136
137     ogg_stream_clear(&os);
138     ogg_sync_clear(&osync);
139     vorbis_info_clear(&vi);
140
141     return 0;
142 }
143
144 static int
145 _parse_ogg (const char *filename, struct lms_audio_info *info)
146 {
147     vorbis_comment vc;
148     FILE *file;
149     char *tag = NULL;
150     int size;
151
152     if( !filename )
153         return -1;
154
155     file = fopen(filename, "rb");
156     if (file == NULL)
157         return -1;
158
159     fseek(file, _id3_tag_size(file), SEEK_SET);
160
161     if (_get_vorbis_comment(file, &vc) != 0)
162         return -1;
163
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     }
170
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);
176     }
177
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);
183     }
184
185     tag = vorbis_comment_query(&vc, "TRACKNUMBER", 0);
186     if (tag && (size = strlen(tag)) > 0) {
187         info->trackno = atoi(tag);
188     }
189
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);
195     }
196
197     fclose(file);
198     vorbis_comment_clear(&vc);
199
200     return 0;
201 }
202
203
204 static const char _name[] = "ogg";
205 static const struct lms_string_size _exts[] = {
206     LMS_STATIC_STRING_SIZE(".ogg")
207 };
208
209 struct plugin {
210     struct lms_plugin plugin;
211     lms_db_audio_t *audio_db;
212 };
213
214 static void *
215 _match(struct plugin *p, const char *path, int len, int base)
216 {
217     int i;
218
219     i = lms_which_extension(path, len, _exts, LMS_ARRAY_SIZE(_exts));
220     if (i < 0)
221       return NULL;
222     else
223       return (void*)(i + 1);
224 }
225
226 static int
227 _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_info *finfo, void *match)
228 {
229     struct lms_audio_info info = {0};
230     int r;
231
232     r = _parse_ogg(finfo->path, &info);
233     if (r != 0)
234       goto done;
235
236     if (!info.title.str) {
237       int ext_idx;
238
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';
244     }
245
246     if (info.title.str)
247       lms_charset_conv(ctxt->cs_conv, &info.title.str, &info.title.len);
248     if (info.artist.str)
249       lms_charset_conv(ctxt->cs_conv, &info.artist.str, &info.artist.len);
250     if (info.album.str)
251       lms_charset_conv(ctxt->cs_conv, &info.album.str, &info.album.len);
252     if (info.genre.str)
253       lms_charset_conv(ctxt->cs_conv, &info.genre.str, &info.genre.len);
254
255     info.id = finfo->id;
256     r = lms_db_audio_add(plugin->audio_db, &info);
257
258  done:
259     if (info.title.str)
260         free(info.title.str);
261     if (info.artist.str)
262         free(info.artist.str);
263     if (info.album.str)
264         free(info.album.str);
265     if (info.genre.str)
266         free(info.genre.str);
267
268     return r;
269 }
270
271 static int
272 _setup(struct plugin *plugin, struct lms_context *ctxt)
273 {
274     plugin->audio_db = lms_db_audio_new(ctxt->db);
275     if (!plugin->audio_db)
276         return -1;
277
278     return 0;
279 }
280
281 static int
282 _start(struct plugin *plugin, struct lms_context *ctxt)
283 {
284     return lms_db_audio_start(plugin->audio_db);
285 }
286
287 static int
288 _finish(struct plugin *plugin, struct lms_context *ctxt)
289 {
290     if (plugin->audio_db)
291         return lms_db_audio_free(plugin->audio_db);
292
293     return 0;
294 }
295
296 static int
297 _close(struct plugin *plugin)
298 {
299     free(plugin);
300     return 0;
301 }
302
303 API struct lms_plugin *
304 lms_plugin_open(void)
305 {
306     struct plugin *plugin;
307
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;
316
317     return (struct lms_plugin *)plugin;
318 }