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

sdt.c 8.1KB

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