fix red.
[kugel-rb.git] / apps / plugins / mpegplayer / mpeg_misc.c
blobe201aa69c73c49d59acb520944c647dfe9dd4a9d
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 /* Initializes the cursor */
29 void stream_scan_init(struct stream_scan *sk)
31 dbuf_l2_init(&sk->l2);
34 /* Ensures direction is -1 or 1 and margin is properly initialized */
35 void stream_scan_normalize(struct stream_scan *sk)
37 if (sk->dir >= 0)
39 sk->dir = SSCAN_FORWARD;
40 sk->margin = sk->len;
42 else if (sk->dir < 0)
44 sk->dir = SSCAN_REVERSE;
45 sk->margin = 0;
49 /* Moves a scan cursor. If amount is positive, the increment is in the scan
50 * direction, otherwise opposite the scan direction */
51 void stream_scan_offset(struct stream_scan *sk, off_t by)
53 off_t bydir = by*sk->dir;
54 sk->pos += bydir;
55 sk->margin -= bydir;
56 sk->len -= by;
59 /** Time helpers **/
60 void ts_to_hms(uint32_t pts, struct hms *hms)
62 hms->frac = pts % TS_SECOND;
63 hms->sec = pts / TS_SECOND;
64 hms->min = hms->sec / 60;
65 hms->hrs = hms->min / 60;
66 hms->sec %= 60;
67 hms->min %= 60;
70 void hms_format(char *buf, size_t bufsize, struct hms *hms)
72 /* Only display hours if nonzero */
73 if (hms->hrs != 0)
75 rb->snprintf(buf, bufsize, "%u:%02u:%02u",
76 hms->hrs, hms->min, hms->sec);
78 else
80 rb->snprintf(buf, bufsize, "%u:%02u",
81 hms->min, hms->sec);
85 /** Maths **/
86 uint32_t muldiv_uint32(uint32_t multiplicand,
87 uint32_t multiplier,
88 uint32_t divisor)
90 if (divisor != 0)
92 uint64_t prod = (uint64_t)multiplier*multiplicand + divisor/2;
94 if ((uint32_t)(prod >> 32) < divisor)
95 return (uint32_t)(prod / divisor);
97 else if (multiplicand == 0 || multiplier == 0)
99 return 0; /* 0/0 = 0 : yaya */
101 /* else (> 0) / 0 = UINT32_MAX */
103 return UINT32_MAX; /* Saturate */
107 /** Lists **/
109 /* Does the list have any members? */
110 bool list_is_empty(void **list)
112 return *list == NULL;
115 /* Is the item inserted into a particular list? */
116 bool list_is_member(void **list, void *item)
118 return *rb->find_array_ptr(list, item) != NULL;
121 /* Removes an item from a list - returns true if item was found
122 * and thus removed. */
123 bool list_remove_item(void **list, void *item)
125 return rb->remove_array_ptr(list, item) != -1;
128 /* Adds a list item, insert last, if not already present. */
129 void list_add_item(void **list, void *item)
131 void **item_p = rb->find_array_ptr(list, item);
132 if (*item_p == NULL)
133 *item_p = item;
136 /* Clears the entire list. */
137 void list_clear_all(void **list)
139 while (*list != NULL)
140 *list++ = NULL;
143 /* Enumerate all items in the array, passing each item in turn to the
144 * callback as well as the data value. The current item may be safely
145 * removed. Other changes during enumeration are undefined. The callback
146 * may return 'false' to stop the enumeration early. */
147 void list_enum_items(void **list,
148 list_enum_callback_t callback,
149 intptr_t data)
151 for (;;)
153 void *item = *list;
155 if (item == NULL)
156 break;
158 if (callback != NULL && !callback(item, data))
159 break;
161 if (*list == item)
162 list++; /* Item still there */