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_pmt.c 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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_pmt *ts_pmt_alloc() {
  8. struct ts_pmt *pmt = calloc(1, sizeof(struct ts_pmt));
  9. pmt->section_header = ts_section_data_alloc();
  10. pmt->streams_max = 128;
  11. pmt->streams = calloc(pmt->streams_max, sizeof(void *));
  12. return pmt;
  13. }
  14. void ts_pmt_free(struct ts_pmt **ppmt) {
  15. struct ts_pmt *pmt = *ppmt;
  16. int i;
  17. if (pmt) {
  18. ts_section_data_free(&pmt->section_header);
  19. for (i=0;i<pmt->streams_num;i++) {
  20. FREE(pmt->streams[i]->ES_info);
  21. FREE(pmt->streams[i]);
  22. }
  23. FREE(pmt->program_info);
  24. FREE(pmt->streams);
  25. FREE(*ppmt);
  26. }
  27. }
  28. static struct ts_pmt *ts_pmt_reset(struct ts_pmt *pmt) {
  29. struct ts_pmt *newpmt = ts_pmt_alloc();
  30. ts_pmt_free(&pmt);
  31. return newpmt;
  32. }
  33. struct ts_pmt *ts_pmt_push_packet(struct ts_pmt *pmt, uint8_t *ts_packet, uint16_t pmt_pid) {
  34. struct ts_header ts_header;
  35. memset(&ts_header, 0, sizeof(struct ts_header));
  36. if (ts_packet_header_parse(ts_packet, &ts_header)) {
  37. if (ts_header.pid != pmt_pid)
  38. goto OUT;
  39. if (!pmt->ts_header.pusi)
  40. pmt->ts_header = ts_header;
  41. }
  42. if (ts_header.pusi) {
  43. struct ts_section_header section_header;
  44. memset(&section_header, 0, sizeof(struct ts_section_header));
  45. uint8_t *section_data = ts_section_header_parse(ts_packet, &pmt->ts_header, &section_header);
  46. if (!section_data || !section_header.section_syntax_indicator) {
  47. memset(&pmt->ts_header, 0, sizeof(struct ts_header));
  48. goto OUT;
  49. }
  50. // table_id should be 0x02 (program_map_section)
  51. if (section_header.table_id != 0x02) {
  52. memset(&pmt->ts_header, 0, sizeof(struct ts_header));
  53. goto OUT;
  54. }
  55. // Set correct section_header
  56. ts_section_header_parse(ts_packet, &pmt->ts_header, pmt->section_header);
  57. }
  58. if (!pmt->initialized) {
  59. if (pmt->section_header->section_syntax_indicator) {
  60. ts_section_add_packet(pmt->section_header, &ts_header, ts_packet);
  61. if (pmt->section_header->initialized) {
  62. if (!ts_pmt_parse(pmt))
  63. goto ERROR;
  64. }
  65. }
  66. }
  67. OUT:
  68. return pmt;
  69. ERROR:
  70. return ts_pmt_reset(pmt);
  71. }
  72. int ts_pmt_parse(struct ts_pmt *pmt) {
  73. uint8_t *section_data = pmt->section_header->data;
  74. int section_len = pmt->section_header->data_len;
  75. pmt->reserved1 = (section_data[0] &~ 0x1F) >> 5; // xxx11111
  76. pmt->PCR_pid = ((section_data[0] &~ 0xE0) << 8) | section_data[1]; // 111xxxxx xxxxxxxx
  77. pmt->reserved2 = (section_data[2] &~ 0x0F) >> 4; // xxxx1111
  78. pmt->program_info_size = ((section_data[2] &~ 0xF0) << 8) | section_data[3]; // 1111xxxx xxxxxxxx
  79. /* Handle streams */
  80. uint8_t *stream_data = section_data + 4 + pmt->program_info_size; // +4 is to compensate for reserved1,PCR,reserved2,program_info_size
  81. int stream_len = section_len - pmt->program_info_size - 4; // -4 for the CRC at the end
  82. pmt->program_info = NULL;
  83. if (pmt->program_info_size) {
  84. pmt->program_info = malloc(pmt->program_info_size);
  85. if (pmt->program_info) {
  86. memcpy(pmt->program_info, stream_data - pmt->program_info_size, pmt->program_info_size);
  87. }
  88. }
  89. while (stream_len > 0) {
  90. if (pmt->streams_num == pmt->streams_max) {
  91. ts_LOGf("PMT contains too many streams (>%d), not all are initialized!\n", pmt->streams_max);
  92. break;
  93. }
  94. struct ts_pmt_stream *sinfo = calloc(1, sizeof(struct ts_pmt_stream));
  95. sinfo->stream_type = stream_data[0];
  96. sinfo->reserved1 = (stream_data[1] &~ 0x1F) >> 5; // xxx11111
  97. sinfo->pid = ((stream_data[1] &~ 0xE0) << 8) | stream_data[2]; // 111xxxxx xxxxxxxx
  98. sinfo->reserved2 = (stream_data[3] &~ 0x0F) >> 4; // xxxx1111
  99. sinfo->ES_info_size = ((stream_data[3] &~ 0xF0) << 8) | stream_data[4]; // 1111xxxx xxxxxxxx
  100. sinfo->ES_info = NULL;
  101. if (sinfo->ES_info_size > 0) {
  102. sinfo->ES_info = malloc(sinfo->ES_info_size);
  103. memcpy(sinfo->ES_info, &stream_data[5], sinfo->ES_info_size);
  104. }
  105. pmt->streams[pmt->streams_num] = sinfo;
  106. pmt->streams_num++;
  107. stream_data += 5 + sinfo->ES_info_size;
  108. stream_len -= 5 + sinfo->ES_info_size;
  109. }
  110. pmt->CRC = (pmt->CRC << 8) | stream_data[3];
  111. pmt->CRC = (pmt->CRC << 8) | stream_data[2];
  112. pmt->CRC = (pmt->CRC << 8) | stream_data[1];
  113. pmt->CRC = (pmt->CRC << 8) | stream_data[0];
  114. u_int32_t check_crc = ts_crc32_section(pmt->section_header);
  115. if (check_crc != 0) {
  116. ts_LOGf("!!! Wrong PMT CRC! It should be 0 but it is %08x (CRC in data is 0x%08x)\n", check_crc, pmt->CRC);
  117. return 0;
  118. }
  119. pmt->initialized = 1;
  120. return 1;
  121. }
  122. void ts_pmt_generate(struct ts_pmt *pmt, uint8_t **ts_packets, int *num_packets) {
  123. uint8_t *secdata = ts_section_data_alloc_section();
  124. ts_section_header_generate(secdata, pmt->section_header, 0);
  125. int curpos = 8; // Compensate for the section header, frist data byte is at offset 8
  126. secdata[curpos + 0] = pmt->reserved1 << 5; // xxx11111
  127. secdata[curpos + 0] |= pmt->PCR_pid >> 8; // 111xxxxx xxxxxxxx
  128. secdata[curpos + 1] = pmt->PCR_pid &~ 0xff00;
  129. secdata[curpos + 2] = pmt->reserved2 << 4; // xxxx1111
  130. secdata[curpos + 2] |= pmt->program_info_size >> 8; // 1111xxxx xxxxxxxx
  131. secdata[curpos + 3] = pmt->program_info_size &~ 0xff00;
  132. curpos += 4; // For thje fields above
  133. if (pmt->program_info_size) {
  134. memcpy(secdata + curpos, pmt->program_info, pmt->program_info_size);
  135. curpos += pmt->program_info_size;
  136. }
  137. int i;
  138. for(i=0;i<pmt->streams_num;i++) {
  139. struct ts_pmt_stream *stream = pmt->streams[i];
  140. secdata[curpos + 0] = stream->stream_type;
  141. secdata[curpos + 1] = stream->reserved1 << 5; // xxx11111
  142. secdata[curpos + 1] |= stream->pid >> 8; // 111xxxxx xxxxxxxx
  143. secdata[curpos + 2] = stream->pid &~ 0xff00;
  144. secdata[curpos + 3] = stream->reserved2 << 4; // xxxx1111
  145. secdata[curpos + 3] |= stream->ES_info_size >> 8; // 1111xxxx xxxxxxxx
  146. secdata[curpos + 4] = stream->ES_info_size &~ 0xff00;
  147. curpos += 5; // Compensate for the above
  148. if (stream->ES_info_size > 0) {
  149. memcpy(secdata + curpos, stream->ES_info, stream->ES_info_size);
  150. curpos += stream->ES_info_size;
  151. }
  152. }
  153. pmt->CRC = ts_section_data_calculate_crc(secdata, curpos);
  154. curpos += 4; // CRC
  155. ts_section_data_gen_ts_packets(&pmt->ts_header, secdata, curpos, pmt->section_header->pointer_field, ts_packets, num_packets);
  156. FREE(secdata);
  157. }
  158. void ts_pmt_regenerate_packets(struct ts_pmt *pmt) {
  159. uint8_t *ts_packets;
  160. int num_packets;
  161. ts_pmt_generate(pmt, &ts_packets, &num_packets);
  162. FREE(pmt->section_header->packet_data);
  163. pmt->section_header->packet_data = ts_packets;
  164. pmt->section_header->num_packets = num_packets;
  165. }
  166. struct ts_pmt *ts_pmt_copy(struct ts_pmt *pmt) {
  167. struct ts_pmt *newpmt = ts_pmt_alloc();
  168. int i;
  169. for (i=0;i<pmt->section_header->num_packets; i++) {
  170. newpmt = ts_pmt_push_packet(newpmt, pmt->section_header->packet_data + (i * TS_PACKET_SIZE), pmt->ts_header.pid);
  171. }
  172. if (newpmt->initialized) {
  173. return newpmt;
  174. } else {
  175. ts_LOGf("Error copying PMT!\n");
  176. ts_pmt_free(&newpmt);
  177. return NULL;
  178. }
  179. }
  180. void ts_pmt_check_generator(struct ts_pmt *pmt) {
  181. struct ts_pmt *pmt1 = ts_pmt_copy(pmt);
  182. if (pmt1) {
  183. ts_compare_data("PMT (tspacket->struct)",
  184. pmt1->section_header->packet_data,
  185. pmt->section_header->packet_data,
  186. pmt->section_header->num_packets * TS_PACKET_SIZE);
  187. ts_pmt_free(&pmt1);
  188. }
  189. uint8_t *ts_packets;
  190. int num_packets;
  191. ts_pmt_generate(pmt, &ts_packets, &num_packets);
  192. if (num_packets != pmt->section_header->num_packets) {
  193. ts_LOGf("ERROR: num_packets:%d != sec->num_packets:%d\n", num_packets, pmt->section_header->num_packets);
  194. }
  195. ts_compare_data("PMT (struct->tspacket)", pmt->section_header->packet_data, ts_packets, num_packets * TS_PACKET_SIZE);
  196. free(ts_packets);
  197. }
  198. void ts_pmt_dump(struct ts_pmt *pmt) {
  199. int i;
  200. ts_LOGf("PMT packet\n");
  201. for(i=0;i<pmt->section_header->num_packets;i++) {
  202. struct ts_header tshdr;
  203. ts_packet_header_parse(pmt->section_header->packet_data + (i * TS_PACKET_SIZE), &tshdr);
  204. ts_packet_header_dump(&tshdr);
  205. }
  206. ts_section_header_dump(pmt->section_header);
  207. ts_LOGf(" * PMT data\n");
  208. ts_LOGf(" * PID : %04x (%d)\n", pmt->ts_header.pid, pmt->ts_header.pid);
  209. ts_LOGf(" * reserved1 : %d\n", pmt->reserved1);
  210. ts_LOGf(" * PCR PID : %04x (%d)\n", pmt->PCR_pid, pmt->PCR_pid);
  211. ts_LOGf(" * reserved2 : %d\n", pmt->reserved2);
  212. ts_LOGf(" * program_len : %d\n", pmt->program_info_size);
  213. ts_LOGf(" * num_streams : %d\n", pmt->streams_num);
  214. if (pmt->program_info_size > 0) {
  215. ts_LOGf(" * Program info:\n");
  216. ts_LOGf(" * program info size: %d\n", pmt->program_info_size);
  217. ts_descriptor_dump(pmt->program_info, pmt->program_info_size);
  218. }
  219. for(i=0;i<pmt->streams_num;i++) {
  220. struct ts_pmt_stream *stream = pmt->streams[i];
  221. ts_LOGf(" * [%02d/%02d] PID %04x (%d) -> Stream type: 0x%02x (%d) /es_info_size: %d/ %s\n",
  222. i+1, pmt->streams_num,
  223. stream->pid, stream->pid,
  224. stream->stream_type, stream->stream_type,
  225. stream->ES_info_size,
  226. h222_stream_type_desc(stream->stream_type)
  227. );
  228. if (stream->ES_info) {
  229. ts_descriptor_dump(stream->ES_info, stream->ES_info_size);
  230. }
  231. }
  232. ts_LOGf(" * CRC 0x%04x\n", pmt->CRC);
  233. ts_pmt_check_generator(pmt);
  234. }
  235. /*
  236. int parse_pmt(uint8_t *ts_packet, uint16_t pmt_pid, uint16_t *pcr_pid, uint16_t *video_pid, uint16_t *audio_pid, int dump) {
  237. struct ts_pmt *pmt = calloc(1, sizeof(struct ts_pmt));
  238. int ret = ts_pmt_init(pmt, pmt_pid, ts_packet);
  239. if (ret) {
  240. int i;
  241. *pcr_pid = pmt->PCR_pid;
  242. for (i=0;i<pmt->streams_num;i++) {
  243. struct ts_pmt_stream *stream = pmt->streams[i];
  244. if (ts_is_stream_type_video(stream->stream_type))
  245. *video_pid = stream->pid;
  246. if (ts_is_stream_type_audio(stream->stream_type))
  247. *audio_pid = stream->pid;
  248. }
  249. }
  250. if (dump)
  251. ts_pmt_dump(pmt);
  252. ts_pmt_free(&pmt);
  253. return ret;
  254. }
  255. */
  256. int ts_pmt_is_same(struct ts_pmt *pmt1, struct ts_pmt *pmt2) {
  257. int i;
  258. if (pmt1->CRC == pmt2->CRC) // Same
  259. return 1;
  260. // If some version is not current, just claim the structures are the same
  261. if (!pmt1->section_header->current_next_indicator || pmt2->section_header->version_number)
  262. return 1;
  263. if (pmt1->section_header->version_number != pmt2->section_header->version_number) // Different
  264. return 0;
  265. if (pmt1->PCR_pid != pmt2->PCR_pid) // Different
  266. return 0;
  267. if (pmt1->streams_num != pmt2->streams_num) // Different
  268. return 0;
  269. // Check each program and PIDs
  270. for (i=0;i<pmt1->streams_num;i++) {
  271. struct ts_pmt_stream *stream1 = pmt1->streams[i];
  272. struct ts_pmt_stream *stream2 = pmt2->streams[i];
  273. if (stream1->pid != stream2->pid) // Different
  274. return 0;
  275. }
  276. return 1; // Same
  277. }