libtsfuncs is a library for mpeg PSI parsing and generation. https://georgi.unixsol.org/programs/libtsfuncs/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tsdata.h 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /*
  2. * Data defintions
  3. * Copyright (C) 2010-2011 Unix Solutions Ltd.
  4. *
  5. * Released under MIT license.
  6. * See LICENSE-MIT.txt for license terms.
  7. */
  8. #ifndef LIBTS_TSDATA_H
  9. #define LIBTS_TSDATA_H
  10. #include <netdb.h>
  11. #include <time.h>
  12. #include <stdint.h>
  13. #ifndef FREE
  14. #define FREE(x) if(x) { free(x); x=NULL; }
  15. #endif
  16. #define TS_PACKET_SIZE 188
  17. #define TS_MAX_PAYLOAD_SIZE (TS_PACKET_SIZE-4)
  18. struct ts_header {
  19. uint8_t sync_byte; // Always 0x47
  20. uint16_t tei : 1, // Transport Error Indicator (TEI)
  21. pusi : 1, // Payload Unit Start Indicator
  22. prio : 1, // Transport Priority
  23. pid : 13; // PID
  24. uint8_t scramble : 2, // 00 - not scrambled, 01 - reserved, 10 - scrambled with even key, 11 - scrambled with odd key
  25. adapt_field : 1,
  26. payload_field : 1,
  27. continuity : 4;
  28. // The variables bellow this line depends may not exist in a packet
  29. uint8_t adapt_len; // adaptation field length
  30. uint8_t adapt_flags; // adaptation field flags
  31. uint8_t payload_size; // Payload size inside the packet
  32. uint8_t payload_offset; // Payload offset inside the packet
  33. };
  34. struct ts_section_header {
  35. uint8_t pointer_field;
  36. uint8_t table_id;
  37. uint16_t section_syntax_indicator: 1, // Section Syntax Indicator
  38. private_indicator : 1, // Private section indicator
  39. reserved1 : 2, // 2 reserved bits
  40. section_length : 12; // Section lenth
  41. uint16_t ts_id_number; // Transport stream id (in PAT), Program number (in PMT)
  42. uint8_t reserved2 : 2,
  43. version_number : 5,
  44. current_next_indicator : 1;
  45. uint8_t section_number;
  46. uint8_t last_section_number;
  47. // The variables bellow this line are not in the physical packet
  48. int section_pos; // Up to this pos the section data has come
  49. int initialized; // Set to 1 when whole sectino is initialized
  50. int section_data_len; // Full section length (3 + section_length)
  51. uint8_t *section_data; // The whole section data
  52. uint8_t *packet_data; // TS packet(s) that were used to transfer the table.
  53. int num_packets; // From how much packets this section is build
  54. int data_len; // Data size without the CRC32 (4 bytes)
  55. uint8_t *data; // Offset into section_data (where the section data start without the section header)
  56. uint32_t CRC;
  57. };
  58. struct ts_pat_program {
  59. uint16_t program;
  60. uint16_t reserved:3,
  61. pid:13;
  62. };
  63. struct ts_pat {
  64. struct ts_header ts_header;
  65. struct ts_section_header *section_header;
  66. struct ts_pat_program **programs;
  67. // The variables bellow are nor part of the physical packet
  68. int programs_max; // How much programs are allocated
  69. int programs_num; // How much programs are initialized
  70. uint8_t initialized; // Set to 1 when full table is initialized
  71. };
  72. enum CA_system {
  73. CA_SECA, // 0x0100 - 0x01FF Canal Plus (SECA/Mediaguard)
  74. CA_VIACCESS, // 0x0500 - 0x05FF France Telecom
  75. CA_IRDETO, // 0x0600 - 0x06FF Irdeto
  76. CA_VIDEOGUARD, // 0x0900 - 0x09FF News Datacom (NDS/Videoguard)
  77. CA_CONAX, // 0x0B00 - 0x0BFF Norwegian Telekom
  78. CA_CRYPTOWORKS, // 0x0D00 - 0x0DFF CrytoWorks
  79. CA_NAGRA, // 0x1800 - 0x18FF Kudelski SA (Nagravision)
  80. CA_DRECRYPT, // 0x4AE0 - 0x4AE1 OOO Cifra (DRE-Crypt)
  81. CA_BULCRYPT, // 0x5581 & 0x4AEE Bulcrypt
  82. CA_GRIFFIN, // 0x5501 & 0x5504 & 0x5511 Griffin (Not in dvbservices.com registration)
  83. CA_DGCRYPT, // 0x4ABF DGCrypt (Beijing Compunicate Technology Inc.)
  84. CA_UNKNOWN,
  85. };
  86. struct ts_cat {
  87. struct ts_header ts_header;
  88. struct ts_section_header *section_header;
  89. int program_info_size;
  90. uint8_t *program_info;
  91. // The variables bellow are nor part of the physical packet
  92. uint8_t initialized; // Set to 1 when full table is initialized
  93. };
  94. struct ts_pmt_stream {
  95. uint8_t stream_type;
  96. uint16_t reserved1 : 3,
  97. pid : 13;
  98. uint16_t reserved2 : 4,
  99. ES_info_size : 12;
  100. uint8_t *ES_info;
  101. };
  102. struct ts_pmt {
  103. struct ts_header ts_header;
  104. struct ts_section_header *section_header;
  105. uint16_t reserved1 : 3,
  106. PCR_pid : 13;
  107. uint16_t reserved2 : 4,
  108. program_info_size : 12;
  109. uint8_t *program_info;
  110. struct ts_pmt_stream **streams;
  111. // The variables bellow are nor part of the physical packet
  112. int streams_max; // How much streams are allocated
  113. int streams_num; // How much streams are initialized
  114. uint8_t initialized; // Set to 1 when full table is initialized
  115. };
  116. struct ts_sdt_stream {
  117. uint16_t service_id;
  118. uint8_t reserved1 : 6,
  119. EIT_schedule_flag : 1,
  120. EIT_present_following_flag : 1;
  121. uint16_t running_status : 3,
  122. free_CA_mode : 1,
  123. descriptor_size : 12;
  124. uint8_t *descriptor_data;
  125. };
  126. struct ts_sdt {
  127. struct ts_header ts_header;
  128. struct ts_section_header *section_header;
  129. uint16_t original_network_id;
  130. uint8_t reserved;
  131. struct ts_sdt_stream **streams;
  132. // The variables bellow are nor part of the physical packet
  133. int streams_max; // How much streams are allocated
  134. int streams_num; // How much streams are initialized
  135. uint8_t initialized; // Set to 1 when full table is initialized
  136. };
  137. struct ts_nit_stream {
  138. uint16_t transport_stream_id;
  139. uint16_t original_network_id;
  140. uint16_t reserved1 : 4,
  141. descriptor_size : 12;
  142. uint8_t *descriptor_data;
  143. };
  144. struct ts_nit {
  145. struct ts_header ts_header;
  146. struct ts_section_header *section_header;
  147. uint16_t reserved1 : 4,
  148. network_info_size : 12;
  149. uint8_t *network_info;
  150. uint16_t reserved2 : 4,
  151. ts_loop_size : 12;
  152. struct ts_nit_stream **streams;
  153. // The variables bellow are nor part of the physical packet
  154. int streams_max; // How much streams are allocated
  155. int streams_num; // How much streams are initialized
  156. uint8_t initialized; // Set to 1 when full NIT table is initialized
  157. };
  158. struct ts_eit_stream {
  159. uint16_t event_id;
  160. uint64_t start_time_mjd : 16,
  161. start_time_bcd : 24, // Total 40, start_time
  162. duration_bcd : 24;
  163. uint16_t running_status : 3,
  164. free_CA_mode : 1,
  165. descriptor_size : 12;
  166. uint8_t *descriptor_data;
  167. };
  168. struct ts_eit {
  169. struct ts_header ts_header;
  170. struct ts_section_header *section_header;
  171. uint16_t transport_stream_id;
  172. uint16_t original_network_id;
  173. uint8_t segment_last_section_number;
  174. uint8_t last_table_id;
  175. struct ts_eit_stream **streams;
  176. // The variables bellow are nor part of the physical packet
  177. int streams_max; // How much streams are allocated
  178. int streams_num; // How much streams are initialized
  179. uint8_t initialized; // Set to 1 when full eit table is initialized
  180. };
  181. struct ts_tdt {
  182. struct ts_header ts_header;
  183. struct ts_section_header *section_header;
  184. uint16_t mjd; // This both are part of one 40 bit field (UTC_time)
  185. uint32_t bcd; // Only 24 bits are used
  186. // The below fields are only in TOT packets, table_id 0x73
  187. uint16_t reserved_3 : 4,
  188. descriptors_size : 12;
  189. uint8_t *descriptors;
  190. // The variables bellow are nor part of the physical packet
  191. time_t utc; // decoded UTC_time
  192. struct tm tm; // decoded UTC_time
  193. uint8_t initialized;
  194. };
  195. struct ts_privsec {
  196. struct ts_header ts_header;
  197. struct ts_section_header *section_header;
  198. uint8_t initialized;
  199. };
  200. // PMT stream types
  201. enum ts_stream_type {
  202. STREAM_TYPE_MPEG1_VIDEO = 0x01, // MPEG-1 video
  203. STREAM_TYPE_MPEG2_VIDEO = 0x02, // H.262 - MPEG-2 video
  204. STREAM_TYPE_MPEG1_AUDIO = 0x03, // MPEG-1 audio
  205. STREAM_TYPE_MPEG2_AUDIO = 0x04, // MPEG-2 audio
  206. STREAM_TYPE_ADTS_AUDIO = 0x0F, // AAC ADTS
  207. STREAM_TYPE_MPEG4_PART2_VIDEO = 0x10, // DIVX - MPEG-4 part 2
  208. STREAM_TYPE_AVC_VIDEO = 0x1B, // H.264 - MPEG-4 part 10
  209. STREAM_TYPE_AVS_VIDEO = 0x42, // Chinese AVS
  210. STREAM_TYPE_DOLBY_DVB_AUDIO = 0x06, // 0x06 - Private stream, look at stream descriptors for AC-3 descriptor
  211. STREAM_TYPE_DOLBY_ATSC_AUDIO = 0x81, // 0x81 - Private stream in ATSC (US system, probably we shouldn't care)
  212. };
  213. // ------------------------------------------------------------
  214. // PES packet stream ids
  215. // See H.222.0 Table 2-17 and Table 2-18
  216. #define STREAM_ID_PROGRAM_STREAM_MAP 0xbc
  217. #define STREAM_ID_PRIVATE_STREAM_1 0xbd
  218. #define STREAM_ID_PADDING_STREAM 0xbe
  219. #define STREAM_ID_PRIVATE_STREAM_2 0xbf
  220. #define STREAM_ID_ECM_STREAM 0xf0
  221. #define STREAM_ID_EMM_STREAM 0xf1
  222. #define STREAM_ID_DSMCC_STREAM 0xf2
  223. #define STREAM_ID_13522_STREAM 0xf3
  224. #define STREAM_ID_H222_A_STREAM 0xf4
  225. #define STREAM_ID_H222_B_STREAM 0xf5
  226. #define STREAM_ID_H222_C_STREAM 0xf6
  227. #define STREAM_ID_H222_D_STREAM 0xf7
  228. #define STREAM_ID_H222_E_STREAM 0xf8
  229. #define STREAM_ID_ANCILLARY_STREAM 0xf9
  230. #define STREAM_ID_PROGRAM_STREAM_DIRECTORY 0xff
  231. #define IS_AUDIO_STREAM_ID(id) ((id) >= 0xc0 && (id) <= 0xdf)
  232. #define IS_VIDEO_STREAM_ID(id) ((id) >= 0xe0 && (id) <= 0xef)
  233. #define IS_PES_STREAM_SUPPORTED(id) (!(id == STREAM_ID_PROGRAM_STREAM_MAP || \
  234. id == STREAM_ID_PADDING_STREAM || \
  235. id == STREAM_ID_PRIVATE_STREAM_2 || \
  236. id == STREAM_ID_ECM_STREAM || \
  237. id == STREAM_ID_EMM_STREAM || \
  238. id == STREAM_ID_PROGRAM_STREAM_DIRECTORY || \
  239. id == STREAM_ID_DSMCC_STREAM || \
  240. id == STREAM_ID_H222_E_STREAM))
  241. struct mpeg_audio_header {
  242. uint32_t syncword : 12,
  243. ID : 1,
  244. layer : 2,
  245. protection_bit : 1,
  246. bitrate_index : 4,
  247. sampl_freq : 2,
  248. padding_bit : 1,
  249. private_bit : 1,
  250. mode : 2,
  251. mode_extension : 2,
  252. copyright : 1,
  253. org_home : 1,
  254. emphasis : 2;
  255. uint8_t initialized;
  256. };
  257. struct ts_pes {
  258. struct ts_header ts_header;
  259. uint32_t have_pts : 1, // Have PTS in the PES (init from PES header)
  260. have_dts : 1, // Have DTS in the PES (init from PES header)
  261. is_audio : 1, // PES carries audio (mpeg2 or AC3) (init from PES stream_id and PMT stream_type and descriptors)
  262. is_audio_mpeg1 : 1, // PES carries MP1 audio (init from PMT stream_id)
  263. is_audio_mpeg1l1: 1, // PES carries MP1 audio Layer I (init from PMT audio descriptor)
  264. is_audio_mpeg1l2: 1, // PES carries MP1 audio Layer II (init from PMT audio descriptor)
  265. is_audio_mpeg1l3: 1, // PES carries MP1 audio Layer III (init from PMT audio descriptor)
  266. is_audio_mpeg2 : 1, // PES carries MP2 audio (init from PMT stream_id)
  267. is_audio_aac : 1, // PES carries AAC audio (init from PMT stream_id)
  268. is_audio_ac3 : 1, // PES carries AC3 audio (init from stream_id and PMT descriptors and elmentary stream)
  269. is_audio_dts : 1, // PES carries DTS audio (init from stream_id and elementary stream)
  270. is_video : 1, // PES carries video (mpeg2 or H.264) (init from PES stream_id)
  271. is_video_mpeg1 : 1, // PES carries mpeg1 video (init from PES stream_id)
  272. is_video_mpeg2 : 1, // PES carries mpeg2 video (init from PES stream_id)
  273. is_video_mpeg4 : 1, // PES carries mpeg4 part 2 video (init from PES stream_id)
  274. is_video_h264 : 1, // PES carries H.264 video (init from PES stream_id)
  275. is_video_avs : 1, // PES carries AVS video (init from PES stream_id)
  276. is_teletext : 1, // PES carries teletext (init from PMT descriptors)
  277. is_subtitle : 1; // PES carries subtitles (init from PMT descriptors)
  278. uint8_t stream_id; // If !0 then the PES has started initializing
  279. uint16_t pes_packet_len; // Allowed to be 0 for video streams
  280. int real_pes_packet_len; // if pes_packet_len is > 0 the this is eq to pes_packet_len
  281. // if pes_packet_len is = 0 this is set to -1 until very last packet
  282. uint8_t flags_1; // Bellow flags
  283. uint8_t reserved1 : 2, // Always eq 2 (10 binary)
  284. scrambling : 2,
  285. priority : 1,
  286. data_alignment : 1,
  287. copyright : 1,
  288. original_or_copy : 1;
  289. uint8_t flags_2; // Bellow flags
  290. uint8_t PTS_flag : 1,
  291. DTS_flag : 1,
  292. ESCR_flag : 1,
  293. ES_rate_flag : 1,
  294. trick_mode_flag : 1,
  295. add_copy_info_flag : 1,
  296. pes_crc_flag : 1,
  297. pes_extension_flag : 1;
  298. uint8_t pes_header_len;
  299. uint64_t PTS; // if (PTS_flag)
  300. uint64_t DTS; // if (DTS_flag)
  301. uint64_t ESCR; // if (ESCR_flag)
  302. uint32_t ES_rate; // if (ES_rate_flag)
  303. uint16_t trick_mode_control : 2, // if (trick_mode_flag)
  304. field_id : 2,
  305. intra_slice_refresh : 1,
  306. freq_truncation : 2,
  307. rep_ctrl : 5,
  308. tm_reserved : 4;
  309. uint8_t reserved_add : 1, // if (add_copy_info_flag)
  310. add_copy_info : 7;
  311. uint16_t prev_pes_crc; // if (pes_crc_flag)
  312. // PES extension
  313. uint8_t flags_3; // Bellow flags
  314. uint8_t pes_private_data_flag : 1,
  315. pack_header_field_flag : 1,
  316. program_packet_seq_counter_flag : 1,
  317. p_std_buffer_flag : 1,
  318. reserved2 : 3,
  319. pes_extension2_flag : 1;
  320. uint64_t pes_private_data_1; // if (pes_private_data_flag)
  321. uint64_t pes_private_data_2; // The whole field is 128 bits
  322. uint8_t pack_header_len; // if (pack_header_field_flag)
  323. uint8_t *pack_header; // Pointer into *pes_data
  324. uint8_t reserved3 : 1, // if (program_packet_seq_counter_flag)
  325. program_packet_seq_counter : 7;
  326. uint8_t mpeg1_mpeg2_identifier : 1,
  327. original_stuff_length : 6;
  328. uint16_t p_std_reserved : 2, // Always 1, if (p_std_buffer_flag)
  329. p_std_buffer_scale : 1,
  330. p_std_buffer_size : 13;
  331. uint16_t reserved4 : 1, // if (pes_extension2_flag)
  332. pes_extension_field_len : 7;
  333. uint8_t *pes_extension2; // Pointer into *pes_data
  334. // Private data
  335. uint8_t *pes_data; // Whole packet is stored here
  336. uint32_t pes_data_pos; // How much data is filled in pes_data
  337. uint32_t pes_data_size; // Total allocated for pes_data
  338. uint8_t pes_data_initialized; // Set to 1 when all of the pes_data is in *pes_data and the parsing can start
  339. // More private data
  340. uint8_t *es_data; // Pointer to start of data after PES header, initialized when the packet is fully assembled
  341. uint32_t es_data_size; // Full pes packet length (used for video streams, otherwise equal to pes_packet_len)
  342. uint8_t initialized; // Set to 1 when the packet is fully assembled
  343. // Extra data
  344. struct mpeg_audio_header mpeg_audio_header;
  345. };
  346. struct pes_entry {
  347. uint16_t pid;
  348. struct ts_pes *pes;
  349. struct ts_pes *pes_next;
  350. };
  351. struct pes_array {
  352. int max;
  353. int cur;
  354. struct pes_entry **entries;
  355. };
  356. typedef uint8_t pidmap_t[0x2000];
  357. #endif