xref: /linux/include/media/dvb_demux.h (revision 9a379e77033f02c4a071891afdf0f0a01eff8ccb)
1 /*
2  * dvb_demux.h: DVB kernel demux API
3  *
4  * Copyright (C) 2000-2001 Marcus Metzler & Ralph Metzler
5  *                         for convergence integrated media GmbH
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  */
18 
19 #ifndef _DVB_DEMUX_H_
20 #define _DVB_DEMUX_H_
21 
22 #include <linux/time.h>
23 #include <linux/timer.h>
24 #include <linux/spinlock.h>
25 #include <linux/mutex.h>
26 
27 #include <media/demux.h>
28 
29 /**
30  * enum dvb_dmx_filter_type - type of demux feed.
31  *
32  * @DMX_TYPE_TS:	feed is in TS mode.
33  * @DMX_TYPE_SEC:	feed is in Section mode.
34  */
35 enum dvb_dmx_filter_type {
36 	DMX_TYPE_TS,
37 	DMX_TYPE_SEC,
38 };
39 
40 /**
41  * enum dvb_dmx_state - state machine for a demux filter.
42  *
43  * @DMX_STATE_FREE:		indicates that the filter is freed.
44  * @DMX_STATE_ALLOCATED:	indicates that the filter was allocated
45  *				to be used.
46  * @DMX_STATE_READY:		indicates that the filter is ready
47  *				to be used.
48  * @DMX_STATE_GO:		indicates that the filter is running.
49  */
50 enum dvb_dmx_state {
51 	DMX_STATE_FREE,
52 	DMX_STATE_ALLOCATED,
53 	DMX_STATE_READY,
54 	DMX_STATE_GO,
55 };
56 
57 #define DVB_DEMUX_MASK_MAX 18
58 
59 #define MAX_PID 0x1fff
60 
61 #define SPEED_PKTS_INTERVAL 50000
62 
63 /**
64  * struct dvb_demux_filter - Describes a DVB demux section filter.
65  *
66  * @filter:		Section filter as defined by &struct dmx_section_filter.
67  * @maskandmode:	logical ``and`` bit mask.
68  * @maskandnotmode:	logical ``and not`` bit mask.
69  * @doneq:		flag that indicates when a filter is ready.
70  * @next:		pointer to the next section filter.
71  * @feed:		&struct dvb_demux_feed pointer.
72  * @index:		index of the used demux filter.
73  * @state:		state of the filter as described by &enum dvb_dmx_state.
74  * @type:		type of the filter as described
75  *			by &enum dvb_dmx_filter_type.
76  */
77 
78 struct dvb_demux_filter {
79 	struct dmx_section_filter filter;
80 	u8 maskandmode[DMX_MAX_FILTER_SIZE];
81 	u8 maskandnotmode[DMX_MAX_FILTER_SIZE];
82 	bool doneq;
83 
84 	struct dvb_demux_filter *next;
85 	struct dvb_demux_feed *feed;
86 	int index;
87 	enum dvb_dmx_state state;
88 	enum dvb_dmx_filter_type type;
89 
90 	/* private: used only by av7110 */
91 	u16 hw_handle;
92 };
93 
94 /**
95  * struct dvb_demux_feed - describes a DVB field
96  *
97  * @feed:	a union describing a digital TV feed.
98  *		Depending on the feed type, it can be either
99  *		@feed.ts or @feed.sec.
100  * @feed.ts:	a &struct dmx_ts_feed pointer.
101  *		For TS feed only.
102  * @feed.sec:	a &struct dmx_section_feed pointer.
103  *		For section feed only.
104  * @cb:		a union describing digital TV callbacks.
105  *		Depending on the feed type, it can be either
106  *		@cb.ts or @cb.sec.
107  * @cb.ts:	a dmx_ts_cb() calback function pointer.
108  *		For TS feed only.
109  * @cb.sec:	a dmx_section_cb() callback function pointer.
110  *		For section feed only.
111  * @demux:	pointer to &struct dvb_demux.
112  * @priv:	private data that can optionally be used by a DVB driver.
113  * @type:	type of the filter, as defined by &enum dvb_dmx_filter_type.
114  * @state:	state of the filter as defined by &enum dvb_dmx_state.
115  * @pid:	PID to be filtered.
116  * @timeout:	feed timeout.
117  * @filter:	pointer to &struct dvb_demux_filter.
118  * @ts_type:	type of TS, as defined by &enum ts_filter_type.
119  * @pes_type:	type of PES, as defined by &enum dmx_ts_pes.
120  * @cc:		MPEG-TS packet continuity counter
121  * @pusi_seen:	if true, indicates that a discontinuity was detected.
122  *		it is used to prevent feeding of garbage from previous section.
123  * @peslen:	length of the PES (Packet Elementary Stream).
124  * @list_head:	head for the list of digital TV demux feeds.
125  * @index:	a unique index for each feed. Can be used as hardware
126  *		pid filter index.
127  */
128 struct dvb_demux_feed {
129 	union {
130 		struct dmx_ts_feed ts;
131 		struct dmx_section_feed sec;
132 	} feed;
133 
134 	union {
135 		dmx_ts_cb ts;
136 		dmx_section_cb sec;
137 	} cb;
138 
139 	struct dvb_demux *demux;
140 	void *priv;
141 	enum dvb_dmx_filter_type type;
142 	enum dvb_dmx_state state;
143 	u16 pid;
144 
145 	ktime_t timeout;
146 	struct dvb_demux_filter *filter;
147 
148 	enum ts_filter_type ts_type;
149 	enum dmx_ts_pes pes_type;
150 
151 	int cc;
152 	bool pusi_seen;
153 
154 	u16 peslen;
155 
156 	struct list_head list_head;
157 	unsigned int index;
158 };
159 
160 /**
161  * struct dvb_demux - represents a digital TV demux
162  * @dmx:		embedded &struct dmx_demux with demux capabilities
163  *			and callbacks.
164  * @priv:		private data that can optionally be used by
165  *			a DVB driver.
166  * @filternum:		maximum amount of DVB filters.
167  * @feednum:		maximum amount of DVB feeds.
168  * @start_feed:		callback routine to be called in order to start
169  *			a DVB feed.
170  * @stop_feed:		callback routine to be called in order to stop
171  *			a DVB feed.
172  * @write_to_decoder:	callback routine to be called if the feed is TS and
173  *			it is routed to an A/V decoder, when a new TS packet
174  *			is received.
175  *			Used only on av7110-av.c.
176  * @check_crc32:	callback routine to check CRC. If not initialized,
177  *			dvb_demux will use an internal one.
178  * @memcopy:		callback routine to memcopy received data.
179  *			If not initialized, dvb_demux will default to memcpy().
180  * @users:		counter for the number of demux opened file descriptors.
181  *			Currently, it is limited to 10 users.
182  * @filter:		pointer to &struct dvb_demux_filter.
183  * @feed:		pointer to &struct dvb_demux_feed.
184  * @frontend_list:	&struct list_head with frontends used by the demux.
185  * @pesfilter:		array of &struct dvb_demux_feed with the PES types
186  *			that will be filtered.
187  * @pids:		list of filtered program IDs.
188  * @feed_list:		&struct list_head with feeds.
189  * @tsbuf:		temporary buffer used internally to store TS packets.
190  * @tsbufp:		temporary buffer index used internally.
191  * @mutex:		pointer to &struct mutex used to protect feed set
192  *			logic.
193  * @lock:		pointer to &spinlock_t, used to protect buffer handling.
194  * @cnt_storage:	buffer used for TS/TEI continuity check.
195  * @speed_last_time:	&ktime_t used for TS speed check.
196  * @speed_pkts_cnt:	packets count used for TS speed check.
197  */
198 struct dvb_demux {
199 	struct dmx_demux dmx;
200 	void *priv;
201 	int filternum;
202 	int feednum;
203 	int (*start_feed)(struct dvb_demux_feed *feed);
204 	int (*stop_feed)(struct dvb_demux_feed *feed);
205 	int (*write_to_decoder)(struct dvb_demux_feed *feed,
206 				 const u8 *buf, size_t len);
207 	u32 (*check_crc32)(struct dvb_demux_feed *feed,
208 			    const u8 *buf, size_t len);
209 	void (*memcopy)(struct dvb_demux_feed *feed, u8 *dst,
210 			 const u8 *src, size_t len);
211 
212 	int users;
213 #define MAX_DVB_DEMUX_USERS 10
214 	struct dvb_demux_filter *filter;
215 	struct dvb_demux_feed *feed;
216 
217 	struct list_head frontend_list;
218 
219 	struct dvb_demux_feed *pesfilter[DMX_PES_OTHER];
220 	u16 pids[DMX_PES_OTHER];
221 
222 #define DMX_MAX_PID 0x2000
223 	struct list_head feed_list;
224 	u8 tsbuf[204];
225 	int tsbufp;
226 
227 	struct mutex mutex;
228 	spinlock_t lock;
229 
230 	uint8_t *cnt_storage; /* for TS continuity check */
231 
232 	ktime_t speed_last_time; /* for TS speed check */
233 	uint32_t speed_pkts_cnt; /* for TS speed check */
234 
235 	/* private: used only on av7110 */
236 	int playing;
237 	int recording;
238 };
239 
240 /**
241  * dvb_dmx_init - initialize a digital TV demux struct.
242  *
243  * @demux: &struct dvb_demux to be initialized.
244  *
245  * Before being able to register a digital TV demux struct, drivers
246  * should call this routine. On its typical usage, some fields should
247  * be initialized at the driver before calling it.
248  *
249  * A typical usecase is::
250  *
251  *	dvb->demux.dmx.capabilities =
252  *		DMX_TS_FILTERING | DMX_SECTION_FILTERING |
253  *		DMX_MEMORY_BASED_FILTERING;
254  *	dvb->demux.priv       = dvb;
255  *	dvb->demux.filternum  = 256;
256  *	dvb->demux.feednum    = 256;
257  *	dvb->demux.start_feed = driver_start_feed;
258  *	dvb->demux.stop_feed  = driver_stop_feed;
259  *	ret = dvb_dmx_init(&dvb->demux);
260  *	if (ret < 0)
261  *		return ret;
262  */
263 int dvb_dmx_init(struct dvb_demux *demux);
264 
265 /**
266  * dvb_dmx_release - releases a digital TV demux internal buffers.
267  *
268  * @demux: &struct dvb_demux to be released.
269  *
270  * The DVB core internally allocates data at @demux. This routine
271  * releases those data. Please notice that the struct itelf is not
272  * released, as it can be embedded on other structs.
273  */
274 void dvb_dmx_release(struct dvb_demux *demux);
275 
276 /**
277  * dvb_dmx_swfilter_packets - use dvb software filter for a buffer with
278  *	multiple MPEG-TS packets with 188 bytes each.
279  *
280  * @demux: pointer to &struct dvb_demux
281  * @buf: buffer with data to be filtered
282  * @count: number of MPEG-TS packets with size of 188.
283  *
284  * The routine will discard a DVB packet that don't start with 0x47.
285  *
286  * Use this routine if the DVB demux fills MPEG-TS buffers that are
287  * already aligned.
288  *
289  * NOTE: The @buf size should have size equal to ``count * 188``.
290  */
291 void dvb_dmx_swfilter_packets(struct dvb_demux *demux, const u8 *buf,
292 			      size_t count);
293 
294 /**
295  * dvb_dmx_swfilter -  use dvb software filter for a buffer with
296  *	multiple MPEG-TS packets with 188 bytes each.
297  *
298  * @demux: pointer to &struct dvb_demux
299  * @buf: buffer with data to be filtered
300  * @count: number of MPEG-TS packets with size of 188.
301  *
302  * If a DVB packet doesn't start with 0x47, it will seek for the first
303  * byte that starts with 0x47.
304  *
305  * Use this routine if the DVB demux fill buffers that may not start with
306  * a packet start mark (0x47).
307  *
308  * NOTE: The @buf size should have size equal to ``count * 188``.
309  */
310 void dvb_dmx_swfilter(struct dvb_demux *demux, const u8 *buf, size_t count);
311 
312 /**
313  * dvb_dmx_swfilter_204 -  use dvb software filter for a buffer with
314  *	multiple MPEG-TS packets with 204 bytes each.
315  *
316  * @demux: pointer to &struct dvb_demux
317  * @buf: buffer with data to be filtered
318  * @count: number of MPEG-TS packets with size of 204.
319  *
320  * If a DVB packet doesn't start with 0x47, it will seek for the first
321  * byte that starts with 0x47.
322  *
323  * Use this routine if the DVB demux fill buffers that may not start with
324  * a packet start mark (0x47).
325  *
326  * NOTE: The @buf size should have size equal to ``count * 204``.
327  */
328 void dvb_dmx_swfilter_204(struct dvb_demux *demux, const u8 *buf,
329 			  size_t count);
330 
331 /**
332  * dvb_dmx_swfilter_raw -  make the raw data available to userspace without
333  *	filtering
334  *
335  * @demux: pointer to &struct dvb_demux
336  * @buf: buffer with data
337  * @count: number of packets to be passed. The actual size of each packet
338  *	depends on the &dvb_demux->feed->cb.ts logic.
339  *
340  * Use it if the driver needs to deliver the raw payload to userspace without
341  * passing through the kernel demux. That is meant to support some
342  * delivery systems that aren't based on MPEG-TS.
343  *
344  * This function relies on &dvb_demux->feed->cb.ts to actually handle the
345  * buffer.
346  */
347 void dvb_dmx_swfilter_raw(struct dvb_demux *demux, const u8 *buf,
348 			  size_t count);
349 
350 #endif /* _DVB_DEMUX_H_ */
351