libtsfuncs is a library for mpeg PSI parsing and generation. https://georgi.unixsol.org/programs/libtsfuncs/

tsfuncs_pmt.c 8.9KB

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