summaryrefslogtreecommitdiff
path: root/apps/plugins/lib/mul_id3.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/lib/mul_id3.c')
-rw-r--r--apps/plugins/lib/mul_id3.c175
1 files changed, 170 insertions, 5 deletions
diff --git a/apps/plugins/lib/mul_id3.c b/apps/plugins/lib/mul_id3.c
index edf44f7282..3ab3a438c8 100644
--- a/apps/plugins/lib/mul_id3.c
+++ b/apps/plugins/lib/mul_id3.c
@@ -40,6 +40,13 @@ struct multiple_tracks_id3 {
40 40
41static struct multiple_tracks_id3 mul_id3; 41static struct multiple_tracks_id3 mul_id3;
42 42
43static const int32_t units[] =
44{
45 LANG_BYTE,
46 LANG_KIBIBYTE,
47 LANG_MEBIBYTE,
48 LANG_GIBIBYTE
49};
43 50
44/* Calculate modified FNV hash of string 51/* Calculate modified FNV hash of string
45 * has good avalanche behaviour and uniform distribution 52 * has good avalanche behaviour and uniform distribution
@@ -130,9 +137,7 @@ void collect_id3(struct mp3entry *id3, bool is_first_track)
130 mul_id3.filesize += id3->filesize; 137 mul_id3.filesize += id3->filesize;
131} 138}
132 139
133/* (!) Note scale factor applied to returned metadata: 140/* (!) Note unit conversion below
134 * - Unit for filesize will be KiB instead of Bytes
135 * - Unit for length will be s instead of ms
136 * 141 *
137 * Use result only as input for browse_id3, 142 * Use result only as input for browse_id3,
138 * with the track_ct parameter set to > 1. 143 * with the track_ct parameter set to > 1.
@@ -159,8 +164,8 @@ void finalize_id3(struct mp3entry *id3)
159 id3->track_string = NULL; 164 id3->track_string = NULL;
160 id3->year_string = NULL; 165 id3->year_string = NULL;
161 id3->year = mul_id3.year; 166 id3->year = mul_id3.year;
162 mul_id3.length /= 1000; 167 mul_id3.length /= 1000; /* convert from ms to s */
163 mul_id3.filesize >>= 10; 168 mul_id3.filesize >>= 10; /* convert from B to KiB */
164 id3->length = mul_id3.length > ULONG_MAX ? 0 : mul_id3.length; 169 id3->length = mul_id3.length > ULONG_MAX ? 0 : mul_id3.length;
165 id3->filesize = mul_id3.filesize > INT_MAX ? 0 : mul_id3.filesize; 170 id3->filesize = mul_id3.filesize > INT_MAX ? 0 : mul_id3.filesize;
166 id3->frequency = mul_id3.frequency; 171 id3->frequency = mul_id3.frequency;
@@ -172,3 +177,163 @@ void finalize_id3(struct mp3entry *id3)
172 id3->track_level = 0; 177 id3->track_level = 0;
173 id3->album_level = 0; 178 id3->album_level = 0;
174} 179}
180
181unsigned long human_size(unsigned long long byte_count, int32_t *unit_lang_id)
182{
183 const size_t n = sizeof(units)/sizeof(units[0]);
184 unsigned int i;
185
186 /* margin set at 10K boundary: 10239 B +1 => 10 KB */
187 for(i = 0; i < n-1 && byte_count >= 10*1024; i++)
188 byte_count >>= 10; /* div by 1024 */
189
190 *unit_lang_id = units[i];
191 return (unsigned long)byte_count;
192}
193
194/* missing filetype attribute for images */
195static const char *image_exts[] = {"bmp","jpg","jpe","jpeg","png","ppm"};
196/* and videos */
197static const char *video_exts[] = {"mpg","mpeg","mpv","m2v"};
198
199static void display_dir_stats_vp(struct dir_stats *stats, struct viewport *vp,
200 struct screen *display)
201{
202 int32_t lang_size_unit;
203 unsigned long display_size = human_size(stats->byte_count, &lang_size_unit);
204 struct viewport *last_vp = display->set_viewport(vp);
205 display->clear_viewport();
206 display->putsf(0, 0, "Files: %d (%lu %s)", stats->file_count,
207 display_size, rb->str(lang_size_unit));
208 display->putsf(0, 1, "Audio: %d", stats->audio_file_count);
209 if (stats->count_all)
210 {
211 display->putsf(0, 2, "Playlists: %d", stats->m3u_file_count);
212 display->putsf(0, 3, "Images: %d", stats->img_file_count);
213 display->putsf(0, 4, "Videos: %d", stats->vid_file_count);
214 display->putsf(0, 5, "Directories: %d", stats->dir_count);
215 display->putsf(0, 6, "Max files in Dir: %d", stats->max_files_in_dir);
216 }
217 else
218 display->putsf(0, 2, "Directories: %d", stats->dir_count);
219
220 display->update_viewport();
221 display->set_viewport(last_vp);
222}
223
224void display_dir_stats(struct dir_stats *stats)
225{
226 struct viewport vps[NB_SCREENS];
227 FOR_NB_SCREENS(i)
228 {
229 rb->viewport_set_defaults(&vps[i], i);
230 display_dir_stats_vp(stats, &vps[i], rb->screens[i]);
231 }
232}
233
234/* Recursively scans directories in search of files
235 * and informs the user of the progress.
236 */
237bool collect_dir_stats(struct dir_stats *stats, bool (*id3_cb)(const char*))
238{
239 bool result = true;
240 unsigned int files_in_dir = 0;
241 static unsigned int id3_count;
242 static unsigned long last_displayed, last_get_action;
243 struct dirent* entry;
244 int dirlen = rb->strlen(stats->dirname);
245 DIR* dir = rb->opendir(stats->dirname);
246 if (!dir)
247 {
248 rb->splashf(HZ*2, "open error: %s", stats->dirname);
249 return false;
250 }
251 else if (!stats->dirname[1]) /* root dir */
252 stats->dirname[0] = dirlen = 0;
253
254 /* walk through the directory content */
255 while(result && (0 != (entry = rb->readdir(dir))))
256 {
257 struct dirinfo info = rb->dir_get_info(dir, entry);
258 if (info.attribute & ATTR_DIRECTORY)
259 {
260 if (!rb->strcmp((char *)entry->d_name, ".") ||
261 !rb->strcmp((char *)entry->d_name, ".."))
262 continue; /* skip these */
263
264 rb->snprintf(stats->dirname + dirlen, sizeof(stats->dirname) - dirlen,
265 "/%s", entry->d_name); /* append name to current directory */
266 if (!id3_cb)
267 {
268 stats->dir_count++; /* new directory */
269 if (*rb->current_tick - last_displayed > (HZ/2))
270 {
271 if (last_displayed)
272 display_dir_stats(stats);
273 last_displayed = *(rb->current_tick);
274 }
275 }
276 result = collect_dir_stats(stats, id3_cb); /* recursion */
277 }
278 else if (!id3_cb)
279 {
280 char *ptr;
281 stats->file_count++; /* new file */
282 files_in_dir++;
283 stats->byte_count += info.size;
284
285 int attr = rb->filetype_get_attr(entry->d_name);
286 if (attr == FILE_ATTR_AUDIO)
287 stats->audio_file_count++;
288 else if (attr == FILE_ATTR_M3U)
289 stats->m3u_file_count++;
290 /* image or video file attributes have to be compared manually */
291 else if (stats->count_all &&
292 (ptr = rb->strrchr(entry->d_name,'.')))
293 {
294 unsigned int i;
295 ptr++;
296 for(i = 0; i < ARRAYLEN(image_exts); i++)
297 {
298 if(!rb->strcasecmp(ptr, image_exts[i]))
299 {
300 stats->img_file_count++;
301 break;
302 }
303 }
304 if (i >= ARRAYLEN(image_exts)) {
305 for(i = 0; i < ARRAYLEN(video_exts); i++) {
306 if(!rb->strcasecmp(ptr, video_exts[i])) {
307 stats->vid_file_count++;
308 break;
309 }
310 }
311 }
312 }
313 }
314 else if (rb->filetype_get_attr(entry->d_name) == FILE_ATTR_AUDIO)
315 {
316 rb->splash_progress(id3_count++, stats->audio_file_count,
317 "%s (%s)",
318 rb->str(LANG_WAIT), rb->str(LANG_OFF_ABORT));
319 rb->snprintf(stats->dirname + dirlen, sizeof(stats->dirname) - dirlen,
320 "/%s", entry->d_name); /* append name to current directory */
321 id3_cb(stats->dirname); /* allow metadata to be collected */
322 }
323
324 if (TIME_AFTER(*(rb->current_tick), last_get_action + HZ/8))
325 {
326 if(ACTION_STD_CANCEL == rb->get_action(CONTEXT_STD,TIMEOUT_NOBLOCK))
327 {
328 stats->canceled = true;
329 result = false;
330 }
331 last_get_action = *(rb->current_tick);
332 }
333 rb->yield();
334 }
335 rb->closedir(dir);
336 if (stats->max_files_in_dir < files_in_dir)
337 stats->max_files_in_dir = files_in_dir;
338 return result;
339}