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.

tdt.c 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <netdb.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <time.h>
  7. #include "tsfuncs.h"
  8. struct ts_tdt *ts_tdt_alloc() {
  9. struct ts_tdt *tdt = calloc(1, sizeof(struct ts_tdt));
  10. tdt->section_header = ts_section_data_alloc();
  11. return tdt;
  12. }
  13. void ts_tdt_clear(struct ts_tdt *tdt) {
  14. if (!tdt)
  15. return;
  16. // save
  17. struct ts_section_header *section_header = tdt->section_header;
  18. // free
  19. FREE(tdt->descriptors);
  20. // clear
  21. ts_section_data_clear(section_header);
  22. memset(tdt, 0, sizeof(struct ts_tdt));
  23. // restore
  24. tdt->section_header = section_header;
  25. }
  26. void ts_tdt_free(struct ts_tdt **ptdt) {
  27. struct ts_tdt *tdt = *ptdt;
  28. if (tdt) {
  29. ts_section_data_free(&tdt->section_header);
  30. FREE(tdt->descriptors);
  31. FREE(*ptdt);
  32. }
  33. }
  34. struct ts_tdt *ts_tdt_push_packet(struct ts_tdt *tdt, uint8_t *ts_packet) {
  35. struct ts_header ts_header;
  36. memset(&ts_header, 0, sizeof(struct ts_header));
  37. if (ts_packet_header_parse(ts_packet, &ts_header)) {
  38. // TDT/TOT should be with PID 0x11
  39. if (ts_header.pid != 0x14)
  40. goto OUT;
  41. if (!tdt->ts_header.pusi)
  42. tdt->ts_header = ts_header;
  43. }
  44. if (ts_header.pusi) {
  45. struct ts_section_header section_header;
  46. memset(&section_header, 0, sizeof(struct ts_section_header));
  47. uint8_t *section_data = ts_section_header_parse(ts_packet, &tdt->ts_header, &section_header);
  48. if (!section_data) {
  49. memset(&tdt->ts_header, 0, sizeof(struct ts_header));
  50. goto OUT;
  51. }
  52. // table_id should be 0x70 (time_date_section)
  53. // or table_id should be 0x73 (time_offset_section)
  54. if (section_header.table_id != 0x70 && section_header.table_id != 0x73) {
  55. memset(&tdt->ts_header, 0, sizeof(struct ts_header));
  56. goto OUT;
  57. }
  58. // Set correct section_header
  59. ts_section_header_parse(ts_packet, &tdt->ts_header, tdt->section_header);
  60. }
  61. if (!tdt->initialized) {
  62. ts_section_add_packet(tdt->section_header, &ts_header, ts_packet);
  63. if (tdt->section_header->initialized) {
  64. if (!ts_tdt_parse(tdt))
  65. goto ERROR;
  66. }
  67. }
  68. OUT:
  69. return tdt;
  70. ERROR:
  71. ts_tdt_clear(tdt);
  72. return tdt;
  73. }
  74. int ts_tdt_parse(struct ts_tdt *tdt) {
  75. struct ts_section_header *sec = tdt->section_header;
  76. uint8_t *data = sec->data;
  77. tdt->mjd = (data[0] << 8) | data[1];
  78. tdt->bcd = ((data[2] << 16) | (data[3] << 8)) | data[4];
  79. data += 5;
  80. tdt->utc = ts_time_decode_mjd(tdt->mjd, tdt->bcd, &tdt->tm);
  81. if (sec->table_id == 0x73) { // TOT
  82. tdt->reserved_3 = data[0] >> 4; // xxxx1111
  83. tdt->descriptors_size = data[0] &~ 0xf0; // 1111xxxx
  84. tdt->descriptors_size |= data[1]; // xxxxxxxx
  85. data += 2;
  86. if (tdt->descriptors_size) {
  87. tdt->descriptors = malloc(tdt->descriptors_size);
  88. memcpy(tdt->descriptors, data, tdt->descriptors_size);
  89. }
  90. if (!ts_crc32_section_check(tdt->section_header, "TOT"))
  91. return 0;
  92. }
  93. tdt->initialized = 1;
  94. return 1;
  95. }
  96. void ts_tdt_generate(struct ts_tdt *tdt, uint8_t **ts_packets, int *num_packets) {
  97. uint8_t *secdata = ts_section_data_alloc_section();
  98. ts_section_header_generate(secdata, tdt->section_header, 0);
  99. int curpos = 3; // Compensate for the section header, first data byte is at offset 3
  100. secdata[curpos + 0] = (tdt->mjd &~ 0x00ff) >> 8;
  101. secdata[curpos + 1] = (tdt->mjd &~ 0xff00);
  102. secdata[curpos + 2] = (tdt->bcd >> 16);
  103. secdata[curpos + 3] = (tdt->bcd >> 8) &~ 0xff00;
  104. secdata[curpos + 4] = (tdt->bcd << 16) >> 16;
  105. curpos += 5; // For the fields above
  106. if (tdt->section_header->table_id == 0x73) { // TOT
  107. secdata[curpos + 0] = tdt->reserved_3 << 4;
  108. secdata[curpos + 0] |= tdt->descriptors_size >> 8;
  109. secdata[curpos + 1] = tdt->descriptors_size &~ 0xf00;
  110. curpos += 2;
  111. if (tdt->descriptors_size > 0) {
  112. memcpy(secdata + curpos, tdt->descriptors, tdt->descriptors_size);
  113. curpos += tdt->descriptors_size;
  114. }
  115. tdt->section_header->CRC = ts_section_data_calculate_crc(secdata, curpos);
  116. curpos += 4; // CRC
  117. }
  118. ts_section_data_gen_ts_packets(&tdt->ts_header, secdata, curpos, tdt->section_header->pointer_field, ts_packets, num_packets);
  119. FREE(secdata);
  120. }
  121. struct ts_tdt *ts_tdt_copy(struct ts_tdt *tdt) {
  122. struct ts_tdt *newtdt = ts_tdt_alloc();
  123. int i;
  124. for (i=0;i<tdt->section_header->num_packets; i++) {
  125. newtdt = ts_tdt_push_packet(newtdt, tdt->section_header->packet_data + (i * TS_PACKET_SIZE));
  126. }
  127. if (newtdt->initialized) {
  128. return newtdt;
  129. } else {
  130. ts_LOGf("Error copying tdt!\n");
  131. ts_tdt_free(&newtdt);
  132. return NULL;
  133. }
  134. }
  135. void ts_tdt_check_generator(struct ts_tdt *tdt) {
  136. struct ts_tdt *tdt1 = ts_tdt_alloc();
  137. int i;
  138. char *prefix1 = "TDT (tspacket->struct)";
  139. char *prefix2 = "TDT (struct->tspacket)";
  140. if (tdt->section_header->table_id == 0x73) {
  141. prefix1[1] = 'O';
  142. prefix2[1] = 'O';
  143. }
  144. for (i=0;i<tdt->section_header->num_packets;i++) {
  145. tdt1 = ts_tdt_push_packet(tdt1, tdt->section_header->packet_data + (i * TS_PACKET_SIZE));
  146. }
  147. ts_compare_data(prefix1, // "TDT (tspacket->struct)",
  148. tdt1->section_header->packet_data,
  149. tdt->section_header->packet_data,
  150. tdt->section_header->num_packets * TS_PACKET_SIZE);
  151. ts_tdt_free(&tdt1);
  152. uint8_t *ts_packets;
  153. int num_packets;
  154. ts_tdt_generate(tdt, &ts_packets, &num_packets);
  155. if (num_packets != tdt->section_header->num_packets) {
  156. ts_LOGf("ERROR: num_packets:%d != sec->num_packets:%d\n", num_packets, tdt->section_header->num_packets);
  157. }
  158. ts_compare_data(prefix2 /* "TDT (struct->tspacket)" */, tdt->section_header->packet_data, ts_packets, num_packets * TS_PACKET_SIZE);
  159. free(ts_packets);
  160. }
  161. void ts_tdt_dump(struct ts_tdt *tdt) {
  162. struct ts_section_header *sec = tdt->section_header;
  163. struct tm tm;
  164. time_t ts;
  165. uint16_t mjd_check;
  166. uint32_t bcd_check;
  167. ts_section_dump(sec);
  168. ts = ts_time_decode_mjd(tdt->mjd, tdt->bcd, &tm);
  169. ts_time_encode_mjd(&mjd_check, &bcd_check, &ts, &tm);
  170. ts_LOGf(" * %s data\n", sec->table_id == 0x70 ? "TDT" : "TOT");
  171. ts_LOGf(" - MJD : 0x%04x (%04d-%02d-%02d) unixts: %ld check:0x%04x\n",
  172. tdt->mjd,
  173. tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
  174. ts, mjd_check);
  175. ts_LOGf(" - BCD : 0x%06x (%02d:%02d:%02d) check:0x%06x\n",
  176. tdt->bcd,
  177. tm.tm_hour, tm.tm_min, tm.tm_sec,
  178. bcd_check);
  179. ts_LOGf(" - UTC Time : %lu (%04d-%02d-%02d %02d:%02d:%02d)\n" , tdt->utc,
  180. tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
  181. tm.tm_hour, tm.tm_min, tm.tm_sec);
  182. if (sec->table_id == 0x73 && tdt->descriptors_size) { // TOT
  183. ts_descriptor_dump(tdt->descriptors, tdt->descriptors_size);
  184. }
  185. ts_tdt_check_generator(tdt);
  186. }
  187. int ts_tdt_is_same(struct ts_tdt *tdt1, struct ts_tdt *tdt2) {
  188. if (tdt1 == tdt2) return 1; // Same
  189. if ((!tdt1 && tdt2) || (tdt1 && !tdt2)) return 0; // Not same (one is NULL)
  190. return ts_section_is_same(tdt1->section_header, tdt2->section_header);
  191. }