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_sdt.c 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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_sdt *ts_sdt_alloc() {
  8. struct ts_sdt *sdt = calloc(1, sizeof(struct ts_sdt));
  9. sdt->section_header = ts_section_data_alloc();
  10. sdt->streams_max = 128;
  11. sdt->streams = calloc(sdt->streams_max, sizeof(void *));
  12. return sdt;
  13. }
  14. void ts_sdt_free(struct ts_sdt **psdt) {
  15. struct ts_sdt *sdt = *psdt;
  16. int i;
  17. if (sdt) {
  18. ts_section_data_free(&sdt->section_header);
  19. for (i=0;i<sdt->streams_num;i++) {
  20. FREE(sdt->streams[i]->descriptor_data);
  21. FREE(sdt->streams[i]);
  22. }
  23. FREE(sdt->streams);
  24. FREE(*psdt);
  25. }
  26. }
  27. static struct ts_sdt *ts_sdt_reset(struct ts_sdt *sdt) {
  28. struct ts_sdt *newsdt = ts_sdt_alloc();
  29. ts_sdt_free(&sdt);
  30. return newsdt;
  31. }
  32. struct ts_sdt *ts_sdt_push_packet(struct ts_sdt *sdt, uint8_t *ts_packet) {
  33. struct ts_header ts_header;
  34. memset(&ts_header, 0, sizeof(struct ts_header));
  35. if (ts_packet_header_parse(ts_packet, &ts_header)) {
  36. // SDT should be with PID 0x11
  37. if (ts_header.pid != 0x11)
  38. goto OUT;
  39. if (!sdt->ts_header.pusi)
  40. sdt->ts_header = ts_header;
  41. }
  42. if (ts_header.pusi) {
  43. struct ts_section_header section_header;
  44. memset(&section_header, 0, sizeof(struct ts_section_header));
  45. uint8_t *section_data = ts_section_header_parse(ts_packet, &sdt->ts_header, &section_header);
  46. if (!section_data || !section_header.section_syntax_indicator) {
  47. memset(&sdt->ts_header, 0, sizeof(struct ts_header));
  48. goto OUT;
  49. }
  50. // table_id should be 0x42 (service_description_section - actual_transport_stream)
  51. if (section_header.table_id != 0x42) {
  52. memset(&sdt->ts_header, 0, sizeof(struct ts_header));
  53. goto OUT;
  54. }
  55. // Set correct section_header
  56. ts_section_header_parse(ts_packet, &sdt->ts_header, sdt->section_header);
  57. }
  58. if (!sdt->initialized) {
  59. if (sdt->section_header->section_syntax_indicator) {
  60. ts_section_add_packet(sdt->section_header, &ts_header, ts_packet);
  61. if (sdt->section_header->initialized) {
  62. if (!ts_sdt_parse(sdt))
  63. goto ERROR;
  64. }
  65. }
  66. }
  67. OUT:
  68. return sdt;
  69. ERROR:
  70. return ts_sdt_reset(sdt);
  71. }
  72. int ts_sdt_parse(struct ts_sdt *sdt) {
  73. uint8_t *section_data = sdt->section_header->section_data + 8; // + 8 to compensate for section table header
  74. int section_len = sdt->section_header->packet_section_len;
  75. // 3 bytes
  76. sdt->original_network_id = (section_data[0] << 8) | section_data[1];
  77. sdt->reserved = section_data[2];
  78. section_data = section_data + 3;
  79. section_len = section_len -3;
  80. while (section_len > 0) {
  81. if (sdt->streams_num == sdt->streams_max) {
  82. ts_LOGf("SDT contains too many streams (>%d), not all are initialized!\n", sdt->streams_max);
  83. break;
  84. }
  85. struct ts_sdt_stream *sinfo = calloc(1, sizeof(struct ts_sdt_stream));
  86. sinfo->service_id = (section_data[0] << 8) | section_data[1];
  87. sinfo->reserved1 = (section_data[2] &~ 0x03) >> 2; // xxxxxx11
  88. sinfo->EIT_schedule_flag = (section_data[2] &~ 0xFD) >> 1; // 111111x1
  89. sinfo->EIT_present_following_flag = (section_data[2] &~ 0xFE); // 1111111x
  90. sinfo->running_status = section_data[3] >> 5; // 111xxxxx
  91. sinfo->free_CA_mode = (section_data[3] &~ 0xE0) >> 4; // xxx1xxxx
  92. sinfo->descriptor_size = ((section_data[3] &~ 0xF0) << 8) | section_data[4]; // 1111xxxx xxxxxxxx
  93. sinfo->descriptor_data = NULL;
  94. if (sinfo->descriptor_size > 0) {
  95. sinfo->descriptor_data = malloc(sinfo->descriptor_size);
  96. memcpy(sinfo->descriptor_data, &section_data[5], sinfo->descriptor_size);
  97. }
  98. sdt->streams[sdt->streams_num] = sinfo;
  99. sdt->streams_num++;
  100. section_data += 5 + sinfo->descriptor_size;
  101. section_len -= 5 + sinfo->descriptor_size;
  102. }
  103. sdt->CRC = (sdt->CRC << 8) | section_data[3];
  104. sdt->CRC = (sdt->CRC << 8) | section_data[2];
  105. sdt->CRC = (sdt->CRC << 8) | section_data[1];
  106. sdt->CRC = (sdt->CRC << 8) | section_data[0];
  107. u_int32_t check_crc = ts_crc32(sdt->section_header->section_data, sdt->section_header->data_size);
  108. if (check_crc != 0) {
  109. ts_LOGf("!!! Wrong SDT CRC! It should be 0 but it is %08x (CRC in data is 0x%08x)\n", check_crc, sdt->CRC);
  110. return 0;
  111. }
  112. sdt->initialized = 1;
  113. return 1;
  114. }
  115. void ts_sdt_generate(struct ts_sdt *sdt, uint8_t **ts_packets, int *num_packets) {
  116. uint8_t *secdata = ts_section_data_alloc_section();
  117. ts_section_header_generate(secdata, sdt->section_header, 0);
  118. int curpos = 8; // Compensate for the section header, frist data byte is at offset 8
  119. secdata[curpos + 0] = sdt->original_network_id >> 8; // 1111xxxx xxxxxxxx
  120. secdata[curpos + 1] = sdt->original_network_id &~ 0xff00;
  121. secdata[curpos + 2] = sdt->reserved;
  122. curpos += 3; // For the fields above
  123. int i;
  124. for(i=0;i<sdt->streams_num;i++) {
  125. struct ts_sdt_stream *stream = sdt->streams[i];
  126. secdata[curpos + 0] = stream->service_id >> 8; // xxxxxxxx xxxxxxxx
  127. secdata[curpos + 1] = stream->service_id &~ 0xff00;
  128. secdata[curpos + 2] = stream->reserved1 << 2; // xxxxxx11
  129. secdata[curpos + 2] |= stream->EIT_schedule_flag << 1; // 111111x1
  130. secdata[curpos + 2] |= stream->EIT_present_following_flag; // 1111111x
  131. secdata[curpos + 3] = stream->running_status << 5; // 111xxxxx
  132. secdata[curpos + 3] |= stream->free_CA_mode << 4; // xxx1xxxx
  133. secdata[curpos + 3] |= stream->descriptor_size >> 8; // 1111xxxx xxxxxxxx
  134. secdata[curpos + 4] = stream->descriptor_size &~ 0xff00;
  135. curpos += 5; // Compensate for the above
  136. if (stream->descriptor_size > 0) {
  137. memcpy(secdata + curpos, stream->descriptor_data, stream->descriptor_size);
  138. curpos += stream->descriptor_size;
  139. }
  140. }
  141. sdt->CRC = ts_section_data_calculate_crc(secdata, curpos);
  142. curpos += 4; // CRC
  143. ts_section_data_gen_ts_packets(&sdt->ts_header, secdata, curpos, sdt->section_header->pointer_field, ts_packets, num_packets);
  144. FREE(secdata);
  145. }
  146. void ts_sdt_check_generator(struct ts_sdt *sdt) {
  147. struct ts_sdt *sdt1 = ts_sdt_alloc();
  148. int i;
  149. for (i=0;i<sdt->section_header->num_packets;i++) {
  150. sdt1 = ts_sdt_push_packet(sdt1, sdt->section_header->packet_data + (i * TS_PACKET_SIZE));
  151. }
  152. ts_compare_data("SDT (tspacket->struct)",
  153. sdt1->section_header->packet_data,
  154. sdt->section_header->packet_data,
  155. sdt->section_header->num_packets * TS_PACKET_SIZE);
  156. ts_sdt_free(&sdt1);
  157. uint8_t *ts_packets;
  158. int num_packets;
  159. ts_sdt_generate(sdt, &ts_packets, &num_packets);
  160. if (num_packets != sdt->section_header->num_packets) {
  161. ts_LOGf("ERROR: num_packets:%d != sec->num_packets:%d\n", num_packets, sdt->section_header->num_packets);
  162. }
  163. ts_compare_data("SDT (struct->tspacket)", sdt->section_header->packet_data, ts_packets, num_packets * TS_PACKET_SIZE);
  164. free(ts_packets);
  165. }
  166. void ts_sdt_dump(struct ts_sdt *sdt) {
  167. int i;
  168. ts_LOGf("SDT packet\n");
  169. for(i=0;i<sdt->section_header->num_packets;i++) {
  170. struct ts_header tshdr;
  171. ts_packet_header_parse(sdt->section_header->packet_data + (i * TS_PACKET_SIZE), &tshdr);
  172. ts_packet_header_dump(&tshdr);
  173. }
  174. ts_section_header_dump(sdt->section_header);
  175. ts_LOGf(" * SDT data\n");
  176. ts_LOGf(" * PID : %04x (%d)\n", sdt->ts_header.pid, sdt->ts_header.pid);
  177. ts_LOGf(" * org_net_id : %04x (%d)\n", sdt->original_network_id, sdt->original_network_id);
  178. ts_LOGf(" * reserved : %d\n", sdt->reserved);
  179. ts_LOGf(" * num_streams : %d\n", sdt->streams_num);
  180. for(i=0;i<sdt->streams_num;i++) {
  181. struct ts_sdt_stream *stream = sdt->streams[i];
  182. ts_LOGf(" * [%02d/%02d] Service_id: %04x (%d) Res1: %d EIT_schedule: %d EIT_present: %d Running_status: %d free_CA_mode: %d /es_info_size: %d/\n",
  183. i+1, sdt->streams_num,
  184. stream->service_id, stream->service_id,
  185. stream->reserved1,
  186. stream->EIT_schedule_flag,
  187. stream->EIT_present_following_flag,
  188. stream->running_status,
  189. stream->free_CA_mode,
  190. stream->descriptor_size);
  191. if (stream->descriptor_data) {
  192. ts_descriptor_dump(stream->descriptor_data, stream->descriptor_size);
  193. }
  194. }
  195. ts_LOGf(" * CRC 0x%04x\n", sdt->CRC);
  196. ts_sdt_check_generator(sdt);
  197. }