MPEGPlayer: Add a second layer of caching to help speed up byte-wise scanning and...
[kugel-rb.git] / apps / plugins / mpegplayer / mpeg_misc.c
blob8e6ccf650fbead052987d4f6b33d244ec7388c4f
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 */