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 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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) {
  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. ts_section_add_packet(pat->section_header, &ts_header, ts_packet);
  59. if (pat->section_header->initialized) {
  60. if (!ts_pat_parse(pat))
  61. goto ERROR;
  62. }
  63. }
  64. OUT:
  65. return pat;
  66. ERROR:
  67. return ts_pat_reset(pat);
  68. }
  69. int ts_pat_parse(struct ts_pat *pat) {
  70. uint8_t *section_data = pat->section_header->data;
  71. int section_len = pat->section_header->data_len;
  72. while (section_len > 0) {
  73. if (pat->programs_num == pat->programs_max) {
  74. ts_LOGf("PAT contains too many programs (>%d), not all are initialized!\n", pat->programs_max);
  75. break;
  76. }
  77. struct ts_pat_program *pinfo = calloc(1, sizeof(struct ts_pat_program));
  78. pinfo->program = (section_data[0] << 8) | section_data[1]; // xxxxxxxx xxxxxxxx
  79. pinfo->reserved = (section_data[2] &~ 0x1F) >> 5; // xxx11111
  80. pinfo->pid = ((section_data[2] &~ 0xE0) << 8) | section_data[3]; // 111xxxxx xxxxxxxx
  81. pat->programs[pat->programs_num] = pinfo;
  82. pat->programs_num++;
  83. section_data += 4;
  84. section_len -= 4;
  85. }
  86. if (!ts_crc32_section_check(pat->section_header, "PAT"))
  87. return 0;
  88. pat->initialized = 1;
  89. return 1;
  90. }
  91. void ts_pat_generate(struct ts_pat *pat, uint8_t **ts_packets, int *num_packets) {
  92. uint8_t *secdata = ts_section_data_alloc_section();
  93. ts_section_header_generate(secdata, pat->section_header, 0);
  94. int curpos = 8; // Compensate for the section header, frist data byte is at offset 8
  95. int i;
  96. for (i=0;i<pat->programs_num;i++) {
  97. struct ts_pat_program *prg = pat->programs[i];
  98. secdata[curpos + 0] = prg->program >> 8;
  99. secdata[curpos + 1] = prg->program &~ 0xff00;
  100. secdata[curpos + 2] = prg->reserved << 5;
  101. secdata[curpos + 2] |= prg->pid >> 8;
  102. secdata[curpos + 3] = prg->pid &~ 0xff00;
  103. curpos += 4; // Compensate for the above
  104. }
  105. pat->section_header->CRC = ts_section_data_calculate_crc(secdata, curpos);
  106. curpos += 4; // CRC
  107. ts_section_data_gen_ts_packets(&pat->ts_header, secdata, curpos, pat->section_header->pointer_field, ts_packets, num_packets);
  108. FREE(secdata);
  109. }
  110. void ts_pat_regenerate_packets(struct ts_pat *pat) {
  111. uint8_t *ts_packets;
  112. int num_packets;
  113. ts_pat_generate(pat, &ts_packets, &num_packets);
  114. FREE(pat->section_header->packet_data);
  115. pat->section_header->packet_data = ts_packets;
  116. pat->section_header->num_packets = num_packets;
  117. }
  118. struct ts_pat *ts_pat_copy(struct ts_pat *pat) {
  119. struct ts_pat *newpat = ts_pat_alloc();
  120. int i;
  121. for (i=0;i<pat->section_header->num_packets; i++) {
  122. newpat = ts_pat_push_packet(newpat, pat->section_header->packet_data + (i * TS_PACKET_SIZE));
  123. }
  124. if (newpat->initialized) {
  125. return newpat;
  126. } else {
  127. ts_LOGf("Error copying PAT!\n");
  128. ts_pat_free(&newpat);
  129. return NULL;
  130. }
  131. }
  132. void ts_pat_check_generator(struct ts_pat *pat) {
  133. struct ts_pat *pat1 = ts_pat_copy(pat);
  134. if (pat1) {
  135. ts_compare_data("PAT (tspacket->struct)",
  136. pat1->section_header->packet_data,
  137. pat->section_header->packet_data,
  138. pat->section_header->num_packets * TS_PACKET_SIZE);
  139. ts_pat_free(&pat1);
  140. }
  141. uint8_t *ts_packets;
  142. int num_packets;
  143. ts_pat_generate(pat, &ts_packets, &num_packets);
  144. if (num_packets != pat->section_header->num_packets) {
  145. ts_LOGf("ERROR: num_packets:%d != sec->num_packets:%d\n", num_packets, pat->section_header->num_packets);
  146. }
  147. ts_compare_data("PAT (struct->tspacket)", pat->section_header->packet_data, ts_packets, num_packets * TS_PACKET_SIZE);
  148. free(ts_packets);
  149. }
  150. void ts_pat_dump(struct ts_pat *pat) {
  151. struct ts_section_header *sec = pat->section_header;
  152. int i;
  153. ts_section_dump(sec);
  154. ts_LOGf(" * PAT data\n");
  155. ts_LOGf(" * num_programs: %d\n", pat->programs_num);
  156. for (i=0;i<pat->programs_num;i++) {
  157. struct ts_pat_program *prg = pat->programs[i];
  158. ts_LOGf(" * [%02d/%02d]: Program No 0x%04x (%5d) -> PID %04x (%d) /res: 0x%02x/\n",
  159. i+1, pat->programs_num,
  160. prg->program, prg->program,
  161. prg->pid, prg->pid,
  162. prg->reserved);
  163. // Program number 0 is Network ID, not program id
  164. if (prg->program == 0) {
  165. ts_LOGf(" - NIT PID %04x (%d)\n", prg->pid, prg->pid);
  166. }
  167. }
  168. ts_pat_check_generator(pat);
  169. }
  170. int ts_pat_is_same(struct ts_pat *pat1, struct ts_pat *pat2) {
  171. if (pat1 == pat2) return 1; // Same
  172. if ((!pat1 && pat2) || (pat1 && !pat2)) return 0; // Not same (one is NULL)
  173. return ts_section_is_same(pat1->section_header, pat2->section_header);
  174. }