rockbox/apps/recorder/albumart.c

371 lines
11 KiB
C
Raw Normal View History

/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* Copyright (C) 2007 Nicolas Pennequin
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#include <stdio.h>
#include "string-extra.h"
#include "system.h"
#include "albumart.h"
#include "metadata.h"
#include "buffering.h"
#include "dircache.h"
#include "misc.h"
Rewrite filesystem code (WIP) This patch redoes the filesystem code from the FAT driver up to the clipboard code in onplay.c. Not every aspect of this is finished therefore it is still "WIP". I don't wish to do too much at once (haha!). What is left to do is get dircache back in the sim and find an implementation for the dircache indicies in the tagcache and playlist code or do something else that has the same benefit. Leaving these out for now does not make anything unusable. All the basics are done. Phone app code should probably get vetted (and app path handling just plain rewritten as environment expansions); the SDL app and Android run well. Main things addressed: 1) Thread safety: There is none right now in the trunk code. Most of what currently works is luck when multiple threads are involved or multiple descriptors to the same file are open. 2) POSIX compliance: Many of the functions behave nothing like their counterparts on a host system. This leads to inconsistent code or very different behavior from native to hosted. One huge offender was rename(). Going point by point would fill a book. 3) Actual running RAM usage: Many targets will use less RAM and less stack space (some more RAM because I upped the number of cache buffers for large memory). There's very little memory lying fallow in rarely-used areas (see 'Key core changes' below). Also, all targets may open the same number of directory streams whereas before those with less than 8MB RAM were limited to 8, not 12 implying those targets will save slightly less. 4) Performance: The test_disk plugin shows markedly improved performance, particularly in the area of (uncached) directory scanning, due partly to more optimal directory reading and to a better sector cache algorithm. Uncached times tend to be better while there is a bit of a slowdown in dircache due to it being a bit heavier of an implementation. It's not noticeable by a human as far as I can say. Key core changes: 1) Files and directories share core code and data structures. 2) The filesystem code knows which descriptors refer to same file. This ensures that changes from one stream are appropriately reflected in every open descriptor for that file (fileobj_mgr.c). 3) File and directory cache buffers are borrowed from the main sector cache. This means that when they are not in use by a file, they are not wasted, but used for the cache. Most of the time, only a few of them are needed. It also means that adding more file and directory handles is less expensive. All one must do in ensure a large enough cache to borrow from. 4) Relative path components are supported and the namespace is unified. It does not support full relative paths to an implied current directory; what is does support is use of "." and "..". Adding the former would not be very difficult. The namespace is unified in the sense that volumes may be specified several times along with relative parts, e.g.: "/<0>/foo/../../<1>/bar" :<=> "/<1>/bar". 5) Stack usage is down due to sharing of data, static allocation and less duplication of strings on the stack. This requires more serialization than I would like but since the number of threads is limited to a low number, the tradoff in favor of the stack seems reasonable. 6) Separates and heirarchicalizes (sic) the SIM and APP filesystem code. SIM path and volume handling is just like the target. Some aspects of the APP file code get more straightforward (e.g. no path hashing is needed). Dircache: Deserves its own section. Dircache is new but pays homage to the old. The old one was not compatible and so it, since it got redone, does all the stuff it always should have done such as: 1) It may be update and used at any time during the build process. No longer has one to wait for it to finish building to do basic file management (create, remove, rename, etc.). 2) It does not need to be either fully scanned or completely disabled; it can be incomplete (i.e. overfilled, missing paths), still be of benefit and be correct. 3) Handles mounting and dismounting of individual volumes which means a full rebuild is not needed just because you pop a new SD card in the slot. Now, because it reuses its freed entry data, may rebuild only that volume. 4) Much more fundamental to the file code. When it is built, it is the keeper of the master file list whether enabled or not ("disabled" is just a state of the cache). Its must always to ready to be started and bind all streams opened prior to being enabled. 5) Maintains any short filenames in OEM format which means that it does not need to be rebuilt when changing the default codepage. Miscellaneous Compatibility: 1) Update any other code that would otherwise not work such as the hotswap mounting code in various card drivers. 2) File management: Clipboard needed updating because of the behavioral changes. Still needs a little more work on some finer points. 3) Remove now-obsolete functionality such as the mutex's "no preempt" flag (which was only for the prior FAT driver). 4) struct dirinfo uses time_t rather than raw FAT directory entry time fields. I plan to follow up on genericizing everything there (i.e. no FAT attributes). 5) unicode.c needed some redoing so that the file code does not try try to load codepages during a scan, which is actually a problem with the current code. The default codepage, if any is required, is now kept in RAM separarately (bufalloced) from codepages specified to iso_decode() (which must not be bufalloced because the conversion may be done by playback threads). Brings with it some additional reusable core code: 1) Revised file functions: Reusable code that does things such as safe path concatenation and parsing without buffer limitations or data duplication. Variants that copy or alter the input path may be based off these. To do: 1) Put dircache functionality back in the sim. Treating it internally as a different kind of file system seems the best approach at this time. 2) Restore use of dircache indexes in the playlist and database or something effectively the same. Since the cache doesn't have to be complete in order to be used, not getting a hit on the cache doesn't unambiguously say if the path exists or not. Change-Id: Ia30f3082a136253e3a0eae0784e3091d138915c8 Reviewed-on: http://gerrit.rockbox.org/566 Reviewed-by: Michael Sevakis <jethead71@rockbox.org> Tested: Michael Sevakis <jethead71@rockbox.org>
2013-08-06 02:02:45 +00:00
#include "pathfuncs.h"
#include "settings.h"
#include "wps.h"
/* Define LOGF_ENABLE to enable logf output in this file */
/*#define LOGF_ENABLE*/
#include "logf.h"
#if defined(HAVE_JPEG) || defined(PLUGIN)
#define USE_JPEG_COVER
#endif
/* Strip filename from a full path
*
* buf - buffer to extract directory to.
* buf_size - size of buffer.
* fullpath - fullpath to extract from.
*
* Split the directory part of the given fullpath and store it in buf
* (including last '/').
* The function return parameter is a pointer to the filename
* inside the given fullpath.
*/
static char* strip_filename(char* buf, int buf_size, const char* fullpath)
{
char* sep;
int len;
if (!buf || buf_size <= 0 || !fullpath)
return NULL;
/* if 'fullpath' is only a filename return immediately */
sep = strrchr(fullpath, '/');
if (sep == NULL)
{
buf[0] = 0;
return (char*)fullpath;
}
len = MIN(sep - fullpath + 1, buf_size - 1);
strlcpy(buf, fullpath, len + 1);
return (sep + 1);
}
/* Make sure part of path only contain chars valid for a FAT32 long name.
* Double quotes are replaced with single quotes, other unsupported chars
* are replaced with an underscore.
*
* path - path to modify.
* offset - where in path to start checking.
* count - number of chars to check.
*/
static void fix_path_part(char* path, int offset, int count)
{
static const char invalid_chars[] = "*/:<>?\\|";
int i;
path += offset;
for (i = 0; i <= count; i++, path++)
{
if (*path == 0)
return;
if (*path == '"')
*path = '\'';
else if (strchr(invalid_chars, *path))
*path = '_';
}
}
#ifdef USE_JPEG_COVER
static const char * extensions[] = { "jpeg", "jpg", "bmp" };
static const unsigned char extension_lens[] = { 4, 3, 3 };
/* Try checking for several file extensions, return true if a file is found and
* leaving the path modified to include the matching extension.
*/
static bool try_exts(char *path, int len)
{
int i;
for (i = 0; i < 3; i++)
{
if (extension_lens[i] + len > MAX_PATH)
continue;
strcpy(path + len, extensions[i]);
if (file_exists(path))
return true;
}
return false;
}
#define EXT
#else
#define EXT "bmp"
#define try_exts(path, len) file_exists(path)
#endif
/* Look for the first matching album art bitmap in the following list:
* ./<trackname><size>.{jpeg,jpg,bmp}
* ./<albumname><size>.{jpeg,jpg,bmp}
* ./cover<size>.bmp
* ../<albumname><size>.{jpeg,jpg,bmp}
* ../cover<size>.{jpeg,jpg,bmp}
* ROCKBOX_DIR/albumart/<artist>-<albumname><size>.{jpeg,jpg,bmp}
* <size> is the value of the size_string parameter, <trackname> and
* <albumname> are read from the ID3 metadata.
* If a matching bitmap is found, its filename is stored in buf.
* Return value is true if a bitmap was found, false otherwise.
*
* If the first symbol in size_string is a colon (e.g. ":100x100")
* then the colon is skipped ("100x100" will be used) and the track
* specific image (./<trackname><size>.bmp) is tried last instead of first.
*/
bool search_albumart_files(const struct mp3entry *id3, const char *size_string,
char *buf, int buflen)
{
char path[MAX_PATH + 1];
char dir[MAX_PATH + 1];
bool found = false;
int track_first = 1;
int pass;
const char *trackname;
const char *artist;
int dirlen;
int albumlen;
int pathlen;
if (!id3 || !buf)
return false;
trackname = id3->path;
if (strcmp(trackname, "No file!") == 0)
return false;
if (*size_string == ':')
{
size_string++;
track_first = 0;
}
strip_filename(dir, sizeof(dir), trackname);
dirlen = strlen(dir);
albumlen = id3->album ? strlen(id3->album) : 0;
for(pass = 0; pass < 2 - track_first; pass++)
{
if (track_first || pass)
{
/* the first file we look for is one specific to the
current track */
strip_extension(path, sizeof(path) - strlen(size_string) - 4,
trackname);
strcat(path, size_string);
strcat(path, "." EXT);
#ifdef USE_JPEG_COVER
pathlen = strlen(path);
#endif
found = try_exts(path, pathlen);
}
if (pass)
break;
if (!found && albumlen > 0)
{
/* if it doesn't exist,
* we look for a file specific to the track's album name */
pathlen = snprintf(path, sizeof(path),
"%s%s%s." EXT, dir, id3->album, size_string);
fix_path_part(path, dirlen, albumlen);
found = try_exts(path, pathlen);
}
if (!found)
{
/* if it still doesn't exist, we look for a generic file */
pathlen = snprintf(path, sizeof(path),
"%scover%s." EXT, dir, size_string);
found = try_exts(path, pathlen);
}
#ifdef USE_JPEG_COVER
if (!found && !*size_string)
{
snprintf (path, sizeof(path), "%sfolder.jpg", dir);
found = file_exists(path);
}
#endif
artist = id3->albumartist != NULL ? id3->albumartist : id3->artist;
if (!found && artist && id3->album)
{
/* look in the albumart subdir of .rockbox */
pathlen = snprintf(path, sizeof(path),
ROCKBOX_DIR "/albumart/%s-%s%s." EXT,
artist,
id3->album,
size_string);
fix_path_part(path, strlen(ROCKBOX_DIR "/albumart/"), MAX_PATH);
found = try_exts(path, pathlen);
}
if (!found)
{
/* if it still doesn't exist,
* we continue to search in the parent directory */
strcpy(path, dir);
path[dirlen - 1] = 0;
strip_filename(dir, sizeof(dir), path);
dirlen = strlen(dir);
}
/* only try parent if there is one */
if (dirlen > 0)
{
if (!found && albumlen > 0)
{
/* we look in the parent directory
* for a file specific to the track's album name */
pathlen = snprintf(path, sizeof(path),
"%s%s%s." EXT, dir, id3->album, size_string);
fix_path_part(path, dirlen, albumlen);
found = try_exts(path, pathlen);
}
if (!found)
{
/* if it still doesn't exist, we look in the parent directory
* for a generic file */
pathlen = snprintf(path, sizeof(path),
"%scover%s." EXT, dir, size_string);
found = try_exts(path, pathlen);
}
}
if (found)
break;
}
if (!found)
return false;
strlcpy(buf, path, buflen);
logf("Album art found: %s", path);
return true;
}
#ifndef PLUGIN
/* Look for albumart bitmap in the same dir as the track and in its parent dir.
* Stores the found filename in the buf parameter.
* Returns true if a bitmap was found, false otherwise */
bool find_albumart(const struct mp3entry *id3, char *buf, int buflen,
const struct dim *dim)
{
if (!id3 || !buf)
return false;
char size_string[9];
logf("Looking for album art for %s", id3->path);
/* Write the size string, e.g. ".100x100". */
snprintf(size_string, sizeof(size_string), ".%dx%d",
dim->width, dim->height);
/* First we look for a bitmap of the right size */
if (search_albumart_files(id3, size_string, buf, buflen))
return true;
/* Then we look for generic bitmaps */
*size_string = 0;
return search_albumart_files(id3, size_string, buf, buflen);
}
/* Draw the album art bitmap from the given handle ID onto the given WPS.
Call with clear = true to clear the bitmap instead of drawing it. */
void draw_album_art(struct gui_wps *gwps, int handle_id, bool clear)
{
if (!gwps || !gwps->data || !gwps->display || handle_id < 0)
return;
struct wps_data *data = gwps->data;
struct skin_albumart *aa = SKINOFFSETTOPTR(get_skin_buffer(data), data->albumart);
if (!aa)
return;
struct bitmap *bmp;
if (bufgetdata(handle_id, 0, (void *)&bmp) <= 0)
return;
short x = aa->x;
short y = aa->y;
short width = bmp->width;
short height = bmp->height;
if (aa->width > 0)
{
/* Crop if the bitmap is too wide */
width = MIN(bmp->width, aa->width);
/* Align */
if (aa->xalign & WPS_ALBUMART_ALIGN_RIGHT)
x += aa->width - width;
else if (aa->xalign & WPS_ALBUMART_ALIGN_CENTER)
x += (aa->width - width) / 2;
}
if (aa->height > 0)
{
/* Crop if the bitmap is too high */
height = MIN(bmp->height, aa->height);
/* Align */
if (aa->yalign & WPS_ALBUMART_ALIGN_BOTTOM)
y += aa->height - height;
else if (aa->yalign & WPS_ALBUMART_ALIGN_CENTER)
y += (aa->height - height) / 2;
}
if (!clear)
{
/* Draw the bitmap */
gwps->display->bitmap_part((fb_data*)bmp->data, 0, 0,
STRIDE(gwps->display->screen_type,
bmp->width, bmp->height),
x, y, width, height);
#ifdef HAVE_LCD_INVERT
if (global_settings.invert) {
gwps->display->set_drawmode(DRMODE_COMPLEMENT);
gwps->display->fillrect(x, y, width, height);
gwps->display->set_drawmode(DRMODE_SOLID);
}
#endif
}
else
{
/* Clear the bitmap */
gwps->display->set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
gwps->display->fillrect(x, y, width, height);
gwps->display->set_drawmode(DRMODE_SOLID);
}
}
#endif /* PLUGIN */