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.

sdt.c 8.4KB

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