48e2f01c |
1 | /* |
2 | * Copyright (C) 2013 nu774 |
3 | * For conditions of distribution and use, see copyright notice in COPYING |
4 | */ |
5 | #if HAVE_CONFIG_H |
6 | # include "config.h" |
7 | #endif |
8 | #include <stdio.h> |
9 | #include <time.h> |
10 | #if HAVE_STDINT_H |
11 | # include <stdint.h> |
12 | #endif |
13 | #if HAVE_INTTYPES_H |
14 | # include <inttypes.h> |
15 | #elif defined _MSC_VER |
16 | # define PRId64 "I64d" |
17 | #endif |
18 | #include "compat.h" |
19 | #include "progress.h" |
20 | |
21 | static |
22 | void seconds_to_hms(double seconds, int *h, int *m, int *s, int *millis) |
23 | { |
24 | *h = (int)(seconds / 3600.0); |
25 | seconds -= *h * 3600; |
26 | *m = (int)(seconds / 60.0); |
27 | seconds -= *m * 60; |
28 | *s = (int)seconds; |
29 | *millis = (int)((seconds - *s) * 1000.0 + 0.5); |
30 | } |
31 | |
32 | static |
33 | void print_seconds(FILE *fp, double seconds) |
34 | { |
35 | int h, m, s, millis; |
36 | seconds_to_hms(seconds, &h, &m, &s, &millis); |
37 | if (h) |
38 | fprintf(stderr, "%d:%02d:%02d.%03d", h, m, s, millis); |
39 | else |
40 | fprintf(stderr, "%02d:%02d.%03d", m, s, millis); |
41 | } |
42 | |
43 | void aacenc_progress_init(aacenc_progress_t *progress, int64_t total, |
44 | int32_t timescale) |
45 | { |
46 | progress->start = aacenc_timer(); |
47 | progress->timescale = timescale; |
48 | progress->total = total; |
49 | } |
50 | |
51 | void aacenc_progress_update(aacenc_progress_t *progress, int64_t current, |
52 | int period) |
53 | { |
54 | int percent = 100.0 * current / progress->total + .5; |
55 | double seconds = current / progress->timescale; |
56 | double ellapsed = (aacenc_timer() - progress->start) / 1000.0; |
57 | double eta = ellapsed * (progress->total / (double)current - 1.0); |
58 | double speed = ellapsed ? seconds / ellapsed : 0.0; |
59 | if (current < progress->processed + period) |
60 | return; |
61 | |
62 | if (progress->total == INT64_MAX) { |
63 | putc('\r', stderr); |
64 | print_seconds(stderr, seconds); |
65 | fprintf(stderr, " (%.0fx) ", speed); |
66 | } else { |
67 | fprintf(stderr, "\r[%d%%] ", percent); |
68 | print_seconds(stderr, seconds); |
69 | putc('/', stderr); |
70 | print_seconds(stderr, progress->total / progress->timescale); |
71 | fprintf(stderr, " (%.0fx), ETA ", speed); |
72 | print_seconds(stderr, eta); |
73 | fputs(" ", stderr); |
74 | } |
75 | progress->processed = current; |
76 | } |
77 | |
78 | void aacenc_progress_finish(aacenc_progress_t *progress, int64_t current) |
79 | { |
80 | double ellapsed = (aacenc_timer() - progress->start) / 1000.0; |
81 | aacenc_progress_update(progress, current, 0); |
82 | if (progress->total == INT64_MAX) |
8960a177 |
83 | fprintf(stderr, "\n%" PRId64 " samples processed in ", current); |
48e2f01c |
84 | else |
85 | fprintf(stderr, "\n%" PRId64 "/%" PRId64 " samples processed in ", |
86 | current, progress->total); |
87 | print_seconds(stderr, ellapsed); |
88 | putc('\n', stderr); |
89 | } |