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_sections.c 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <netdb.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <ctype.h>
  7. #include "tsfuncs.h"
  8. uint8_t *ts_section_header_parse(uint8_t *ts_packet, struct ts_header *ts_header, struct ts_section_header *ts_section_header) {
  9. if (ts_header->payload_offset + 8 > TS_PACKET_SIZE) {
  10. ts_packet_header_dump(ts_header);
  11. ts_LOGf("!!! Section start outside of TS packet %d!\n", ts_header->payload_offset + 8);
  12. return NULL;
  13. }
  14. uint8_t *data = ts_packet + ts_header->payload_offset;
  15. ts_section_header->pointer_field = data[0];
  16. data += ts_section_header->pointer_field + 1;
  17. ts_section_header->table_id = data[0];
  18. ts_section_header->section_syntax_indicator = data[1] >> 7; // x1111111
  19. ts_section_header->private_indicator = (data[1] &~ 0xBF) >> 6; // 1x111111
  20. ts_section_header->reserved1 = (data[1] &~ 0xCF) >> 4; // 11xx1111
  21. ts_section_header->section_length = ((data[1] &~ 0xF0) << 8) | data[2]; // 1111xxxx xxxxxxxx
  22. if (ts_section_header->section_length == 0)
  23. return NULL;
  24. // Stuffing table, ignore.
  25. if (ts_section_header->table_id == 0x72)
  26. return NULL;
  27. if (ts_section_header->section_syntax_indicator) {
  28. ts_section_header->ts_id_number = (data[3] << 8) | data[4]; // xxxxxxx xxxxxxx
  29. ts_section_header->reserved2 = data[5] >> 6; // xx111111
  30. ts_section_header->version_number = (data[5] &~ 0xC1) >> 1; // 11xxxxx1
  31. ts_section_header->current_next_indicator = data[5] &~ 0xFE; // 1111111x
  32. ts_section_header->section_number = data[6];
  33. ts_section_header->last_section_number = data[7];
  34. ts_section_header->data = ts_section_header->section_data + 8; // Skip header
  35. ts_section_header->data_len = ts_section_header->section_length - (5 + 4); // 5 for extended syntax, 4 for crc at the end
  36. return data + 8;
  37. } else {
  38. ts_section_header->data = ts_section_header->section_data + 3; // Skip header
  39. ts_section_header->data_len = ts_section_header->section_length;
  40. return data + 3;
  41. }
  42. }
  43. void ts_section_header_generate(uint8_t *ts_packet, struct ts_section_header *ts_section_header, uint8_t start) {
  44. ts_packet[start + 0] = ts_section_header->table_id;
  45. ts_packet[start + 1] = ts_section_header->section_syntax_indicator << 7; // x1111111
  46. ts_packet[start + 1] |= ts_section_header->private_indicator << 6; // 1x111111
  47. ts_packet[start + 1] |= ts_section_header->reserved1 << 4; // 11xx1111
  48. ts_packet[start + 1] |= ts_section_header->section_length >> 8; // 1111xxxx xxxxxxxx
  49. ts_packet[start + 2] = ts_section_header->section_length &~ 0xff00; // 1111xxxx xxxxxxxx
  50. ts_packet[start + 3] = ts_section_header->ts_id_number >> 8; // xxxxxxxx xxxxxxxx
  51. ts_packet[start + 4] = ts_section_header->ts_id_number &~ 0xff00;
  52. ts_packet[start + 5] = ts_section_header->reserved2 << 6; // xx111111
  53. ts_packet[start + 5] |= ts_section_header->version_number << 1; // 11xxxxx1
  54. ts_packet[start + 5] |= ts_section_header->current_next_indicator; // 1111111x
  55. ts_packet[start + 6] = ts_section_header->section_number;
  56. ts_packet[start + 7] = ts_section_header->last_section_number;
  57. }
  58. int ts_section_is_same(struct ts_section_header *s1, struct ts_section_header *s2) {
  59. if (s1->CRC != s2->CRC)
  60. return 0;
  61. if (s1->version_number != s2->version_number)
  62. return 0;
  63. if (s1->section_number != s2->section_number)
  64. return 0;
  65. return 1; // Should be the same
  66. }
  67. #define IN(x, a, b) \
  68. (x >= a && x <= b)
  69. void ts_section_header_dump(struct ts_section_header *t) {
  70. ts_LOGf("%s", " * Section header\n");
  71. if (t->pointer_field)
  72. ts_LOGf(" - Pointer field : %d\n", t->pointer_field);
  73. ts_LOGf(" - Table id : %03x (%d) %s\n", t->table_id, t->table_id,
  74. t->table_id == 0x00 ? "program_association_section" :
  75. t->table_id == 0x01 ? "conditional_access_section" :
  76. t->table_id == 0x02 ? "program_map_section" :
  77. t->table_id == 0x03 ? "transport_stream_description_section" :
  78. IN(t->table_id, 0x04, 0x3f) ? "reserved" :
  79. t->table_id == 0x40 ? "network_information_section - actual_network" :
  80. t->table_id == 0x41 ? "network_information_section - other_network" :
  81. t->table_id == 0x42 ? "service_description_section - actual_transport_stream" :
  82. IN(t->table_id, 0x43, 0x45) ? "reserved for future use" :
  83. t->table_id == 0x46 ? "service_description_section - other_transport_stream" :
  84. IN(t->table_id, 0x47, 0x49) ? "reserved for future use" :
  85. t->table_id == 0x4a ? "bouquet_association_section" :
  86. IN(t->table_id, 0x4b, 0x4d) ? "reserved for future use" :
  87. t->table_id == 0x4e ? "event_information_section - actual_transport_stream, present/following" :
  88. t->table_id == 0x4f ? "event_information_section - other_transport_stream, present/following" :
  89. IN(t->table_id, 0x50, 0x5f) ? "event_information_section - actual_transport_stream, schedule" :
  90. IN(t->table_id, 0x60, 0x6f) ? "event_information_section - other_transport_stream, schedule" :
  91. t->table_id == 0x70 ? "time_date_section" :
  92. t->table_id == 0x71 ? "running_status_section" :
  93. t->table_id == 0x72 ? "stuffing_section" :
  94. t->table_id == 0x73 ? "time_offset_section" :
  95. t->table_id == 0x74 ? "application information section (TS 102 812 [15])" :
  96. t->table_id == 0x75 ? "container section (TS 102 323 [13])" :
  97. t->table_id == 0x76 ? "related content section (TS 102 323 [13])" :
  98. t->table_id == 0x77 ? "content identifier section (TS 102 323 [13])" :
  99. t->table_id == 0x78 ? "MPE-FEC section (EN 301 192 [4])" :
  100. t->table_id == 0x79 ? "resolution notification section (TS 102 323 [13])" :
  101. IN(t->table_id, 0x79, 0x7d) ? "reserved for future use" :
  102. t->table_id == 0x7e ? "discontinuity_information_section" :
  103. t->table_id == 0x7f ? "section_information_section" :
  104. IN(t->table_id, 0x80, 0xfe) ? "user defined" :
  105. t->table_id == 0xff ? "reserved" : "Impossible!"
  106. );
  107. ts_LOGf(" - Section length : %03x (%d)\n", t->section_length, t->section_length);
  108. if (!t->section_syntax_indicator) {
  109. ts_LOGf(" - Private section syntax\n");
  110. } else {
  111. ts_LOGf(" - TS ID / Program No : %04x (%d)\n", t->ts_id_number, t->ts_id_number);
  112. ts_LOGf(" - Version number %d, current next %d, section number %d, last section number %d\n",
  113. t->version_number,
  114. t->current_next_indicator,
  115. t->section_number,
  116. t->last_section_number);
  117. ts_LOGf(" - CRC 0x%08x\n", t->CRC);
  118. }
  119. ts_LOGf(" - Private vars : num_packets:%d section_pos:%d\n",
  120. t->num_packets,
  121. t->section_pos);
  122. }
  123. void ts_section_dump(struct ts_section_header *sec) {
  124. int i;
  125. ts_LOGf("%s table\n",
  126. sec->table_id == 0x00 ? "PAT" :
  127. sec->table_id == 0x01 ? "CAT" :
  128. sec->table_id == 0x02 ? "PMT" :
  129. sec->table_id == 0x03 ? "TSDT" :
  130. IN(sec->table_id, 0x40, 0x41) ? "NIT" :
  131. sec->table_id == 0x42 ? "SDT" :
  132. sec->table_id == 0x46 ? "SDT" :
  133. sec->table_id == 0x4a ? "BAT" :
  134. IN(sec->table_id, 0x4e, 0x6f) ? "EIT" :
  135. sec->table_id == 0x70 ? "TDT" :
  136. sec->table_id == 0x71 ? "RST" :
  137. sec->table_id == 0x72 ? "STUFFING" :
  138. sec->table_id == 0x73 ? "TOT" :
  139. sec->table_id == 0x7e ? "DIS" :
  140. sec->table_id == 0x7f ? "SIS" :
  141. IN(sec->table_id, 0x80, 0xfe) ? "USER_DEFINED" :
  142. sec->table_id == 0xff ? "RESERVED" : "UNKNOWN"
  143. );
  144. for (i=0;i<sec->num_packets;i++) {
  145. struct ts_header tshdr;
  146. ts_packet_header_parse(sec->packet_data + (i * TS_PACKET_SIZE), &tshdr);
  147. ts_packet_header_dump(&tshdr);
  148. }
  149. ts_section_header_dump(sec);
  150. }
  151. #undef IN