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.

sections.c 8.6KB

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