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

tsfuncs_pat.c 6.3KB

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