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