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