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_eit.c 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <netdb.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "tsfuncs.h"
  7. struct ts_eit *ts_eit_alloc() {
  8. struct ts_eit *eit = calloc(1, sizeof(struct ts_eit));
  9. eit->section_header = ts_section_data_alloc();
  10. eit->streams_max = 128;
  11. eit->streams = calloc(eit->streams_max, sizeof(void *));
  12. return eit;
  13. }
  14. void ts_eit_free(struct ts_eit **peit) {
  15. struct ts_eit *eit = *peit;
  16. if (eit) {
  17. int i;
  18. ts_section_data_free(&eit->section_header);
  19. for (i=0;i<eit->streams_num;i++) {
  20. if (eit->streams[i]) {
  21. FREE(eit->streams[i]->descriptor_data);
  22. FREE(eit->streams[i]);
  23. }
  24. }
  25. FREE(eit->streams);
  26. FREE(*peit);
  27. }
  28. }
  29. static struct ts_eit *ts_eit_reset(struct ts_eit *eit) {
  30. struct ts_eit *neweit = ts_eit_alloc();
  31. ts_eit_free(&eit);
  32. return neweit;
  33. }
  34. struct ts_eit *ts_eit_push_packet(struct ts_eit *eit, uint8_t *ts_packet) {
  35. struct ts_header ts_header;
  36. memset(&ts_header, 0, sizeof(struct ts_header));
  37. if (ts_packet_header_parse(ts_packet, &ts_header)) {
  38. // EIT should be with PID 0x12
  39. if (ts_header.pid != 0x12)
  40. goto OUT;
  41. if (!eit->ts_header.pusi)
  42. eit->ts_header = ts_header;
  43. }
  44. if (ts_header.pusi) {
  45. struct ts_section_header section_header;
  46. memset(&section_header, 0, sizeof(struct ts_section_header));
  47. uint8_t *section_data = ts_section_header_parse(ts_packet, &eit->ts_header, &section_header);
  48. if (!section_data || !section_header.section_syntax_indicator) {
  49. memset(&eit->ts_header, 0, sizeof(struct ts_header));
  50. goto OUT;
  51. }
  52. // table_id should be 0x4e (event_information_section - actual_transport_stream, present/following)
  53. // table_id 0x50 - 0x5f (event_information_section - actual_transport_stream, schedule)
  54. if (section_header.table_id != 0x4e && (section_header.table_id < 0x50 && section_header.table_id > 0x5f)) {
  55. memset(&eit->ts_header, 0, sizeof(struct ts_header));
  56. goto OUT;
  57. }
  58. // Set correct section_header
  59. ts_section_header_parse(ts_packet, &eit->ts_header, eit->section_header);
  60. }
  61. if (!eit->initialized) {
  62. if (eit->section_header->section_syntax_indicator) {
  63. ts_section_add_packet(eit->section_header, &ts_header, ts_packet);
  64. if (eit->section_header->initialized) {
  65. if (!ts_eit_parse(eit))
  66. goto ERROR;
  67. }
  68. }
  69. }
  70. OUT:
  71. return eit;
  72. ERROR:
  73. return ts_eit_reset(eit);
  74. }
  75. int ts_eit_parse(struct ts_eit *eit) {
  76. uint8_t *section_data = eit->section_header->data;
  77. int section_len = eit->section_header->data_len;
  78. /* Table data (6 bytes) */
  79. eit->transport_stream_id = (section_data[0] << 8) | section_data[1]; // 11111111 11111111
  80. eit->original_network_id = (section_data[2] << 8) | section_data[3]; // 11111111 11111111
  81. eit->segment_last_section_number = section_data[4];
  82. eit->last_table_id = section_data[5];
  83. uint8_t *stream_data = section_data + 6; // +5 is to compensate for the above
  84. int stream_len = section_len - 6 - 4; // -4 for the CRC at the end
  85. while (stream_len > 0) {
  86. if (eit->streams_num == eit->streams_max) {
  87. ts_LOGf("!!! Too many streams in EIT, max %d\n", eit->streams_max);
  88. break;
  89. }
  90. struct ts_eit_stream *sinfo = calloc(1, sizeof(struct ts_eit_stream));
  91. sinfo->event_id = (stream_data[0] << 8) | stream_data[1];
  92. sinfo->start_time_mjd = (stream_data[2] << 8) | stream_data[3];
  93. sinfo->start_time_bcd = stream_data[4] << 16;
  94. sinfo->start_time_bcd |= stream_data[5] << 8;
  95. sinfo->start_time_bcd |= stream_data[6];
  96. sinfo->duration_bcd = stream_data[7] << 16;
  97. sinfo->duration_bcd |= stream_data[8] << 8;
  98. sinfo->duration_bcd |= stream_data[9];
  99. sinfo->running_status = stream_data[10] >> 5; // 111xxxxx
  100. sinfo->free_CA_mode = (stream_data[10] &~ 0xE0) >> 4; // xxx1xxxx
  101. sinfo->descriptor_size = ((stream_data[10] &~ 0xF0) << 8) | stream_data[11]; // 1111xxxx xxxxxxxx
  102. stream_data += 12; // Compensate for the the above vars
  103. stream_len -= 12 + sinfo->descriptor_size;
  104. sinfo->descriptor_data = NULL;
  105. if (sinfo->descriptor_size > 0) {
  106. sinfo->descriptor_data = malloc(sinfo->descriptor_size);
  107. memcpy(sinfo->descriptor_data, stream_data, sinfo->descriptor_size);
  108. }
  109. eit->streams[eit->streams_num] = sinfo;
  110. eit->streams_num++;
  111. stream_data += sinfo->descriptor_size;
  112. }
  113. eit->CRC = (eit->CRC << 8) | stream_data[3];
  114. eit->CRC = (eit->CRC << 8) | stream_data[2];
  115. eit->CRC = (eit->CRC << 8) | stream_data[1];
  116. eit->CRC = (eit->CRC << 8) | stream_data[0];
  117. u_int32_t check_crc = ts_crc32_section(eit->section_header);
  118. if (check_crc != 0) {
  119. ts_LOGf("!!! Wrong EIT CRC! It should be 0 but it is %08x (CRC in data is 0x%08x)\n", check_crc, eit->CRC);
  120. return 0;
  121. }
  122. eit->initialized = 1;
  123. return 1;
  124. }
  125. void ts_eit_generate(struct ts_eit *eit, uint8_t **ts_packets, int *num_packets) {
  126. uint8_t *secdata = ts_section_data_alloc_section();
  127. ts_section_header_generate(secdata, eit->section_header, 0);
  128. int curpos = 8; // Compensate for the section header, frist data byte is at offset 8
  129. secdata[curpos + 0] = eit->transport_stream_id >> 8; // xxxxxxxx xxxxxxxx
  130. secdata[curpos + 1] = eit->transport_stream_id &~ 0xff00;
  131. secdata[curpos + 2] = eit->original_network_id >> 8; // xxxxxxxx xxxxxxxx
  132. secdata[curpos + 3] = eit->original_network_id &~ 0xff00;
  133. secdata[curpos + 4] = eit->segment_last_section_number;
  134. secdata[curpos + 5] = eit->last_table_id;
  135. curpos += 6; // For the fields above
  136. int i;
  137. for(i=0;i<eit->streams_num;i++) {
  138. struct ts_eit_stream *stream = eit->streams[i];
  139. secdata[curpos + 0] = stream->event_id >> 8; // xxxxxxxx xxxxxxxx
  140. secdata[curpos + 1] = stream->event_id &~ 0xff00;
  141. secdata[curpos + 2] = stream->start_time_mjd >> 8; // xxxxxxxx xxxxxxxx
  142. secdata[curpos + 3] = stream->start_time_mjd &~ 0xff00;
  143. secdata[curpos + 4] = stream->start_time_bcd >> 16;
  144. secdata[curpos + 5] =(stream->start_time_bcd >> 8) &~ 0xff00;
  145. secdata[curpos + 6] = stream->start_time_bcd &~ 0xffff00;
  146. secdata[curpos + 7] = stream->duration_bcd >> 16;
  147. secdata[curpos + 8] =(stream->duration_bcd >> 8) &~ 0xff00;
  148. secdata[curpos + 9] = stream->duration_bcd &~ 0xffff00;
  149. secdata[curpos +10] = stream->running_status << 5; // 111xxxxx
  150. secdata[curpos +10] |= stream->free_CA_mode << 4; // xxx1xxxx
  151. secdata[curpos +10] |= stream->descriptor_size >> 8; // 1111xxxx xxxxxxxx
  152. secdata[curpos +11] = stream->descriptor_size &~ 0xff00;
  153. curpos += 12; // Compensate for the above
  154. if (stream->descriptor_size > 0) {
  155. memcpy(secdata + curpos, stream->descriptor_data, stream->descriptor_size);
  156. curpos += stream->descriptor_size;
  157. }
  158. }
  159. eit->CRC = ts_section_data_calculate_crc(secdata, curpos);
  160. curpos += 4; // CRC
  161. ts_section_data_gen_ts_packets(&eit->ts_header, secdata, curpos, eit->section_header->pointer_field, ts_packets, num_packets);
  162. FREE(secdata);
  163. }
  164. void ts_eit_check_generator(struct ts_eit *eit) {
  165. struct ts_eit *eit1 = ts_eit_alloc();
  166. int i;
  167. for (i=0;i<eit->section_header->num_packets;i++) {
  168. eit1 = ts_eit_push_packet(eit1, eit->section_header->packet_data + (i * TS_PACKET_SIZE));
  169. }
  170. ts_compare_data("EIT (tspacket->struct)",
  171. eit1->section_header->packet_data,
  172. eit->section_header->packet_data,
  173. eit->section_header->num_packets * TS_PACKET_SIZE);
  174. ts_eit_free(&eit1);
  175. uint8_t *ts_packets;
  176. int num_packets;
  177. ts_eit_generate(eit, &ts_packets, &num_packets);
  178. if (num_packets != eit->section_header->num_packets) {
  179. ts_LOGf("ERROR: num_packets:%d != sec->num_packets:%d\n", num_packets, eit->section_header->num_packets);
  180. }
  181. ts_compare_data("EIT (struct->tspacket)", eit->section_header->packet_data, ts_packets, num_packets * TS_PACKET_SIZE);
  182. free(ts_packets);
  183. }
  184. void ts_eit_regenerate_packets(struct ts_eit *eit) {
  185. uint8_t *ts_packets;
  186. int num_packets;
  187. ts_eit_generate(eit, &ts_packets, &num_packets);
  188. FREE(eit->section_header->packet_data);
  189. eit->section_header->packet_data = ts_packets;
  190. eit->section_header->num_packets = num_packets;
  191. }
  192. struct ts_eit *ts_eit_copy(struct ts_eit *eit) {
  193. struct ts_eit *neweit = ts_eit_alloc();
  194. int i;
  195. for (i=0;i<eit->section_header->num_packets; i++) {
  196. neweit = ts_eit_push_packet(neweit, eit->section_header->packet_data + (i * TS_PACKET_SIZE));
  197. }
  198. if (neweit->initialized) {
  199. return neweit;
  200. } else {
  201. ts_LOGf("Error copying EIT!\n");
  202. ts_eit_free(&neweit);
  203. return NULL;
  204. }
  205. }
  206. void ts_eit_dump(struct ts_eit *eit) {
  207. int i;
  208. ts_LOGf("EIT table\n");
  209. ts_LOGf(" * TS Packets\n");
  210. for(i=0;i<eit->section_header->num_packets;i++) {
  211. struct ts_header tshdr;
  212. ts_packet_header_parse(eit->section_header->packet_data + (i * TS_PACKET_SIZE), &tshdr);
  213. ts_packet_header_dump(&tshdr);
  214. }
  215. ts_section_header_dump(eit->section_header);
  216. ts_LOGf(" * EIT data\n");
  217. ts_LOGf(" * PID : 0x%04x (%d)\n", eit->ts_header.pid, eit->ts_header.pid);
  218. ts_LOGf(" * ts_stream_id : 0x%04x (%d)\n", eit->transport_stream_id, eit->transport_stream_id);
  219. ts_LOGf(" * org_network_id : 0x%04x (%d)\n", eit->original_network_id, eit->original_network_id);
  220. ts_LOGf(" * seg_last_sec_num: %d\n", eit->segment_last_section_number);
  221. ts_LOGf(" * last_table_id : 0x%02x (%d)\n", eit->last_table_id, eit->last_table_id);
  222. ts_LOGf(" * num_streams : %d\n", eit->streams_num);
  223. for(i=0;i<eit->streams_num;i++) {
  224. struct ts_eit_stream *stream = eit->streams[i];
  225. int hour, min, sec;
  226. struct tm tm;
  227. ts_time_decode_mjd(stream->start_time_mjd, stream->start_time_bcd, &tm);
  228. ts_time_decode_bcd(stream->duration_bcd, NULL, &hour, &min, &sec);
  229. ts_LOGf(" * Event_id [%02d/%02d]\n", i+1, eit->streams_num);
  230. ts_LOGf(" - Event_id : 0x%04x (%d)\n", stream->event_id, stream->event_id);
  231. ts_LOGf(" - Start_time: %04d-%02d-%02d %02d:%02d:%02d (0x%04x%06x) ts: %ld\n",
  232. tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
  233. stream->start_time_mjd,
  234. stream->start_time_bcd, timegm(&tm));
  235. ts_LOGf(" - Duration : %02d:%02d:%02d (0x%06x)\n",
  236. hour, min, sec,
  237. stream->duration_bcd);
  238. ts_LOGf(" - Running_status: %d free_CA_mode: %d /desc_size: %d/\n",
  239. stream->running_status,
  240. stream->free_CA_mode,
  241. stream->descriptor_size);
  242. if (stream->descriptor_data) {
  243. ts_descriptor_dump(stream->descriptor_data, stream->descriptor_size);
  244. }
  245. }
  246. ts_LOGf(" * CRC 0x%04x\n", eit->CRC);
  247. ts_eit_check_generator(eit);
  248. }