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.

tsfuncs.h 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * Main header file
  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_TSFUNCS_H
  9. #define LIBTS_TSFUNCS_H
  10. #include <time.h>
  11. #include <netdb.h>
  12. #include "tsdata.h"
  13. #include "log.h"
  14. // Usage bit_on(0xff, 0x02)
  15. #define bit_on(__bit, __mask) ((__bit & __mask) ? 1 : 0)
  16. #define bit_1 (0x01)
  17. #define bit_2 (0x02)
  18. #define bit_3 (0x04)
  19. #define bit_4 (0x08)
  20. #define bit_5 (0x10)
  21. #define bit_6 (0x20)
  22. #define bit_7 (0x40)
  23. #define bit_8 (0x80)
  24. #define NO_PCR (-1ull)
  25. #define NO_PCR_BASE (-1ull)
  26. #define NO_PCR_EXT (0xffff)
  27. #define NO_PCR (-1ull)
  28. #define NO_PTS (-1ull)
  29. #define NO_DTS (-1ull)
  30. enum ts_scrambled_type {
  31. not_scrambled = 0x00,
  32. scrambled_reserved = 0x01,
  33. scrambled_with_odd_key = 0x02,
  34. scrambled_with_even_key = 0x03
  35. };
  36. // Packet manipulation
  37. void ts_packet_init_null (uint8_t *ts_packet);
  38. static inline int ts_packet_is_pusi(uint8_t *ts_packet) {
  39. return (ts_packet[1] &~ 0xbf) >> 6;
  40. }
  41. static inline uint16_t ts_packet_get_pid(uint8_t *ts_packet) {
  42. return (ts_packet[1] &~ 0xE0) << 8 | ts_packet[2];
  43. }
  44. static inline void ts_packet_set_pid(uint8_t *ts_packet, uint16_t new_pid) {
  45. ts_packet[1] = (ts_packet[1] &~ 0x1f) | (new_pid >> 8); // 111xxxxx xxxxxxxx
  46. ts_packet[2] = new_pid &~ 0xff00;
  47. }
  48. static inline uint8_t ts_packet_get_cont(uint8_t *ts_packet) {
  49. return (ts_packet[3] &~ 0xF0); // 1111xxxx
  50. }
  51. static inline void ts_packet_set_cont(uint8_t *ts_packet, uint8_t value) {
  52. // Mask the last 4 bits (continuity), then set the continuity
  53. ts_packet[3] = (ts_packet[3] &~ 0x0F) | (value &~ 0xF0);
  54. }
  55. static inline void ts_packet_inc_cont(uint8_t *ts_packet, uint8_t increment) {
  56. ts_packet_set_cont(ts_packet, ts_packet_get_cont(ts_packet) + increment);
  57. }
  58. static inline int ts_packet_get_scrambled(uint8_t *ts_packet) {
  59. return ts_packet[3] >> 6; // 0 is not scamlbed, 1 is reserved, 2 or 3 mean scrambled
  60. }
  61. static inline int ts_packet_is_scrambled(uint8_t *ts_packet) {
  62. return ts_packet_get_scrambled(ts_packet) > 1;
  63. }
  64. static inline void ts_packet_set_not_scrambled(uint8_t *ts_packet) {
  65. ts_packet[3] = ts_packet[3] &~ 0xc0; // Mask top two bits (11xxxxxx)
  66. }
  67. void ts_packet_set_scrambled(uint8_t *ts_packet, enum ts_scrambled_type stype);
  68. uint8_t ts_packet_get_payload_offset(uint8_t *ts_packet);
  69. int ts_packet_has_pcr (uint8_t *ts_packet);
  70. uint64_t ts_packet_get_pcr_ex (uint8_t *ts_packet, uint64_t *pcr_base, uint16_t *pcr_ext);
  71. uint64_t ts_packet_get_pcr (uint8_t *ts_packet);
  72. void ts_packet_set_pcr_ex (uint8_t *ts_packet, uint64_t pcr_base, uint16_t pcr_ext);
  73. void ts_packet_set_pcr (uint8_t *ts_packet, uint64_t pcr);
  74. /*
  75. * guard 2 == pts
  76. * guard 3 == pts before dts
  77. * guard 1 == dts
  78. */
  79. void ts_encode_pts_dts (uint8_t *data, int guard, uint64_t value);
  80. void ts_decode_pts_dts (uint8_t *data, uint64_t *value);
  81. int ts_packet_has_pes (uint8_t *ts_packet, uint16_t *pes_packet_len);
  82. int ts_packet_has_pts_dts (uint8_t *ts_packet, uint64_t *pts, uint64_t *dts);
  83. void ts_packet_change_pts (uint8_t *ts_packet, uint64_t pts);
  84. void ts_packet_change_pts_dts (uint8_t *ts_packet, uint64_t pts, uint64_t dts);
  85. // TS packet headers
  86. uint8_t * ts_packet_header_parse (uint8_t *ts_packet, struct ts_header *ts_header);
  87. void ts_packet_header_generate (uint8_t *ts_packet, struct ts_header *ts_header);
  88. void ts_packet_header_dump (struct ts_header *ts_header);
  89. // Sections
  90. uint8_t * ts_section_header_parse (uint8_t *ts_packet, struct ts_header *ts_header, struct ts_section_header *ts_section_header);
  91. void ts_section_header_generate (uint8_t *ts_packet, struct ts_section_header *ts_section_header, uint8_t start);
  92. void ts_section_header_dump (struct ts_section_header *t);
  93. void ts_section_dump (struct ts_section_header *sec);
  94. void ts_section_header_set_private_vars (struct ts_section_header *ts_section_header);
  95. int ts_section_is_same(struct ts_section_header *s1, struct ts_section_header *s2);
  96. uint8_t * ts_section_data_alloc_section (void);
  97. uint8_t * ts_section_data_alloc_packet (void);
  98. struct ts_section_header * ts_section_data_alloc (void);
  99. void ts_section_data_clear (struct ts_section_header *sec);
  100. void ts_section_data_free (struct ts_section_header **ts_section_header);
  101. void ts_section_data_copy (struct ts_section_header *src, struct ts_section_header *dst);
  102. void ts_section_add_packet (struct ts_section_header *sec, struct ts_header *ts_header, uint8_t *ts_packet);
  103. uint32_t ts_section_data_calculate_crc (uint8_t *section_data, int section_data_size);
  104. void ts_section_data_gen_ts_packets (struct ts_header *ts_header, uint8_t *section_data, int section_data_sz, uint8_t pointer_field, uint8_t **packets, int *num_packets);
  105. // PAT
  106. struct ts_pat * ts_pat_alloc (void);
  107. struct ts_pat * ts_pat_init (struct ts_pat *pat, uint16_t transport_stream_id);
  108. struct ts_pat * ts_pat_alloc_init (uint16_t transport_stream_id);
  109. struct ts_pat * ts_pat_push_packet (struct ts_pat *pat, uint8_t *ts_packet);
  110. void ts_pat_clear (struct ts_pat *pat);
  111. void ts_pat_free (struct ts_pat **pat);
  112. int ts_pat_parse (struct ts_pat *pat);
  113. void ts_pat_dump (struct ts_pat *pat);
  114. void ts_pat_generate (struct ts_pat *pat, uint8_t **ts_packets, int *num_packets);
  115. struct ts_pat * ts_pat_copy (struct ts_pat *pat);
  116. void ts_pat_regenerate_packets (struct ts_pat *pat);
  117. int ts_pat_add_program (struct ts_pat *pat, uint16_t program, uint16_t pat_pid);
  118. int ts_pat_del_program (struct ts_pat *pat, uint16_t program);
  119. int ts_pat_is_same (struct ts_pat *pat1, struct ts_pat *pat2);
  120. // CAT
  121. struct ts_cat * ts_cat_alloc (void);
  122. struct ts_cat * ts_cat_push_packet (struct ts_cat *cat, uint8_t *ts_packet);
  123. void ts_cat_clear (struct ts_cat *cat);
  124. void ts_cat_free (struct ts_cat **cat);
  125. int ts_cat_parse (struct ts_cat *cat);
  126. void ts_cat_dump (struct ts_cat *cat);
  127. struct ts_cat * ts_cat_copy (struct ts_cat *cat);
  128. int ts_cat_is_same (struct ts_cat *cat1, struct ts_cat *cat2);
  129. enum CA_system ts_get_CA_sys (uint16_t CA_id);
  130. char * ts_get_CA_sys_txt (enum CA_system CA_sys);
  131. int ts_get_emm_info (struct ts_cat *cat, enum CA_system CA_sys, uint16_t *CA_id, uint16_t *CA_pid);
  132. int ts_get_ecm_info (struct ts_pmt *pmt, enum CA_system CA_sys, uint16_t *CA_id, uint16_t *CA_pid);
  133. int ts_get_emm_info_by_caid (struct ts_cat *cat, uint16_t caid, uint16_t *ca_pid);
  134. int ts_get_ecm_info_by_caid (struct ts_pmt *pmt, uint16_t caid, uint16_t *ca_pid);
  135. int ts_get_emm_info_by_pid (struct ts_cat *cat, uint16_t *caid, uint16_t ca_pid);
  136. int ts_get_ecm_info_by_pid (struct ts_pmt *pmt, uint16_t *caid, uint16_t ca_pid);
  137. // PMT
  138. struct ts_pmt * ts_pmt_alloc (void);
  139. struct ts_pmt * ts_pmt_push_packet (struct ts_pmt *pmt, uint8_t *ts_packet);
  140. void ts_pmt_clear (struct ts_pmt *pmt);
  141. void ts_pmt_free (struct ts_pmt **pmt);
  142. int ts_pmt_parse (struct ts_pmt *pmt);
  143. void ts_pmt_dump (struct ts_pmt *pmt);
  144. void ts_pmt_generate (struct ts_pmt *pmt, uint8_t **ts_packets, int *num_packets);
  145. struct ts_pmt * ts_pmt_copy (struct ts_pmt *pmt);
  146. void ts_pmt_regenerate_packets (struct ts_pmt *pmt);
  147. int ts_pmt_is_same (struct ts_pmt *pmt1, struct ts_pmt *pmt2);
  148. // NIT
  149. struct ts_nit * ts_nit_alloc (void);
  150. struct ts_nit * ts_nit_init (struct ts_nit *nit, uint16_t network_id);
  151. struct ts_nit * ts_nit_alloc_init (uint16_t network_id);
  152. struct ts_nit * ts_nit_push_packet (struct ts_nit *nit, uint8_t *ts_packet);
  153. void ts_nit_clear (struct ts_nit *nit);
  154. void ts_nit_free (struct ts_nit **nit);
  155. int ts_nit_parse (struct ts_nit *nit);
  156. void ts_nit_dump (struct ts_nit *nit);
  157. void ts_nit_generate (struct ts_nit *nit, uint8_t **ts_packets, int *num_packets);
  158. int ts_nit_add_network_name_descriptor (struct ts_nit *nit, char *network_name);
  159. int ts_nit_add_frequency_list_descriptor_neutral (struct ts_nit *nit, uint16_t ts_id, uint16_t org_net_id, uint32_t *freqs, uint8_t num_freqs);
  160. int ts_nit_add_frequency_list_descriptor_satellite (struct ts_nit *nit, uint16_t ts_id, uint16_t org_net_id, uint32_t *freqs, uint8_t num_freqs);
  161. int ts_nit_add_frequency_list_descriptor_cable (struct ts_nit *nit, uint16_t ts_id, uint16_t org_net_id, uint32_t *freqs, uint8_t num_freqs);
  162. int ts_nit_add_frequency_list_descriptor_terrestrial(struct ts_nit *nit, uint16_t ts_id, uint16_t org_net_id, uint32_t *freqs, uint8_t num_freqs);
  163. int ts_nit_add_cable_delivery_descriptor (struct ts_nit *nit, uint16_t ts_id, uint16_t org_net_id, uint32_t freq, uint8_t modulation, uint32_t symbol_rate);
  164. int ts_nit_add_service_list_descriptor (struct ts_nit *nit, uint16_t ts_id, uint16_t org_net_id, uint32_t *services, uint8_t num_services);
  165. int ts_nit_add_nordig_specifier_descriptor (struct ts_nit *nit, uint16_t ts_id, uint16_t org_net_id);
  166. int ts_nit_add_lcn_descriptor (struct ts_nit *nit, uint16_t ts_id, uint16_t org_net_id, uint32_t *services, uint8_t num_services);
  167. int ts_nit_add_stream_descriptors (struct ts_nit *nit, uint16_t ts_id, uint16_t org_net_id, uint32_t freq, uint8_t modulation, uint32_t symbol_rate, uint32_t *lcn_services, uint32_t *svc_services, uint8_t num_services);
  168. struct ts_nit * ts_nit_copy (struct ts_nit *nit);
  169. int ts_nit_is_same (struct ts_nit *nit1, struct ts_nit *nit2);
  170. // SDT
  171. struct ts_sdt * ts_sdt_alloc (void);
  172. struct ts_sdt * ts_sdt_init (struct ts_sdt *sdt, uint16_t org_network_id, uint16_t transport_stream_id);
  173. struct ts_sdt * ts_sdt_alloc_init (uint16_t org_network_id, uint16_t transport_stream_id);
  174. struct ts_sdt * ts_sdt_push_packet (struct ts_sdt *sdt, uint8_t *ts_packet);
  175. void ts_sdt_clear (struct ts_sdt *sdt);
  176. void ts_sdt_free (struct ts_sdt **sdt);
  177. int ts_sdt_parse (struct ts_sdt *sdt);
  178. void ts_sdt_dump (struct ts_sdt *sdt);
  179. void ts_sdt_generate (struct ts_sdt *sdt, uint8_t **ts_packets, int *num_packets);
  180. int ts_sdt_add_service_descriptor(struct ts_sdt *sdt, uint16_t service_id, uint8_t video, char *provider_name, char *service_name);
  181. struct ts_sdt * ts_sdt_copy (struct ts_sdt *sdt);
  182. int ts_sdt_is_same (struct ts_sdt *sdt1, struct ts_sdt *sdt2);
  183. // EIT
  184. struct ts_eit * ts_eit_alloc (void);
  185. struct ts_eit * ts_eit_init (struct ts_eit *eit, uint16_t service_id, uint16_t transport_stream_id, uint16_t org_network_id, uint8_t table_id, uint8_t sec_number, uint8_t last_sec_number);
  186. struct ts_eit * ts_eit_alloc_init (uint16_t service_id, uint16_t transport_stream_id, uint16_t org_network_id, uint8_t table_id, uint8_t sec_number, uint8_t last_sec_number);
  187. struct ts_eit * ts_eit_alloc_init_pf (uint16_t service_id, uint16_t transport_stream_id, uint16_t org_network_id, uint8_t sec_number, uint8_t last_sec_number); // Shortcut using table_id 0x4e
  188. struct ts_eit * ts_eit_alloc_init_schedule (uint16_t service_id, uint16_t transport_stream_id, uint16_t org_network_id, uint8_t sec_number, uint8_t last_sec_number); // Shortcut using table_id 0x50
  189. struct ts_eit * ts_eit_push_packet (struct ts_eit *eit, uint8_t *ts_packet);
  190. void ts_eit_clear (struct ts_eit *eit);
  191. void ts_eit_free (struct ts_eit **eit);
  192. int ts_eit_parse (struct ts_eit *eit);
  193. void ts_eit_dump (struct ts_eit *eit);
  194. void ts_eit_generate (struct ts_eit *eit, uint8_t **ts_packets, int *num_packets);
  195. struct ts_eit * ts_eit_copy (struct ts_eit *eit);
  196. void ts_eit_regenerate_packets (struct ts_eit *eit);
  197. int ts_eit_add_short_event_descriptor (struct ts_eit *eit, uint16_t event_id, uint8_t running, time_t start_time, int duration_sec, char *event_name, char *event_short_descr);
  198. int ts_eit_add_extended_event_descriptor(struct ts_eit *eit, uint16_t event_id, uint8_t running, time_t start_time, int duration_sec, char *text);
  199. int ts_eit_is_same (struct ts_eit *eit1, struct ts_eit *eit2);
  200. // TDT
  201. struct ts_tdt * ts_tdt_alloc(void);
  202. struct ts_tdt * ts_tdt_init (struct ts_tdt *tdt, time_t ts);
  203. struct ts_tdt * ts_tot_init (struct ts_tdt *tot, time_t ts);
  204. struct ts_tdt * ts_tdt_alloc_init (time_t ts);
  205. struct ts_tdt * ts_tot_alloc_init (time_t ts);
  206. void ts_tdt_clear (struct ts_tdt *tdt);
  207. void ts_tdt_free (struct ts_tdt **tdt);
  208. int ts_tdt_parse (struct ts_tdt *tdt);
  209. struct ts_tdt * ts_tdt_push_packet (struct ts_tdt *tdt, uint8_t *ts_packet);
  210. void ts_tdt_generate (struct ts_tdt *tdt, uint8_t **ts_packets, int *num_packets);
  211. void ts_tdt_dump (struct ts_tdt *tdt);
  212. void ts_tdt_set_time (struct ts_tdt *tdt, time_t ts);
  213. void ts_tot_set_localtime_offset (struct ts_tdt *tdt, time_t now, time_t change_time, uint8_t polarity, uint16_t ofs, uint16_t ofs_next);
  214. void ts_tot_set_localtime_offset_sofia (struct ts_tdt *tdt, time_t now);
  215. struct ts_tdt * ts_tdt_copy (struct ts_tdt *tdt);
  216. int ts_tdt_is_same (struct ts_tdt *tdt1, struct ts_tdt *tdt2);
  217. // Private section
  218. struct ts_privsec * ts_privsec_alloc(void);
  219. void ts_privsec_clear (struct ts_privsec *pprivsec);
  220. void ts_privsec_free (struct ts_privsec **pprivsec);
  221. struct ts_privsec * ts_privsec_push_packet (struct ts_privsec *privsec, uint8_t *ts_packet);
  222. int ts_privsec_is_same (struct ts_privsec *p1, struct ts_privsec *p2);
  223. void ts_privsec_dump (struct ts_privsec *privsec);
  224. void ts_privsec_copy (struct ts_privsec *src, struct ts_privsec *dst);
  225. // Time
  226. uint32_t ts_time_encode_bcd (int duration_sec);
  227. void ts_time_decode_bcd (int duration_bcd, int *duration_sec, int *hour, int *min, int *sec);
  228. void ts_time_encode_mjd (uint16_t *mjd, uint32_t *bcd, time_t *ts, struct tm *tm);
  229. time_t ts_time_decode_mjd (uint16_t mjd, uint32_t bcd, struct tm *tm);
  230. // Descriptors
  231. void ts_descriptor_dump (uint8_t *desc_data, int desc_data_len);
  232. int ts_is_stream_type_video (uint8_t stream_type);
  233. int ts_is_stream_type_ac3 (uint8_t stream_type);
  234. int ts_is_stream_type_audio (uint8_t stream_type);
  235. char * h222_stream_type_desc (uint8_t stream_type);
  236. char * h222_stream_id_desc (uint8_t stream_id);
  237. // PES
  238. struct ts_pes * ts_pes_alloc (void);
  239. void ts_pes_clear (struct ts_pes *pes);
  240. void ts_pes_free (struct ts_pes **pes);
  241. void ts_pes_fill_type (struct ts_pes *pes, struct ts_pmt *pmt, uint16_t pid);
  242. int ts_pes_is_finished (struct ts_pes *pes, uint8_t *ts_packet);
  243. struct ts_pes * ts_pes_push_packet (struct ts_pes *pes, uint8_t *ts_packet, struct ts_pmt *pmt, uint16_t pid);
  244. int ts_pes_parse (struct ts_pes *pes);
  245. void ts_pes_dump (struct ts_pes *pes);
  246. struct pes_array * pes_array_alloc (void);
  247. void pes_array_dump (struct pes_array *pa);
  248. void pes_array_free (struct pes_array **ppa);
  249. struct pes_entry * pes_array_push_packet (struct pes_array *pa, uint16_t pid, struct ts_pat *pat, struct ts_pmt *pmt, uint8_t *ts_packet);
  250. // ES functions
  251. int ts_pes_es_mpeg_audio_header_parse (struct mpeg_audio_header *mpghdr, uint8_t *data, int datasz);
  252. void ts_pes_es_mpeg_audio_header_dump (struct mpeg_audio_header *mpghdr);
  253. void ts_pes_es_parse (struct ts_pes *pes);
  254. void ts_pes_es_dump (struct ts_pes *pes);
  255. // CRC
  256. uint32_t ts_crc32 (uint8_t *data, int data_size);
  257. uint32_t ts_crc32_section (struct ts_section_header *section_header);
  258. int ts_crc32_section_check (struct ts_section_header *section_header, char *table);
  259. // Misc
  260. int dec2bcd (int dec);
  261. int bcd2dec (int bcd);
  262. void ts_compare_data (char *prefix, uint8_t *a, uint8_t *b, int size);
  263. void ts_hex_dump_buf (char *buf, int bufsz, uint8_t *d, int size, int col);
  264. char * ts_hex_dump (uint8_t *d, int size, int col);
  265. void ts_print_bytes (char *prefix, uint8_t *d, int size);
  266. char * init_dvb_string_utf8 (char *text);
  267. char * init_dvb_string_iso_8859_5 (char *text);
  268. int ts_is_psi_pid (uint16_t pid, struct ts_pat *pat);
  269. void pidmap_clear (pidmap_t *pm);
  270. void pidmap_set (pidmap_t *pm, uint16_t pid);
  271. void pidmap_set_val (pidmap_t *pm, uint16_t pid, uint8_t val);
  272. int pidmap_get (pidmap_t *pm, uint16_t pid);
  273. #endif