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_pat.c 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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_pat *ts_pat_alloc() {
  8. struct ts_pat *pat = calloc(1, sizeof(struct ts_pat));
  9. pat->section_header = ts_section_data_alloc();
  10. pat->programs_max = 128;
  11. pat->programs = calloc(pat->programs_max, sizeof(void *));
  12. return pat;
  13. }
  14. void ts_pat_free(struct ts_pat **ppat) {
  15. struct ts_pat *pat = *ppat;
  16. int i;
  17. if (pat) {
  18. ts_section_data_free(&pat->section_header);
  19. for (i=0;i<pat->programs_num;i++) {
  20. FREE(pat->programs[i]);
  21. }
  22. FREE(pat->programs);
  23. FREE(*ppat);
  24. }
  25. }
  26. static struct ts_pat *ts_pat_reset(struct ts_pat *pat) {
  27. struct ts_pat *newpat = ts_pat_alloc();
  28. ts_pat_free(&pat);
  29. return newpat;
  30. }
  31. struct ts_pat *ts_pat_push_packet(struct ts_pat *pat, uint8_t *ts_packet) {
  32. struct ts_header ts_header;
  33. memset(&ts_header, 0, sizeof(struct ts_header));
  34. if (ts_packet_header_parse(ts_packet, &ts_header)) {
  35. // PAT should be with PID 0x00
  36. if (ts_header.pid != 0x00)
  37. goto OUT;
  38. if (!pat->ts_header.pusi)
  39. pat->ts_header = ts_header;
  40. }
  41. if (ts_header.pusi) {
  42. struct ts_section_header section_header;
  43. memset(&section_header, 0, sizeof(struct ts_section_header));
  44. uint8_t *section_data = ts_section_header_parse(ts_packet, &pat->ts_header, &section_header);
  45. if (!section_data || !section_header.section_syntax_indicator) {
  46. memset(&pat->ts_header, 0, sizeof(struct ts_header));
  47. goto OUT;
  48. }
  49. // table_id should be 0x00 (program_association_section)
  50. if (section_header.table_id != 0x00) {
  51. memset(&pat->ts_header, 0, sizeof(struct ts_header));
  52. goto OUT;
  53. }
  54. // Set correct section_header
  55. ts_section_header_parse(ts_packet, &pat->ts_header, pat->section_header);
  56. }
  57. if (!pat->initialized) {
  58. if (pat->section_header->section_syntax_indicator) {
  59. ts_section_add_packet(pat->section_header, &ts_header, ts_packet);
  60. if (pat->section_header->initialized) {
  61. if (!ts_pat_parse(pat))
  62. goto ERROR;
  63. }
  64. }
  65. }
  66. OUT:
  67. return pat;
  68. ERROR:
  69. return ts_pat_reset(pat);
  70. }
  71. int ts_pat_parse(struct ts_pat *pat) {
  72. uint8_t *section_data = pat->section_header->data;
  73. int section_len = pat->section_header->data_len;
  74. while (section_len > 0) {
  75. if (pat->programs_num == pat->programs_max) {
  76. ts_LOGf("PAT contains too many programs (>%d), not all are initialized!\n", pat->programs_max);
  77. break;
  78. }
  79. struct ts_pat_program *pinfo = calloc(1, sizeof(struct ts_pat_program));
  80. pinfo->program = (section_data[0] << 8) | section_data[1]; // xxxxxxxx xxxxxxxx
  81. pinfo->reserved = (section_data[2] &~ 0x1F) >> 5; // xxx11111
  82. pinfo->pid = ((section_data[2] &~ 0xE0) << 8) | section_data[3]; // 111xxxxx xxxxxxxx
  83. pat->programs[pat->programs_num] = pinfo;
  84. pat->programs_num++;
  85. section_data += 4;
  86. section_len -= 4;
  87. }
  88. pat->CRC = (pat->CRC << 8) | section_data[3];
  89. pat->CRC = (pat->CRC << 8) | section_data[2];
  90. pat->CRC = (pat->CRC << 8) | section_data[1];
  91. pat->CRC = (pat->CRC << 8) | section_data[0];
  92. u_int32_t check_crc = ts_crc32_section(pat->section_header);
  93. if (check_crc != 0) {
  94. ts_LOGf("!!! Wrong PAT CRC! It should be 0 but it is %08x (CRC in data is 0x%08x)\n", check_crc, pat->CRC);
  95. return 0;
  96. }
  97. pat->initialized = 1;
  98. return 1;
  99. }
  100. void ts_pat_generate(struct ts_pat *pat, uint8_t **ts_packets, int *num_packets) {
  101. uint8_t *secdata = ts_section_data_alloc_section();
  102. ts_section_header_generate(secdata, pat->section_header, 0);
  103. int curpos = 8; // Compensate for the section header, frist data byte is at offset 8
  104. int i;
  105. for (i=0;i<pat->programs_num;i++) {
  106. struct ts_pat_program *prg = pat->programs[i];
  107. secdata[curpos + 0] = prg->program >> 8;
  108. secdata[curpos + 1] = prg->program &~ 0xff00;
  109. secdata[curpos + 2] = prg->reserved << 5;
  110. secdata[curpos + 2] |= prg->pid >> 8;
  111. secdata[curpos + 3] = prg->pid &~ 0xff00;
  112. curpos += 4; // Compensate for the above
  113. }
  114. pat->CRC = ts_section_data_calculate_crc(secdata, curpos);
  115. curpos += 4; // CRC
  116. ts_section_data_gen_ts_packets(&pat->ts_header, secdata, curpos, pat->section_header->pointer_field, ts_packets, num_packets);
  117. FREE(secdata);
  118. }
  119. void ts_pat_regenerate_packets(struct ts_pat *pat) {
  120. uint8_t *ts_packets;
  121. int num_packets;
  122. ts_pat_generate(pat, &ts_packets, &num_packets);
  123. FREE(pat->section_header->packet_data);
  124. pat->section_header->packet_data = ts_packets;
  125. pat->section_header->num_packets = num_packets;
  126. }
  127. struct ts_pat *ts_pat_copy(struct ts_pat *pat) {
  128. struct ts_pat *newpat = ts_pat_alloc();
  129. int i;
  130. for (i=0;i<pat->section_header->num_packets; i++) {
  131. newpat = ts_pat_push_packet(newpat, pat->section_header->packet_data + (i * TS_PACKET_SIZE));
  132. }
  133. if (newpat->initialized) {
  134. return newpat;
  135. } else {
  136. ts_LOGf("Error copying PAT!\n");
  137. ts_pat_free(&newpat);
  138. return NULL;
  139. }
  140. }
  141. void ts_pat_check_generator(struct ts_pat *pat) {
  142. struct ts_pat *pat1 = ts_pat_copy(pat);
  143. if (pat1) {
  144. ts_compare_data("PAT (tspacket->struct)",
  145. pat1->section_header->packet_data,
  146. pat->section_header->packet_data,
  147. pat->section_header->num_packets * TS_PACKET_SIZE);
  148. ts_pat_free(&pat1);
  149. }
  150. uint8_t *ts_packets;
  151. int num_packets;
  152. ts_pat_generate(pat, &ts_packets, &num_packets);
  153. if (num_packets != pat->section_header->num_packets) {
  154. ts_LOGf("ERROR: num_packets:%d != sec->num_packets:%d\n", num_packets, pat->section_header->num_packets);
  155. }
  156. ts_compare_data("PAT (struct->tspacket)", pat->section_header->packet_data, ts_packets, num_packets * TS_PACKET_SIZE);
  157. free(ts_packets);
  158. }
  159. void ts_pat_dump(struct ts_pat *pat) {
  160. int i;
  161. ts_LOGf("PAT packet\n");
  162. for(i=0;i<pat->section_header->num_packets;i++) {
  163. struct ts_header tshdr;
  164. ts_packet_header_parse(pat->section_header->packet_data + (i * TS_PACKET_SIZE), &tshdr);
  165. ts_packet_header_dump(&tshdr);
  166. }
  167. ts_section_header_dump(pat->section_header);
  168. ts_LOGf(" * PAT data\n");
  169. ts_LOGf(" * num_programs: %d\n", pat->programs_num);
  170. for (i=0;i<pat->programs_num;i++) {
  171. struct ts_pat_program *prg = pat->programs[i];
  172. ts_LOGf(" * [%02d/%02d]: Program No 0x%04x (%5d) -> PID %04x (%d) /res: 0x%02x/\n",
  173. i+1, pat->programs_num,
  174. prg->program, prg->program,
  175. prg->pid, prg->pid,
  176. prg->reserved);
  177. // Program number 0 is Network ID, not program id
  178. if (prg->program == 0) {
  179. ts_LOGf(" - NIT PID %04x (%d)\n", prg->pid, prg->pid);
  180. }
  181. }
  182. ts_LOGf(" * CRC 0x%08x\n", pat->CRC);
  183. ts_pat_check_generator(pat);
  184. }
  185. int ts_pat_is_same(struct ts_pat *pat1, struct ts_pat *pat2) {
  186. int i;
  187. if (pat1->CRC == pat2->CRC) // Same
  188. return 1;
  189. // If some version is not current, just claim the structures are the same
  190. if (!pat1->section_header->current_next_indicator || pat2->section_header->version_number)
  191. return 1;
  192. if (pat1->section_header->version_number != pat2->section_header->version_number) // Different
  193. return 0;
  194. if (pat1->programs_num != pat2->programs_num) // Different
  195. return 0;
  196. // Check each program and PIDs
  197. for (i=0;i<pat1->programs_num;i++) {
  198. struct ts_pat_program *prg1 = pat1->programs[i];
  199. struct ts_pat_program *prg2 = pat2->programs[i];
  200. if (prg1->program != prg2->program || prg1->pid != prg2->pid) // Different
  201. return 0;
  202. }
  203. return 1; // Same
  204. }