Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[3cdd0c]: albumart.c Maximize Restore History

Download this file

albumart.c    380 lines (351 with data), 9.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/* MiniDLNA media server
* Copyright (C) 2008 Justin Maggard
*
* This file is part of MiniDLNA.
*
* MiniDLNA is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* MiniDLNA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MiniDLNA. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <libgen.h>
#include <setjmp.h>
#include <errno.h>
#include <jpeglib.h>
#include "upnpglobalvars.h"
#include "albumart.h"
#include "sql.h"
#include "utils.h"
#include "image_utils.h"
#include "log.h"
static int
art_cache_exists(const char *orig_path, char **cache_file)
{
if( asprintf(cache_file, "%s/art_cache%s", db_path, orig_path) < 0 )
{
*cache_file = NULL;
return 0;
}
strcpy(strchr(*cache_file, '\0')-4, ".jpg");
return (!access(*cache_file, F_OK));
}
static char *
save_resized_album_art(image_s *imsrc, const char *path)
{
int dstw, dsth;
image_s *imdst;
char *cache_file;
char cache_dir[MAXPATHLEN];
if( !imsrc )
return NULL;
if( art_cache_exists(path, &cache_file) )
return cache_file;
strncpyt(cache_dir, cache_file, sizeof(cache_dir));
make_dir(dirname(cache_dir), S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
if( imsrc->width > imsrc->height )
{
dstw = 160;
dsth = (imsrc->height<<8) / ((imsrc->width<<8)/160);
}
else
{
dstw = (imsrc->width<<8) / ((imsrc->height<<8)/160);
dsth = 160;
}
imdst = image_resize(imsrc, dstw, dsth);
if( !imdst )
goto error;
if( image_save_to_jpeg_file(imdst, cache_file) == 0 )
{
image_free(imdst);
return cache_file;
}
error:
free(cache_file);
return NULL;
}
/* Simple, efficient hash function from Daniel J. Bernstein */
static unsigned int
DJBHash(const char *str, int len)
{
unsigned int hash = 5381;
unsigned int i = 0;
for(i = 0; i < len; str++, i++)
{
hash = ((hash << 5) + hash) + (*str);
}
return hash;
}
/* And our main album art functions */
void
update_if_album_art(const char *path)
{
char *dir;
char *match;
char file[MAXPATHLEN];
char fpath[MAXPATHLEN];
char dpath[MAXPATHLEN];
int ncmp = 0;
int album_art;
DIR *dh;
struct dirent *dp;
enum file_types type = TYPE_UNKNOWN;
sqlite_int64 art_id = 0;
strncpyt(fpath, path, sizeof(fpath));
match = basename(fpath);
/* Check if this file name matches a specific audio or video file */
if( ends_with(match, ".cover.jpg") )
{
ncmp = strlen(match)-10;
}
else
{
ncmp = strrchr(match, '.') - match;
}
/* Check if this file name matches one of the default album art names */
album_art = is_album_art(match);
strncpyt(dpath, path, sizeof(dpath));
dir = dirname(dpath);
dh = opendir(dir);
if( !dh )
return;
while ((dp = readdir(dh)) != NULL)
{
switch( dp->d_type )
{
case DT_REG:
type = TYPE_FILE;
break;
case DT_LNK:
case DT_UNKNOWN:
snprintf(file, sizeof(file), "%s/%s", dir, dp->d_name);
type = resolve_unknown_type(file, ALL_MEDIA);
break;
default:
type = TYPE_UNKNOWN;
break;
}
if( type != TYPE_FILE )
continue;
if( (*(dp->d_name) != '.') &&
(is_video(dp->d_name) || is_audio(dp->d_name)) &&
(album_art || strncmp(dp->d_name, match, ncmp) == 0) )
{
DPRINTF(E_DEBUG, L_METADATA, "New file %s looks like cover art for %s\n", path, dp->d_name);
snprintf(file, sizeof(file), "%s/%s", dir, dp->d_name);
art_id = find_album_art(file, NULL, 0);
if( sql_exec(db, "UPDATE DETAILS set ALBUM_ART = %lld where PATH = '%q'", art_id, file) != SQLITE_OK )
DPRINTF(E_WARN, L_METADATA, "Error setting %s as cover art for %s\n", match, dp->d_name);
}
}
closedir(dh);
}
char *
check_embedded_art(const char *path, const char *image_data, int image_size)
{
int width = 0, height = 0;
char *art_path = NULL;
char *cache_dir;
FILE *dstfile;
image_s *imsrc;
static char last_path[PATH_MAX];
static unsigned int last_hash = 0;
static int last_success = 0;
unsigned int hash;
if( !image_data || !image_size || !path )
{
return NULL;
}
/* If the embedded image matches the embedded image from the last file we
* checked, just make a hard link. Better than storing it on the disk twice. */
hash = DJBHash(image_data, image_size);
if( hash == last_hash )
{
if( !last_success )
return NULL;
art_cache_exists(path, &art_path);
if( link(last_path, art_path) == 0 )
{
return(art_path);
}
else
{
if( errno == ENOENT )
{
cache_dir = strdup(art_path);
make_dir(dirname(cache_dir), S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
free(cache_dir);
if( link(last_path, art_path) == 0 )
return(art_path);
}
DPRINTF(E_WARN, L_METADATA, "Linking %s to %s failed [%s]\n", art_path, last_path, strerror(errno));
free(art_path);
art_path = NULL;
}
}
last_hash = hash;
imsrc = image_new_from_jpeg(NULL, 0, image_data, image_size, 1, ROTATE_NONE);
if( !imsrc )
{
last_success = 0;
return NULL;
}
width = imsrc->width;
height = imsrc->height;
if( width > 160 || height > 160 )
{
art_path = save_resized_album_art(imsrc, path);
}
else if( width > 0 && height > 0 )
{
size_t nwritten;
if( art_cache_exists(path, &art_path) )
goto end_art;
cache_dir = strdup(art_path);
make_dir(dirname(cache_dir), S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
free(cache_dir);
dstfile = fopen(art_path, "w");
if( !dstfile )
{
free(art_path);
art_path = NULL;
goto end_art;
}
nwritten = fwrite((void *)image_data, 1, image_size, dstfile);
fclose(dstfile);
if( nwritten != image_size )
{
DPRINTF(E_WARN, L_METADATA, "Embedded art error: wrote %d/%d bytes\n", nwritten, image_size);
remove(art_path);
free(art_path);
art_path = NULL;
goto end_art;
}
}
end_art:
image_free(imsrc);
if( !art_path )
{
DPRINTF(E_WARN, L_METADATA, "Invalid embedded album art in %s\n", basename((char *)path));
last_success = 0;
return NULL;
}
DPRINTF(E_DEBUG, L_METADATA, "Found new embedded album art in %s\n", basename((char *)path));
last_success = 1;
strcpy(last_path, art_path);
return(art_path);
}
static char *
check_for_album_file(const char *path)
{
char file[MAXPATHLEN];
char mypath[MAXPATHLEN];
struct album_art_name_s *album_art_name;
image_s *imsrc = NULL;
int width=0, height=0;
char *art_file;
const char *dir;
struct stat st;
if( stat(path, &st) != 0 )
return NULL;
if( S_ISDIR(st.st_mode) )
{
dir = path;
goto check_dir;
}
strncpyt(mypath, path, sizeof(mypath));
dir = dirname(mypath);
/* First look for file-specific cover art */
snprintf(file, sizeof(file), "%s.cover.jpg", path);
if( access(file, R_OK) == 0 )
{
if( art_cache_exists(file, &art_file) )
goto existing_file;
free(art_file);
imsrc = image_new_from_jpeg(file, 1, NULL, 0, 1, ROTATE_NONE);
if( imsrc )
goto found_file;
}
snprintf(file, sizeof(file), "%s", path);
art_file = strrchr(file, '.');
if( art_file )
strcpy(art_file, ".jpg");
if( access(file, R_OK) == 0 )
{
if( art_cache_exists(file, &art_file) )
goto existing_file;
free(art_file);
imsrc = image_new_from_jpeg(file, 1, NULL, 0, 1, ROTATE_NONE);
if( imsrc )
goto found_file;
}
check_dir:
/* Then fall back to possible generic cover art file names */
for( album_art_name = album_art_names; album_art_name; album_art_name = album_art_name->next )
{
snprintf(file, sizeof(file), "%s/%s", dir, album_art_name->name);
if( access(file, R_OK) == 0 )
{
if( art_cache_exists(file, &art_file) )
{
existing_file:
return art_file;
}
free(art_file);
imsrc = image_new_from_jpeg(file, 1, NULL, 0, 1, ROTATE_NONE);
if( !imsrc )
continue;
found_file:
width = imsrc->width;
height = imsrc->height;
if( width > 160 || height > 160 )
art_file = save_resized_album_art(imsrc, file);
else
art_file = strdup(file);
image_free(imsrc);
return(art_file);
}
}
return NULL;
}
sqlite_int64
find_album_art(const char *path, const char *image_data, int image_size)
{
char *album_art = NULL;
char *sql;
char **result;
int cols, rows;
sqlite_int64 ret = 0;
if( (image_size && (album_art = check_embedded_art(path, image_data, image_size))) ||
(album_art = check_for_album_file(path)) )
{
sql = sqlite3_mprintf("SELECT ID from ALBUM_ART where PATH = '%q'", album_art ? album_art : path);
if( (sql_get_table(db, sql, &result, &rows, &cols) == SQLITE_OK) && rows )
{
ret = strtoll(result[1], NULL, 10);
}
else
{
if( sql_exec(db, "INSERT into ALBUM_ART (PATH) VALUES ('%q')", album_art) == SQLITE_OK )
ret = sqlite3_last_insert_rowid(db);
}
sqlite3_free_table(result);
sqlite3_free(sql);
}
free(album_art);
return ret;
}