utils refactor.
[platform/upstream/lightmediascanner.git] / src / plugins / flac / flac.c
1 /**
2  * Copyright (C) 2008 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * @author Andre Moreira Magalhaes <andre.magalhaes@openbossa.org>
19  */
20
21 /**
22  * @brief
23  *
24  * flac file parser.
25  */
26
27 #include <lightmediascanner_plugin.h>
28 #include <lightmediascanner_db.h>
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <FLAC/metadata.h>
34
35 struct plugin {
36     struct lms_plugin plugin;
37     lms_db_audio_t *audio_db;
38 };
39
40 static const char _name[] = "flac";
41 static const struct lms_string_size _codec = LMS_STATIC_STRING_SIZE("flac");
42 static const struct lms_string_size _exts[] = {
43     LMS_STATIC_STRING_SIZE(".flac")
44 };
45 static const char *_cats[] = {
46     "multimedia",
47     "audio",
48     NULL
49 };
50 static const char *_authors[] = {
51     "Andre Moreira Magalhaes",
52     NULL
53 };
54
55 static void *
56 _match(struct plugin *p, const char *path, int len, int base)
57 {
58     long i;
59
60     i = lms_which_extension(path, len, _exts, LMS_ARRAY_SIZE(_exts));
61     if (i < 0)
62       return NULL;
63     else
64       return (void*)(i + 1);
65 }
66
67 static int
68 _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_info *finfo, void *match)
69 {
70     struct lms_audio_info info = { };
71     FLAC__StreamMetadata *tags = NULL;
72     FLAC__StreamMetadata si;
73     char *str;
74     int len, r;
75     unsigned int i;
76
77     if (!FLAC__metadata_get_streaminfo(finfo->path, &si)) {
78         fprintf(stderr, "ERROR: cannot retrieve file %s STREAMINFO block\n",
79                 finfo->path);
80         return -1;
81     }
82
83     info.channels = si.data.stream_info.channels;
84     info.sampling_rate = si.data.stream_info.sample_rate;
85
86     if (si.data.stream_info.sample_rate) {
87         info.length = si.data.stream_info.total_samples / si.data.stream_info.sample_rate;
88         info.bitrate = (finfo->size * 8) / info.length;
89     }
90
91     info.codec = _codec;
92     info.container = _codec;
93
94     if (!FLAC__metadata_get_tags(finfo->path, &tags)) {
95         fprintf(stderr, "ERROR: cannot retrieve file %s tags\n", finfo->path);
96         goto title_fallback;
97     }
98
99     for (i = 0; i < tags->data.vorbis_comment.num_comments; i++) {
100         str = (char *) tags->data.vorbis_comment.comments[i].entry;
101         len = tags->data.vorbis_comment.comments[i].length;
102         if (strncmp(str, "TITLE=", 6) == 0)
103             lms_string_size_strndup(&info.title, str + 6, len - 6);
104         else if (strncmp(str, "ARTIST=", 7) == 0)
105             lms_string_size_strndup(&info.artist, str + 7, len - 7);
106         else if (strncmp(str, "ALBUM=", 6) == 0)
107             lms_string_size_strndup(&info.album, str + 7, len - 7);
108         else if (strncmp(str, "GENRE=", 6) == 0)
109             lms_string_size_strndup(&info.genre, str + 6, len - 6);
110         else if (strncmp(str, "TRACKNUMBER=", 12) == 0)
111             info.trackno = atoi(str + 12);
112     }
113
114     FLAC__metadata_object_delete(tags);
115
116     lms_string_size_strip_and_free(&info.title);
117     lms_string_size_strip_and_free(&info.artist);
118     lms_string_size_strip_and_free(&info.album);
119     lms_string_size_strip_and_free(&info.genre);
120
121 title_fallback:
122     if (!info.title.str)
123         lms_name_from_path(&info.title, finfo->path, finfo->path_len,
124                            finfo->base, _exts[((long) match) - 1].len,
125                            NULL);
126     if (info.title.str)
127         lms_charset_conv(ctxt->cs_conv, &info.title.str, &info.title.len);
128
129     if (info.artist.str)
130         lms_charset_conv(ctxt->cs_conv, &info.artist.str, &info.artist.len);
131     if (info.album.str)
132         lms_charset_conv(ctxt->cs_conv, &info.album.str, &info.album.len);
133     if (info.genre.str)
134         lms_charset_conv(ctxt->cs_conv, &info.genre.str, &info.genre.len);
135
136 #if 0
137     fprintf(stderr, "file %s info\n", finfo->path);
138     fprintf(stderr, "\ttitle='%s'\n", info.title.str);
139     fprintf(stderr, "\tartist='%s'\n", info.artist.str);
140     fprintf(stderr, "\talbum='%s'\n", info.album.str);
141     fprintf(stderr, "\tgenre='%s'\n", info.genre.str);
142     fprintf(stderr, "\ttrack number='%d'\n", info.trackno);
143 #endif
144
145     info.id = finfo->id;
146     r = lms_db_audio_add(plugin->audio_db, &info);
147
148     free(info.title.str);
149     free(info.artist.str);
150     free(info.album.str);
151     free(info.genre.str);
152
153     return r;
154 }
155
156 static int
157 _setup(struct plugin *plugin, struct lms_context *ctxt)
158 {
159     plugin->audio_db = lms_db_audio_new(ctxt->db);
160     if (!plugin->audio_db)
161         return -1;
162     return 0;
163 }
164
165 static int
166 _start(struct plugin *plugin, struct lms_context *ctxt)
167 {
168     return lms_db_audio_start(plugin->audio_db);
169 }
170
171 static int
172 _finish(struct plugin *plugin, struct lms_context *ctxt)
173 {
174     if (plugin->audio_db)
175         lms_db_audio_free(plugin->audio_db);
176     return 0;
177 }
178
179 static int
180 _close(struct plugin *plugin)
181 {
182     free(plugin);
183     return 0;
184 }
185
186 API struct lms_plugin *
187 lms_plugin_open(void)
188 {
189     struct plugin *plugin;
190
191     plugin = (struct plugin *)malloc(sizeof(*plugin));
192     plugin->plugin.name = _name;
193     plugin->plugin.match = (lms_plugin_match_fn_t)_match;
194     plugin->plugin.parse = (lms_plugin_parse_fn_t)_parse;
195     plugin->plugin.close = (lms_plugin_close_fn_t)_close;
196     plugin->plugin.setup = (lms_plugin_setup_fn_t)_setup;
197     plugin->plugin.start = (lms_plugin_start_fn_t)_start;
198     plugin->plugin.finish = (lms_plugin_finish_fn_t)_finish;
199
200     return (struct lms_plugin *)plugin;
201 }
202
203 API const struct lms_plugin_info *
204 lms_plugin_info(void)
205 {
206     static struct lms_plugin_info info = {
207         _name,
208         _cats,
209         "FLAC parser",
210         PACKAGE_VERSION,
211         _authors,
212         "http://lms.garage.maemo.org"
213     };
214
215     return &info;
216 }