mpegplayer: Make playback engine fully seekable and frame-accurate and split into...
[Rockbox.git] / apps / plugins / mpegplayer / stream_thread.h
blob1962a668787abe4197be81b514158a9120653cdd
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Declarations for stream-specific threading
12 * Copyright (c) 2007 Michael Sevakis
14 * All files in this archive are subject to the GNU General Public License.
15 * See the file COPYING in the source tree root for full license agreement.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #ifndef STREAM_THREAD_H
22 #define STREAM_THREAD_H
24 #define PKT_HAS_TS 0x1
26 /* Stream header which is the minimum to receive asynchronous buffering
27 * notifications.
28 * Layed-out to allow streaming access after random-access parsing */
29 struct stream_hdr
31 struct event_queue *q; /* Communication queue - separate to allow it
32 to be placed in another section */
33 off_t win_left; /* Left position within data stream */
34 union
36 off_t win_right; /* Right position within data stream */
37 off_t pos; /* Start/current position for random-access read */
39 off_t limit; /* Limit for random-access read */
40 struct list_item nf; /* List for data notification */
43 struct stream
45 struct stream_hdr hdr; /* Base stream data */
46 struct thread_entry *thread; /* Stream's thread */
47 uint8_t* curr_packet; /* Current stream packet beginning */
48 uint8_t* curr_packet_end; /* Current stream packet end */
49 struct list_item l; /* List of streams - either reserve pool
50 or active pool */
51 int state; /* State machine parsing mode */
52 uint32_t start_pts; /* First timestamp for stream */
53 uint32_t end_pts; /* Last timestamp for stream */
54 uint32_t pts; /* Last presentation timestamp */
55 uint32_t pkt_flags; /* PKT_* flags */
56 unsigned id; /* Stream identifier */
59 /* Make sure there there is always enough data buffered ahead for
60 * the worst possible case - regardless of whether a valid stream
61 * would actually produce that */
62 #define MIN_BUFAHEAD (21+65535+6+65535+6) /* 131103 */
64 /* States that a stream's thread assumes internally */
65 enum thread_states
67 /* Stream thread... */
68 TSTATE_INIT = 0, /* is initialized and primed */
69 TSTATE_DATA, /* is awaiting data to be available */
70 TSTATE_BUFFERING, /* is buffering data */
71 TSTATE_EOS, /* has hit the end of data */
72 TSTATE_DECODE, /* is in a decoding state */
73 TSTATE_RENDER, /* is in a rendering state */
74 TSTATE_RENDER_WAIT, /* is waiting to render */
75 TSTATE_RENDER_WAIT_END, /* is waiting on remaining data */
78 /* Commands that streams respond to */
79 enum stream_message
81 STREAM_NULL = 0, /* A NULL message for whatever reason -
82 usually ignored */
83 STREAM_PLAY, /* Start playback at current position */
84 STREAM_PAUSE, /* Stop playing and await further commands */
85 STREAM_RESET, /* Reset the stream for a discontinuity */
86 STREAM_STOP, /* Stop stream - requires a reset later */
87 STREAM_SEEK, /* Seek the current stream to a new location */
88 STREAM_OPEN, /* Open a new file */
89 STREAM_CLOSE, /* Close the current file */
90 STREAM_QUIT, /* Exit the stream and thread */
91 STREAM_NEEDS_SYNC, /* Need to sync before stream decoding? */
92 STREAM_SYNC, /* Sync to the specified time from some key point */
93 STREAM_FIND_END_TIME, /* Get the exact end time of an elementary
94 * stream - ie. time just after last frame is finished */
95 /* Disk buffer */
96 STREAM_DISK_BUF_FIRST,
97 DISK_BUF_DATA_NOTIFY = STREAM_DISK_BUF_FIRST,
98 DISK_BUF_CLEAR_DATA_NOTIFY, /* Cancel pending data notification */
99 DISK_BUF_CACHE_RANGE, /* Cache a range of the file in the buffer */
100 /* Audio stream */
101 STREAM_AUDIO_FIRST,
102 /* Video stream */
103 STREAM_VIDEO_FIRST,
104 VIDEO_DISPLAY_SHOW = STREAM_VIDEO_FIRST, /* Show/hide video output */
105 VIDEO_DISPLAY_IS_VISIBLE, /* Is the video output visible? */
106 VIDEO_GET_SIZE, /* Get the video dimensions */
107 VIDEO_PRINT_FRAME, /* Print the frame at the current position */
108 VIDEO_PRINT_THUMBNAIL, /* Print a thumbnail of the current position */
109 #ifdef GRAY_CACHE_MAINT
110 VIDEO_GRAY_CACHEOP,
111 #endif
112 STREAM_MESSAGE_LAST,
115 /* Data parameter for STREAM_SEEK */
116 struct stream_seek_data
118 uint32_t time; /* Time to seek to/by */
119 int whence; /* Specification of relationship to current position/file */
122 /* Data parameter for STREAM_SYNC */
123 struct str_sync_data
125 uint32_t time; /* Time to sync to */
126 struct stream_scan sk; /* Specification of start/limits/direction */
129 /* Stream status codes - not eqivalent to thread states */
130 enum stream_status
132 /* Stream status is... */
133 STREAM_DATA_END = -4, /* Stream has ended */
134 STREAM_DATA_NOT_READY = -3, /* Data was not available yet */
135 STREAM_UNSUPPORTED = -2, /* Format is unsupported */
136 STREAM_ERROR = -1, /* some kind of error - quit it or reset it */
137 STREAM_OK = 0, /* General inequality for success >= is OK, < error */
138 STREAM_STOPPED = 0, /* stopped and awaiting commands - send STREAM_INIT */
139 STREAM_PLAYING, /* playing and rendering its data */
140 STREAM_PAUSED, /* paused and awaiting commands */
141 /* Other status codes (> STREAM_OK) */
142 STREAM_MATCH, /* A good match was found */
143 STREAM_PERFECT_MATCH, /* Exactly what was wanted was found or
144 no better match is possible */
145 STREAM_NOT_FOUND, /* Match not found */
148 #define STR_FROM_HEADER(sh) ((struct stream *)(sh))
150 /* Clip time to range for a particular stream */
151 static inline uint32_t clip_time(struct stream *str, uint32_t time)
153 if (time < str->start_pts)
154 time = str->start_pts;
155 else if (time >= str->end_pts)
156 time = str->end_pts;
158 return time;
161 extern struct stream video_str IBSS_ATTR;
162 extern struct stream audio_str IBSS_ATTR;
164 bool video_thread_init(void);
165 void video_thread_exit(void);
166 bool audio_thread_init(void);
167 void audio_thread_exit(void);
169 /* Some queue function wrappers to keep things clean-ish */
171 /* For stream use only */
172 static inline bool str_have_msg(struct stream *str)
173 { return !rb->queue_empty(str->hdr.q); }
175 static inline void str_get_msg(struct stream *str, struct queue_event *ev)
176 { rb->queue_wait(str->hdr.q, ev); }
178 static inline void str_get_msg_w_tmo(struct stream *str, struct queue_event *ev,
179 int timeout)
180 { rb->queue_wait_w_tmo(str->hdr.q, ev, timeout); }
182 static inline void str_reply_msg(struct stream *str, intptr_t reply)
183 { rb->queue_reply(str->hdr.q, reply); }
185 /* Public use */
186 static inline intptr_t str_send_msg(struct stream *str, long id, intptr_t data)
187 { return rb->queue_send(str->hdr.q, id, data); }
189 static inline void str_post_msg(struct stream *str, long id, intptr_t data)
190 { rb->queue_post(str->hdr.q, id, data); }
192 #endif /* STREAM_THREAD_H */