diff options
Diffstat (limited to 'common/common.c')
-rw-r--r-- | common/common.c | 92 |
1 files changed, 86 insertions, 6 deletions
diff --git a/common/common.c b/common/common.c index 5068995ded..7089a6674b 100644 --- a/common/common.c +++ b/common/common.c @@ -21,6 +21,7 @@ #include <libavutil/common.h> #include <libavutil/error.h> +#include <libavutil/mathematics.h> #include "mpv_talloc.h" #include "misc/bstr.h" @@ -93,6 +94,25 @@ char *mp_format_time(double time, bool fractions) return mp_format_time_fmt(fractions ? "%H:%M:%S.%T" : "%H:%M:%S", time); } +char *mp_format_double(void *talloc_ctx, double val, int precision, + bool plus_sign, bool percent_sign, bool trim) +{ + bstr str = {0}; + const char *fmt = plus_sign ? "%+.*f" : "%.*f"; + bstr_xappend_asprintf(talloc_ctx, &str, fmt, precision, val); + size_t pos = str.len; + if (trim) { + while (--pos && str.start[pos] == '0') + str.len--; + if (str.start[pos] == '.') + str.len--; + } + if (percent_sign) + bstr_xappend(talloc_ctx, &str, bstr0("%")); + str.start[str.len] = '\0'; + return str.start; +} + // Set rc to the union of rc and rc2 void mp_rect_union(struct mp_rect *rc, const struct mp_rect *rc2) { @@ -120,12 +140,65 @@ bool mp_rect_intersection(struct mp_rect *rc, const struct mp_rect *rc2) return rc->x1 > rc->x0 && rc->y1 > rc->y0; } -bool mp_rect_equals(struct mp_rect *rc1, struct mp_rect *rc2) +bool mp_rect_equals(const struct mp_rect *rc1, const struct mp_rect *rc2) { return rc1->x0 == rc2->x0 && rc1->y0 == rc2->y0 && rc1->x1 == rc2->x1 && rc1->y1 == rc2->y1; } +// Rotate mp_rect by 90 degrees increments +void mp_rect_rotate(struct mp_rect *rc, int w, int h, int rotation) +{ + rotation %= 360; + + if (rotation >= 180) { + rotation -= 180; + MPSWAP(int, rc->x0, rc->x1); + MPSWAP(int, rc->y0, rc->y1); + } + + if (rotation == 90) { + *rc = (struct mp_rect) { + .x0 = rc->y1, + .y0 = rc->x0, + .x1 = rc->y0, + .y1 = rc->x1, + }; + } + + if (rc->x1 < rc->x0) { + rc->x0 = w - rc->x0; + rc->x1 = w - rc->x1; + } + + if (rc->y1 < rc->y0) { + rc->y0 = h - rc->y0; + rc->y1 = h - rc->y1; + } +} + +// Compute rc1-rc2, put result in res_array, return number of rectangles in +// res_array. In the worst case, there are 4 rectangles, so res_array must +// provide that much storage space. +int mp_rect_subtract(const struct mp_rect *rc1, const struct mp_rect *rc2, + struct mp_rect res[4]) +{ + struct mp_rect rc = *rc1; + if (!mp_rect_intersection(&rc, rc2)) + return 0; + + int cnt = 0; + if (rc1->y0 < rc.y0) + res[cnt++] = (struct mp_rect){rc1->x0, rc1->y0, rc1->x1, rc.y0}; + if (rc1->x0 < rc.x0) + res[cnt++] = (struct mp_rect){rc1->x0, rc.y0, rc.x0, rc.y1}; + if (rc1->x1 > rc.x1) + res[cnt++] = (struct mp_rect){rc.x1, rc.y0, rc1->x1, rc.y1}; + if (rc1->y1 > rc.y1) + res[cnt++] = (struct mp_rect){rc1->x0, rc.y1, rc1->x1, rc1->y1}; + return cnt; +} + // This works like snprintf(), except that it starts writing the first output // character to str[strlen(str)]. This returns the number of characters the // string would have *appended* assuming a large enough buffer, will make sure @@ -344,9 +417,16 @@ unsigned int mp_log2(uint32_t v) // mp_round_next_power_of_2(UINT32_MAX) == 0 uint32_t mp_round_next_power_of_2(uint32_t v) { - for (int n = 0; n < 30; n++) { - if ((1 << n) >= v) - return 1 << n; - } - return 0; + if (!v) + return 1; + if (!(v & (v - 1))) + return v; + int l = mp_log2(v) + 1; + return l == 32 ? 0 : (uint32_t)1 << l; +} + +int mp_lcm(int x, int y) +{ + assert(x && y); + return x * (y / av_gcd(x, y)); } |