blob: 7ba053d2badf9bd084db0f860036f998c0205983 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 * You should have received a copy of the GNU Lesser General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 *
21 */
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#ifndef _DVB_DEMUX_H_
24#define _DVB_DEMUX_H_
25
26#include <linux/time.h>
27#include <linux/timer.h>
28#include <linux/spinlock.h>
Ingo Molnar3593cab2006-02-07 06:49:14 -020029#include <linux/mutex.h>
Udaya Bhaskara Reddy Mallavarapuba29a3e82017-06-12 14:57:05 +053030#include <linux/debugfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#include "demux.h"
33
34#define DMX_TYPE_TS 0
35#define DMX_TYPE_SEC 1
36#define DMX_TYPE_PES 2
37
38#define DMX_STATE_FREE 0
39#define DMX_STATE_ALLOCATED 1
40#define DMX_STATE_SET 2
41#define DMX_STATE_READY 3
42#define DMX_STATE_GO 4
43
44#define DVB_DEMUX_MASK_MAX 18
45
Abylay Ospan0d834632009-06-06 09:31:34 -030046#define MAX_PID 0x1fff
47
Udaya Bhaskara Reddy Mallavarapuba29a3e82017-06-12 14:57:05 +053048#define TIMESTAMP_LEN 4
49
Abylay Ospan26b9d6c2009-11-01 18:46:53 -030050#define SPEED_PKTS_INTERVAL 50000
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052struct dvb_demux_filter {
Andreas Oberritterdad4a732005-09-09 13:02:26 -070053 struct dmx_section_filter filter;
54 u8 maskandmode[DMX_MAX_FILTER_SIZE];
55 u8 maskandnotmode[DMX_MAX_FILTER_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 int doneq;
57
Andreas Oberritterdad4a732005-09-09 13:02:26 -070058 struct dvb_demux_filter *next;
59 struct dvb_demux_feed *feed;
60 int index;
61 int state;
62 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Andreas Oberritterdad4a732005-09-09 13:02:26 -070064 u16 hw_handle;
65 struct timer_list timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066};
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#define DMX_FEED_ENTRY(pos) list_entry(pos, struct dvb_demux_feed, list_head)
69
Udaya Bhaskara Reddy Mallavarapuba29a3e82017-06-12 14:57:05 +053070
71struct dmx_index_entry {
72 struct dmx_index_event_info event;
73 struct list_head next;
74};
75
76#define DMX_IDX_EVENT_QUEUE_SIZE DMX_EVENT_QUEUE_SIZE
77
78struct dvb_demux_rec_info {
79 /* Reference counter for number of feeds using this information */
80 int ref_count;
81
82 /* Counter for number of TS packets output to recording buffer */
83 u64 ts_output_count;
84
85 /* Indexing information */
86 struct {
87 /*
88 * Minimum TS packet number encountered in recording filter
89 * among all feeds that search for video patterns
90 */
91 u64 min_pattern_tsp_num;
92
93 /* Number of indexing-enabled feeds */
94 u8 indexing_feeds_num;
95
96 /* Number of feeds with video pattern search request */
97 u8 pattern_search_feeds_num;
98
99 /* Index entries pool */
100 struct dmx_index_entry events[DMX_IDX_EVENT_QUEUE_SIZE];
101
102 /* List of free entries that can be used for new index events */
103 struct list_head free_list;
104
105 /* List holding ready index entries not notified to user yet */
106 struct list_head ready_list;
107 } idx_info;
108};
109
110#define DVB_DMX_MAX_PATTERN_LEN 6
111struct dvb_dmx_video_patterns {
112 /* the byte pattern to look for */
113 u8 pattern[DVB_DMX_MAX_PATTERN_LEN];
114
115 /* the byte mask to use (same length as pattern) */
116 u8 mask[DVB_DMX_MAX_PATTERN_LEN];
117
118 /* the length of the pattern, in bytes */
119 size_t size;
120
121 /* the type of the pattern. One of DMX_IDX_* definitions */
122 u64 type;
123};
124
125#define DVB_DMX_MAX_FOUND_PATTERNS 20
126#define DVB_DMX_MAX_SEARCH_PATTERN_NUM 20
127struct dvb_dmx_video_prefix_size_masks {
128 /*
129 * a bit mask (per pattern) of possible prefix sizes to use
130 * when searching for a pattern that started in the previous TS packet.
131 * Updated by dvb_dmx_video_pattern_search for use in the next lookup.
132 */
133 u32 size_mask[DVB_DMX_MAX_FOUND_PATTERNS];
134};
135
136struct dvb_dmx_video_patterns_results {
137 struct {
138 /*
139 * The offset in the buffer where the pattern was found.
140 * If a pattern is found using a prefix (i.e. started on the
141 * previous buffer), offset is zero.
142 */
143 u32 offset;
144
145 /*
146 * The type of the pattern found.
147 * One of DMX_IDX_* definitions.
148 */
149 u64 type;
150
151 /* The prefix size that was used to find this pattern */
152 u32 used_prefix_size;
153 } info[DVB_DMX_MAX_FOUND_PATTERNS];
154};
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156struct dvb_demux_feed {
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700157 union {
158 struct dmx_ts_feed ts;
159 struct dmx_section_feed sec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 } feed;
161
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700162 union {
163 dmx_ts_cb ts;
164 dmx_section_cb sec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 } cb;
166
Udaya Bhaskara Reddy Mallavarapuba29a3e82017-06-12 14:57:05 +0530167 union {
168 dmx_ts_data_ready_cb ts;
169 dmx_section_data_ready_cb sec;
170 } data_ready_cb;
171
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700172 struct dvb_demux *demux;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 void *priv;
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700174 int type;
175 int state;
176 u16 pid;
177 u8 *buffer;
178 int buffer_size;
Udaya Bhaskara Reddy Mallavarapuba29a3e82017-06-12 14:57:05 +0530179 enum dmx_tsp_format_t tsp_out_format;
180 struct dmx_secure_mode secure_mode;
181 struct dmx_cipher_operations cipher_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Arnd Bergmanne95be152016-06-17 17:46:28 -0300183 ktime_t timeout;
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700184 struct dvb_demux_filter *filter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700186 int ts_type;
187 enum dmx_ts_pes pes_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700189 int cc;
Udaya Bhaskara Reddy Mallavarapuba29a3e82017-06-12 14:57:05 +0530190 int first_cc;
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700191 int pusi_seen; /* prevents feeding of garbage from previous section */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Udaya Bhaskara Reddy Mallavarapuba29a3e82017-06-12 14:57:05 +0530193 u8 scrambling_bits;
194
195 struct dvb_demux_rec_info *rec_info;
196 u64 prev_tsp_num;
197 u64 prev_stc;
198 u64 curr_pusi_tsp_num;
199 u64 prev_pusi_tsp_num;
200 int prev_frame_valid;
201 u64 prev_frame_type;
202 int first_frame_in_seq;
203 int first_frame_in_seq_notified;
204 u64 last_pattern_tsp_num;
205 int pattern_num;
206const struct dvb_dmx_video_patterns *patterns[DVB_DMX_MAX_SEARCH_PATTERN_NUM];
207 struct dvb_dmx_video_prefix_size_masks prefix_size;
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700208 u16 peslen;
Udaya Bhaskara Reddy Mallavarapuba29a3e82017-06-12 14:57:05 +0530209 u32 pes_tei_counter;
210 u32 pes_cont_err_counter;
211 u32 pes_ts_packets_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 struct list_head list_head;
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700214 unsigned int index; /* a unique index for each feed (can be used as hardware pid filter index) */
Udaya Bhaskara Reddy Mallavarapuba29a3e82017-06-12 14:57:05 +0530215
216 enum dmx_video_codec video_codec;
217 struct dmx_indexing_params idx_params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218};
219
220struct dvb_demux {
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700221 struct dmx_demux dmx;
222 void *priv;
223 int filternum;
224 int feednum;
225 int (*start_feed)(struct dvb_demux_feed *feed);
226 int (*stop_feed)(struct dvb_demux_feed *feed);
227 int (*write_to_decoder)(struct dvb_demux_feed *feed,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 const u8 *buf, size_t len);
Udaya Bhaskara Reddy Mallavarapuba29a3e82017-06-12 14:57:05 +0530229 int (*decoder_fullness_init)(struct dvb_demux_feed *feed);
230 int (*decoder_fullness_wait)(struct dvb_demux_feed *feed,
231 size_t required_space);
232 int (*decoder_fullness_abort)(struct dvb_demux_feed *feed);
233 int (*decoder_buffer_status)(struct dvb_demux_feed *feed,
234 struct dmx_buffer_status *dmx_buffer_status);
235 int (*reuse_decoder_buffer)(struct dvb_demux_feed *feed,
236 int cookie);
237 int (*set_cipher_op)(struct dvb_demux_feed *feed,
238 struct dmx_cipher_operations *cipher_ops);
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700239 u32 (*check_crc32)(struct dvb_demux_feed *feed,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 const u8 *buf, size_t len);
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700241 void (*memcopy)(struct dvb_demux_feed *feed, u8 *dst,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 const u8 *src, size_t len);
Udaya Bhaskara Reddy Mallavarapuba29a3e82017-06-12 14:57:05 +0530243 int (*oob_command)(struct dvb_demux_feed *feed,
244 struct dmx_oob_command *cmd);
245 void (*convert_ts)(struct dvb_demux_feed *feed,
246 const u8 timestamp[TIMESTAMP_LEN],
247 u64 *timestampIn27Mhz);
248 int (*set_indexing)(struct dvb_demux_feed *feed);
249 int (*flush_decoder_buffer)(struct dvb_demux_feed *feed, size_t length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700251 int users;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252#define MAX_DVB_DEMUX_USERS 10
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700253 struct dvb_demux_filter *filter;
254 struct dvb_demux_feed *feed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700256 struct list_head frontend_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Mauro Carvalho Chehabfde04ab2013-04-04 13:25:30 -0300258 struct dvb_demux_feed *pesfilter[DMX_PES_OTHER];
259 u16 pids[DMX_PES_OTHER];
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700260 int playing;
261 int recording;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263#define DMX_MAX_PID 0x2000
264 struct list_head feed_list;
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700265 u8 tsbuf[204];
266 int tsbufp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Ingo Molnar3593cab2006-02-07 06:49:14 -0200268 struct mutex mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 spinlock_t lock;
Abylay Ospan0d834632009-06-06 09:31:34 -0300270
271 uint8_t *cnt_storage; /* for TS continuity check */
Abylay Ospan26b9d6c2009-11-01 18:46:53 -0300272
Arnd Bergmanne95be152016-06-17 17:46:28 -0300273 ktime_t speed_last_time; /* for TS speed check */
Abylay Ospan26b9d6c2009-11-01 18:46:53 -0300274 uint32_t speed_pkts_cnt; /* for TS speed check */
Udaya Bhaskara Reddy Mallavarapuba29a3e82017-06-12 14:57:05 +0530275
276 enum dmx_tsp_format_t tsp_format;
277 size_t ts_packet_size;
278
279 enum dmx_playback_mode_t playback_mode;
280 int sw_filter_abort;
281
282 struct {
283 dmx_ts_fullness ts;
284 dmx_section_fullness sec;
285 } buffer_ctrl;
286
287 struct dvb_demux_rec_info *rec_info_pool;
288
289 /*
290 * the following is used for debugfs exposing info
291 * about dvb demux performance.
292 */
293#define MAX_DVB_DEMUX_NAME_LEN 10
294 char alias[MAX_DVB_DEMUX_NAME_LEN];
295
296 u32 total_process_time;
297 u32 total_crc_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298};
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300int dvb_dmx_init(struct dvb_demux *dvbdemux);
Andreas Oberritter93653462005-09-09 13:02:23 -0700301void dvb_dmx_release(struct dvb_demux *dvbdemux);
Udaya Bhaskara Reddy Mallavarapuba29a3e82017-06-12 14:57:05 +0530302int dvb_dmx_swfilter_section_packet(struct dvb_demux_feed *feed, const u8 *buf,
303 int should_lock);
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700304void dvb_dmx_swfilter_packets(struct dvb_demux *dvbdmx, const u8 *buf,
305 size_t count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306void dvb_dmx_swfilter(struct dvb_demux *demux, const u8 *buf, size_t count);
Andreas Oberritterdad4a732005-09-09 13:02:26 -0700307void dvb_dmx_swfilter_204(struct dvb_demux *demux, const u8 *buf,
308 size_t count);
Michael Krufky8e156702011-08-27 18:46:37 -0300309void dvb_dmx_swfilter_raw(struct dvb_demux *demux, const u8 *buf,
310 size_t count);
Udaya Bhaskara Reddy Mallavarapuba29a3e82017-06-12 14:57:05 +0530311void dvb_dmx_swfilter_format(
312 struct dvb_demux *demux, const u8 *buf,
313 size_t count,
314 enum dmx_tsp_format_t tsp_format);
315void dvb_dmx_swfilter_packet(struct dvb_demux *demux, const u8 *buf,
316 const u8 timestamp[TIMESTAMP_LEN]);
317const struct dvb_dmx_video_patterns *dvb_dmx_get_pattern(u64 dmx_idx_pattern);
318int dvb_dmx_video_pattern_search(
319 const struct dvb_dmx_video_patterns
320 *patterns[DVB_DMX_MAX_SEARCH_PATTERN_NUM],
321 int patterns_num,
322 const u8 *buf, size_t buf_size,
323 struct dvb_dmx_video_prefix_size_masks *prefix_size_masks,
324 struct dvb_dmx_video_patterns_results *results);
325int dvb_demux_push_idx_event(struct dvb_demux_feed *feed,
326 struct dmx_index_event_info *idx_event, int should_lock);
327void dvb_dmx_process_idx_pattern(struct dvb_demux_feed *feed,
328 struct dvb_dmx_video_patterns_results *patterns, int pattern,
329 u64 curr_stc, u64 prev_stc,
330 u64 curr_match_tsp, u64 prev_match_tsp,
331 u64 curr_pusi_tsp, u64 prev_pusi_tsp);
332void dvb_dmx_notify_idx_events(struct dvb_demux_feed *feed, int should_lock);
333int dvb_dmx_notify_section_event(struct dvb_demux_feed *feed,
334 struct dmx_data_ready *event, int should_lock);
335void dvbdmx_ts_reset_pes_state(struct dvb_demux_feed *feed);
336
337/**
338 * dvb_dmx_is_video_feed - Returns whether the PES feed
339 * is video one.
340 *
341 * @feed: The feed to be checked.
342 *
343 * Return 1 if feed is video feed, 0 otherwise.
344 */
345static inline int dvb_dmx_is_video_feed(struct dvb_demux_feed *feed)
346{
347 if (feed->type != DMX_TYPE_TS)
348 return 0;
349
350 if (feed->ts_type & (~TS_DECODER))
351 return 0;
352
353 if ((feed->pes_type == DMX_PES_VIDEO0) ||
354 (feed->pes_type == DMX_PES_VIDEO1) ||
355 (feed->pes_type == DMX_PES_VIDEO2) ||
356 (feed->pes_type == DMX_PES_VIDEO3))
357 return 1;
358
359 return 0;
360}
361
362/**
363 * dvb_dmx_is_audio_feed - Returns whether the PES feed
364 * is audio one.
365 *
366 * @feed: The feed to be checked.
367 *
368 * Return 1 if feed is audio feed, 0 otherwise.
369 */
370static inline int dvb_dmx_is_audio_feed(struct dvb_demux_feed *feed)
371{
372 if (feed->type != DMX_TYPE_TS)
373 return 0;
374
375 if (feed->ts_type & (~TS_DECODER))
376 return 0;
377
378 if ((feed->pes_type == DMX_PES_AUDIO0) ||
379 (feed->pes_type == DMX_PES_AUDIO1) ||
380 (feed->pes_type == DMX_PES_AUDIO2) ||
381 (feed->pes_type == DMX_PES_AUDIO3))
382 return 1;
383
384 return 0;
385}
386
387/**
388 * dvb_dmx_is_pcr_feed - Returns whether the PES feed
389 * is PCR one.
390 *
391 * @feed: The feed to be checked.
392 *
393 * Return 1 if feed is PCR feed, 0 otherwise.
394 */
395static inline int dvb_dmx_is_pcr_feed(struct dvb_demux_feed *feed)
396{
397 if (feed->type != DMX_TYPE_TS)
398 return 0;
399
400 if (feed->ts_type & (~TS_DECODER))
401 return 0;
402
403 if ((feed->pes_type == DMX_PES_PCR0) ||
404 (feed->pes_type == DMX_PES_PCR1) ||
405 (feed->pes_type == DMX_PES_PCR2) ||
406 (feed->pes_type == DMX_PES_PCR3))
407 return 1;
408
409 return 0;
410}
411
412/**
413 * dvb_dmx_is_sec_feed - Returns whether this is a section feed
414 *
415 * @feed: The feed to be checked.
416 *
417 * Return 1 if feed is a section feed, 0 otherwise.
418 */
419static inline int dvb_dmx_is_sec_feed(struct dvb_demux_feed *feed)
420{
421 return (feed->type == DMX_TYPE_SEC);
422}
423
424/**
425 * dvb_dmx_is_rec_feed - Returns whether this is a recording feed
426 *
427 * @feed: The feed to be checked.
428 *
429 * Return 1 if feed is recording feed, 0 otherwise.
430 */
431static inline int dvb_dmx_is_rec_feed(struct dvb_demux_feed *feed)
432{
433 if (feed->type != DMX_TYPE_TS)
434 return 0;
435
436 if (feed->ts_type & (TS_DECODER | TS_PAYLOAD_ONLY))
437 return 0;
438
439 return 1;
440}
441
442static inline u16 ts_pid(const u8 *buf)
443{
444 return ((buf[1] & 0x1f) << 8) + buf[2];
445}
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448#endif /* _DVB_DEMUX_H_ */