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

tdt.c 6.8KB

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