demux.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. * demux.h
  3. *
  4. * Copyright (c) 2002 Convergence GmbH
  5. *
  6. * based on code:
  7. * Copyright (c) 2000 Nokia Research Center
  8. * Tampere, FINLAND
  9. *
  10. * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Lesser General Public License
  14. * as published by the Free Software Foundation; either version 2.1
  15. * of the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  25. *
  26. */
  27. #ifndef __DEMUX_H
  28. #define __DEMUX_H
  29. #include <linux/types.h>
  30. #include <linux/errno.h>
  31. #include <linux/list.h>
  32. #include <linux/time.h>
  33. #include <linux/dvb/dmx.h>
  34. /*--------------------------------------------------------------------------*/
  35. /* Common definitions */
  36. /*--------------------------------------------------------------------------*/
  37. /*
  38. * DMX_MAX_FILTER_SIZE: Maximum length (in bytes) of a section/PES filter.
  39. */
  40. #ifndef DMX_MAX_FILTER_SIZE
  41. #define DMX_MAX_FILTER_SIZE 18
  42. #endif
  43. /*
  44. * DMX_MAX_SECFEED_SIZE: Maximum length (in bytes) of a private section feed filter.
  45. */
  46. #ifndef DMX_MAX_SECTION_SIZE
  47. #define DMX_MAX_SECTION_SIZE 4096
  48. #endif
  49. #ifndef DMX_MAX_SECFEED_SIZE
  50. #define DMX_MAX_SECFEED_SIZE (DMX_MAX_SECTION_SIZE + 188)
  51. #endif
  52. /*
  53. * enum dmx_success: Success codes for the Demux Callback API.
  54. */
  55. enum dmx_success {
  56. DMX_OK = 0, /* Received Ok */
  57. DMX_OK_PES_END, /* Received OK, data reached end of PES packet */
  58. DMX_OK_PCR, /* Received OK, data with new PCR/STC pair */
  59. DMX_OK_EOS, /* Received OK, reached End-of-Stream (EOS) */
  60. DMX_OK_MARKER, /* Received OK, reached a data Marker */
  61. DMX_LENGTH_ERROR, /* Incorrect length */
  62. DMX_OVERRUN_ERROR, /* Receiver ring buffer overrun */
  63. DMX_CRC_ERROR, /* Incorrect CRC */
  64. DMX_FRAME_ERROR, /* Frame alignment error */
  65. DMX_FIFO_ERROR, /* Receiver FIFO overrun */
  66. DMX_MISSED_ERROR, /* Receiver missed packet */
  67. DMX_OK_DECODER_BUF, /* Received OK, new ES data in decoder buffer */
  68. DMX_OK_IDX, /* Received OK, new index event */
  69. DMX_OK_SCRAMBLING_STATUS, /* Received OK, new scrambling status */
  70. } ;
  71. /*
  72. * struct dmx_data_ready: Parameters for event notification callback.
  73. * Event notification notifies demux device that data is written
  74. * and available in the device's output buffer or provides
  75. * notification on errors and other events. In the latter case
  76. * data_length is zero.
  77. */
  78. struct dmx_data_ready {
  79. enum dmx_success status;
  80. /*
  81. * data_length may be 0 in case of DMX_OK_PES_END or DMX_OK_EOS
  82. * and in non-DMX_OK_XXX events. In DMX_OK_PES_END,
  83. * data_length is for data comming after the end of PES.
  84. */
  85. int data_length;
  86. union {
  87. struct {
  88. int start_gap;
  89. int actual_length;
  90. int disc_indicator_set;
  91. int pes_length_mismatch;
  92. u64 stc;
  93. u32 tei_counter;
  94. u32 cont_err_counter;
  95. u32 ts_packets_num;
  96. } pes_end;
  97. struct {
  98. u64 pcr;
  99. u64 stc;
  100. int disc_indicator_set;
  101. } pcr;
  102. struct {
  103. int handle;
  104. int cookie;
  105. u32 offset;
  106. u32 len;
  107. int pts_exists;
  108. u64 pts;
  109. int dts_exists;
  110. u64 dts;
  111. u32 tei_counter;
  112. u32 cont_err_counter;
  113. u32 ts_packets_num;
  114. u32 ts_dropped_bytes;
  115. u64 stc;
  116. } buf;
  117. struct {
  118. u64 id;
  119. } marker;
  120. struct dmx_index_event_info idx_event;
  121. struct dmx_scrambling_status_event_info scrambling_bits;
  122. };
  123. };
  124. /*
  125. * struct data_buffer: Parameters of buffer allocated by
  126. * demux device for input/output. Can be used to directly map the
  127. * demux-device buffer to HW output if HW supports it.
  128. */
  129. struct data_buffer {
  130. /* dvb_ringbuffer managed by demux-device */
  131. const struct dvb_ringbuffer *ringbuff;
  132. /*
  133. * Private handle returned by kernel demux when
  134. * map_buffer is called in case external buffer
  135. * is used. NULL if buffer is allocated internally.
  136. */
  137. void *priv_handle;
  138. };
  139. /*--------------------------------------------------------------------------*/
  140. /* TS packet reception */
  141. /*--------------------------------------------------------------------------*/
  142. /* TS filter type for set() */
  143. #define TS_PACKET 1 /* send TS packets (188 bytes) to callback (default) */
  144. #define TS_PAYLOAD_ONLY 2 /* in case TS_PACKET is set, only send the TS
  145. payload (<=184 bytes per packet) to callback */
  146. #define TS_DECODER 4 /* send stream to built-in decoder (if present) */
  147. #define TS_DEMUX 8 /* in case TS_PACKET is set, send the TS to
  148. the demux device, not to the dvr device */
  149. /* PES type for filters which write to built-in decoder */
  150. /* these should be kept identical to the types in dmx.h */
  151. enum dmx_ts_pes
  152. { /* also send packets to decoder (if it exists) */
  153. DMX_TS_PES_AUDIO0,
  154. DMX_TS_PES_VIDEO0,
  155. DMX_TS_PES_TELETEXT0,
  156. DMX_TS_PES_SUBTITLE0,
  157. DMX_TS_PES_PCR0,
  158. DMX_TS_PES_AUDIO1,
  159. DMX_TS_PES_VIDEO1,
  160. DMX_TS_PES_TELETEXT1,
  161. DMX_TS_PES_SUBTITLE1,
  162. DMX_TS_PES_PCR1,
  163. DMX_TS_PES_AUDIO2,
  164. DMX_TS_PES_VIDEO2,
  165. DMX_TS_PES_TELETEXT2,
  166. DMX_TS_PES_SUBTITLE2,
  167. DMX_TS_PES_PCR2,
  168. DMX_TS_PES_AUDIO3,
  169. DMX_TS_PES_VIDEO3,
  170. DMX_TS_PES_TELETEXT3,
  171. DMX_TS_PES_SUBTITLE3,
  172. DMX_TS_PES_PCR3,
  173. DMX_TS_PES_OTHER
  174. };
  175. #define DMX_TS_PES_AUDIO DMX_TS_PES_AUDIO0
  176. #define DMX_TS_PES_VIDEO DMX_TS_PES_VIDEO0
  177. #define DMX_TS_PES_TELETEXT DMX_TS_PES_TELETEXT0
  178. #define DMX_TS_PES_SUBTITLE DMX_TS_PES_SUBTITLE0
  179. #define DMX_TS_PES_PCR DMX_TS_PES_PCR0
  180. struct dmx_ts_feed;
  181. typedef int (*dmx_ts_data_ready_cb)(
  182. struct dmx_ts_feed *source,
  183. struct dmx_data_ready *dmx_data_ready);
  184. struct dmx_ts_feed {
  185. int is_filtering; /* Set to non-zero when filtering in progress */
  186. struct dmx_demux *parent; /* Back-pointer */
  187. struct data_buffer buffer;
  188. void *priv; /* Pointer to private data of the API client */
  189. struct dmx_decoder_buffers *decoder_buffers;
  190. int (*set) (struct dmx_ts_feed *feed,
  191. u16 pid,
  192. int type,
  193. enum dmx_ts_pes pes_type,
  194. size_t circular_buffer_size,
  195. struct timespec timeout);
  196. int (*start_filtering) (struct dmx_ts_feed* feed);
  197. int (*stop_filtering) (struct dmx_ts_feed* feed);
  198. int (*set_video_codec) (struct dmx_ts_feed *feed,
  199. enum dmx_video_codec video_codec);
  200. int (*set_idx_params) (struct dmx_ts_feed *feed,
  201. struct dmx_indexing_params *idx_params);
  202. int (*get_decoder_buff_status)(
  203. struct dmx_ts_feed *feed,
  204. struct dmx_buffer_status *dmx_buffer_status);
  205. int (*reuse_decoder_buffer)(
  206. struct dmx_ts_feed *feed,
  207. int cookie);
  208. int (*data_ready_cb)(struct dmx_ts_feed *feed,
  209. dmx_ts_data_ready_cb callback);
  210. int (*notify_data_read)(struct dmx_ts_feed *feed,
  211. u32 bytes_num);
  212. int (*set_tsp_out_format)(struct dmx_ts_feed *feed,
  213. enum dmx_tsp_format_t tsp_format);
  214. int (*set_secure_mode)(struct dmx_ts_feed *feed,
  215. struct dmx_secure_mode *sec_mode);
  216. int (*set_cipher_ops)(struct dmx_ts_feed *feed,
  217. struct dmx_cipher_operations *cipher_ops);
  218. int (*oob_command) (struct dmx_ts_feed *feed,
  219. struct dmx_oob_command *cmd);
  220. int (*ts_insertion_init)(struct dmx_ts_feed *feed);
  221. int (*ts_insertion_terminate)(struct dmx_ts_feed *feed);
  222. int (*ts_insertion_insert_buffer)(struct dmx_ts_feed *feed,
  223. char *data, size_t size);
  224. int (*get_scrambling_bits)(struct dmx_ts_feed *feed, u8 *value);
  225. };
  226. /*--------------------------------------------------------------------------*/
  227. /* Section reception */
  228. /*--------------------------------------------------------------------------*/
  229. struct dmx_section_filter {
  230. u8 filter_value [DMX_MAX_FILTER_SIZE];
  231. u8 filter_mask [DMX_MAX_FILTER_SIZE];
  232. u8 filter_mode [DMX_MAX_FILTER_SIZE];
  233. struct dmx_section_feed* parent; /* Back-pointer */
  234. struct data_buffer buffer;
  235. void* priv; /* Pointer to private data of the API client */
  236. };
  237. struct dmx_section_feed;
  238. typedef int (*dmx_section_data_ready_cb)(
  239. struct dmx_section_filter *source,
  240. struct dmx_data_ready *dmx_data_ready);
  241. struct dmx_section_feed {
  242. int is_filtering; /* Set to non-zero when filtering in progress */
  243. struct dmx_demux* parent; /* Back-pointer */
  244. void* priv; /* Pointer to private data of the API client */
  245. int check_crc;
  246. u32 crc_val;
  247. u8 *secbuf;
  248. u8 secbuf_base[DMX_MAX_SECFEED_SIZE];
  249. u16 secbufp, seclen, tsfeedp;
  250. int (*set) (struct dmx_section_feed* feed,
  251. u16 pid,
  252. size_t circular_buffer_size,
  253. int check_crc);
  254. int (*allocate_filter) (struct dmx_section_feed* feed,
  255. struct dmx_section_filter** filter);
  256. int (*release_filter) (struct dmx_section_feed* feed,
  257. struct dmx_section_filter* filter);
  258. int (*start_filtering) (struct dmx_section_feed* feed);
  259. int (*stop_filtering) (struct dmx_section_feed* feed);
  260. int (*data_ready_cb)(struct dmx_section_feed *feed,
  261. dmx_section_data_ready_cb callback);
  262. int (*notify_data_read)(struct dmx_section_filter *filter,
  263. u32 bytes_num);
  264. int (*set_secure_mode)(struct dmx_section_feed *feed,
  265. struct dmx_secure_mode *sec_mode);
  266. int (*set_cipher_ops)(struct dmx_section_feed *feed,
  267. struct dmx_cipher_operations *cipher_ops);
  268. int (*oob_command) (struct dmx_section_feed *feed,
  269. struct dmx_oob_command *cmd);
  270. int (*get_scrambling_bits)(struct dmx_section_feed *feed, u8 *value);
  271. };
  272. /*--------------------------------------------------------------------------*/
  273. /* Callback functions */
  274. /*--------------------------------------------------------------------------*/
  275. typedef int (*dmx_ts_cb) ( const u8 * buffer1,
  276. size_t buffer1_length,
  277. const u8 * buffer2,
  278. size_t buffer2_length,
  279. struct dmx_ts_feed* source,
  280. enum dmx_success success);
  281. typedef int (*dmx_section_cb) ( const u8 * buffer1,
  282. size_t buffer1_len,
  283. const u8 * buffer2,
  284. size_t buffer2_len,
  285. struct dmx_section_filter * source,
  286. enum dmx_success success);
  287. typedef int (*dmx_ts_fullness) (
  288. struct dmx_ts_feed *source,
  289. int required_space);
  290. typedef int (*dmx_section_fullness) (
  291. struct dmx_section_filter *source,
  292. int required_space);
  293. /*--------------------------------------------------------------------------*/
  294. /* DVB Front-End */
  295. /*--------------------------------------------------------------------------*/
  296. enum dmx_frontend_source {
  297. DMX_MEMORY_FE,
  298. DMX_FRONTEND_0,
  299. DMX_FRONTEND_1,
  300. DMX_FRONTEND_2,
  301. DMX_FRONTEND_3,
  302. DMX_STREAM_0, /* external stream input, e.g. LVDS */
  303. DMX_STREAM_1,
  304. DMX_STREAM_2,
  305. DMX_STREAM_3
  306. };
  307. struct dmx_frontend {
  308. struct list_head connectivity_list; /* List of front-ends that can
  309. be connected to a particular
  310. demux */
  311. enum dmx_frontend_source source;
  312. };
  313. /*--------------------------------------------------------------------------*/
  314. /* MPEG-2 TS Demux */
  315. /*--------------------------------------------------------------------------*/
  316. /*
  317. * Flags OR'ed in the capabilities field of struct dmx_demux.
  318. */
  319. #define DMX_TS_FILTERING 1
  320. #define DMX_PES_FILTERING 2
  321. #define DMX_SECTION_FILTERING 4
  322. #define DMX_MEMORY_BASED_FILTERING 8 /* write() available */
  323. #define DMX_CRC_CHECKING 16
  324. #define DMX_TS_DESCRAMBLING 32
  325. /*
  326. * Demux resource type identifier.
  327. */
  328. /*
  329. * DMX_FE_ENTRY(): Casts elements in the list of registered
  330. * front-ends from the generic type struct list_head
  331. * to the type * struct dmx_frontend
  332. *.
  333. */
  334. #define DMX_FE_ENTRY(list) list_entry(list, struct dmx_frontend, connectivity_list)
  335. struct dmx_demux {
  336. u32 capabilities; /* Bitfield of capability flags */
  337. struct dmx_frontend* frontend; /* Front-end connected to the demux */
  338. void* priv; /* Pointer to private data of the API client */
  339. struct data_buffer dvr_input; /* DVR input buffer */
  340. int dvr_input_protected;
  341. struct dentry *debugfs_demux_dir; /* debugfs dir */
  342. int (*open) (struct dmx_demux* demux);
  343. int (*close) (struct dmx_demux* demux);
  344. int (*write) (struct dmx_demux *demux, const char *buf, size_t count);
  345. int (*allocate_ts_feed) (struct dmx_demux* demux,
  346. struct dmx_ts_feed** feed,
  347. dmx_ts_cb callback);
  348. int (*release_ts_feed) (struct dmx_demux* demux,
  349. struct dmx_ts_feed* feed);
  350. int (*allocate_section_feed) (struct dmx_demux* demux,
  351. struct dmx_section_feed** feed,
  352. dmx_section_cb callback);
  353. int (*release_section_feed) (struct dmx_demux* demux,
  354. struct dmx_section_feed* feed);
  355. int (*add_frontend) (struct dmx_demux* demux,
  356. struct dmx_frontend* frontend);
  357. int (*remove_frontend) (struct dmx_demux* demux,
  358. struct dmx_frontend* frontend);
  359. struct list_head* (*get_frontends) (struct dmx_demux* demux);
  360. int (*connect_frontend) (struct dmx_demux* demux,
  361. struct dmx_frontend* frontend);
  362. int (*disconnect_frontend) (struct dmx_demux* demux);
  363. int (*get_pes_pids) (struct dmx_demux* demux, u16 *pids);
  364. int (*get_caps) (struct dmx_demux* demux, struct dmx_caps *caps);
  365. int (*set_source) (struct dmx_demux *demux, const dmx_source_t *src);
  366. int (*set_tsp_format) (struct dmx_demux *demux,
  367. enum dmx_tsp_format_t tsp_format);
  368. int (*set_playback_mode) (struct dmx_demux *demux,
  369. enum dmx_playback_mode_t mode,
  370. dmx_ts_fullness ts_fullness_callback,
  371. dmx_section_fullness sec_fullness_callback);
  372. int (*write_cancel) (struct dmx_demux *demux);
  373. int (*get_stc) (struct dmx_demux* demux, unsigned int num,
  374. u64 *stc, unsigned int *base);
  375. int (*map_buffer) (struct dmx_demux *demux,
  376. struct dmx_buffer *dmx_buffer,
  377. void **priv_handle, void **mem);
  378. int (*unmap_buffer) (struct dmx_demux *demux,
  379. void *priv_handle);
  380. int (*get_tsp_size) (struct dmx_demux *demux);
  381. };
  382. #endif /* #ifndef __DEMUX_H */