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.

nit.c 9.3KB

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