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

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