Tweaked dependencies a bit
[kugel-rb.git] / apps / plugins / mpegplayer / mpeg_misc.c
blobfd564a49c3ea49999c6b0459123567f44ec96fba
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Miscellaneous helper API definitions
12 * Copyright (c) 2007 Michael Sevakis
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
22 ****************************************************************************/
23 #include "plugin.h"
24 #include "mpegplayer.h"
26 /** Streams **/
28 /* Ensures direction is -1 or 1 and margin is properly initialized */
29 void stream_scan_normalize(struct stream_scan *sk)
31 if (sk->dir >= 0)
33 sk->dir = SSCAN_FORWARD;
34 sk->margin = sk->len;
36 else if (sk->dir < 0)
38 sk->dir = SSCAN_REVERSE;
39 sk->margin = 0;
43 /* Moves a scan cursor. If amount is positive, the increment is in the scan
44 * direction, otherwise opposite the scan direction */
45 void stream_scan_offset(struct stream_scan *sk, off_t by)
47 off_t bydir = by*sk->dir;
48 sk->pos += bydir;
49 sk->margin -= bydir;
50 sk->len -= by;
53 /** Time helpers **/
54 void ts_to_hms(uint32_t pts, struct hms *hms)
56 hms->frac = pts % TS_SECOND;
57 hms->sec = pts / TS_SECOND;
58 hms->min = hms->sec / 60;
59 hms->hrs = hms->min / 60;
60 hms->sec %= 60;
61 hms->min %= 60;
64 void hms_format(char *buf, size_t bufsize, struct hms *hms)
66 /* Only display hours if nonzero */
67 if (hms->hrs != 0)
69 rb->snprintf(buf, bufsize, "%u:%02u:%02u",
70 hms->hrs, hms->min, hms->sec);
72 else
74 rb->snprintf(buf, bufsize, "%u:%02u",
75 hms->min, hms->sec);
79 /** Maths **/
80 uint32_t muldiv_uint32(uint32_t multiplicand,
81 uint32_t multiplier,
82 uint32_t divisor)
84 if (divisor != 0)
86 uint64_t prod = (uint64_t)multiplier*multiplicand + divisor/2;
88 if ((uint32_t)(prod >> 32) < divisor)
89 return (uint32_t)(prod / divisor);
91 else if (multiplicand == 0 || multiplier == 0)
93 return 0; /* 0/0 = 0 : yaya */
95 /* else (> 0) / 0 = UINT32_MAX */
97 return UINT32_MAX; /* Saturate */