2 * Copyright (C) 2008-2011 by ProFUSION embedded systems
3 * Copyright (C) 2007 by INdT
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public License
7 * as published by the Free Software Foundation; either version 2.1 of
8 * the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * @author Gustavo Sverzut Barbieri <barbieri@profusion.mobi>
30 #include <lightmediascanner_plugin.h>
31 #include <lightmediascanner_utils.h>
32 #include <lightmediascanner_db.h>
34 #include <sys/types.h>
44 static const struct lms_string_size dlna_mime =
45 LMS_STATIC_STRING_SIZE("image/png");
46 static const struct lms_string_size dlna_sm_ico =
47 LMS_STATIC_STRING_SIZE("PNG_SM_ICO");
48 static const struct lms_string_size dlna_lrg_ico =
49 LMS_STATIC_STRING_SIZE("PNG_LRG_ICO");
50 static const struct lms_string_size dlna_tn =
51 LMS_STATIC_STRING_SIZE("PNG_TN");
52 static const struct lms_string_size dlna_lrg =
53 LMS_STATIC_STRING_SIZE("PNG_LRG");
56 _fill_dlna_profile(struct lms_image_info *info)
58 const unsigned short w = info->width;
59 const unsigned short h = info->height;
61 info->dlna_mime = dlna_mime;
66 if (w == 48 && h == 48)
67 info->dlna_profile = dlna_sm_ico;
68 else if (w == 120 && h == 120)
69 info->dlna_profile = dlna_lrg_ico;
70 else if (w <= 160 && h <= 160)
71 info->dlna_profile = dlna_tn;
72 else if (w <= 4096 && h <= 4096)
73 info->dlna_profile = dlna_lrg;
76 static inline unsigned int
77 _chunk_to_uint(unsigned char *buf)
79 return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
83 _png_data_get(int fd, struct lms_image_info *info)
85 unsigned char buf[16], *p;
86 const unsigned char sig[8] = {0x89, 0x50, 0x4e, 0x47, 0xd, 0xa, 0x1a, 0xa};
87 const unsigned char ihdr[4] = {'I', 'H', 'D', 'R'};
90 if (read(fd, buf, sizeof(buf)) != sizeof(buf)) {
95 if (memcmp(buf, sig, sizeof(sig)) != 0) {
96 fprintf(stderr, "ERROR: invalid PNG signature.\n");
100 p = buf + sizeof(sig) + 4;
101 if (memcmp(p, ihdr, sizeof(ihdr)) != 0) {
102 fprintf(stderr, "ERROR: invalid first chunk: %4.4s.\n", p);
106 p = buf + sizeof(sig);
107 length = _chunk_to_uint(p);
109 fprintf(stderr, "ERROR: IHDR chunk size is too small: %d.\n", length);
113 if (read(fd, buf, 8) != 8) {
117 info->width = _chunk_to_uint(buf);
118 info->height = _chunk_to_uint(buf + 4);
123 static const char _name[] = "png";
124 static const struct lms_string_size _exts[] = {
125 LMS_STATIC_STRING_SIZE(".png")
127 static const char *_cats[] = {
132 static const char *_authors[] = {
133 "Gustavo Sverzut Barbieri",
138 struct lms_plugin plugin;
139 lms_db_image_t *img_db;
143 _match(struct plugin *p, const char *path, int len, int base)
147 i = lms_which_extension(path, len, _exts, LMS_ARRAY_SIZE(_exts));
151 return (void*)(i + 1);
155 _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_info *finfo, void *match)
157 struct lms_image_info info = { };
160 fd = open(finfo->path, O_RDONLY);
166 if (_png_data_get(fd, &info) != 0) {
172 info.date = finfo->mtime;
175 lms_name_from_path(&info.title, finfo->path, finfo->path_len,
176 finfo->base, _exts[((long) match) - 1].len,
179 lms_charset_conv(ctxt->cs_conv, &info.title.str, &info.title.len);
181 lms_charset_conv(ctxt->cs_conv, &info.artist.str, &info.artist.len);
183 _fill_dlna_profile(&info);
186 r = lms_db_image_add(plugin->img_db, &info);
189 free(info.title.str);
190 free(info.artist.str);
192 posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
199 _setup(struct plugin *plugin, struct lms_context *ctxt)
201 plugin->img_db = lms_db_image_new(ctxt->db);
209 _start(struct plugin *plugin, struct lms_context *ctxt)
211 return lms_db_image_start(plugin->img_db);
215 _finish(struct plugin *plugin, struct lms_context *ctxt)
218 return lms_db_image_free(plugin->img_db);
225 _close(struct plugin *plugin)
231 API struct lms_plugin *
232 lms_plugin_open(void)
234 struct plugin *plugin;
236 plugin = malloc(sizeof(*plugin));
237 plugin->plugin.name = _name;
238 plugin->plugin.match = (lms_plugin_match_fn_t)_match;
239 plugin->plugin.parse = (lms_plugin_parse_fn_t)_parse;
240 plugin->plugin.close = (lms_plugin_close_fn_t)_close;
241 plugin->plugin.setup = (lms_plugin_setup_fn_t)_setup;
242 plugin->plugin.start = (lms_plugin_start_fn_t)_start;
243 plugin->plugin.finish = (lms_plugin_finish_fn_t)_finish;
245 return (struct lms_plugin *)plugin;
248 API const struct lms_plugin_info *
249 lms_plugin_info(void)
251 static struct lms_plugin_info info = {
257 "http://lms.garage.maemo.org"