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.3KB

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