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.

pmt.c 9.2KB

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