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

pat.c 6.6KB

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