MPEGPlayer: Add a second layer of caching to help speed up byte-wise scanning and...
[kugel-rb.git] / apps / plugins / mpegplayer / disk_buf.h
bloba5a10cfe69143fba1186834fa9810d56c578d951
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * AV disk buffer declarations
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 #ifndef DISK_BUF_H
24 #define DISK_BUF_H
26 #ifndef OFF_T_MAX
27 #define OFF_T_MAX (~(off_t)1 << (sizeof (off_t)*8 - 1))
28 #endif
30 #define DISK_BUF_PAGE_SHIFT 15 /* 32KB cache lines */
31 #define DISK_BUF_PAGE_SIZE (1 << DISK_BUF_PAGE_SHIFT)
32 #define DISK_BUF_PAGE_MASK (DISK_BUF_PAGE_SIZE-1)
34 enum
36 DISK_BUF_NOTIFY_ERROR = -1,
37 DISK_BUF_NOTIFY_NULL = 0,
38 DISK_BUF_NOTIFY_OK,
39 DISK_BUF_NOTIFY_TIMEDOUT,
40 DISK_BUF_NOTIFY_PROCESS_EVENT,
41 DISK_BUF_NOTIFY_REGISTERED,
44 /** Macros to map file offsets to cached data **/
46 /* Returns a cache tag given a file offset */
47 #define MAP_OFFSET_TO_TAG(o) \
48 ((o) >> DISK_BUF_PAGE_SHIFT)
50 /* Returns the cache page number given a file offset */
51 #define MAP_OFFSET_TO_PAGE(o) \
52 (MAP_OFFSET_TO_TAG(o) % disk_buf.pgcount)
54 /* Returns the buffer offset given a file offset */
55 #define MAP_OFFSET_TO_BUFFER(o) \
56 (MAP_OFFSET_TO_PAGE(o) * DISK_BUF_PAGE_SIZE)
58 struct dbuf_range
60 uint32_t tag_start;
61 uint32_t tag_end;
62 int pg_start;
65 #define DISK_BUF_L2_CACHE_SHIFT 6
66 #define DISK_BUF_L2_CACHE_SIZE (1 << DISK_BUF_L2_CACHE_SHIFT)
67 #define DISK_BUF_L2_CACHE_MASK (DISK_BUF_L2_CACHE_SIZE-1)
69 struct dbuf_l2_cache
71 off_t addr; /* L2 file offset */
72 size_t size; /* Real size */
73 uint8_t data[DISK_BUF_L2_CACHE_SIZE*2]; /* Local data and guard */
76 void dbuf_l2_init(struct dbuf_l2_cache *l2_p);
78 /* This object is an extension of the stream manager and handles some
79 * playback events as well as buffering */
80 struct disk_buf
82 unsigned int thread;
83 struct event_queue *q;
84 uint8_t *start; /* Start pointer */
85 uint8_t *end; /* End of buffer pointer less MPEG_GUARDBUF_SIZE. The
86 guard space is used to wrap data at the buffer start to
87 pass continuous data packets */
88 uint8_t *tail; /* Location of last data + 1 filled into the buffer */
89 ssize_t size; /* The buffer length _not_ including the guard space (end-start) */
90 int pgcount; /* Total number of available cached pages */
91 uint32_t *cache; /* Pointer to cache structure - allocated on buffer */
92 int in_file; /* File being read */
93 ssize_t filesize; /* Size of file in_file in bytes */
94 int file_pages; /* Number of pages in file (rounded up) */
95 off_t offset; /* Current position (random access) */
96 off_t win_left; /* Left edge of buffer window (streaming) */
97 off_t win_right; /* Right edge of buffer window (streaming) */
98 uint32_t time_last; /* Last time watermark was checked */
99 off_t pos_last; /* Last position at watermark check time */
100 ssize_t low_wm; /* The low watermark for automatic rebuffering */
101 int status; /* Status as stream */
102 int state; /* Current thread state */
103 bool need_seek; /* Need to seek because a read was not contiguous */
106 extern struct disk_buf disk_buf SHAREDBSS_ATTR;
108 struct stream_hdr;
109 bool disk_buf_is_data_ready(struct stream_hdr *sh, ssize_t margin);
111 bool disk_buf_init(void);
112 void disk_buf_exit(void);
114 static inline int disk_buf_status(void)
115 { return disk_buf.status; }
117 int disk_buf_open(const char *filename);
118 void disk_buf_close(void);
119 ssize_t disk_buf_getbuffer(size_t size, void **pp, void **pwrap,
120 size_t *sizewrap);
121 ssize_t disk_buf_getbuffer_l2(struct dbuf_l2_cache *l2,
122 size_t size, void **pp);
123 ssize_t disk_buf_read(void *buffer, size_t size);
124 ssize_t disk_buf_lseek(off_t offset, int whence);
126 static inline off_t disk_buf_ftell(void)
127 { return disk_buf.offset; }
129 static inline ssize_t disk_buf_filesize(void)
130 { return disk_buf.filesize; }
132 ssize_t disk_buf_prepare_streaming(off_t pos, size_t len);
133 ssize_t disk_buf_set_streaming_window(off_t left, off_t right);
134 void * disk_buf_offset2ptr(off_t offset);
135 int disk_buf_check_streaming_window(off_t left, off_t right);
137 intptr_t disk_buf_send_msg(long id, intptr_t data);
138 void disk_buf_post_msg(long id, intptr_t data);
139 void disk_buf_reply_msg(intptr_t retval);
141 #endif /* DISK_BUF_H */