libtsfuncs is a library for mpeg PSI parsing and generation. https://georgi.unixsol.org/programs/libtsfuncs/

tsfuncs.h 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #ifndef LIBTS_TSFUNCS_H
  2. #define LIBTS_TSFUNCS_H
  3. #include <time.h>
  4. #include <netdb.h>
  5. #include "tsdata.h"
  6. #include "log.h"
  7. // Usage bit_on(0xff, 0x02)
  8. #define bit_on(__bit, __mask) ((__bit & __mask) ? 1 : 0)
  9. #define bit_1 (0x01)
  10. #define bit_2 (0x02)
  11. #define bit_3 (0x04)
  12. #define bit_4 (0x08)
  13. #define bit_5 (0x10)
  14. #define bit_6 (0x20)
  15. #define bit_7 (0x40)
  16. #define bit_8 (0x80)
  17. #define NO_PCR (-1ull)
  18. #define NO_PCR_BASE (-1ull)
  19. #define NO_PCR_EXT (0xffff)
  20. #define NO_PCR (-1ull)
  21. #define NO_PTS (-1ull)
  22. #define NO_DTS (-1ull)
  23. enum ts_scrambled_type {
  24. not_scrambled = 0x00,
  25. scrambled_reserved = 0x01,
  26. scrambled_with_odd_key = 0x02,
  27. scrambled_with_even_key = 0x03
  28. };
  29. // Packet manipulation
  30. void ts_packet_init_null (uint8_t *ts_packet);
  31. int ts_packet_is_pusi (uint8_t *ts_packet);
  32. uint16_t ts_packet_get_pid (uint8_t *ts_packet);
  33. void ts_packet_set_pid (uint8_t *ts_packet, uint16_t new_pid);
  34. uint8_t ts_packet_get_cont (uint8_t *ts_packet);
  35. void ts_packet_set_cont (uint8_t *ts_packet, uint8_t value);
  36. void ts_packet_inc_cont (uint8_t *ts_packet, uint8_t increment);
  37. uint8_t ts_packet_get_payload_offset(uint8_t *ts_packet);
  38. int ts_packet_is_scrambled(uint8_t *ts_packet);
  39. void ts_packet_set_scrambled(uint8_t *ts_packet, enum ts_scrambled_type stype);
  40. int ts_packet_has_pcr (uint8_t *ts_packet);
  41. uint64_t ts_packet_get_pcr_ex (uint8_t *ts_packet, uint64_t *pcr_base, uint16_t *pcr_ext);
  42. uint64_t ts_packet_get_pcr (uint8_t *ts_packet);
  43. void ts_packet_set_pcr_ex (uint8_t *ts_packet, uint64_t pcr_base, uint16_t pcr_ext);
  44. void ts_packet_set_pcr (uint8_t *ts_packet, uint64_t pcr);
  45. void ts_encode_pts_dts (uint8_t *data, int guard_bits, uint64_t value);
  46. int ts_decode_pts_dts (uint8_t *data, int required_guard, uint64_t *value);
  47. int ts_packet_has_pes (uint8_t *ts_packet, uint16_t *pes_packet_len);
  48. int ts_packet_has_pts_dts (uint8_t *ts_packet, uint64_t *pts, uint64_t *dts);
  49. void ts_packet_change_pts (uint8_t *ts_packet, uint64_t pts);
  50. void ts_packet_change_pts_dts (uint8_t *ts_packet, uint64_t pts, uint64_t dts);
  51. // TS packet headers
  52. uint8_t * ts_packet_header_parse (uint8_t *ts_packet, struct ts_header *ts_header);
  53. void ts_packet_header_generate (uint8_t *ts_packet, struct ts_header *ts_header);
  54. void ts_packet_header_dump (struct ts_header *ts_header);
  55. // Sections
  56. uint8_t * ts_section_header_parse (uint8_t *ts_packet, struct ts_header *ts_header, struct ts_section_header *ts_section_header);
  57. void ts_section_header_generate (uint8_t *ts_packet, struct ts_section_header *ts_section_header, uint8_t start);
  58. void ts_section_header_dump (struct ts_section_header *t);
  59. uint8_t * ts_section_data_alloc_section ();
  60. uint8_t * ts_section_data_alloc_packet ();
  61. struct ts_section_header * ts_section_data_alloc ();
  62. void ts_section_data_free (struct ts_section_header **ts_section_header);
  63. void ts_section_add_packet (struct ts_section_header *sec, struct ts_header *ts_header, uint8_t *ts_packet);
  64. uint32_t ts_section_data_calculate_crc (uint8_t *section_data, int section_data_size);
  65. 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);
  66. // PAT
  67. struct ts_pat * ts_pat_alloc ();
  68. struct ts_pat * ts_pat_alloc_init (uint16_t transport_stream_id);
  69. struct ts_pat * ts_pat_push_packet (struct ts_pat *pat, uint8_t *ts_packet);
  70. void ts_pat_free (struct ts_pat **pat);
  71. int ts_pat_parse (struct ts_pat *pat);
  72. void ts_pat_dump (struct ts_pat *pat);
  73. void ts_pat_generate (struct ts_pat *pat, uint8_t **ts_packets, int *num_packets);
  74. struct ts_pat * ts_pat_copy (struct ts_pat *pat);
  75. void ts_pat_regenerate_packets (struct ts_pat *pat);
  76. int ts_pat_add_program (struct ts_pat *pat, uint16_t program, uint16_t pat_pid);
  77. int ts_pat_del_program (struct ts_pat *pat, uint16_t program);
  78. int ts_pat_is_same (struct ts_pat *pat1, struct ts_pat *pat2);
  79. // CAT
  80. struct ts_cat * ts_cat_alloc ();
  81. struct ts_cat * ts_cat_push_packet (struct ts_cat *cat, uint8_t *ts_packet);
  82. void ts_cat_free (struct ts_cat **cat);
  83. int ts_cat_parse (struct ts_cat *cat);
  84. void ts_cat_dump (struct ts_cat *cat);
  85. int ts_cat_is_same (struct ts_cat *cat1, struct ts_cat *cat2);
  86. enum CA_system ts_get_CA_sys (uint16_t CA_id);
  87. char * ts_get_CA_sys_txt (enum CA_system CA_sys);
  88. // PMT
  89. struct ts_pmt * ts_pmt_alloc ();
  90. struct ts_pmt * ts_pmt_alloc_init (uint16_t org_network_id, uint16_t transport_stream_id);
  91. struct ts_pmt * ts_pmt_push_packet (struct ts_pmt *pmt, uint8_t *ts_packet, uint16_t pmt_pid);
  92. void ts_pmt_free (struct ts_pmt **pmt);
  93. int ts_pmt_parse (struct ts_pmt *pmt);
  94. void ts_pmt_dump (struct ts_pmt *pmt);
  95. void ts_pmt_generate (struct ts_pmt *pmt, uint8_t **ts_packets, int *num_packets);
  96. struct ts_pmt * ts_pmt_copy (struct ts_pmt *pmt);
  97. void ts_pmt_regenerate_packets (struct ts_pmt *pmt);
  98. int ts_pmt_is_same (struct ts_pmt *pmt1, struct ts_pmt *pmt2);
  99. // NIT
  100. struct ts_nit * ts_nit_alloc ();
  101. struct ts_nit * ts_nit_alloc_init (uint16_t network_id);
  102. struct ts_nit * ts_nit_push_packet (struct ts_nit *nit, uint8_t *ts_packet);
  103. void ts_nit_free (struct ts_nit **nit);
  104. int ts_nit_parse (struct ts_nit *nit);
  105. void ts_nit_dump (struct ts_nit *nit);
  106. void ts_nit_generate (struct ts_nit *nit, uint8_t **ts_packets, int *num_packets);
  107. int ts_nit_add_network_name_descriptor (struct ts_nit *nit, char *network_name);
  108. 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);
  109. 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);
  110. 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);
  111. // SDT
  112. struct ts_sdt * ts_sdt_alloc ();
  113. struct ts_sdt * ts_sdt_alloc_init (uint16_t org_network_id, uint16_t transport_stream_id);
  114. struct ts_sdt * ts_sdt_push_packet (struct ts_sdt *sdt, uint8_t *ts_packet);
  115. void ts_sdt_free (struct ts_sdt **sdt);
  116. int ts_sdt_parse (struct ts_sdt *sdt);
  117. void ts_sdt_dump (struct ts_sdt *sdt);
  118. void ts_sdt_generate (struct ts_sdt *sdt, uint8_t **ts_packets, int *num_packets);
  119. int ts_sdt_add_service_descriptor(struct ts_sdt *sdt, uint16_t service_id, uint8_t video, char *provider_name, char *service_name);
  120. // EIT
  121. struct ts_eit * ts_eit_alloc ();
  122. 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);
  123. 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
  124. 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
  125. struct ts_eit * ts_eit_push_packet (struct ts_eit *eit, uint8_t *ts_packet);
  126. void ts_eit_free (struct ts_eit **eit);
  127. int ts_eit_parse (struct ts_eit *eit);
  128. void ts_eit_dump (struct ts_eit *eit);
  129. void ts_eit_generate (struct ts_eit *eit, uint8_t **ts_packets, int *num_packets);
  130. struct ts_eit * ts_eit_copy (struct ts_eit *eit);
  131. void ts_eit_regenerate_packets (struct ts_eit *eit);
  132. 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);
  133. 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);
  134. // TDT
  135. struct ts_tdt * ts_tdt_alloc_init (time_t ts);
  136. struct ts_tdt * ts_tot_alloc_init (time_t ts);
  137. void ts_tdt_free (struct ts_tdt **tdt);
  138. int ts_tdt_parse (struct ts_tdt *tdt, uint8_t *ts_packet);
  139. void ts_tdt_generate (struct ts_tdt *tdt, uint8_t *ts_packet);
  140. void ts_tdt_dump (struct ts_tdt *tdt);
  141. void ts_tdt_set_time (struct ts_tdt *tdt, time_t ts);
  142. 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);
  143. void ts_tot_set_localtime_offset_sofia (struct ts_tdt *tdt);
  144. // Time
  145. uint32_t ts_time_encode_bcd (int duration_sec);
  146. void ts_time_decode_bcd (int duration_bcd, int *duration_sec, int *hour, int *min, int *sec);
  147. void ts_time_encode_mjd (uint16_t *mjd, uint32_t *bcd, time_t *ts, struct tm *tm);
  148. time_t ts_time_decode_mjd (uint16_t mjd, uint32_t bcd, struct tm *tm);
  149. // Descriptors
  150. void ts_descriptor_dump (uint8_t *desc_data, int desc_data_len);
  151. int ts_is_stream_type_video (uint8_t stream_type);
  152. int ts_is_stream_type_ac3 (uint8_t stream_type);
  153. int ts_is_stream_type_audio (uint8_t stream_type);
  154. char * h222_stream_type_desc (uint8_t stream_type);
  155. char * h222_stream_id_desc (uint8_t stream_id);
  156. // PES
  157. struct ts_pes * ts_pes_alloc ();
  158. void ts_pes_free (struct ts_pes **pes);
  159. struct ts_pes * ts_pes_reset (struct ts_pes *pes);
  160. void ts_pes_fill_type (struct ts_pes *pes, struct ts_pmt *pmt, uint16_t pid);
  161. int ts_pes_is_finished (struct ts_pes *pes, uint8_t *ts_packet);
  162. struct ts_pes * ts_pes_push_packet (struct ts_pes *pes, uint8_t *ts_packet, struct ts_pmt *pmt, uint16_t pid);
  163. int ts_pes_parse (struct ts_pes *pes);
  164. void ts_pes_dump (struct ts_pes *pes);
  165. struct pes_array * pes_array_alloc ();
  166. void pes_array_dump (struct pes_array *pa);
  167. void pes_array_free (struct pes_array **ppa);
  168. 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);
  169. // ES functions
  170. int ts_pes_es_mpeg_audio_header_parse (struct mpeg_audio_header *mpghdr, uint8_t *data, int datasz);
  171. void ts_pes_es_mpeg_audio_header_dump (struct mpeg_audio_header *mpghdr);
  172. void ts_pes_es_parse (struct ts_pes *pes);
  173. void ts_pes_es_dump (struct ts_pes *pes);
  174. // CRC
  175. uint32_t ts_crc32 (uint8_t *data, int data_size);
  176. // Misc
  177. int dec2bcd (int dec);
  178. int bcd2dec (int bcd);
  179. void ts_compare_data (char *prefix, uint8_t *a, uint8_t *b, int size);
  180. char * ts_hex_dump (uint8_t *d, int size);
  181. void ts_print_bytes (char *prefix, uint8_t *d, int size);
  182. char * init_dvb_string_utf8 (char *text);
  183. char * init_dvb_string_iso_8859_5 (char *text);
  184. int ts_is_psi_pid (uint16_t pid, struct ts_pat *pat);
  185. // Shortcuts
  186. int parse_tdt (uint8_t *ts_packet, int dump);
  187. #endif