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>
33 #include <shared/util.h>
35 #include <sys/types.h>
45 static const struct lms_string_size dlna_mime =
46 LMS_STATIC_STRING_SIZE("image/png");
47 static const struct lms_string_size dlna_sm_ico =
48 LMS_STATIC_STRING_SIZE("PNG_SM_ICO");
49 static const struct lms_string_size dlna_lrg_ico =
50 LMS_STATIC_STRING_SIZE("PNG_LRG_ICO");
51 static const struct lms_string_size dlna_tn =
52 LMS_STATIC_STRING_SIZE("PNG_TN");
53 static const struct lms_string_size dlna_lrg =
54 LMS_STATIC_STRING_SIZE("PNG_LRG");
57 _fill_dlna_profile(struct lms_image_info *info)
59 const unsigned short w = info->width;
60 const unsigned short h = info->height;
62 info->dlna_mime = dlna_mime;
67 if (w == 48 && h == 48)
68 info->dlna_profile = dlna_sm_ico;
69 else if (w == 120 && h == 120)
70 info->dlna_profile = dlna_lrg_ico;
71 else if (w <= 160 && h <= 160)
72 info->dlna_profile = dlna_tn;
73 else if (w <= 4096 && h <= 4096)
74 info->dlna_profile = dlna_lrg;
77 static inline unsigned int
78 _chunk_to_uint(unsigned char *buf)
80 return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
84 _png_data_get(int fd, struct lms_image_info *info)
86 unsigned char buf[16], *p;
87 const unsigned char sig[8] = {0x89, 0x50, 0x4e, 0x47, 0xd, 0xa, 0x1a, 0xa};
88 const unsigned char ihdr[4] = {'I', 'H', 'D', 'R'};
91 if (read(fd, buf, sizeof(buf)) != sizeof(buf)) {
96 if (memcmp(buf, sig, sizeof(sig)) != 0) {
97 fprintf(stderr, "ERROR: invalid PNG signature.\n");
101 p = buf + sizeof(sig) + 4;
102 if (memcmp(p, ihdr, sizeof(ihdr)) != 0) {
103 fprintf(stderr, "ERROR: invalid first chunk: %4.4s.\n", p);
107 p = buf + sizeof(sig);
108 length = _chunk_to_uint(p);
110 fprintf(stderr, "ERROR: IHDR chunk size is too small: %d.\n", length);
114 if (read(fd, buf, 8) != 8) {
118 info->width = _chunk_to_uint(buf);
119 info->height = _chunk_to_uint(buf + 4);
124 static const char _name[] = "png";
125 static const struct lms_string_size _exts[] = {
126 LMS_STATIC_STRING_SIZE(".png")
128 static const char *_cats[] = {
133 static const char *_authors[] = {
134 "Gustavo Sverzut Barbieri",
139 struct lms_plugin plugin;
140 lms_db_image_t *img_db;
144 _match(struct plugin *p, const char *path, int len, int base)
148 i = lms_which_extension(path, len, _exts, LMS_ARRAY_SIZE(_exts));
152 return (void*)(i + 1);
156 _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_info *finfo, void *match)
158 struct lms_image_info info = { };
161 fd = open(finfo->path, O_RDONLY);
167 if (_png_data_get(fd, &info) != 0) {
173 info.date = finfo->mtime;
176 info.title = str_extract_name_from_path(finfo->path, finfo->path_len,
178 &_exts[((long) match) - 1],
181 lms_charset_conv(ctxt->cs_conv, &info.title.str, &info.title.len);
183 lms_charset_conv(ctxt->cs_conv, &info.artist.str, &info.artist.len);
185 _fill_dlna_profile(&info);
188 r = lms_db_image_add(plugin->img_db, &info);
191 free(info.title.str);
192 free(info.artist.str);
194 posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
201 _setup(struct plugin *plugin, struct lms_context *ctxt)
203 plugin->img_db = lms_db_image_new(ctxt->db);
211 _start(struct plugin *plugin, struct lms_context *ctxt)
213 return lms_db_image_start(plugin->img_db);
217 _finish(struct plugin *plugin, struct lms_context *ctxt)
220 return lms_db_image_free(plugin->img_db);
227 _close(struct plugin *plugin)
233 API struct lms_plugin *
234 lms_plugin_open(void)
236 struct plugin *plugin;
238 plugin = malloc(sizeof(*plugin));
239 plugin->plugin.name = _name;
240 plugin->plugin.match = (lms_plugin_match_fn_t)_match;
241 plugin->plugin.parse = (lms_plugin_parse_fn_t)_parse;
242 plugin->plugin.close = (lms_plugin_close_fn_t)_close;
243 plugin->plugin.setup = (lms_plugin_setup_fn_t)_setup;
244 plugin->plugin.start = (lms_plugin_start_fn_t)_start;
245 plugin->plugin.finish = (lms_plugin_finish_fn_t)_finish;
247 return (struct lms_plugin *)plugin;
250 API const struct lms_plugin_info *
251 lms_plugin_info(void)
253 static struct lms_plugin_info info = {
259 "http://lms.garage.maemo.org"