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_nit.c 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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_nit *ts_nit_alloc() {
  8. struct ts_nit *nit = calloc(1, sizeof(struct ts_nit));
  9. nit->section_header = ts_section_data_alloc();
  10. nit->streams_max = 128;
  11. nit->streams = calloc(nit->streams_max, sizeof(void *));
  12. return nit;
  13. }
  14. void ts_nit_free(struct ts_nit **pnit) {
  15. struct ts_nit *nit = *pnit;
  16. int i;
  17. if (nit) {
  18. ts_section_data_free(&nit->section_header);
  19. FREE(nit->network_info);
  20. for (i=0;i<nit->streams_num;i++) {
  21. if (nit->streams[i]) {
  22. FREE(nit->streams[i]->descriptor_data);
  23. FREE(nit->streams[i]);
  24. }
  25. }
  26. FREE(nit->streams);
  27. FREE(*pnit);
  28. }
  29. }
  30. static struct ts_nit *ts_nit_reset(struct ts_nit *nit) {
  31. struct ts_nit *newnit = ts_nit_alloc();
  32. ts_nit_free(&nit);
  33. return newnit;
  34. }
  35. struct ts_nit *ts_nit_push_packet(struct ts_nit *nit, uint8_t *ts_packet) {
  36. struct ts_header ts_header;
  37. memset(&ts_header, 0, sizeof(struct ts_header));
  38. if (ts_packet_header_parse(ts_packet, &ts_header)) {
  39. // NIT should be with PID 0x10
  40. if (ts_header.pid != 0x10)
  41. goto OUT;
  42. if (!nit->ts_header.pusi)
  43. nit->ts_header = ts_header;
  44. }
  45. if (ts_header.pusi) {
  46. struct ts_section_header section_header;
  47. memset(&section_header, 0, sizeof(struct ts_section_header));
  48. uint8_t *section_data = ts_section_header_parse(ts_packet, &nit->ts_header, &section_header);
  49. if (!section_data || !section_header.section_syntax_indicator)
  50. goto OUT;
  51. // table_id should be 0x40 (network_information_section - actual_network)
  52. if (section_header.table_id != 0x40) {
  53. memset(&nit->ts_header, 0, sizeof(struct ts_header));
  54. goto OUT;
  55. }
  56. // Set correct section_header
  57. ts_section_header_parse(ts_packet, &nit->ts_header, nit->section_header);
  58. }
  59. if (!nit->initialized) {
  60. if (nit->section_header->section_syntax_indicator) {
  61. ts_section_add_packet(nit->section_header, &ts_header, ts_packet);
  62. if (nit->section_header->initialized) {
  63. if (!ts_nit_parse(nit))
  64. goto ERROR;
  65. }
  66. }
  67. }
  68. OUT:
  69. return nit;
  70. ERROR:
  71. return ts_nit_reset(nit);
  72. }
  73. int ts_nit_parse(struct ts_nit *nit) {
  74. uint8_t *section_data = nit->section_header->data;
  75. int section_len = nit->section_header->data_len;
  76. /* Table data (2 bytes) */
  77. nit->reserved1 = (section_data[0] &~ 0x0F) >> 4; // xxxx1111
  78. nit->network_info_size = ((section_data[0] &~ 0xF0) << 8) | section_data[1]; // 1111xxxx xxxxxxxx
  79. /* Handle streams */
  80. uint8_t *stream_data = section_data + 2 + nit->network_info_size; // +2 is to compensate for reserved1 and network_info_size
  81. int stream_len = section_len - nit->network_info_size - 4; // -4 for the CRC at the end
  82. nit->network_info = NULL;
  83. if (nit->network_info_size) {
  84. nit->network_info = malloc(nit->network_info_size);
  85. if (nit->network_info) {
  86. memcpy(nit->network_info, stream_data - nit->network_info_size, nit->network_info_size);
  87. }
  88. }
  89. // Before the table there are two more fields
  90. nit->reserved2 = (stream_data[0] &~ 0x0F) >> 4; // xxxx1111
  91. nit->ts_loop_size = ((stream_data[0] &~ 0xF0) << 8) | stream_data[1]; // 1111xxxx xxxxxxxx
  92. stream_data += 2;
  93. stream_len = nit->ts_loop_size;
  94. while (stream_len > 0) {
  95. if (nit->streams_num == nit->streams_max) {
  96. ts_LOGf("!!! Too many streams in NIT, max %d\n", nit->streams_max);
  97. break;
  98. }
  99. struct ts_nit_stream *sinfo = calloc(1, sizeof(struct ts_nit_stream));
  100. sinfo->transport_stream_id = (stream_data[0] << 8) | stream_data[1];
  101. sinfo->original_network_id = (stream_data[2] << 8) | stream_data[3];
  102. sinfo->reserved1 = (stream_data[4] &~ 0x0F) >> 4; // xxxx1111
  103. sinfo->descriptor_size = ((stream_data[4] &~ 0xF0) << 8) | stream_data[5]; // 1111xxxx xxxxxxxx
  104. sinfo->descriptor_data = NULL;
  105. if (sinfo->descriptor_size > 0) {
  106. sinfo->descriptor_data = malloc(sinfo->descriptor_size);
  107. memcpy(sinfo->descriptor_data, &stream_data[6], sinfo->descriptor_size);
  108. }
  109. nit->streams[nit->streams_num] = sinfo;
  110. nit->streams_num++;
  111. stream_data += 6 + sinfo->descriptor_size;
  112. stream_len -= 6 + sinfo->descriptor_size;
  113. }
  114. nit->CRC = (nit->CRC << 8) | stream_data[3];
  115. nit->CRC = (nit->CRC << 8) | stream_data[2];
  116. nit->CRC = (nit->CRC << 8) | stream_data[1];
  117. nit->CRC = (nit->CRC << 8) | stream_data[0];
  118. u_int32_t check_crc = ts_crc32_section(nit->section_header);
  119. if (check_crc != 0) {
  120. ts_LOGf("!!! Wrong NIT CRC! It should be 0 but it is %08x (CRC in data is 0x%08x)\n", check_crc, nit->CRC);
  121. return 0;
  122. }
  123. nit->initialized = 1;
  124. return 1;
  125. }
  126. void ts_nit_generate(struct ts_nit *nit, uint8_t **ts_packets, int *num_packets) {
  127. uint8_t *secdata = ts_section_data_alloc_section();
  128. ts_section_header_generate(secdata, nit->section_header, 0);
  129. int curpos = 8; // Compensate for the section header, frist data byte is at offset 8
  130. secdata[curpos + 0] = nit->reserved1 << 4; // xxxx1111
  131. secdata[curpos + 0] |= nit->network_info_size >> 8; // 1111xxxx xxxxxxxx
  132. secdata[curpos + 1] = nit->network_info_size &~ 0xff00;
  133. curpos += 2; // For the fields above
  134. if (nit->network_info_size) {
  135. memcpy(secdata + curpos, nit->network_info, nit->network_info_size);
  136. curpos += nit->network_info_size;
  137. }
  138. // Before the table there are two more fields
  139. secdata[curpos + 0] = nit->reserved2 << 4; // xxxx1111
  140. secdata[curpos + 0] |= nit->ts_loop_size >> 8; // 1111xxxx xxxxxxxx
  141. secdata[curpos + 1] = nit->ts_loop_size &~ 0xff00;
  142. curpos += 2; // For the fields above
  143. int i;
  144. for(i=0;i<nit->streams_num;i++) {
  145. struct ts_nit_stream *stream = nit->streams[i];
  146. secdata[curpos + 0] = stream->transport_stream_id >> 8; // xxxxxxxx xxxxxxxx
  147. secdata[curpos + 1] = stream->transport_stream_id &~ 0xff00;
  148. secdata[curpos + 2] = stream->original_network_id >> 8; // xxxxxxxx xxxxxxxx
  149. secdata[curpos + 3] = stream->original_network_id &~ 0xff00;
  150. secdata[curpos + 4] = stream->reserved1 << 4; // xxxx1111
  151. secdata[curpos + 4] |= stream->descriptor_size >> 8; // 1111xxxx xxxxxxxx
  152. secdata[curpos + 5] = stream->descriptor_size &~ 0xff00;
  153. curpos += 6; // Compensate for the above
  154. if (stream->descriptor_size > 0) {
  155. memcpy(secdata + curpos, stream->descriptor_data, stream->descriptor_size);
  156. curpos += stream->descriptor_size;
  157. }
  158. }
  159. nit->CRC = ts_section_data_calculate_crc(secdata, curpos);
  160. curpos += 4; // CRC
  161. ts_section_data_gen_ts_packets(&nit->ts_header, secdata, curpos, nit->section_header->pointer_field, ts_packets, num_packets);
  162. FREE(secdata);
  163. }
  164. void ts_nit_check_generator(struct ts_nit *nit) {
  165. struct ts_nit *nit1 = ts_nit_alloc();
  166. int i;
  167. for (i=0;i<nit->section_header->num_packets;i++) {
  168. nit1 = ts_nit_push_packet(nit1, nit->section_header->packet_data + (i * TS_PACKET_SIZE));
  169. }
  170. ts_compare_data("NIT (tspacket->struct)",
  171. nit1->section_header->packet_data,
  172. nit->section_header->packet_data,
  173. nit->section_header->num_packets * TS_PACKET_SIZE);
  174. ts_nit_free(&nit1);
  175. uint8_t *ts_packets;
  176. int num_packets;
  177. ts_nit_generate(nit, &ts_packets, &num_packets);
  178. if (num_packets != nit->section_header->num_packets) {
  179. ts_LOGf("ERROR: num_packets:%d != sec->num_packets:%d\n", num_packets, nit->section_header->num_packets);
  180. }
  181. ts_compare_data("NIT (struct->tspacket)", nit->section_header->packet_data, ts_packets, num_packets * TS_PACKET_SIZE);
  182. free(ts_packets);
  183. }
  184. void ts_nit_dump(struct ts_nit *nit) {
  185. int i;
  186. ts_LOGf("NIT table\n");
  187. ts_LOGf(" * TS Packets\n");
  188. for(i=0;i<nit->section_header->num_packets;i++) {
  189. struct ts_header tshdr;
  190. ts_packet_header_parse(nit->section_header->packet_data + (i * TS_PACKET_SIZE), &tshdr);
  191. ts_packet_header_dump(&tshdr);
  192. }
  193. ts_section_header_dump(nit->section_header);
  194. ts_LOGf(" * NIT data\n");
  195. ts_LOGf(" * PID : 0x%04x (%d)\n", nit->ts_header.pid, nit->ts_header.pid);
  196. ts_LOGf(" * reserved1 : 0x%02x\n", nit->reserved1);
  197. ts_LOGf(" * network_len : 0x%02x (%d)\n", nit->network_info_size, nit->network_info_size);
  198. ts_LOGf(" * reserved2 : 0x%02x\n", nit->reserved1);
  199. ts_LOGf(" * ts_loop_len : %d\n", nit->ts_loop_size);
  200. ts_LOGf(" * num_streams : %d\n", nit->streams_num);
  201. if (nit->network_info_size > 0) {
  202. ts_LOGf(" * Network info:\n");
  203. ts_LOGf(" * network info size: %d\n", nit->network_info_size);
  204. ts_descriptor_dump(nit->network_info, nit->network_info_size);
  205. }
  206. for(i=0;i<nit->streams_num;i++) {
  207. struct ts_nit_stream *stream = nit->streams[i];
  208. ts_LOGf(" - [%02d/%02d] | TS_id: 0x%04x (%d) ORG_net_id: 0x%04x (%d) Reserved: 0x%0x Desc_size: %d\n",
  209. i+1, nit->streams_num,
  210. stream->transport_stream_id, stream->transport_stream_id,
  211. stream->original_network_id, stream->original_network_id,
  212. stream->reserved1,
  213. stream->descriptor_size);
  214. if (stream->descriptor_data) {
  215. ts_descriptor_dump(stream->descriptor_data, stream->descriptor_size);
  216. }
  217. }
  218. ts_LOGf(" * CRC 0x%04x\n", nit->CRC);
  219. ts_nit_check_generator(nit);
  220. }