1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
27 #define OFF_T_MAX (~((off_t)1 << (sizeof (off_t)*8 - 1)))
31 #define OFF_T_MIN ((off_t)1 << (sizeof (off_t)*8 - 1))
34 #define DISK_BUF_PAGE_SHIFT 15 /* 32KB cache lines */
35 #define DISK_BUF_PAGE_SIZE (1 << DISK_BUF_PAGE_SHIFT)
36 #define DISK_BUF_PAGE_MASK (DISK_BUF_PAGE_SIZE-1)
40 DISK_BUF_NOTIFY_ERROR
= -1,
41 DISK_BUF_NOTIFY_NULL
= 0,
43 DISK_BUF_NOTIFY_TIMEDOUT
,
44 DISK_BUF_NOTIFY_PROCESS_EVENT
,
45 DISK_BUF_NOTIFY_REGISTERED
,
48 /** Macros to map file offsets to cached data **/
50 /* Returns a cache tag given a file offset */
51 #define MAP_OFFSET_TO_TAG(o) \
52 ((o) >> DISK_BUF_PAGE_SHIFT)
54 /* Returns the cache page number given a file offset */
55 #define MAP_OFFSET_TO_PAGE(o) \
56 (MAP_OFFSET_TO_TAG(o) % disk_buf.pgcount)
58 /* Returns the buffer offset given a file offset */
59 #define MAP_OFFSET_TO_BUFFER(o) \
60 (MAP_OFFSET_TO_PAGE(o) * DISK_BUF_PAGE_SIZE)
69 #define DISK_BUF_L2_CACHE_SHIFT 6
70 #define DISK_BUF_L2_CACHE_SIZE (1 << DISK_BUF_L2_CACHE_SHIFT)
71 #define DISK_BUF_L2_CACHE_MASK (DISK_BUF_L2_CACHE_SIZE-1)
75 off_t addr
; /* L2 file offset */
76 size_t size
; /* Real size */
77 uint8_t data
[DISK_BUF_L2_CACHE_SIZE
*2]; /* Local data and guard */
80 void dbuf_l2_init(struct dbuf_l2_cache
*l2_p
);
82 /* This object is an extension of the stream manager and handles some
83 * playback events as well as buffering */
87 struct event_queue
*q
;
88 uint8_t *start
; /* Start pointer */
89 uint8_t *end
; /* End of buffer pointer less MPEG_GUARDBUF_SIZE. The
90 guard space is used to wrap data at the buffer start to
91 pass continuous data packets */
92 uint8_t *tail
; /* Location of last data + 1 filled into the buffer */
93 ssize_t size
; /* The buffer length _not_ including the guard space (end-start) */
94 int pgcount
; /* Total number of available cached pages */
95 uint32_t *cache
; /* Pointer to cache structure - allocated on buffer */
96 int in_file
; /* File being read */
97 ssize_t filesize
; /* Size of file in_file in bytes */
98 int file_pages
; /* Number of pages in file (rounded up) */
99 off_t offset
; /* Current position (random access) */
100 off_t win_left
; /* Left edge of buffer window (streaming) */
101 off_t win_right
; /* Right edge of buffer window (streaming) */
102 uint32_t time_last
; /* Last time watermark was checked */
103 off_t pos_last
; /* Last position at watermark check time */
104 ssize_t low_wm
; /* The low watermark for automatic rebuffering */
105 int status
; /* Status as stream */
106 int state
; /* Current thread state */
107 bool need_seek
; /* Need to seek because a read was not contiguous */
110 extern struct disk_buf disk_buf SHAREDBSS_ATTR
;
113 bool disk_buf_is_data_ready(struct stream_hdr
*sh
, ssize_t margin
);
115 bool disk_buf_init(void);
116 void disk_buf_exit(void);
118 static inline int disk_buf_status(void)
119 { return disk_buf
.status
; }
121 int disk_buf_open(const char *filename
);
122 void disk_buf_close(void);
123 ssize_t
_disk_buf_getbuffer(size_t size
, void **pp
, void **pwrap
,
125 #define disk_buf_getbuffer(size, pp, pwrap, sizewrap) \
126 _disk_buf_getbuffer((size), PUN_PTR(void **, (pp)), \
127 PUN_PTR(void **, (pwrap)), (sizewrap))
129 ssize_t
_disk_buf_getbuffer_l2(struct dbuf_l2_cache
*l2
,
130 size_t size
, void **pp
);
131 #define disk_buf_getbuffer_l2(l2, size, pp) \
132 _disk_buf_getbuffer_l2((l2), (size), PUN_PTR(void **, (pp)))
134 ssize_t
disk_buf_read(void *buffer
, size_t size
);
135 ssize_t
disk_buf_lseek(off_t offset
, int whence
);
137 static inline off_t
disk_buf_ftell(void)
138 { return disk_buf
.offset
; }
140 static inline ssize_t
disk_buf_filesize(void)
141 { return disk_buf
.filesize
; }
143 ssize_t
disk_buf_prepare_streaming(off_t pos
, size_t len
);
144 ssize_t
disk_buf_set_streaming_window(off_t left
, off_t right
);
145 void * disk_buf_offset2ptr(off_t offset
);
146 int disk_buf_check_streaming_window(off_t left
, off_t right
);
148 intptr_t disk_buf_send_msg(long id
, intptr_t data
);
149 void disk_buf_post_msg(long id
, intptr_t data
);
150 void disk_buf_reply_msg(intptr_t retval
);
152 #endif /* DISK_BUF_H */