ogg.c: Minor coding style fixes
[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     const char *tag;
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     size = strlen(tag);
166     if (tag && size > 0) {
167         info->title.len = size;
168         info->title.str = malloc(size * sizeof(char));
169         memcpy(info->title.str, tag, size);
170         lms_string_size_strip_and_free(&info->title);
171     }
172
173     tag = vorbis_comment_query(&vc, "ARTIST", 0);
174     size = strlen(tag);
175     if (tag && size > 0) {
176         info->artist.len = size;
177         info->artist.str = malloc(size * sizeof(char));
178         memcpy(info->artist.str, tag, size);
179         lms_string_size_strip_and_free(&info->artist);
180     }
181
182     tag = vorbis_comment_query(&vc, "ALBUM", 0);
183     size = strlen(tag);
184     if (tag && size > 0) {
185         info->album.len = size;
186         info->album.str = malloc(size * sizeof(char));
187         memcpy(info->album.str, tag, size);
188         lms_string_size_strip_and_free(&info->album);
189     }
190
191     tag = vorbis_comment_query(&vc, "TRACKNUMBER", 0);
192     if (tag)
193         info->trackno = atoi(tag);
194
195     tag = vorbis_comment_query(&vc, "GENRE", 0);
196     size = strlen(tag);
197     if (tag && size > 0) {
198         info->genre.len = size;
199         info->genre.str = malloc(size * sizeof(char));
200         memcpy(info->genre.str, tag, size);
201         lms_string_size_strip_and_free(&info->genre);
202     }
203
204     fclose(file);
205     vorbis_comment_clear(&vc);
206
207     return 0;
208 }
209
210
211 static const char _name[] = "ogg";
212 static const struct lms_string_size _exts[] = {
213     LMS_STATIC_STRING_SIZE(".ogg")
214 };
215
216 struct plugin {
217     struct lms_plugin plugin;
218     lms_db_audio_t *audio_db;
219 };
220
221 static void *
222 _match(struct plugin *p, const char *path, int len, int base)
223 {
224     int i;
225
226     i = lms_which_extension(path, len, _exts, LMS_ARRAY_SIZE(_exts));
227     if (i < 0)
228       return NULL;
229     else
230       return (void*)(i + 1);
231 }
232
233 static int
234 _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_info *finfo, void *match)
235 {
236     struct lms_audio_info info = {0};
237     int r;
238
239     r = _parse_ogg(finfo->path, &info);
240     if (r != 0)
241       goto done;
242
243     if (!info.title.str) {
244       int ext_idx;
245
246       ext_idx = ((int)match) - 1;
247       info.title.len = finfo->path_len - finfo->base - _exts[ext_idx].len;
248       info.title.str = (char *)malloc((info.title.len + 1) * sizeof(char));
249       memcpy(info.title.str, finfo->path + finfo->base, info.title.len);
250       info.title.str[info.title.len] = '\0';
251     }
252
253     if (info.title.str)
254       lms_charset_conv(ctxt->cs_conv, &info.title.str, &info.title.len);
255     if (info.artist.str)
256       lms_charset_conv(ctxt->cs_conv, &info.artist.str, &info.artist.len);
257     if (info.album.str)
258       lms_charset_conv(ctxt->cs_conv, &info.album.str, &info.album.len);
259     if (info.genre.str)
260       lms_charset_conv(ctxt->cs_conv, &info.genre.str, &info.genre.len);
261
262     info.id = finfo->id;
263     r = lms_db_audio_add(plugin->audio_db, &info);
264
265  done:
266     if (info.title.str)
267         free(info.title.str);
268     if (info.artist.str)
269         free(info.artist.str);
270     if (info.album.str)
271         free(info.album.str);
272     if (info.genre.str)
273         free(info.genre.str);
274
275     return r;
276 }
277
278 static int
279 _setup(struct plugin *plugin, struct lms_context *ctxt)
280 {
281     plugin->audio_db = lms_db_audio_new(ctxt->db);
282     if (!plugin->audio_db)
283         return -1;
284
285     return 0;
286 }
287
288 static int
289 _start(struct plugin *plugin, struct lms_context *ctxt)
290 {
291     return lms_db_audio_start(plugin->audio_db);
292 }
293
294 static int
295 _finish(struct plugin *plugin, struct lms_context *ctxt)
296 {
297     if (plugin->audio_db)
298         return lms_db_audio_free(plugin->audio_db);
299
300     return 0;
301 }
302
303 static int
304 _close(struct plugin *plugin)
305 {
306     free(plugin);
307     return 0;
308 }
309
310 API struct lms_plugin *
311 lms_plugin_open(void)
312 {
313     struct plugin *plugin;
314
315     plugin = (struct plugin *)malloc(sizeof(*plugin));
316     plugin->plugin.name = _name;
317     plugin->plugin.match = (lms_plugin_match_fn_t)_match;
318     plugin->plugin.parse = (lms_plugin_parse_fn_t)_parse;
319     plugin->plugin.close = (lms_plugin_close_fn_t)_close;
320     plugin->plugin.setup = (lms_plugin_setup_fn_t)_setup;
321     plugin->plugin.start = (lms_plugin_start_fn_t)_start;
322     plugin->plugin.finish = (lms_plugin_finish_fn_t)_finish;
323
324     return (struct lms_plugin *)plugin;
325 }