blob: fd564a49c3ea49999c6b0459123567f44ec96fba [file] [log] [blame]
Michael Sevakisa222f272007-12-29 19:46:35 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Miscellaneous helper API definitions
11 *
12 * Copyright (c) 2007 Michael Sevakis
13 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000014 * 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.
Michael Sevakisa222f272007-12-29 19:46:35 +000018 *
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
21 *
22 ****************************************************************************/
23#include "plugin.h"
24#include "mpegplayer.h"
25
26/** Streams **/
27
28/* Ensures direction is -1 or 1 and margin is properly initialized */
29void stream_scan_normalize(struct stream_scan *sk)
30{
31 if (sk->dir >= 0)
32 {
33 sk->dir = SSCAN_FORWARD;
34 sk->margin = sk->len;
35 }
36 else if (sk->dir < 0)
37 {
38 sk->dir = SSCAN_REVERSE;
39 sk->margin = 0;
40 }
41}
42
43/* Moves a scan cursor. If amount is positive, the increment is in the scan
44 * direction, otherwise opposite the scan direction */
45void stream_scan_offset(struct stream_scan *sk, off_t by)
46{
47 off_t bydir = by*sk->dir;
48 sk->pos += bydir;
49 sk->margin -= bydir;
50 sk->len -= by;
51}
52
53/** Time helpers **/
54void ts_to_hms(uint32_t pts, struct hms *hms)
55{
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;
62}
63
64void hms_format(char *buf, size_t bufsize, struct hms *hms)
65{
66 /* Only display hours if nonzero */
67 if (hms->hrs != 0)
68 {
69 rb->snprintf(buf, bufsize, "%u:%02u:%02u",
70 hms->hrs, hms->min, hms->sec);
71 }
72 else
73 {
74 rb->snprintf(buf, bufsize, "%u:%02u",
75 hms->min, hms->sec);
76 }
77}
78
79/** Maths **/
80uint32_t muldiv_uint32(uint32_t multiplicand,
81 uint32_t multiplier,
82 uint32_t divisor)
83{
84 if (divisor != 0)
85 {
86 uint64_t prod = (uint64_t)multiplier*multiplicand + divisor/2;
87
88 if ((uint32_t)(prod >> 32) < divisor)
89 return (uint32_t)(prod / divisor);
90 }
91 else if (multiplicand == 0 || multiplier == 0)
92 {
93 return 0; /* 0/0 = 0 : yaya */
94 }
95 /* else (> 0) / 0 = UINT32_MAX */
96
97 return UINT32_MAX; /* Saturate */
98}