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
|
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* Copyright (C) 2023 Christian Soffke
*
* 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 "plugin.h"
#include "mul_id3.h"
struct multiple_tracks_id3 {
unsigned long long filesize;
unsigned long long length;
unsigned long frequency;
unsigned int artist_hash;
unsigned int composer_hash;
unsigned int albumartist_hash;
unsigned int grouping_hash;
unsigned int comment_hash;
unsigned int album_hash;
unsigned int genre_hash;
unsigned int codectype;
unsigned int bitrate;
int year;
bool vbr;
};
static struct multiple_tracks_id3 mul_id3;
static const int32_t units[] =
{
LANG_BYTE,
LANG_KIBIBYTE,
LANG_MEBIBYTE,
LANG_GIBIBYTE
};
/* Calculate modified FNV hash of string
* has good avalanche behaviour and uniform distribution
* see http://home.comcast.net/~bretm/hash/ */
static unsigned int mfnv(char *str)
{
const unsigned int p = 16777619;
unsigned int hash = 0x811C9DC5; // 2166136261;
if (!str)
return 0;
while(*str)
hash = (hash ^ *str++) * p;
hash += hash << 13;
hash ^= hash >> 7;
hash += hash << 3;
hash ^= hash >> 17;
hash += hash << 5;
return hash;
}
static void init_mul_id3(void)
{
mul_id3.artist_hash = 0;
mul_id3.album_hash = 0;
mul_id3.genre_hash = 0;
mul_id3.composer_hash = 0;
mul_id3.albumartist_hash = 0;
mul_id3.grouping_hash = 0;
mul_id3.comment_hash = 0;
mul_id3.codectype = 0;
mul_id3.vbr = false;
mul_id3.bitrate = 0;
mul_id3.frequency = 0;
mul_id3.length = 0;
mul_id3.filesize = 0;
mul_id3.year = 0;
}
void collect_id3(struct mp3entry *id3, bool is_first_track)
{
if (is_first_track)
{
init_mul_id3();
mul_id3.artist_hash = mfnv(id3->artist);
mul_id3.album_hash = mfnv(id3->album);
mul_id3.genre_hash = mfnv(id3->genre_string);
mul_id3.composer_hash = mfnv(id3->composer);
mul_id3.albumartist_hash = mfnv(id3->albumartist);
mul_id3.grouping_hash = mfnv(id3->grouping);
mul_id3.comment_hash = mfnv(id3->comment);
mul_id3.codectype = id3->codectype;
mul_id3.vbr = id3->vbr;
mul_id3.bitrate = id3->bitrate;
mul_id3.frequency = id3->frequency;
mul_id3.year = id3->year;
}
else
{
if (mul_id3.artist_hash && (mfnv(id3->artist) != mul_id3.artist_hash))
mul_id3.artist_hash = 0;
if (mul_id3.album_hash && (mfnv(id3->album) != mul_id3.album_hash))
mul_id3.album_hash = 0;
if (mul_id3.genre_hash && (mfnv(id3->genre_string) != mul_id3.genre_hash))
mul_id3.genre_hash = 0;
if (mul_id3.composer_hash && (mfnv(id3->composer) != mul_id3.composer_hash))
mul_id3.composer_hash = 0;
if (mul_id3.albumartist_hash && (mfnv(id3->albumartist) !=
mul_id3.albumartist_hash))
mul_id3.albumartist_hash = 0;
if (mul_id3.grouping_hash && (mfnv(id3->grouping) != mul_id3.grouping_hash))
mul_id3.grouping_hash = 0;
if (mul_id3.comment_hash && (mfnv(id3->comment) != mul_id3.comment_hash))
mul_id3.comment_hash = 0;
if (mul_id3.codectype && (id3->codectype != mul_id3.codectype))
mul_id3.codectype = AFMT_UNKNOWN;
if (mul_id3.bitrate && (id3->bitrate != mul_id3.bitrate ||
id3->vbr != mul_id3.vbr))
mul_id3.bitrate = 0;
if (mul_id3.frequency && (id3->frequency != mul_id3.frequency))
mul_id3.frequency = 0;
if (mul_id3.year && (id3->year != mul_id3.year))
mul_id3.year = 0;
}
mul_id3.length += id3->length;
mul_id3.filesize += id3->filesize;
}
/* (!) Note unit conversion below
*
* Use result only as input for browse_id3,
* with the track_ct parameter set to > 1.
*/
void finalize_id3(struct mp3entry *id3)
{
id3->path[0] = '\0';
id3->title = NULL;
if (!mul_id3.artist_hash)
id3->artist = NULL;
if (!mul_id3.album_hash)
id3->album = NULL;
if (!mul_id3.genre_hash)
id3->genre_string = NULL;
if (!mul_id3.composer_hash)
id3->composer = NULL;
if (!mul_id3.albumartist_hash)
id3->albumartist = NULL;
if (!mul_id3.grouping_hash)
id3->grouping = NULL;
if (!mul_id3.comment_hash)
id3->comment = NULL;
id3->disc_string = NULL;
id3->track_string = NULL;
id3->year_string = NULL;
id3->year = mul_id3.year;
mul_id3.length /= 1000; /* convert from ms to s */
mul_id3.filesize >>= 10; /* convert from B to KiB */
id3->length = mul_id3.length > ULONG_MAX ? 0 : mul_id3.length;
id3->filesize = mul_id3.filesize > INT_MAX ? 0 : mul_id3.filesize;
id3->frequency = mul_id3.frequency;
id3->bitrate = mul_id3.bitrate;
id3->codectype = mul_id3.codectype;
id3->vbr = mul_id3.vbr;
id3->discnum = 0;
id3->tracknum = 0;
id3->track_level = 0;
id3->album_level = 0;
}
unsigned long human_size(unsigned long long byte_count, int32_t *unit_lang_id)
{
const size_t n = sizeof(units)/sizeof(units[0]);
unsigned int i;
/* margin set at 10K boundary: 10239 B +1 => 10 KB */
for(i = 0; i < n-1 && byte_count >= 10*1024; i++)
byte_count >>= 10; /* div by 1024 */
*unit_lang_id = units[i];
return (unsigned long)byte_count;
}
/* missing filetype attribute for images */
static const char *image_exts[] = {"bmp","jpg","jpe","jpeg","png","ppm"};
/* and videos */
static const char *video_exts[] = {"mpg","mpeg","mpv","m2v"};
static void prn(const char *str, int y)
{
rb->lcd_puts(0, y, str);
#ifdef HAVE_REMOTE_LCD
rb->lcd_remote_puts(0, y, str);
#endif
}
void display_dir_stats(struct dir_stats *stats)
{
char buf[32];
int32_t lang_size_unit;
unsigned long display_size = human_size(stats->byte_count, &lang_size_unit);
rb->lcd_clear_display();
#ifdef HAVE_REMOTE_LCD
rb->lcd_remote_clear_display();
#endif
rb->snprintf(buf, sizeof(buf), "Files: %d (%lu %s)", stats->file_count,
display_size, rb->str(lang_size_unit));
prn(buf, 0);
rb->snprintf(buf, sizeof(buf), "Audio: %d", stats->audio_file_count);
prn(buf, 1);
if (stats->count_all)
{
rb->snprintf(buf, sizeof(buf), "Playlists: %d", stats->m3u_file_count);
prn(buf, 2);
rb->snprintf(buf, sizeof(buf), "Images: %d", stats->img_file_count);
prn(buf, 3);
rb->snprintf(buf, sizeof(buf), "Videos: %d", stats->vid_file_count);
prn(buf, 4);
rb->snprintf(buf, sizeof(buf), "Directories: %d", stats->dir_count);
prn(buf, 5);
rb->snprintf(buf, sizeof(buf), "Max files in Dir: %d",
stats->max_files_in_dir);
prn(buf, 6);
}
else
{
rb->snprintf(buf, sizeof(buf), "Directories: %d", stats->dir_count);
prn(buf, 2);
}
rb->lcd_update();
#ifdef HAVE_REMOTE_LCD
rb->lcd_remote_update();
#endif
}
/* Recursively scans directories in search of files
* and informs the user of the progress.
*/
bool collect_dir_stats(struct dir_stats *stats, bool (*id3_cb)(const char*))
{
bool result = true;
unsigned int files_in_dir = 0;
static unsigned int id3_count;
static unsigned long last_displayed, last_get_action;
struct dirent* entry;
int dirlen = rb->strlen(stats->dirname);
DIR* dir = rb->opendir(stats->dirname);
if (!dir)
{
rb->splashf(HZ*2, "open error: %s", stats->dirname);
return false;
}
else if (!stats->dirname[1]) /* root dir */
stats->dirname[0] = dirlen = 0;
/* walk through the directory content */
while(result && (0 != (entry = rb->readdir(dir))))
{
struct dirinfo info = rb->dir_get_info(dir, entry);
if (info.attribute & ATTR_DIRECTORY)
{
if (!rb->strcmp((char *)entry->d_name, ".") ||
!rb->strcmp((char *)entry->d_name, ".."))
continue; /* skip these */
rb->snprintf(stats->dirname + dirlen, sizeof(stats->dirname) - dirlen,
"/%s", entry->d_name); /* append name to current directory */
if (!id3_cb)
{
stats->dir_count++; /* new directory */
if (*rb->current_tick - last_displayed > (HZ/2))
{
if (last_displayed)
display_dir_stats(stats);
last_displayed = *(rb->current_tick);
}
}
result = collect_dir_stats(stats, id3_cb); /* recursion */
}
else if (!id3_cb)
{
char *ptr;
stats->file_count++; /* new file */
files_in_dir++;
stats->byte_count += info.size;
int attr = rb->filetype_get_attr(entry->d_name);
if (attr == FILE_ATTR_AUDIO)
stats->audio_file_count++;
else if (attr == FILE_ATTR_M3U)
stats->m3u_file_count++;
/* image or video file attributes have to be compared manually */
else if (stats->count_all &&
(ptr = rb->strrchr(entry->d_name,'.')))
{
unsigned int i;
ptr++;
for(i = 0; i < ARRAYLEN(image_exts); i++)
{
if(!rb->strcasecmp(ptr, image_exts[i]))
{
stats->img_file_count++;
break;
}
}
if (i >= ARRAYLEN(image_exts)) {
for(i = 0; i < ARRAYLEN(video_exts); i++) {
if(!rb->strcasecmp(ptr, video_exts[i])) {
stats->vid_file_count++;
break;
}
}
}
}
}
else if (rb->filetype_get_attr(entry->d_name) == FILE_ATTR_AUDIO)
{
rb->splash_progress(id3_count++, stats->audio_file_count,
"%s (%s)",
rb->str(LANG_WAIT), rb->str(LANG_OFF_ABORT));
rb->snprintf(stats->dirname + dirlen, sizeof(stats->dirname) - dirlen,
"/%s", entry->d_name); /* append name to current directory */
id3_cb(stats->dirname); /* allow metadata to be collected */
}
if (TIME_AFTER(*(rb->current_tick), last_get_action + HZ/8))
{
if(ACTION_STD_CANCEL == rb->get_action(CONTEXT_STD,TIMEOUT_NOBLOCK))
{
stats->canceled = true;
result = false;
}
last_get_action = *(rb->current_tick);
}
rb->yield();
}
rb->closedir(dir);
if (stats->max_files_in_dir < files_in_dir)
stats->max_files_in_dir = files_in_dir;
return result;
}
|