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

eit.c 10KB

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